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.TrsNameWords;
import com.ruoyi.project.service.ITrsNameWordsService;
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/nameduty")
public class TrsNameWordsController extends BaseController
{
    private String prefix = "project/nameduty";

    @Autowired
    private ITrsNameWordsService trsNameWordsService;

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

    /**
     * 查询人名职务列表
     */
    @RequiresPermissions("project:nameduty:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsNameWords trsNameWords)
    {
        startPage();
        List<TrsNameWords> list = trsNameWordsService.selectTrsNameWordsList(trsNameWords);
        return getDataTable(list);
    }

    /**
     * 导出人名职务列表
     */
    @RequiresPermissions("project:nameduty:export")
    @Log(title = "人名职务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsNameWords trsNameWords)
    {
        List<TrsNameWords> list = trsNameWordsService.selectTrsNameWordsList(trsNameWords);
        ExcelUtil<TrsNameWords> util = new ExcelUtil<TrsNameWords>(TrsNameWords.class);
        return util.exportExcel(list, "人名职务数据");
    }

    /**
     * 新增人名职务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存人名职务
     */
    @RequiresPermissions("project:nameduty:add")
    @Log(title = "人名职务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsNameWords trsNameWords)
    {
        trsNameWords.setTime(new Date());
        trsNameWords.setStatus("待审核");
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 0L;
        }
        trsNameWords.setDeptId(deptId);
        int status = trsNameWordsService.insertTrsNameWords(trsNameWords);
        if(401 == status){
            return AjaxResult.warn("数据已存在");
        }else{
            return toAjax(status);
        }
    }

    /**
     * 修改人名职务
     */
    @RequiresPermissions("project:nameduty:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsNameWords trsNameWords = trsNameWordsService.selectTrsNameWordsById(id);
        mmap.put("trsNameWords", trsNameWords);
        return prefix + "/edit";
    }

    /**
     * 修改保存人名职务
     */
    @RequiresPermissions("project:nameduty:edit")
    @Log(title = "人名职务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsNameWords trsNameWords)
    {
        trsNameWords.setTime(new Date());
        trsNameWords.setStatus("待审核");
        return toAjax(trsNameWordsService.updateTrsNameWords(trsNameWords));
    }

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

    /**
     * 删除人名职务
     */
    @RequiresPermissions("project:nameduty:remove")
    @Log(title = "人名职务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsNameWordsService.deleteTrsNameWordsByIds(ids));
    }

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