package com.xingxi.web.controller.master.product.controller;

import com.xingxi.common.annotation.Log;
import com.xingxi.common.core.controller.BaseController;
import com.xingxi.common.core.domain.AjaxResult;
import com.xingxi.common.core.page.TableDataInfo;
import com.xingxi.common.core.text.Convert;
import com.xingxi.common.enums.BusinessType;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.utils.poi.ExcelUtil;
import com.xingxi.master.product.domain.ProdClass;
import com.xingxi.master.product.service.IProdClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2025-03-09
 */
@Controller
@RequestMapping("/master/product/class")
public class ProdClassController extends BaseController {
    private final String prefix = "master/product/class";

    @Resource
    private IProdClassService prodClassService;

    @RequiresPermissions("master:product:class:view")
    @GetMapping()
    public String prodClass() {
        return prefix + "/prodClass";
    }

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("master:product:class:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProdClass prodClass) {
        startPage();
        prodClass.setDelFlag(EDelFlag.NO.getVal());
        List<ProdClass> list = prodClassService.selectProdClassList(prodClass);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("master:product:class:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProdClass prodClass) {
        List<ProdClass> list = prodClassService.selectProdClassList(prodClass);
        ExcelUtil<ProdClass> util = new ExcelUtil<>(ProdClass.class);
        return util.exportExcel(list, "商品分类数据");
    }

    /**
     * 新增商品分类
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("master:product:class:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProdClass prodClass) {
        return toAjax(prodClassService.insertProdClass(prodClass));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("master:product:class:edit")
    @GetMapping("/edit/{prodClassId}")
    public String edit(@PathVariable("prodClassId") Long prodClassId, ModelMap mMap) {
        ProdClass prodClass = prodClassService.selectProdClassByProdClassId(prodClassId);
        mMap.put("prodClass", prodClass);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("master:product:class:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProdClass prodClass) {
        return toAjax(prodClassService.updateProdClass(prodClass));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("master:product:class:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(prodClassService.logicDeleteProdClassByProdClassIds(Convert.toStrArray(ids)));
    }
}
