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.web.controller.master.product.domain.ProdAttrVo;
import com.xingxi.web.controller.master.product.service.IProdAttrVoService;
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/prodAttr")
public class ProdAttrController extends BaseController {
    private final String prefix = "master/product/prodAttr";

    @Resource
    private IProdAttrVoService prodAttrVoService;

    @RequiresPermissions("master:product:prod:viewAttrs")
    @GetMapping("/{id}")
    public String prodAttr(@PathVariable("id") Long id, ModelMap mMap) {
        mMap.put("prodId", id);
        return prefix + "/prodAttr";
    }

    /**
     * 查询商品属性列表
     */
    @RequiresPermissions("master:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProdAttrVo prodAttrVo) {
        startPage();
        List<ProdAttrVo> list = prodAttrVoService.selectProdAttrVoList(prodAttrVo);
        return getDataTable(list);
    }

    /**
     * 查询商品属性列表
     */
    @RequiresPermissions("master:product:list")
    @PostMapping("/jointList")
    @ResponseBody
    public TableDataInfo jointList(ProdAttrVo prodAttrVo) {
        startPage();
        List<ProdAttrVo> list = prodAttrVoService.selectProdAttrJointList(prodAttrVo);
        return getDataTable(list);
    }

    /**
     * 新增商品属性
     */
    @RequiresPermissions("master:product:addAttr")
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mMap) {
        mMap.put("prodId", id);
        return prefix + "/add";
    }

    /**
     * 修改商品属性
     */
    @RequiresPermissions("master:product:editAttr")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long prodAttrId, ModelMap mMap) {
        ProdAttrVo prodAttrVo = prodAttrVoService.selectProdAttrVoByProdAttrId(prodAttrId);
        mMap.put("prodId", prodAttrVo.getProdId());
        mMap.put("prodAttrId", prodAttrId);
        mMap.put("prodAttrVo", prodAttrVo);
        return prefix + "/edit";
    }

    /**
     * 添加商品属性
     */
    @RequiresPermissions("master:product:addAttr")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping("/addAttr")
    @ResponseBody
    public AjaxResult addProdAttr(@RequestBody ProdAttrVo prodAttrVo) {
        return toAjax(prodAttrVoService.addProdAttr(prodAttrVo));
    }

    /**
     * 修改商品属性
     */
    @RequiresPermissions("master:product:editAttr")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PostMapping("/editAttr")
    @ResponseBody
    public AjaxResult editAttr(@RequestBody ProdAttrVo prodAttrVo) {
        return toAjax(prodAttrVoService.updateProdAttr(prodAttrVo));
    }

    /**
     * 删除商品属性
     */
    @RequiresPermissions("master:product:tag:remove")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(prodAttrVoService.logicDeleteProdAttrByCondition(Long.parseLong(ids)));
    }
}
