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.TrsTechnicalWords;
import com.ruoyi.project.service.ITrsTechnicalWordsService;
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/technical")
public class TrsTechnicalWordsController extends BaseController
{
    private String prefix = "project/technical";

    @Autowired
    private ITrsTechnicalWordsService trsTechnicalWordsService;

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

    /**
     * 查询专业术语列表
     */
    @RequiresPermissions("project:technical:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsTechnicalWords trsTechnicalWords)
    {
        startPage();
        List<TrsTechnicalWords> list = trsTechnicalWordsService.selectTrsTechnicalWordsList(trsTechnicalWords);
        return getDataTable(list);
    }

    /**
     * 导出专业术语列表
     */
    @RequiresPermissions("project:technical:export")
    @Log(title = "专业术语", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsTechnicalWords trsTechnicalWords)
    {
        List<TrsTechnicalWords> list = trsTechnicalWordsService.selectTrsTechnicalWordsList(trsTechnicalWords);
        ExcelUtil<TrsTechnicalWords> util = new ExcelUtil<TrsTechnicalWords>(TrsTechnicalWords.class);
        return util.exportExcel(list, "专业术语数据");
    }

    /**
     * 新增专业术语
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专业术语
     */
    @RequiresPermissions("project:technical:add")
    @Log(title = "专业术语", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsTechnicalWords trsTechnicalWords)
    {
        trsTechnicalWords.setTime(new Date());
        trsTechnicalWords.setStatus("待审核");
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 100L;
        }
        trsTechnicalWords.setDeptId(deptId);
        int status = trsTechnicalWordsService.insertTrsTechnicalWords(trsTechnicalWords);
        if(401 == status){
            return AjaxResult.warn("数据已存在");
        }else{
            return toAjax(status);
        }
    }

    /**
     * 修改专业术语
     */
    @RequiresPermissions("project:technical:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsTechnicalWords trsTechnicalWords = trsTechnicalWordsService.selectTrsTechnicalWordsById(id);
        mmap.put("trsTechnicalWords", trsTechnicalWords);
        return prefix + "/edit";
    }

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

    /**
     * 修改保存专业术语
     */
    @RequiresPermissions("project:technical:edit")
    @Log(title = "专业术语", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsTechnicalWords trsTechnicalWords)
    {
        trsTechnicalWords.setTime(new Date());
        trsTechnicalWords.setStatus("待审核");
        return toAjax(trsTechnicalWordsService.updateTrsTechnicalWords(trsTechnicalWords));
    }

    /**
     * 删除专业术语
     */
    @RequiresPermissions("project:technical:remove")
    @Log(title = "专业术语", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsTechnicalWordsService.deleteTrsTechnicalWordsByIds(ids));
    }

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