package com.ruoyi.project.controller;

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

import com.ruoyi.project.domain.TrsDomainWords;
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.TrsErrorUrl;
import com.ruoyi.project.service.ITrsErrorUrlService;
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-04-01
 */
@Controller
@RequestMapping("/project/errorurl")
public class TrsErrorUrlController extends BaseController
{
    private String prefix = "project/errorurl";

    @Autowired
    private ITrsErrorUrlService trsErrorUrlService;

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

    /**
     * 查询敏感链接列表
     */
    @RequiresPermissions("project:errorurl:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsErrorUrl trsErrorUrl)
    {
        startPage();
        List<TrsErrorUrl> list = trsErrorUrlService.selectTrsErrorUrlList(trsErrorUrl);
        return getDataTable(list);
    }

    /**
     * 导出敏感链接列表
     */
    @RequiresPermissions("project:errorurl:export")
    @Log(title = "敏感链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsErrorUrl trsErrorUrl)
    {
        List<TrsErrorUrl> list = trsErrorUrlService.selectTrsErrorUrlList(trsErrorUrl);
        ExcelUtil<TrsErrorUrl> util = new ExcelUtil<TrsErrorUrl>(TrsErrorUrl.class);
        return util.exportExcel(list, "敏感链接数据");
    }

    /**
     * 新增敏感链接
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存敏感链接
     */
    @RequiresPermissions("project:errorurl:add")
    @Log(title = "敏感链接", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsErrorUrl trsErrorUrl)
    {
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 0L;
        }
        trsErrorUrl.setDeptId(deptId);
        trsErrorUrl.setTime(new Date());
        return toAjax(trsErrorUrlService.insertTrsErrorUrl(trsErrorUrl));
    }

    /**
     * 修改敏感链接
     */
    @RequiresPermissions("project:errorurl:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsErrorUrl trsErrorUrl = trsErrorUrlService.selectTrsErrorUrlById(id);
        mmap.put("trsErrorUrl", trsErrorUrl);
        return prefix + "/edit";
    }

    /**
     * 修改保存敏感链接
     */
    @RequiresPermissions("project:errorurl:edit")
    @Log(title = "敏感链接", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsErrorUrl trsErrorUrl)
    {
        trsErrorUrl.setTime(new Date());
        return toAjax(trsErrorUrlService.updateTrsErrorUrl(trsErrorUrl));
    }

    /**
     * 删除敏感链接
     */
    @RequiresPermissions("project:errorurl:remove")
    @Log(title = "敏感链接", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsErrorUrlService.deleteTrsErrorUrlByIds(ids));
    }

    @RequiresPermissions("system:errorurl:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TrsErrorUrl> util = new ExcelUtil<TrsErrorUrl>(TrsErrorUrl.class);
        return util.importTemplateExcel("敏感链接数据");
    }

    @Log(title = "敏感链接管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:errorurl:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TrsErrorUrl> util = new ExcelUtil<TrsErrorUrl>(TrsErrorUrl.class);
        List<TrsErrorUrl> wordList = util.importExcel(file.getInputStream());
        Long deptId = getSysUser().getDeptId();
        if(deptId == 1){
            deptId = 0L;
        }
        String message = trsErrorUrlService.importWords(wordList, updateSupport, deptId);
        return AjaxResult.success(message);
    }

}
