<?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.sundot.airport.exam.mapper.DailyTaskMapper">

    <!-- 按部门统计任务完成情况 -->
    <select id="statisticsByDept" resultType="com.sundot.airport.exam.dto.DailyTaskDeptStatisticsDTO">
        SELECT
            dt_dept_id AS deptId,
            dt_dept_name AS deptName,
            COUNT(*) AS totalCount,
            SUM(CASE WHEN dt_status = 'PENDING' THEN 1 ELSE 0 END) AS pendingCount,
            SUM(CASE WHEN dt_status = 'IN_PROGRESS' THEN 1 ELSE 0 END) AS inProgressCount,
            SUM(CASE WHEN dt_status = 'COMPLETED' THEN 1 ELSE 0 END) AS completedCount,
            SUM(CASE WHEN dt_status = 'EXPIRED' THEN 1 ELSE 0 END) AS expiredCount,
            ROUND(
                SUM(CASE WHEN dt_status = 'COMPLETED' THEN 1 ELSE 0 END) * 100.0 / COUNT(*),
                2
            ) AS completionRate
        FROM edu_cs_daily_task
        WHERE del_status = 0
        <if test="query.deptId != null">
            AND dt_dept_id = #{query.deptId}
        </if>
        <if test="query.deptName != null and query.deptName != ''">
            AND dt_dept_name LIKE CONCAT('%', #{query.deptName}, '%')
        </if>
        <if test="query.role != null and query.role != ''">
            AND dt_role = #{query.role}
        </if>
        <if test="query.businessDateStart != null">
            AND dt_business_date &gt;= #{query.businessDateStart}
        </if>
        <if test="query.businessDateEnd != null">
            AND dt_business_date &lt;= #{query.businessDateEnd}
        </if>
        <if test="query.createTimeStart != null">
            AND create_time &gt;= #{query.createTimeStart}
        </if>
        <if test="query.createTimeEnd != null">
            AND create_time &lt;= #{query.createTimeEnd}
        </if>
        GROUP BY dt_dept_id, dt_dept_name
        HAVING dt_dept_id IS NOT NULL
        ORDER BY totalCount DESC, completionRate DESC
    </select>

    <!-- 按用户统计任务完成情况 -->
    <select id="statisticsByUser" resultType="com.sundot.airport.exam.dto.DailyTaskUserStatisticsDTO">
        SELECT
            dt_user_id AS userId,
            dt_user_name AS userName,
            dt_dept_id AS deptId,
            dt_dept_name AS deptName,
            dt_role AS role,
            COUNT(*) AS totalCount,
            SUM(CASE WHEN dt_status = 'PENDING' THEN 1 ELSE 0 END) AS pendingCount,
            SUM(CASE WHEN dt_status = 'IN_PROGRESS' THEN 1 ELSE 0 END) AS inProgressCount,
            SUM(CASE WHEN dt_status = 'COMPLETED' THEN 1 ELSE 0 END) AS completedCount,
            SUM(CASE WHEN dt_status = 'EXPIRED' THEN 1 ELSE 0 END) AS expiredCount,
            ROUND(
                SUM(CASE WHEN dt_status = 'COMPLETED' THEN 1 ELSE 0 END) * 100.0 / COUNT(*),
                2
            ) AS completionRate,
            ROUND(AVG(dt_total_score), 2) AS avgScore
        FROM edu_cs_daily_task
        WHERE del_status = 0
        <if test="query.userId != null">
            AND dt_user_id = #{query.userId}
        </if>
        <if test="query.userName != null and query.userName != ''">
            AND dt_user_name LIKE CONCAT('%', #{query.userName}, '%')
        </if>
        <if test="query.deptId != null">
            AND dt_dept_id = #{query.deptId}
        </if>
        <if test="query.deptName != null and query.deptName != ''">
            AND dt_dept_name LIKE CONCAT('%', #{query.deptName}, '%')
        </if>
        <if test="query.role != null and query.role != ''">
            AND dt_role = #{query.role}
        </if>
        <if test="query.businessDateStart != null">
            AND dt_business_date &gt;= #{query.businessDateStart}
        </if>
        <if test="query.businessDateEnd != null">
            AND dt_business_date &lt;= #{query.businessDateEnd}
        </if>
        <if test="query.createTimeStart != null">
            AND create_time &gt;= #{query.createTimeStart}
        </if>
        <if test="query.createTimeEnd != null">
            AND create_time &lt;= #{query.createTimeEnd}
        </if>
        GROUP BY dt_user_id, dt_user_name, dt_dept_id, dt_dept_name, dt_role
        ORDER BY totalCount DESC, completionRate DESC
    </select>

</mapper>
