package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCodeRule;
import com.ruoyi.system.service.ISysCodeRuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 编码规则Controller
 * 
 * @author Yangk
 * @date 2026-06-08
 */
@Controller
@RequestMapping("/system/rule")
public class SysCodeRuleController extends BaseController
{
    private String prefix = "system/rule";

    @Autowired
    private ISysCodeRuleService sysCodeRuleService;

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

    /**
     * 查询编码规则列表
     */
    @RequiresPermissions("system:rule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCodeRule sysCodeRule)
    {
        startPage();
        List<SysCodeRule> list = sysCodeRuleService.selectSysCodeRuleList(sysCodeRule);
        return getDataTable(list);
    }

    /**
     * 导出编码规则列表
     */
    @RequiresPermissions("system:rule:export")
    @Log(title = "编码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysCodeRule sysCodeRule)
    {
        List<SysCodeRule> list = sysCodeRuleService.selectSysCodeRuleList(sysCodeRule);
        ExcelUtil<SysCodeRule> util = new ExcelUtil<SysCodeRule>(SysCodeRule.class);
        return util.exportExcel(list, "编码规则数据");
    }

    /**
     * 查看编码规则详情
     */
    @RequiresPermissions("system:rule:view")
    @GetMapping("/view/{ruleId}")
    public String view(@PathVariable("ruleId") Long ruleId, ModelMap mmap)
    {
        SysCodeRule sysCodeRule = sysCodeRuleService.selectSysCodeRuleByRuleId(ruleId);
        mmap.put("sysCodeRule", sysCodeRule);
        return prefix + "/view";
    }

    /**
     * 新增编码规则
     */
    @RequiresPermissions("system:rule:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存编码规则
     */
    @RequiresPermissions("system:rule:add")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysCodeRule sysCodeRule)
    {
        if (!sysCodeRuleService.checkRuleCodeUnique(sysCodeRule))
        {
            return error("新增编码规则失败，规则编码已存在");
        }
        sysCodeRule.setCreateBy(getLoginName());
        return toAjax(sysCodeRuleService.insertSysCodeRule(sysCodeRule));
    }

    /**
     * 修改编码规则
     */
    @RequiresPermissions("system:rule:edit")
    @GetMapping("/edit/{ruleId}")
    public String edit(@PathVariable("ruleId") Long ruleId, ModelMap mmap)
    {
        SysCodeRule sysCodeRule = sysCodeRuleService.selectSysCodeRuleByRuleId(ruleId);
        mmap.put("sysCodeRule", sysCodeRule);
        return prefix + "/edit";
    }

    /**
     * 修改保存编码规则
     */
    @RequiresPermissions("system:rule:edit")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysCodeRule sysCodeRule)
    {
        if (!sysCodeRuleService.checkRuleCodeUnique(sysCodeRule))
        {
            return error("修改编码规则失败，规则编码已存在");
        }
        sysCodeRule.setUpdateBy(getLoginName());
        return toAjax(sysCodeRuleService.updateSysCodeRule(sysCodeRule));
    }

    /**
     * 删除编码规则
     */
    @RequiresPermissions("system:rule:remove")
    @Log(title = "编码规则", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysCodeRuleService.deleteSysCodeRuleByRuleIds(ids));
    }
}
