package com.xingxi.web.controller.master.vendor.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.vendor.domain.Vendor;
import com.xingxi.master.vendor.service.IVendorService;
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/vendor")
public class VendorController extends BaseController {
    private final String prefix = "master/vendor";

    @Resource
    private IVendorService vendorService;

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

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("master:vendor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Vendor vendor) {
        startPage();
        List<Vendor> list = vendorService.selectVendorList(vendor);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @RequiresPermissions("master:vendor:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Vendor vendor) {
        List<Vendor> list = vendorService.selectVendorList(vendor);
        ExcelUtil<Vendor> util = new ExcelUtil<>(Vendor.class);
        return util.exportExcel(list, "供应商数据");
    }

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

    /**
     * 新增保存供应商
     */
    @RequiresPermissions("master:vendor:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Vendor vendor) {
        Vendor cond = new Vendor();
        cond.setVendorName(vendor.getVendorName());
        cond.setDelFlag(EDelFlag.NO.getVal());
        List<Vendor> checkResult = vendorService.selectVendorList(cond);
        if (checkResult.size() > 0) {
            throw new BusinessException("已添加过的供应商！");
        }
        return toAjax(vendorService.insertVendor(vendor));
    }

    /**
     * 修改供应商
     */
    @RequiresPermissions("master:vendor:edit")
    @GetMapping("/edit/{vendorId}")
    public String edit(@PathVariable("vendorId") Long vendorId, ModelMap mMap) {
        Vendor vendor = vendorService.selectVendorByVendorId(vendorId);
        mMap.put("vendor", vendor);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商
     */
    @RequiresPermissions("master:vendor:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Vendor vendor) {
        Vendor cond = new Vendor();
        cond.setVendorName(vendor.getVendorName());
        cond.setDelFlag(EDelFlag.NO.getVal());
        List<Vendor> checkResult = vendorService.selectVendorList(cond);
        if (checkResult.size() > 0) {
            throw new BusinessException("已添加过的供应商！");
        }
        return toAjax(vendorService.updateVendor(vendor));
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("master:vendor:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(vendorService.logicDeleteVendorByVendorId(Long.parseLong(ids)));
    }
}
