package com.ruoyi.project.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.SecurityUtils;
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.project.domain.TrsSensitiveWords;
import com.ruoyi.project.service.ITrsSensitiveWordsService;
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;
import org.springframework.web.multipart.MultipartFile;

/**
 * 常见错误Controller
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Controller
@RequestMapping("/project/check")
public class TrsSensitiveWordsController extends BaseController
{
    private String prefix = "project/check";

    @Autowired
    private ITrsSensitiveWordsService trsSensitiveWordsService;

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

    /**
     * 查询常见错误列表
     */
    @RequiresPermissions("project:check:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsSensitiveWords trsSensitiveWords)
    {
        startPage();
        List<TrsSensitiveWords> list = trsSensitiveWordsService.selectTrsSensitiveWordsList(trsSensitiveWords);
        return getDataTable(list);
    }

    /**
     * 导出常见错误列表
     */
    @RequiresPermissions("project:check:export")
    @Log(title = "常见错误", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsSensitiveWords trsSensitiveWords)
    {
        List<TrsSensitiveWords> list = trsSensitiveWordsService.selectTrsSensitiveWordsList(trsSensitiveWords);
        ExcelUtil<TrsSensitiveWords> util = new ExcelUtil<TrsSensitiveWords>(TrsSensitiveWords.class);
        return util.exportExcel(list, "常见错误数据");
    }

    /**
     * 新增常见错误
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存常见错误
     */
    @RequiresPermissions("project:check:add")
    @Log(title = "常见错误", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsSensitiveWords trsSensitiveWords)
    {
        trsSensitiveWords.setStatus("待审核");
        trsSensitiveWords.setTime(new Date());

        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 0L;
        }
        trsSensitiveWords.setDeptId(deptId);
        int status = trsSensitiveWordsService.insertTrsSensitiveWords(trsSensitiveWords);
        if(401 == status){
            return AjaxResult.warn("数据已存在");
        }else{
            return toAjax(status);
        }

    }

    /**
     * 修改常见错误
     */
    @RequiresPermissions("project:check:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsSensitiveWords trsSensitiveWords = trsSensitiveWordsService.selectTrsSensitiveWordsById(id);
        mmap.put("trsSensitiveWords", trsSensitiveWords);
        return prefix + "/edit";
    }

    /**
     * 修改保存常见错误
     */
    @RequiresPermissions("project:check:edit")
    @Log(title = "常见错误", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsSensitiveWords trsSensitiveWords)
    {
        trsSensitiveWords.setTime(new Date());
        trsSensitiveWords.setStatus("待审核");
        return toAjax(trsSensitiveWordsService.updateTrsSensitiveWords(trsSensitiveWords));
    }

    @RequiresPermissions("project:check:edit")
    @Log(title = "常见错误", businessType = BusinessType.UPDATE)
    @PostMapping("/editstatus")
    @ResponseBody
    public AjaxResult editStatusSave(String ids, String status)
    {
        for (String id : ids.split(",")) {
            TrsSensitiveWords trsSensitiveWords = trsSensitiveWordsService.selectTrsSensitiveWordsById(Long.parseLong(id));
            trsSensitiveWords.setStatus(status);
            trsSensitiveWords.setTime(new Date());
            trsSensitiveWordsService.updateTrsSensitiveWords(trsSensitiveWords);
        }
        return toAjax(1);
    }

    /**
     * 删除常见错误
     */
    @RequiresPermissions("project:check:remove")
    @Log(title = "常见错误", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsSensitiveWordsService.deleteTrsSensitiveWordsByIds(ids));
    }

    @RequiresPermissions("system:check:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TrsSensitiveWords> util = new ExcelUtil<TrsSensitiveWords>(TrsSensitiveWords.class);
        return util.importTemplateExcel("常见错误数据");
    }

    @Log(title = "常见错误管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:check:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TrsSensitiveWords> util = new ExcelUtil<TrsSensitiveWords>(TrsSensitiveWords.class);
        List<TrsSensitiveWords> wordList = util.importExcel(file.getInputStream());
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 0L;
        }
        String message = trsSensitiveWordsService.importWords(wordList, updateSupport,deptId);
//
        if(("导入成功"+System.lineSeparator()).equals(message)){
            return AjaxResult.success(message);
        }else{
            return AjaxResult.warn2(message);
        }
    }


}
