package com.xingxi.web.controller.master.merchant.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.utils.poi.ExcelUtil;
import com.xingxi.master.merchant.domain.MerchantProd;
import com.xingxi.master.merchant.service.IMerchantProdService;
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("/system/merchant/product")
public class MerchantProdController extends BaseController {
    private final String prefix = "system/merchant/product";

    @Resource
    private IMerchantProdService merchantProdService;

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

    /**
     * 查询店铺商品列表
     */
    @RequiresPermissions("system:merchant:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantProd merchantProd) {
        startPage();
        List<MerchantProd> list = merchantProdService.selectMerchantProdList(merchantProd);
        return getDataTable(list);
    }

    /**
     * 导出店铺商品列表
     */
    @RequiresPermissions("system:merchant:product:export")
    @Log(title = "店铺商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerchantProd merchantProd) {
        List<MerchantProd> list = merchantProdService.selectMerchantProdList(merchantProd);
        ExcelUtil<MerchantProd> util = new ExcelUtil<>(MerchantProd.class);
        return util.exportExcel(list, "店铺商品数据");
    }

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

    /**
     * 新增保存店铺商品
     */
    @RequiresPermissions("system:merchant:product:add")
    @Log(title = "店铺商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MerchantProd merchantProd) {
        return toAjax(merchantProdService.insertMerchantProd(merchantProd));
    }

    /**
     * 修改店铺商品
     */
    @RequiresPermissions("system:merchant:product:edit")
    @GetMapping("/edit/{mercProdId}")
    public String edit(@PathVariable("mercProdId") Long mercProdId, ModelMap mMap) {
        MerchantProd merchantProd = merchantProdService.selectMerchantProdByMercProdId(mercProdId);
        mMap.put("merchantProd", merchantProd);
        return prefix + "/edit";
    }

    /**
     * 修改保存店铺商品
     */
    @RequiresPermissions("system:merchant:product:edit")
    @Log(title = "店铺商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MerchantProd merchantProd) {
        return toAjax(merchantProdService.updateMerchantProd(merchantProd));
    }

    /**
     * 删除店铺商品
     */
    @RequiresPermissions("system:merchant:product:remove")
    @Log(title = "店铺商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(merchantProdService.deleteMerchantProdByMercProdIds(ids));
    }
}
