package com.ruoyi.project.controller;

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

import com.ruoyi.project.domain.TrsSensitiveWords;
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.TrsPolicyWords;
import com.ruoyi.project.service.ITrsPolicyWordsService;
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-21
 */
@Controller
@RequestMapping("/project/words")
public class TrsPolicyWordsController extends BaseController
{
    private String prefix = "project/words";

    @Autowired
    private ITrsPolicyWordsService trsPolicyWordsService;

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

    /**
     * 查询政治常识列表
     */
    @RequiresPermissions("project:words:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsPolicyWords trsPolicyWords)
    {
        startPage();
        List<TrsPolicyWords> list = trsPolicyWordsService.selectTrsPolicyWordsList(trsPolicyWords);
        return getDataTable(list);
    }

    /**
     * 导出政治常识列表
     */
    @RequiresPermissions("project:words:export")
    @Log(title = "政治常识", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsPolicyWords trsPolicyWords)
    {
        List<TrsPolicyWords> list = trsPolicyWordsService.selectTrsPolicyWordsList(trsPolicyWords);
        ExcelUtil<TrsPolicyWords> util = new ExcelUtil<TrsPolicyWords>(TrsPolicyWords.class);
        return util.exportExcel(list, "政治常识数据");
    }

    /**
     * 新增政治常识
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存政治常识
     */
    @RequiresPermissions("project:words:add")
    @Log(title = "政治常识", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsPolicyWords trsPolicyWords)
    {
        trsPolicyWords.setTime(new Date());
        trsPolicyWords.setStatus("待审核");
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 100L;
        }
        trsPolicyWords.setDeptId(deptId);
        int status = trsPolicyWordsService.insertTrsPolicyWords(trsPolicyWords);
        if(401 == status){
            return AjaxResult.warn("数据已存在");
        }else{
            return toAjax(status);
        }
    }

    /**
     * 修改政治常识
     */
    @RequiresPermissions("project:words:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsPolicyWords trsPolicyWords = trsPolicyWordsService.selectTrsPolicyWordsById(id);
        mmap.put("trsPolicyWords", trsPolicyWords);
        return prefix + "/edit";
    }

    /**
     * 修改保存政治常识
     */
    @RequiresPermissions("project:words:edit")
    @Log(title = "政治常识", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsPolicyWords trsPolicyWords)
    {
        trsPolicyWords.setTime(new Date());
        trsPolicyWords.setStatus("待审核");
        return toAjax(trsPolicyWordsService.updateTrsPolicyWords(trsPolicyWords));
    }

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

    /**
     * 删除政治常识
     */
    @RequiresPermissions("project:words:remove")
    @Log(title = "政治常识", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsPolicyWordsService.deleteTrsPolicyWordsByIds(ids));
    }

    @RequiresPermissions("system:check:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TrsPolicyWords> util = new ExcelUtil<TrsPolicyWords>(TrsPolicyWords.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<TrsPolicyWords> util = new ExcelUtil<TrsPolicyWords>(TrsPolicyWords.class);
        List<TrsPolicyWords> wordList = util.importExcel(file.getInputStream());
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 100L;
        }
        String message = trsPolicyWordsService.importWords(wordList, updateSupport,deptId);
        if(("导入成功"+System.lineSeparator()).equals(message)){
            return AjaxResult.success(message);
        }else{
            return AjaxResult.warn2(message);
        }
    }
}
