package com.ruoyi.project.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.project.domain.TrsSiteconfig;
import com.ruoyi.project.domain.TrsStatisticsData;

import com.ruoyi.project.service.ITrsSiteconfigService;
import com.ruoyi.project.service.ITrsStatisticsService;
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.*;

@Controller
@RequestMapping("/project/statistics")
public class TrsStatisticsController extends BaseController {
    private String prefix = "project/statistics";

    @Autowired
    private ITrsStatisticsService trsStatisticsService;

    @Autowired
    private ITrsSiteconfigService trsSiteconfigService;

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

    /**
     * 获取统计数据
     */
//    @RequiresPermissions("module:statistics:view")
    @PostMapping("/getData")
    @ResponseBody
    public AjaxResult getData(@RequestParam(required = false) String sitename,
                              @RequestParam(required = false) String startTime,
                              @RequestParam(required = false) String endTime) {
        try {
            Date startDate = StringUtils.isNotEmpty(startTime) ? DateUtils.parseDate(startTime) : null;
            Date endDate = StringUtils.isNotEmpty(endTime) ? DateUtils.parseDate(endTime) : null;

            TrsStatisticsData data = new TrsStatisticsData();

            // 1. 处理文本统计
            int totalTexts = trsStatisticsService.getTotalTexts(sitename, startDate, endDate);
            int problemTexts = trsStatisticsService.getProblemTexts(sitename, startDate, endDate);
            data.setTotalTexts(totalTexts);
            data.setProblemTexts(problemTexts);

            // 2. 高频错词统计
            List<Map<String, Object>> highFrequencyErrors = trsStatisticsService.getHighFrequencyErrors(sitename, startDate, endDate, 10);
            data.setHighFrequencyErrors(highFrequencyErrors);

            // 3. 常见错误统计
            Map<String, Object> commonErrors = trsStatisticsService.getCommonErrors(sitename, startDate, endDate);
            data.setCommonErrors(commonErrors);

            // 4. 政治常识错误统计
            Map<String, Object> politicalErrors = trsStatisticsService.getPoliticalErrors(sitename, startDate, endDate);
            data.setPoliticalErrors(politicalErrors);

            return success(data);
        } catch (Exception e) {
            logger.error("获取统计数据失败", e);
            return error("获取统计数据失败：" + e.getMessage());
        }
    }


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

    /**
     * 获取统计数据
     */
//    @RequiresPermissions("module:statistics:view")
    @PostMapping("/getJdData")
    @ResponseBody
    public AjaxResult getJdData(@RequestParam(required = false) String sitename,
                              @RequestParam(required = false) String startTime,
                              @RequestParam(required = false) String endTime) {
        try {
            Date startDate = null;
            Date endDate = null;

            // 解析开始时间（设置为当天的 00:00:00）
            if (StringUtils.isNotEmpty(startTime)) {
                startTime = startTime + " 00:00:00";
                startDate = DateUtils.parseDate(startTime);
            }

            // 解析结束时间（设置为当天的 23:59:59）
            if (StringUtils.isNotEmpty(endTime)) {
                endTime = endTime + " 23:59:59";
                endDate = DateUtils.parseDate(endTime);
            }

            TrsStatisticsData data = new TrsStatisticsData();
            sitename = null;
            // 1. 处理文本统计
            int totalTexts = trsStatisticsService.getTotalTexts_jd(sitename, startDate, endDate);
            int problemTexts = trsStatisticsService.getProblemTexts_jd(sitename, startDate, endDate);
            data.setTotalTexts(totalTexts);
            data.setProblemTexts(problemTexts);

            // 2. 高频错词统计
            List<Map<String, Object>> highFrequencyErrors = trsStatisticsService.getHighFrequencyErrors_jd(sitename,"", startDate, endDate, 10);
            data.setHighFrequencyErrors(highFrequencyErrors);

            // 2. 高频错词统计
            List<Map<String, Object>> errorFrequencyErrors = trsStatisticsService.getHighFrequencyErrors_jd(sitename,"1", startDate, endDate, 10);
            data.setErrorFrequencyErrors(errorFrequencyErrors);

            // 3. 常见错误统计
            Map<String, Object> commonErrors = trsStatisticsService.getCommonErrors_jd(sitename, startDate, endDate);
            data.setCommonErrors(commonErrors);

            // 4. 政治常识错误统计
            Map<String, Object> politicalErrors = trsStatisticsService.getPoliticalErrors_jd(sitename, startDate, endDate);
            data.setPoliticalErrors(politicalErrors);

            return success(data);
        } catch (Exception e) {
            logger.error("获取统计数据失败", e);
            return error("获取统计数据失败：" + e.getMessage());
        }
    }

}