package com.ruoyi.project.controller;

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

import com.ruoyi.project.domain.TrsNameResults;
import com.ruoyi.project.domain.TrsSensitiveResults;
import com.ruoyi.project.domain.TrsSiteconfig;
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 com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.TrsPolicyResults;
import com.ruoyi.project.service.ITrsPolicyResultsService;
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;

/**
 * 政治常识监测Controller
 * 
 * @author ruoyi
 * @date 2025-03-24
 */
@Controller
@RequestMapping("/project/policyresults")
public class TrsPolicyResultsController extends BaseController
{
    private String prefix = "project/policyresults";

    @Autowired
    private ITrsPolicyResultsService trsPolicyResultsService;
    @Autowired
    private ITrsSiteconfigService trsSiteconfigService;

    @RequiresPermissions("project:wxpolicyresults:view")
    @GetMapping()
    public String policyresults(ModelMap mmap)
    {
        // 获取站点列表并添加到Model中
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg("site");
        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 + "/policyresults";
    }

    /**
     * 查询政治常识监测列表
     */
    @RequiresPermissions("project:wxpolicyresults:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsPolicyResults trsPolicyResults)
    {
        startPage();
        List<TrsPolicyResults> list = trsPolicyResultsService.selectTrsPolicyResultsList(trsPolicyResults);
        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) {
        TrsPolicyResults results = trsPolicyResultsService.selectTrsPolicyResultsById(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.getErrorType());
        map.put("count", 1);
        errors.add(map);
        result.put("content", content);
        result.put("errors", errors);
        return result;
    }


    /**
     * 查询常见错误监测统计
     */
    @RequiresPermissions("project:wxpolicyresults:list")
    @RequestMapping ("/statistics")
    @ResponseBody
    public AjaxResult  getStatistics(TrsPolicyResults trsSensitiveResults)
    {
        Map<String, Integer> stats = trsPolicyResultsService.getStatistics(trsSensitiveResults);
        return AjaxResult.success(stats);
    }

    /**
     * 导出政治常识监测列表
     */
    @RequiresPermissions("project:wxpolicyresults:export")
    @Log(title = "政治常识监测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsPolicyResults trsPolicyResults)
    {
        List<TrsPolicyResults> list = trsPolicyResultsService.selectTrsPolicyResultsList(trsPolicyResults);
        int serialNumber = 1;
        for (TrsPolicyResults item : list) {
            item.setIndex(serialNumber++);
            if ("site".equals(item.getSitetag())) {
                item.setSitetag("网站");
            } else if ("weixin".equals(item.getSitetag())) {
                item.setSitetag("微信");
            } else if ("weibo".equals(item.getSitetag())) {
                item.setSitetag("微博");
            } else if ("douyin".equals(item.getSitetag())) {
                item.setSitetag("抖音");
            } else if ("kuaishou".equals(item.getSitetag())) {
                item.setSitetag("快手");
            } else if ("toutiao".equals(item.getSitetag())) {
                item.setSitetag("头条");
            }
        }
        ExcelUtil<TrsPolicyResults> util = new ExcelUtil<TrsPolicyResults>(TrsPolicyResults.class);
        return util.exportExcel(list, "政治常识监测数据");
    }

    /**
     * 新增政治常识监测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存政治常识监测
     */
    @RequiresPermissions("project:wxpolicyresults:add")
    @Log(title = "政治常识监测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsPolicyResults trsPolicyResults)
    {
        return toAjax(trsPolicyResultsService.insertTrsPolicyResults(trsPolicyResults));
    }

    /**
     * 修改政治常识监测
     */
    @RequiresPermissions("project:wxpolicyresults:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsPolicyResults trsPolicyResults = trsPolicyResultsService.selectTrsPolicyResultsById(id);
        mmap.put("trsPolicyResults", trsPolicyResults);
        return prefix + "/edit";
    }

    /**
     * 修改保存政治常识监测
     */
    @RequiresPermissions("project:wxpolicyresults:edit")
    @Log(title = "政治常识监测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsPolicyResults trsPolicyResults)
    {
        return toAjax(trsPolicyResultsService.updateTrsPolicyResults(trsPolicyResults));
    }

    /**
     * 修改保存常见错误监测
     */
    @RequiresPermissions("project:wxpolicyresults:edit")
    @Log(title = "常见错误监测", businessType = BusinessType.UPDATE)
    @PostMapping("/biaoji")
    @ResponseBody
    public AjaxResult biaoji(String ids, TrsPolicyResults trsSensitiveResults)
    {
        Integer count = 0;
        for (String id : ids.split(",")) {
            trsSensitiveResults.setId(Long.parseLong(id));
            trsSensitiveResults.setStatus(trsSensitiveResults.getStatus());
            trsPolicyResultsService.updateTrsPolicyResults(trsSensitiveResults);
        }
        return toAjax(count);
    }

    /**
     * 删除政治常识监测
     */
    @RequiresPermissions("project:wxpolicyresults:remove")
    @Log(title = "政治常识监测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsPolicyResultsService.deleteTrsPolicyResultsByIds(ids));
    }
}
