package com.ruoyi.project.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.TrsPersonResult;
import com.ruoyi.project.domain.TrsSiteconfig;
import com.ruoyi.project.service.ITrsPersonResultService;
import com.ruoyi.project.service.ITrsSiteconfigService;
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.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人信息隐私检测Controller
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Controller
@RequestMapping("/project/wxpersionresult")
public class TrsWxPersonResultController extends BaseController
{
    private String prefix = "project/wxpersionresult";

    @Autowired
    private ITrsPersonResultService trsPersonResultService;
    @Autowired
    private ITrsSiteconfigService trsSiteconfigService;

    @RequiresPermissions("project:wxpersionresult:view")
    @GetMapping()
    public String persionresult(ModelMap mmap)
    {
        // 获取站点列表并添加到Model中
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg("weixin");
        List<TrsSiteconfig> list = trsSiteconfigService.selectTrsSiteconfigList(trsSiteconfig);
        for (TrsSiteconfig siteconfig : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("sitename", siteconfig.getSitename());
            sites.add(map);
        }
        mmap.put("sites", sites);
        return prefix + "/persionresult";
    }

    @RequiresPermissions("project:wxpersionresult:view")
    @GetMapping("/getSitesByType")
    @ResponseBody
    public AjaxResult getSitesByType(String type) {
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg(type); // 使用传入的type参数
        List<TrsSiteconfig> list = trsSiteconfigService.selectTrsSiteconfigList(trsSiteconfig);

        for (TrsSiteconfig siteconfig : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("sitename", siteconfig.getSitename());
            sites.add(map);
        }

        return AjaxResult.success(sites);
    }

    /**
     * 查询个人信息隐私检测列表
     */
    @RequiresPermissions("project:wxpersionresult:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsPersonResult trsPersonResult)
    {
        startPage();
        List<TrsPersonResult> list = trsPersonResultService.selectTrsPersonResultList(trsPersonResult);
        return getDataTable(list);
    }

    @RequestMapping("/mark/{id}")
    public String markPage(@PathVariable("id") Long id,ModelMap mmap) {
        Map<String, Object> data = new HashMap<>();
        data.put("content", "这是包含错别字的示例内容...");

        List<Map<String, String>> errors = new ArrayList<>();
        Map<String, String> demoText = new HashMap<>();
        demoText.put("word", "错别字");
        demoText.put("correct", "正确词");
        demoText.put("count", "2");
        errors.add(demoText);

        // 2. 添加数据到模型
        mmap.addAttribute("content", data.get("content"));
        mmap.addAttribute("errors", errors);
        // 2. 添加数据到模型
        mmap.addAttribute("dataid", id);
        return prefix + "/mark";
    }

    @PostMapping("/analyze")
    @ResponseBody
    public AjaxResult analyzeText(@RequestParam String content) {
        // 这里可以添加文本分析逻辑（如使用敏感词库）
        // 本例直接返回空结果，由前端管理错词列表
        return AjaxResult.success();
    }

    @GetMapping("/markData/{id}")
    @ResponseBody
    public Map<String, Object> getMarkData(@PathVariable("id") Long id) {
        TrsPersonResult results = trsPersonResultService.selectTrsPersonResultById(id);

        Map<String, Object> result = new HashMap<>();

        // 假内容
        String content = results.getAllcontext();

        // 假错词数据
        List<Map<String, Object>> errors = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("word", results.getWrongWord());
        map.put("correct", results.getWrongType());
        map.put("count", 1);
        errors.add(map);
        result.put("content", content);
        result.put("errors", errors);
        return result;
    }


    /**
     * 导出个人信息隐私检测列表
     */
    @RequiresPermissions("project:wxpersionresult:export")
    @Log(title = "个人信息隐私检测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsPersonResult trsPersonResult)
    {
        List<TrsPersonResult> list = trsPersonResultService.selectTrsPersonResultList(trsPersonResult);
        ExcelUtil<TrsPersonResult> util = new ExcelUtil<TrsPersonResult>(TrsPersonResult.class);
        return util.exportExcel(list, "个人信息隐私检测数据");
    }

    /**
     * 新增个人信息隐私检测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存个人信息隐私检测
     */
    @RequiresPermissions("project:wxpersionresult:add")
    @Log(title = "个人信息隐私检测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsPersonResult trsPersonResult)
    {
        return toAjax(trsPersonResultService.insertTrsPersonResult(trsPersonResult));
    }

    /**
     * 修改个人信息隐私检测
     */
    @RequiresPermissions("project:wxpersionresult:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsPersonResult trsPersonResult = trsPersonResultService.selectTrsPersonResultById(id);
        mmap.put("trsPersonResult", trsPersonResult);
        return prefix + "/edit";
    }

    /**
     * 修改保存个人信息隐私检测
     */
    @RequiresPermissions("project:wxpersionresult:edit")
    @Log(title = "个人信息隐私检测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsPersonResult trsPersonResult)
    {
        return toAjax(trsPersonResultService.updateTrsPersonResult(trsPersonResult));
    }

    /**
     * 删除个人信息隐私检测
     */
    @RequiresPermissions("project:wxpersionresult:remove")
    @Log(title = "个人信息隐私检测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsPersonResultService.deleteTrsPersonResultByIds(ids));
    }
}
