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.enums.BusinessType;
import com.xingxi.common.enums.EProdStatus;
import com.xingxi.common.utils.poi.ExcelUtil;
import com.xingxi.master.product.domain.Prod;
import com.xingxi.web.controller.master.product.domain.ProdVo;
import com.xingxi.web.controller.master.product.service.IProdAttrVoService;
import com.xingxi.web.controller.master.product.service.IProdVoService;
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/prod")
public class ProdController extends BaseController {
    private final String prefix = "master/product/prod";

    @Resource
    private IProdVoService prodVoService;

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

    /**
     * 查询商品列表
     */
    @RequiresPermissions("master:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProdVo prodVo) {
        startPage();
        List<ProdVo> list = prodVoService.selectProdVoList(prodVo);
        return getDataTable(list);
    }

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

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

    /**
     * 新增保存商品
     */
    @RequiresPermissions("master:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Prod prod) {
        return toAjax(prodVoService.insertProd(prod));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("master:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mMap) {
        ProdVo prodVo = prodVoService.selectProdVoById(id);
        mMap.put("prodVo", prodVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("master:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Prod prod) {
        return toAjax(prodVoService.updateProd(prod));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("master:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(prodVoService.logicDeleteProdByIds(ids));
    }

    /**
     * 审核商品
     */
    @RequiresPermissions("master:product:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(Prod prod) {
        prod.setProdStatus(EProdStatus.APPROVED.getVal());
        return toAjax(prodVoService.updateProd(prod));
    }

    /**
     * 审核商品
     */
    @RequiresPermissions("master:product:approve")
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(Prod prod) {
        prod.setProdStatus(EProdStatus.REJECTED.getVal());
        return toAjax(prodVoService.updateProd(prod));
    }
}
