package com.yiqi.admin.controller.core;

import com.yiqi.common.annotation.Log;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.core.domain.GoodsCategory;
import com.yiqi.core.service.IGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品类型管理Controller
 *
 * @author ruoyi
 * @date 2023-08-21
 */
@Api(tags = "管理后台 - 商品类型管理")
@RestController
@RequestMapping(UrlConstants.managerApi + "/core/category")
public class GoodsCategoryController extends BaseController {
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 查询商品类型管理列表
     */
//    @PreAuthorize("@ss.hasPermi('goods:category:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询商品类型管理列表")
    public TableDataInfo list(GoodsCategory goodsCategory) {
        startPage();
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品类型管理列表
     */
    @PreAuthorize("@ss.hasPermi('goods:category:export')")
    @Log(title = "商品类型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出商品类型管理列表")
    public void export(HttpServletResponse response, GoodsCategory goodsCategory) {
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        ExcelUtil<GoodsCategory> util = new ExcelUtil<GoodsCategory>(GoodsCategory.class);
        util.exportExcel(response, list, "商品类型管理数据");
    }

    /**
     * 获取商品类型管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:category:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取商品类型管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(goodsCategoryService.selectGoodsCategoryById(id));
    }

    /**
     * 新增商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('goods:category:add')")
    @Log(title = "商品类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增商品类型管理")
    public AjaxResult add(@RequestBody GoodsCategory goodsCategory) {
        goodsCategory.setCreateById(getUserId());
        goodsCategory.setCreateBy(getUsername());
        return toAjax(goodsCategoryService.insertGoodsCategory(goodsCategory));
    }

    /**
     * 修改商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('goods:category:edit')")
    @Log(title = "商品类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改商品类型管理")
    public AjaxResult edit(@RequestBody GoodsCategory goodsCategory) {
        goodsCategory.setUpdateById(getUserId());
        goodsCategory.setUpdateBy(getUsername());
        return toAjax(goodsCategoryService.updateGoodsCategory(goodsCategory));
    }

    /**
     * 删除商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('goods:category:remove')")
    @Log(title = "商品类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除商品类型管理")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(goodsCategoryService.deleteGoodsCategoryByIds(ids));
    }

    /**
     * 修改商品类型管理状态
     */
    @PreAuthorize("@ss.hasPermi('goods:category:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "updateStatus")
    @ApiOperation(value = "修改商品类型管理状态")
    public AjaxResult updateCategoryStatus(@RequestBody GoodsCategory goodsCategory) {
        return toAjax(goodsCategoryService.updateCategoryStatus(goodsCategory));
    }

}
