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.enums.EDelFlag;
import com.xingxi.common.exception.BusinessException;
import com.xingxi.common.utils.poi.ExcelUtil;
import com.xingxi.master.merchant.domain.Merchant;
import com.xingxi.master.merchant.service.IMerchantService;
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")
public class MerchantController extends BaseController {
    private final String prefix = "master/merchant";

    @Resource
    private IMerchantService merchantService;

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

    /**
     * 查询商户列表
     */
    @RequiresPermissions("master:merchant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Merchant merchant) {
        startPage();
        merchant.setDelFlag(EDelFlag.NO.getVal());
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("master:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Merchant merchant) {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<>(Merchant.class);
        return util.exportExcel(list, "商户数据");
    }

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

    /**
     * 新增保存商户
     */
    @RequiresPermissions("master:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Merchant merchant) {
        Merchant cond;
        List<Merchant> checkResult;
        cond = new Merchant();
        cond.setMercName(merchant.getMercName());
        cond.setDelFlag(EDelFlag.NO.getVal());
        checkResult = merchantService.selectMerchantList(cond);
        if (checkResult.size() > 0) {
            throw new BusinessException("商户名已存在！");
        }
        return toAjax(merchantService.insertMerchant(merchant));
    }

    /**
     * 修改商户
     */
    @RequiresPermissions("master:merchant:edit")
    @GetMapping("/edit/{merchantId}")
    public String edit(@PathVariable("merchantId") Long merchantId, ModelMap mMap) {
        Merchant merchant = merchantService.selectMerchantByMercId(merchantId);
        mMap.put("merchant", merchant);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户
     */
    @RequiresPermissions("master:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Merchant merchant) {
        Merchant cond;
        List<Merchant> checkResult;
        cond = new Merchant();
        cond.setMercName(merchant.getMercName());
        cond.setDelFlag(EDelFlag.NO.getVal());
        checkResult = merchantService.selectMerchantList(cond);
        if (checkResult.size() > 0) {
            throw new BusinessException("商户名已存在！");
        }
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("master:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(merchantService.logicDeleteMerchantByMercId(Long.parseLong(ids)));
    }
}
