<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.project.mapper.TrsResultsLogStatsSimpleMapper">

    <!-- 1. 总体活跃程度 - 优化版本 -->
    <select id="selectTotalStats" resultType="java.util.Map">
        SELECT
        COUNT(DISTINCT t.id) as totalLogCount,
        COUNT(DISTINCT w.id) as totalWordCount,
        COUNT(DISTINCT t.user_id) as activeUserCount,
        COUNT(DISTINCT root_dept_id) as activeDeptCount
        FROM trs_results_log t
        LEFT JOIN trs_word_results w ON t.id = w.log_id
        LEFT JOIN (
        -- 盟市级别
        SELECT dept_id, dept_id as root_dept_id
        FROM sys_dept
        WHERE parent_id = 100 AND del_flag = 0
        UNION
        -- 旗县区级别
        SELECT d2.dept_id, d1.dept_id as root_dept_id
        FROM sys_dept d1
        INNER JOIN sys_dept d2 ON d2.parent_id = d1.dept_id
        WHERE d1.parent_id = 100 AND d1.del_flag = 0 AND d2.del_flag = 0
        ) dept_map ON t.dept_id = dept_map.dept_id
        <where>
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
    </select>

    <!-- 2. 按内蒙古自治区直接下级部门统计 - 优化版本 -->
    <select id="selectDeptStats" resultType="java.util.Map">
        SELECT
        dept_map.root_dept_id as deptId,
        dept_map.root_dept_name as deptName,
        COUNT(DISTINCT t.user_id) as userCount,
        COUNT(DISTINCT t.id) as logCount,
        COUNT(DISTINCT w.id) as wordCount
        FROM (
        -- 盟市级别
        SELECT
        d1.dept_id as dept_id,
        d1.dept_id as root_dept_id,
        d1.dept_name as root_dept_name
        FROM sys_dept d1
        WHERE d1.parent_id = 100 and d1.del_flag = 0
        UNION ALL
        -- 旗县区级别
        SELECT
        d2.dept_id as dept_id,
        d1.dept_id as root_dept_id,
        d1.dept_name as root_dept_name
        FROM sys_dept d1
        INNER JOIN sys_dept d2 ON d2.parent_id = d1.dept_id
        WHERE d1.parent_id = 100 and d1.del_flag = 0 and d2.del_flag = 0
        ) dept_map
        LEFT JOIN trs_results_log t ON dept_map.dept_id = t.dept_id
        LEFT JOIN trs_word_results w ON t.id = w.log_id
        <where>
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY dept_map.root_dept_id, dept_map.root_dept_name
        ORDER BY logCount DESC
    </select>

    <!-- 2.1 部门下的活跃用户详情 - 优化索引使用 -->
    <select id="selectDeptActiveUsers" resultType="java.util.Map">
        SELECT
        t.user_id as userId,
        MAX(u.user_name) as userName,
        MAX(u.login_name) as loginName,
        COUNT(t.id) as logCount,
        COUNT(DISTINCT w.id) as wordCount
        FROM trs_results_log t
        INNER JOIN (
        SELECT dept_id FROM sys_dept WHERE parent_id = #{rootDeptId} AND del_flag = 0
        UNION
        SELECT dept_id FROM sys_dept WHERE parent_id IN (
        SELECT dept_id FROM sys_dept WHERE parent_id = #{rootDeptId} AND del_flag = 0
        ) AND del_flag = 0
        ) depts ON t.dept_id = depts.dept_id
        LEFT JOIN sys_user u ON t.user_id = u.user_id
        LEFT JOIN trs_word_results w ON t.id = w.log_id
        <where>
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY t.user_id
        ORDER BY logCount DESC
        LIMIT 50
    </select>

    <!-- 3. 自定义词增加排行 - 优化索引 -->
    <select id="selectCustomWordRank" resultType="java.util.Map">
        SELECT * FROM (
        -- 个人排名
        SELECT
        'user' as type,
        u.user_id as id,
        u.user_name as name,
        u.login_name as loginName,
        d.dept_name as deptName,
        COUNT(w.id) as addCount
        FROM trs_word_results w
        INNER JOIN trs_results_log t ON w.log_id = t.id AND w.source = 'custom'
        INNER JOIN sys_user u ON t.user_id = u.user_id
        INNER JOIN sys_dept d ON t.dept_id = d.dept_id
        <where>
            <if test="beginTime != null">
                AND w.create_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND w.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY u.user_id, u.user_name, u.login_name, d.dept_name
        HAVING COUNT(w.id) > 0

        UNION ALL

        -- 部门排名
        SELECT
        'dept' as type,
        d.dept_id as id,
        d.dept_name as name,
        '' as loginName,
        '' as deptName,
        COUNT(w.id) as addCount
        FROM trs_word_results w
        INNER JOIN trs_results_log t ON w.log_id = t.id AND w.source = 'custom'
        INNER JOIN sys_dept d ON t.dept_id = d.dept_id
        <where>
            <if test="beginTime != null">
                AND w.create_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND w.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY d.dept_id, d.dept_name
        HAVING COUNT(w.id) > 0
        ) ranked
        ORDER BY addCount DESC
        LIMIT 20
    </select>

    <!-- 4. 错误类型占比 - 优化索引 -->
    <select id="selectErrorTypeRatio" resultType="java.util.Map">
        SELECT
        w.error_type as errorType,
        COUNT(w.id) as count
        FROM trs_word_results w
        INNER JOIN trs_results_log t ON w.log_id = t.id
        <where>
            w.error_type IS NOT NULL
            AND w.error_type != ''
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY w.error_type
        ORDER BY count DESC
    </select>

    <!-- 5. 趋势数据（按天统计）- 优化索引 -->
    <select id="selectTrendData" resultType="java.util.Map">
        SELECT
        DATE_FORMAT(t.time, '%Y-%m-%d') as date,
        COUNT(t.id) as logCount,
        COUNT(DISTINCT w.id) as wordCount,
        COUNT(DISTINCT t.user_id) as userCount
        FROM trs_results_log t
        LEFT JOIN trs_word_results w ON t.id = w.log_id
        <where>
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY DATE_FORMAT(t.time, '%Y-%m-%d')
        ORDER BY date
    </select>

    <!-- 部门校对次数统计 - 优化版本 -->
    <select id="selectDeptProofreadStats" parameterType="map" resultType="com.ruoyi.project.domain.DeptProofreadStatsVo">
        SELECT
        dept_map.root_dept_name AS deptName,
        COUNT(DISTINCT t.id) AS proofreadCount
        FROM (
        -- 盟市级别
        SELECT
        d1.dept_id as dept_id,
        d1.dept_name as root_dept_name
        FROM sys_dept d1
        WHERE d1.parent_id = 100 AND d1.del_flag = 0
        UNION ALL
        -- 旗县区级别
        SELECT
        d2.dept_id as dept_id,
        d1.dept_name as root_dept_name
        FROM sys_dept d1
        INNER JOIN sys_dept d2 ON d2.parent_id = d1.dept_id
        WHERE d1.parent_id = 100 AND d1.del_flag = 0 AND d2.del_flag = 0
        ) dept_map
        LEFT JOIN trs_results_log t ON dept_map.dept_id = t.dept_id
        <where>
            <if test="beginTime != null">
                AND t.time &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                AND t.time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY dept_map.root_dept_name
        ORDER BY proofreadCount DESC
    </select>

</mapper>