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.core.text.Convert;
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 com.xingxi.web.controller.master.merchant.domain.MerchantProdVo;
import com.xingxi.web.controller.master.merchant.service.IMerchantProdVoService;
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/merchant/prod")
public class MerchantProdController extends BaseController {
    private final String prefix = "master/merchant/prod";

    @Resource
    private IMerchantProdVoService merchantProdVoService;

    @RequiresPermissions("master:merchant:prod:view")
    @GetMapping("/view/{mercId}")
    public String prod(@PathVariable("mercId") Long mercId, ModelMap mMap) {
        mMap.put("mercId", mercId);
        return prefix + "/merchantProd";
    }

    /**
     * 查询店铺商品列表
     */
    @RequiresPermissions("master:merchant:prod:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantProdVo merchantProdVo) {
        startPage();
        List<MerchantProdVo> list = merchantProdVoService.selectMerchantProdVoList(merchantProdVo);
        return getDataTable(list);
    }

    /**
     * 修改店铺商品
     */
    @RequiresPermissions("master:merchant:prod:edit")
    @GetMapping("/edit/{mercId}")
    public String edit(@PathVariable("mercId") Long mercId, ModelMap mMap) {
        List<MerchantProdVo> merchantProdVoList = merchantProdVoService.selectMerchantProdVoByMercId(mercId);
        mMap.put("mercId", mercId);
        mMap.put("merchantProdVoList", merchantProdVoList);
        return prefix + "/edit";
    }

    /**
     * 修改保存店铺商品
     */
    @RequiresPermissions("master:merchant:prod:edit")
    @Log(title = "店铺商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody MerchantProdVo merchantProdVo) {
        return toAjax(merchantProdVoService.updateMerchantProdVo(merchantProdVo));
    }

    /**
     * 删除店铺商品
     */
    @RequiresPermissions("master:merchant:prod:remove")
    @Log(title = "店铺商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (ids.indexOf(",") > 0) {
            return toAjax(merchantProdVoService.logicDeleteMerchantProdByMercProdIds(Convert.toStrArray(ids)));
        } else {
            return toAjax(merchantProdVoService.logicDeleteMerchantProdByMercProdId(Long.parseLong(ids)));
        }
    }
}
