<?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.hotwj.platform.securityManagement.trainingParticipant.mapper.HotTrainingParticipantMapper">

    <select id="selectDriverLearningRecordPage"
            resultType="com.hotwj.platform.securityManagement.trainingParticipant.domain.vo.DriverLearningRecordVo">
        SELECT p.id             AS id,
               p.training_id    AS trainingId,
               t.plan_name      AS planName,
               t.start_time     AS startTime,
               t.end_time       AS endTime,
               t.is_enabled     AS isEnabled,
               t.is_make_up     AS isMakeUp,
               p.is_completed   AS isCompleted,
               p.learn_progress AS learnProgress,
               p.is_pass        AS isPassed
        FROM hot_training_participant p
                 INNER JOIN hot_training t
                            ON t.id = p.training_id
                                AND t.company_id = p.company_id
                                AND t.is_deleted = 0
        WHERE p.company_id = #{companyId}
          AND p.user_id = #{userId}
          AND p.is_deleted = 0
          AND t.is_enabled = 1
          AND t.start_time IS NOT NULL
          AND t.end_time IS NOT NULL
          AND t.start_time &lt;= #{now}
          AND (
            t.end_time &gt;= #{now}
                OR (t.end_time &lt; #{now} AND t.is_make_up = 1)
            )
        ORDER BY t.start_time DESC, p.id DESC
    </select>

    <select id="selectTrainingParticipantStats"
            resultType="com.hotwj.platform.securityManagement.trainingParticipant.domain.vo.TrainingParticipantStatVo">
        SELECT
            tp.training_id                                        AS trainingId,
            COUNT(*)                                              AS totalCount,
            SUM(CASE WHEN tp.is_pass = 1 THEN 1 ELSE 0 END)      AS passedCount,
            SUM(CASE WHEN tp.is_completed = 0 THEN 1 ELSE 0 END) AS incompleteCount,
        SUM(CASE WHEN tp.is_completed = 1 THEN 1 ELSE 0 END) AS completeCount,
            SUM(CASE WHEN tp.is_pass IS NULL AND tp.is_completed = 1 THEN 1 ELSE 0 END)  AS pendingReviewCount
        FROM hot_training_participant tp
        WHERE tp.is_deleted = 0
          AND tp.training_id IN
          <foreach collection="trainingIds" item="id" open="(" separator="," close=")">
              #{id}
          </foreach>
        GROUP BY tp.training_id
    </select>

</mapper>
