package com.ruoyi.project.controller;

import java.util.Date;
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.project.domain.TrsDomainWords;
import com.ruoyi.project.service.ITrsDomainWordsService;
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/domain")
public class TrsDomainWordsController extends BaseController
{
    private String prefix = "project/domain";

    @Autowired
    private ITrsDomainWordsService trsDomainWordsService;

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

    /**
     * 查询领域常识列表
     */
    @RequiresPermissions("project:domain:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsDomainWords trsDomainWords)
    {
        startPage();
        List<TrsDomainWords> list = trsDomainWordsService.selectTrsDomainWordsList(trsDomainWords);
        return getDataTable(list);
    }

    /**
     * 导出领域常识列表
     */
    @RequiresPermissions("project:domain:export")
    @Log(title = "领域常识", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsDomainWords trsDomainWords)
    {
        List<TrsDomainWords> list = trsDomainWordsService.selectTrsDomainWordsList(trsDomainWords);
        ExcelUtil<TrsDomainWords> util = new ExcelUtil<TrsDomainWords>(TrsDomainWords.class);
        return util.exportExcel(list, "领域常识数据");
    }

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

    /**
     * 新增保存领域常识
     */
    @RequiresPermissions("project:domain:add")
    @Log(title = "领域常识", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsDomainWords trsDomainWords)
    {
        trsDomainWords.setTime(new Date());
        trsDomainWords.setStatus("待审核");
        int status = trsDomainWordsService.insertTrsDomainWords(trsDomainWords);
        if(401 == status){
            return AjaxResult.warn("数据已存在");
        }else{
            return toAjax(status);
        }}

    /**
     * 修改领域常识
     */
    @RequiresPermissions("project:domain:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsDomainWords trsDomainWords = trsDomainWordsService.selectTrsDomainWordsById(id);
        mmap.put("trsDomainWords", trsDomainWords);
        return prefix + "/edit";
    }

    /**
     * 修改保存领域常识
     */
    @RequiresPermissions("project:domain:edit")
    @Log(title = "领域常识", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsDomainWords trsDomainWords)
    {
        trsDomainWords.setTime(new Date());
        trsDomainWords.setStatus("待审核");
        return toAjax(trsDomainWordsService.updateTrsDomainWords(trsDomainWords));
    }

    @RequiresPermissions("project:domain:edit")
    @Log(title = "常见错误", businessType = BusinessType.UPDATE)
    @PostMapping("/editstatus")
    @ResponseBody
    public AjaxResult editStatusSave(String ids, String status)
    {
        for (String id : ids.split(",")) {
            TrsDomainWords trsDomainWords = trsDomainWordsService.selectTrsDomainWordsById(Long.parseLong(id));
            trsDomainWords.setStatus(status);
            trsDomainWords.setTime(new Date());
            trsDomainWordsService.updateTrsDomainWords(trsDomainWords);
        }
        return toAjax(1);
    }

    /**
     * 删除领域常识
     */
    @RequiresPermissions("project:domain:remove")
    @Log(title = "领域常识", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsDomainWordsService.deleteTrsDomainWordsByIds(ids));
    }

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

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