package com.xinelu.manage.controller.goodsCategory;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.goodsCategory.GoodsCategory;
import com.xinelu.manage.service.goodsCategory.IGoodsCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 商品分类信息Controller
 *
 * @author xinyilu
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/system/goodsCategory")
public class GoodsCategoryController extends BaseController {
    @Resource
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 查询商品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsCategory goodsCategory) {
        startPage();
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        return getDataTable(list);
    }

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

    /**
     * 获取商品分类信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("商品分类信息不能为空!");
        }
        return AjaxResult.success(goodsCategoryService.selectGoodsCategoryById(id));
    }

    /**
     * 新增商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:add')")
    @Log(title = "商品分类信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Insert.class) GoodsCategory goodsCategory) {
        return goodsCategoryService.insertGoodsCategory(goodsCategory);
    }

    /**
     * 修改商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:edit')")
    @Log(title = "商品分类信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated(Update.class) GoodsCategory goodsCategory) {
        return goodsCategoryService.updateGoodsCategory(goodsCategory);
    }

    /**
     * 删除商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsCategory:remove')")
    @Log(title = "商品分类信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return goodsCategoryService.deleteGoodsCategoryByIds(ids);
    }

    /**
     * 查询商品分类信息集合树图
     *
     * @param goodsCategory 查询条件
     * @return 返回结果信息
     */
    @GetMapping("/getStationCategoryList")
    public AjaxResult getStationCategoryList(GoodsCategory goodsCategory) {
        return AjaxResult.success(goodsCategoryService.selectGoodsCategoryList(goodsCategory));
    }

    /**
     * 查询所有的分类 (暂时不用 可做demo)
     *
     * @param parentId 查询条件 父级id
     * @return 返回结果信息
     */
    @GetMapping(value = "/getListAllChildren/{parentId}")
    public AjaxResult getListAllChildren(@PathVariable String parentId) {
        if (StringUtils.isBlank(parentId)) {
            return AjaxResult.error("请输入父级id");
        }
        return AjaxResult.success(goodsCategoryService.listAllChildren(parentId));
    }
}
