package com.younike.member;

import java.util.List;

import com.younike.common.utils.StringUtils;
import com.younike.framework.config.AnonymousAccess;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.younike.common.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.system.domain.Category;
import com.younike.system.service.ICategoryService;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.common.core.page.TableDataInfo;
/**
 * 分类Controller
 *
 * @author elev
 * @date 2022-01-05
 */
@Api("分类接口")
@RestController
@RequestMapping("/member/category")
public class MemberCategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询分类列表
     */
    @ApiOperation("查询分类列表")
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }

    /**
     * 导出分类列表
     */
    @ApiOperation("导出分类列表")
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @ApiOperation("获取分类详细信息")
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(categoryService.selectCategoryByCategoryId(categoryId));
    }

    /**
     * 新增分类
     */
    @ApiOperation("新增分类")
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类
     */
    @ApiOperation("修改分类")
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类")
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(categoryService.deleteCategoryByCategoryIds(categoryIds));
    }

    /**
     * 获取首页商品分类数据
     *
     * @return 首页商品分类数据
     */
    @AnonymousAccess
    @ApiOperation("全部服务数据")
    @GetMapping("/categoriesList")
    public AjaxResult getCategoriesList(@RequestParam("bizType") Integer bizType) {
        //业务类型不能为空
        if(!StringUtils.isNotNull(bizType)){
            return AjaxResult.error("参数错误(业务类型不能为空)");
        }
        return categoryService.selectCategoriesList(bizType);
    }

    /**
     * 获取首页商品分类数据
     *
     * @return 首页商品分类数据
     */
    @AnonymousAccess
    @ApiOperation("首页服务数据")
    @GetMapping("/homeList")
    public AjaxResult getHomeCategoriesList(@RequestParam("bizType") Integer bizType) {
        //业务类型不能为空
        if(!StringUtils.isNotNull(bizType)){
            return AjaxResult.error("参数错误(业务类型不能为空)");
        }
        return categoryService.getHomeCategoriesList(bizType);
    }

}

