<?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.reportStatistics.mapper.LedgerReportMapper">
    <select id="selectDriverInfoPage" resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverInfoLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.driver_license_no AS driverLicenseNo,
        d.driver_license_vehicle_type AS driverLicenseVehicleType,
        d.driver_license_first_issue_date AS driverLicenseFirstIssueDate,
        (CASE WHEN d.driver_license_long_term = 1 THEN '长期' ELSE DATE_FORMAT(d.driver_license_expire_date, '%Y-%m-%d')
        END) AS driverLicenseExpireDate,
        d.qualification_no AS qualificationNo,
        d.qualification_type AS qualificationType,
        d.qualification_first_issue_date AS qualificationFirstIssueDate,
        d.qualification_valid_end_date AS qualificationValidEndDate,
        d.plate_number AS plateNumber,
        (SELECT MIN(t.start_time) FROM hot_training t INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE tp.user_id = d.id AND t.is_deleted = 0) AS entryTrainingDate,
        NULL AS entryTrainingScore,
        (SELECT hr.report_date FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER BY
        hr.report_date DESC LIMIT 1) AS entryPhysicalExamDate,
        d.entry_date AS entryDate,
        (CASE WHEN d.status = 0 THEN d.update_time ELSE NULL END) AS leaveDate,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_accident_archive aa WHERE aa.driver_id = d.id AND aa.is_deleted = 0 AND
        (aa.death_toll > 0 OR aa.number_seriously_injured > 0 OR aa.number_minor_injuries > 0)) THEN '有' ELSE '无' END)
        AS hasAccident,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_violation_record vr WHERE vr.driver_id = d.id AND vr.is_deleted = 0 AND
        vr.is_drug_drunk_driving = 1) THEN '有' ELSE '无' END) AS hasDrugOrDrinkDrive,
        (CASE WHEN d.status = 1 THEN '是' ELSE '否' END) AS isEmployed
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND d.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.driver_license_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.startDate != null">
                AND d.entry_date &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND d.entry_date &lt;= #{bo.endDate}
            </if>
            <if test="bo.auditStatus != null">
                AND d.audit_status = #{bo.auditStatus}
            </if>
        </where>
        ORDER BY d.entry_date DESC
    </select>

    <!-- 从业人员岗前培训明细行（仅在线培训） -->
    <select id="selectDriverPreJobTrainingRows"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.security.PreJobTrainingDetail$PreJobTrainingRow">
        SELECT
        t.trainingTime,
        t.location,
        t.courseName,
        CAST(t.studyDuration AS UNSIGNED) AS studyDuration,
        CAST(t.scoreSum AS CHAR) AS examScore
        FROM (
        SELECT
        DATE_FORMAT(hcr.create_time, '%Y-%m-%d') AS trainingTime,
        ht.training_location AS location,
        htcc.course_name AS courseName,
        hc.class_hours AS studyDuration,
        SUM(har.gained_score) OVER (PARTITION BY DATE_FORMAT(hcr.create_time, '%Y-%m-%d'),
        ht.training_location, htcc.course_name, hc.class_hours) AS scoreSum,
        ROW_NUMBER() OVER (PARTITION BY DATE_FORMAT(hcr.create_time, '%Y-%m-%d'),
        ht.training_location, htcc.course_name, hc.class_hours ORDER BY hcr.create_time) AS rn
        FROM hot_training_course_record hcr
        INNER JOIN hot_training ht
        ON ht.id = hcr.training_id
        AND ht.is_deleted = 0
        AND ht.is_enabled = 1
        AND ht.training_type = 'pre-job'
        LEFT JOIN hot_training_course_config htcc
        ON htcc.id = hcr.training_course_id
        AND htcc.is_deleted = 0
        INNER JOIN hot_course hc
        ON hc.id = htcc.course_id
        LEFT JOIN hot_training_answer_record har
        ON har.company_id = hcr.company_id
        AND har.training_id = hcr.training_id
        AND har.course_id = htcc.course_id
        AND har.user_id = hcr.user_id
        <where>
            hcr.is_deleted = 0
            AND hcr.company_id = #{companyId}
            AND hcr.user_id = #{userId}
            <if test="trainingId != null">
                AND hcr.training_id = #{trainingId}
            </if>
        </where>
        ) t
        WHERE t.rn = 1
        ORDER BY t.trainingTime ASC
    </select>

    <select id="selectDriverPreJobTrainingIds" resultType="java.lang.Long">
        SELECT DISTINCT ht.id
        FROM hot_training ht
                 INNER JOIN hot_training_participant tp ON ht.id = tp.training_id
        WHERE tp.user_id = #{userId}
          AND ht.company_id = #{companyId}
          AND tp.is_deleted = 0
          AND ht.is_deleted = 0
          AND ht.is_enabled = 1
          AND ht.training_type = 'pre-job'
        ORDER BY ht.id DESC
    </select>

    <select id="selectDriverInfoList" resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverInfoLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.driver_license_no AS driverLicenseNo,
        d.driver_license_vehicle_type AS driverLicenseVehicleType,
        d.driver_license_first_issue_date AS driverLicenseFirstIssueDate,
        (CASE WHEN d.driver_license_long_term = 1 THEN '长期' ELSE DATE_FORMAT(d.driver_license_expire_date, '%Y-%m-%d')
        END) AS driverLicenseExpireDate,
        d.qualification_no AS qualificationNo,
        d.qualification_type AS qualificationType,
        d.qualification_first_issue_date AS qualificationFirstIssueDate,
        d.qualification_valid_end_date AS qualificationValidEndDate,
        d.plate_number AS plateNumber,
        (SELECT MIN(t.start_time) FROM hot_training t INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE tp.user_id = d.id AND t.is_deleted = 0) AS entryTrainingDate,
        NULL AS entryTrainingScore,
        (SELECT hr.report_date FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER BY
        hr.report_date DESC LIMIT 1) AS entryPhysicalExamDate,
        d.entry_date AS entryDate,
        (CASE WHEN d.status = 0 THEN d.update_time ELSE NULL END) AS leaveDate,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_accident_archive aa WHERE aa.driver_id = d.id AND aa.is_deleted = 0 AND
        (aa.death_toll > 0 OR aa.number_seriously_injured > 0 OR aa.number_minor_injuries > 0)) THEN '有' ELSE '无' END)
        AS hasAccident,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_violation_record vr WHERE vr.driver_id = d.id AND vr.is_deleted = 0 AND
        vr.is_drug_drunk_driving = 1) THEN '有' ELSE '无' END) AS hasDrugOrDrinkDrive,
        (CASE WHEN d.status = 1 THEN '是' ELSE '否' END) AS isEmployed
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND d.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.driver_license_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.startDate != null">
                AND d.entry_date &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND d.entry_date &lt;= #{bo.endDate}
            </if>
        </where>
        ORDER BY d.entry_date DESC
    </select>

    <select id="selectDriverHealthPage" resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverHealthLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.id_card_no AS idCardNo,
        d.gender AS gender,
        d.birth_date AS birthDate,
        (SELECT hr.report_date FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER BY
        hr.report_date DESC LIMIT 1) AS reportDate,
        (SELECT hr.health_status FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER
        BY hr.report_date DESC LIMIT 1) AS healthStatus,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_violation_record vr WHERE vr.driver_id = d.id AND vr.is_deleted = 0 AND
        vr.is_drug_drunk_driving = 1) THEN '有' ELSE '无' END) AS hasDrugOrDrinkDrive,
        NULL AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            AND d.audit_status = 1
            AND d.status = 1
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
        </where>
        ORDER BY d.create_time DESC
    </select>

    <select id="selectDriverHealthList" resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverHealthLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.id_card_no AS idCardNo,
        d.gender AS gender,
        d.birth_date AS birthDate,
        (SELECT hr.report_date FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER BY
        hr.report_date DESC LIMIT 1) AS reportDate,
        (SELECT hr.health_status FROM hot_driver_health_report hr WHERE hr.driver_id = d.id AND hr.is_deleted = 0 ORDER
        BY hr.report_date DESC LIMIT 1) AS healthStatus,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_violation_record vr WHERE vr.driver_id = d.id AND vr.is_deleted = 0 AND
        vr.is_drug_drunk_driving = 1) THEN '有' ELSE '无' END) AS hasDrugOrDrinkDrive,
        NULL AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            AND d.audit_status = 1
            AND d.status = 1
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
        </where>
        ORDER BY d.create_time DESC
    </select>

    <select id="selectProfCertScorePage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.ProfCertScoreLedgerVo">
        SELECT c.company_name                   AS companyName,
               d.name                           AS driverName,
               d.id_card_no                     AS idCardNo,
               d.qualification_no               AS certNo,
               d.qualification_valid_start_date AS validFrom,
               d.qualification_valid_end_date   AS validTo,
                (
                    SELECT DATE_FORMAT(vi.occur_time, '%Y-%m-%d %H:%i')
                    FROM hot_violation_info vi
                    WHERE vi.driver_id = d.id
                      AND vi.is_deleted = 0
                    ORDER BY vi.occur_time DESC
                    LIMIT 1
                )                                AS scoreTime,
                (
                    SELECT vi.violation_content
                    FROM hot_violation_info vi
                    WHERE vi.driver_id = d.id
                      AND vi.is_deleted = 0
                    ORDER BY vi.occur_time DESC
                    LIMIT 1
                )                                AS scoreItem,
                COALESCE((
                    SELECT vi.deduct_points
                    FROM hot_violation_info vi
                    WHERE vi.driver_id = d.id
                      AND vi.is_deleted = 0
                    ORDER BY vi.occur_time DESC
                    LIMIT 1
                ), 0)                            AS score,
                COALESCE((
                    SELECT SUM(vi.deduct_points)
                    FROM hot_violation_info vi
                    WHERE vi.driver_id = d.id
                      AND vi.is_deleted = 0
                      AND vi.deduction_type = 1
                ), 0)                            AS totalScore,
               (
                   CASE
                        WHEN EXISTS (
                            SELECT 1
                            FROM hot_violation_info vi
                            WHERE vi.driver_id = d.id
                              AND vi.is_deleted = 0
                              AND vi.need_interview = 1
                              AND (vi.interviewed_signature_image IS NULL OR vi.interviewed_signature_image = '')
                        ) THEN '否'
                       ELSE '是'
                       END
                   )                            AS isUrgeStudy,
                (
                    SELECT vi.remark
                    FROM hot_violation_info vi
                    WHERE vi.driver_id = d.id
                      AND vi.is_deleted = 0
                    ORDER BY vi.occur_time DESC
                    LIMIT 1
                )                                AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.qualification_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
            <if test="bo.auditStatus != null">
                AND d.audit_status = #{bo.auditStatus}
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            AND EXISTS (
                SELECT 1
                FROM hot_violation_info vi
                WHERE vi.driver_id = d.id
                  AND vi.is_deleted = 0
            )
        </where>
        ORDER BY d.create_time DESC
    </select>

    <select id="selectProfCertScoreList"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.ProfCertScoreLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.id_card_no AS idCardNo,
        d.qualification_no AS certNo,
        d.qualification_valid_start_date AS validFrom,
        d.qualification_valid_end_date AS validTo,
        (SELECT DATE_FORMAT(vi.occur_time, '%Y-%m-%d %H:%i') FROM hot_violation_info vi WHERE vi.driver_id = d.id AND
        vi.is_deleted = 0 ORDER BY vi.occur_time DESC LIMIT 1) AS scoreTime,
        (SELECT vi.violation_content FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.is_deleted = 0
        ORDER BY vi.occur_time DESC LIMIT 1) AS scoreItem,
        COALESCE((SELECT vi.deduct_points FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.is_deleted = 0
        ORDER BY vi.occur_time DESC LIMIT 1), 0) AS score,
        COALESCE((SELECT SUM(vi.deduct_points) FROM hot_violation_info vi WHERE vi.driver_id = d.id AND
        vi.is_deleted = 0), 0) AS totalScore,
        (CASE WHEN EXISTS (
        SELECT 1 FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.is_deleted = 0
        AND vi.need_interview = 1 AND (vi.interviewed_signature_image IS NULL OR vi.interviewed_signature_image = '')
        ) THEN '否' ELSE '是' END) AS isUrgeStudy,
        (SELECT vi.remark FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.is_deleted = 0
        ORDER BY vi.occur_time DESC LIMIT 1) AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.qualification_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
        </where>
        ORDER BY d.create_time DESC
    </select>

    <select id="selectDriverIntegrityPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverIntegrityLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.id_card_no AS idCardNo,
        d.qualification_no AS certNo,
        CONCAT(DATE_FORMAT(NOW(), '%Y-01-01'), '至', DATE_FORMAT(NOW(), '%Y-12-31')) AS scoringCycle,
        COALESCE((SELECT SUM(vi.deduct_points) FROM hot_violation_info vi WHERE vi.driver_id = d.id AND
        vi.violation_type = 1 AND YEAR(vi.occur_time) = YEAR(NOW())), 0) AS accumulatedScore,
        d.integrity_level AS integrityLevel,
        COALESCE((SELECT GROUP_CONCAT(CONCAT('(', DATE_FORMAT(vi.occur_time, '%Y-%m-%d'), ')扣', vi.deduct_points, '分。')
        SEPARATOR '') FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.violation_type = 1 AND
        YEAR(vi.occur_time) = YEAR(NOW())), '') AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.qualification_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
            <if test="bo.auditStatus != null">
                AND d.audit_status = #{bo.auditStatus}
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            AND EXISTS (
                SELECT 1
                FROM hot_violation_info vi
                WHERE vi.driver_id = d.id
                  AND vi.violation_type = 1
                  AND vi.is_deleted = 0
            )
        </where>
        ORDER BY d.create_time DESC
    </select>

    <select id="selectDriverIntegrityList"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.DriverIntegrityLedgerVo">
        SELECT c.company_name AS companyName,
        d.name AS driverName,
        d.id_card_no AS idCardNo,
        d.qualification_no AS certNo,
        CONCAT(DATE_FORMAT(NOW(), '%Y-01-01'), '至', DATE_FORMAT(NOW(), '%Y-12-31')) AS scoringCycle,
        COALESCE((SELECT SUM(vi.deduct_points) FROM hot_violation_info vi WHERE vi.driver_id = d.id AND
        vi.violation_type = 1 AND YEAR(vi.occur_time) = YEAR(NOW())), 0) AS accumulatedScore,
        d.integrity_level AS integrityLevel,
        COALESCE((SELECT GROUP_CONCAT(CONCAT('(', DATE_FORMAT(vi.occur_time, '%Y-%m-%d'), ')扣', vi.deduct_points, '分。')
        SEPARATOR '') FROM hot_violation_info vi WHERE vi.driver_id = d.id AND vi.violation_type = 1 AND
        YEAR(vi.occur_time) = YEAR(NOW())), '') AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        <where>
            d.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.driverLicenseNo != null and bo.driverLicenseNo != ''">
                AND d.qualification_no LIKE CONCAT('%', #{bo.driverLicenseNo}, '%')
            </if>
        </where>
        ORDER BY d.create_time DESC
    </select>

    <!-- 业务尽量在SQL中完成，而不是Java代码 -->
    <select id="selectOnlineTrainingMonthlyRows"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.OnlineTrainingMonthlyRowVo">
        SELECT
        ROW_NUMBER() OVER (
        ORDER BY (
        CASE
        WHEN ex.user_id IS NULL THEN 0
        WHEN ep.pass_score IS NOT NULL AND ex.exam_score >= ep.pass_score THEN 2
        ELSE 1
        END
        ) DESC, p.user_name
        ) AS `index`,
        p.user_id AS userId,
        p.user_name AS userName,
        d.plate_number AS plateNumber,
        CASE
        WHEN p.learn_progress IS NOT NULL THEN CONCAT(TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM p.learn_progress)),
        '%')
        ELSE ''
        END AS progressLabel,
        p.phone AS phone,
        p.study_photo_oss_id AS photoUrl,
        p.signature_oss_id AS signatureUrl,
        CASE
        WHEN ex.user_id IS NULL THEN ''
        WHEN ep.pass_score IS NOT NULL AND ex.exam_score >= ep.pass_score THEN '合格'
        ELSE '不及格'
        END AS examStatus,
        p.complete_time AS completeTime
        FROM hot_training_participant p
        LEFT JOIN hot_driver d ON d.id = p.user_id AND d.is_deleted = 0
        LEFT JOIN hot_training t
        ON t.id = p.training_id
        AND t.company_id = p.company_id
        AND t.is_deleted = 0
        LEFT JOIN hot_exam_paper ep
        ON ep.id = CAST(SUBSTRING_INDEX(t.paper_ids, ',', 1) AS UNSIGNED)
        AND ep.is_deleted = 0
        LEFT JOIN (
        SELECT
        company_id,
        training_id,
        user_id,
        SUM(COALESCE(gained_score, 0)) AS exam_score
        FROM hot_training_answer_record
        WHERE is_deleted = 0
        AND scene_type = 'PLAN'
        AND company_id = #{companyId}
        AND training_id = #{trainingId}
        GROUP BY company_id, training_id, user_id
        ) ex
        ON ex.company_id = p.company_id
        AND ex.training_id = p.training_id
        AND ex.user_id = p.user_id
        WHERE p.company_id = #{companyId}
        AND p.training_id = #{trainingId}
        AND p.is_deleted = 0
        <if test="userIds != null and userIds.size() > 0">
            AND p.user_id IN
            <foreach collection="userIds" item="uid" open="(" separator="," close=")">
                #{uid}
            </foreach>
        </if>
        ORDER BY (
        CASE
        WHEN ex.user_id IS NULL THEN 0
        WHEN ep.pass_score IS NOT NULL AND ex.exam_score >= ep.pass_score THEN 2
        ELSE 1
        END
        ) DESC, p.user_name
    </select>

    <select id="selectVehicleInfoPage" resultType="com.hotwj.platform.reportStatistics.domain.vo.VehicleInfoLedgerVo">
        SELECT v.plate_number AS plateNumber,
        v.vehicle_register_date AS firstRegisterDate,
        v.first_register_date AS vehicleLicenseRegisterDate,
        CONCAT(v.inspection_valid_month, '-01') AS vehicleLicenseValidEndDate,
        v.transport_issue_date AS roadTransportCertIssueDate,
        v.transport_valid_month AS roadTransportCertValidEndDate,
        (SELECT CASE ar.evaluation_level WHEN 1 THEN '一级' WHEN 2 THEN '二级' ELSE CAST(ar.evaluation_level AS CHAR)
        END
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS technicalLevel,
        (SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS inspectionDate,
        DATE_ADD((SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1), INTERVAL 1 YEAR) AS inspectionValidDate,
        (SELECT CONCAT('一年 (', mc.inspection_times, '次)')
        FROM hot_vehicle_secondary_maintenance_config mc
        WHERE mc.company_id = v.company_id AND mc.is_deleted = 0 AND mc.is_enabled = 1
        LIMIT 1) AS maintenanceCycle,
        (SELECT m.finish_time
        FROM hot_vehicle_maintenance m
        WHERE m.vehicle_id = v.id AND m.is_deleted = 0
        ORDER BY m.finish_time DESC LIMIT 1) AS maintenanceDate,
        v.remark AS remark
        FROM hot_vehicle v
        <where>
            v.is_deleted = 0
            <if test="bo.companyId != null">
                AND v.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY v.first_register_date DESC
    </select>

    <select id="selectVehicleInfoList" resultType="com.hotwj.platform.reportStatistics.domain.vo.VehicleInfoLedgerVo">
        SELECT v.plate_number AS plateNumber,
        v.vehicle_register_date AS firstRegisterDate,
        v.first_register_date AS vehicleLicenseRegisterDate,
        CONCAT(v.inspection_valid_month, '-01') AS vehicleLicenseValidEndDate,
        v.transport_issue_date AS roadTransportCertIssueDate,
        v.transport_valid_month AS roadTransportCertValidEndDate,
        (SELECT CASE ar.evaluation_level WHEN 1 THEN '一级' WHEN 2 THEN '二级' ELSE CAST(ar.evaluation_level AS CHAR)
        END
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS technicalLevel,
        (SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS inspectionDate,
        DATE_ADD((SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1), INTERVAL 1 YEAR) AS inspectionValidDate,
        (SELECT CONCAT('一年 (', mc.inspection_times, '次)')
        FROM hot_vehicle_secondary_maintenance_config mc
        WHERE mc.company_id = v.company_id AND mc.is_deleted = 0 AND mc.is_enabled = 1
        LIMIT 1) AS maintenanceCycle,
        (SELECT m.finish_time
        FROM hot_vehicle_maintenance m
        WHERE m.vehicle_id = v.id AND m.is_deleted = 0
        ORDER BY m.finish_time DESC LIMIT 1) AS maintenanceDate,
        v.remark AS remark
        FROM hot_vehicle v
        <where>
            v.is_deleted = 0
            <if test="bo.companyId != null">
                AND v.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY v.first_register_date DESC
    </select>

    <select id="selectGovVehicleInfoPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovVehicleInfoLedgerVo">
        SELECT c.company_name AS companyName,
        v.plate_number AS plateNumber,
        v.vehicle_register_date AS firstRegisterDate,
        v.first_register_date AS vehicleLicenseRegisterDate,
        CONCAT(v.inspection_valid_month, '-01') AS vehicleLicenseValidEndDate,
        v.transport_issue_date AS roadTransportCertIssueDate,
        v.transport_valid_month AS roadTransportCertValidEndDate,
        (SELECT CASE ar.evaluation_level WHEN 1 THEN '一级' WHEN 2 THEN '二级' ELSE CAST(ar.evaluation_level AS CHAR)
        END
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS technicalLevel,
        (SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS inspectionDate,
        DATE_ADD((SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1), INTERVAL 1 YEAR) AS inspectionValidDate,
        (SELECT CONCAT('一年 (', mc.inspection_times, '次)')
        FROM hot_vehicle_secondary_maintenance_config mc
        WHERE mc.company_id = v.company_id AND mc.is_deleted = 0 AND mc.is_enabled = 1
        LIMIT 1) AS maintenanceCycle,
        (SELECT m.finish_time
        FROM hot_vehicle_maintenance m
        WHERE m.vehicle_id = v.id AND m.is_deleted = 0
        ORDER BY m.finish_time DESC LIMIT 1) AS maintenanceDate,
        v.remark AS remark
        FROM hot_vehicle v
        LEFT JOIN sys_company c ON v.company_id = c.id
        <where>
            v.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code LIKE CONCAT(#{districtCode}, '%')
            </if>
            <if test="bo.companyId != null">
                AND v.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%')
            </if>
            <if test="bo.vehicleStatus != null">
                AND v.vehicle_status = #{bo.vehicleStatus}
            </if>
            <if test="bo.operationStatus != null">
                AND v.operation_status = #{bo.operationStatus}
            </if>
        </where>
        ORDER BY v.first_register_date DESC
    </select>

    <select id="selectStudyRecordCourses"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.security.StudyRecordCourseVo">
        SELECT c.course_name             AS courseName,
               (SELECT IFNULL(SUM(mr.duration_seconds), 0)
                FROM hot_course_resource cr2
                         LEFT JOIN hot_media_resource mr ON mr.id = cr2.resource_id
                WHERE cr2.course_id = c.course_id
                  AND cr2.resource_type = 1
                  AND cr2.is_deleted = 0
                  AND mr.is_deleted = 0) AS duration,
               MIN(cr.create_time)       AS studyTime,
               MAX(cr.complete_time)     AS completeTime,
               CASE
                   WHEN t.is_make_up = 1 AND MIN(cr.create_time) > t.end_time THEN '是'
                   ELSE '否'
                   END                   AS isMakeUp
        FROM hot_training_course_record cr
                 INNER JOIN hot_training t ON t.id = cr.training_id
                 INNER JOIN hot_training_course_config c ON c.id = cr.training_course_id
        WHERE cr.company_id = #{companyId}
          AND cr.training_id = #{trainingId}
          AND cr.user_id = #{userId}
          AND cr.is_deleted = 0
        GROUP BY c.id, c.course_name, t.is_make_up, t.end_time
        ORDER BY MIN(cr.create_time)
    </select>

    <select id="selectGovVehicleInfoList"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovVehicleInfoLedgerVo">
        SELECT c.company_name AS companyName,
        v.plate_number AS plateNumber,
        v.vehicle_register_date AS firstRegisterDate,
        v.first_register_date AS vehicleLicenseRegisterDate,
        CONCAT(v.inspection_valid_month, '-01') AS vehicleLicenseValidEndDate,
        v.transport_issue_date AS roadTransportCertIssueDate,
        v.transport_valid_month AS roadTransportCertValidEndDate,
        (SELECT CASE ar.evaluation_level WHEN 1 THEN '一级' WHEN 2 THEN '二级' ELSE CAST(ar.evaluation_level AS CHAR)
        END
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS technicalLevel,
        (SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1) AS inspectionDate,
        DATE_ADD((SELECT ar.review_date
        FROM hot_vehicle_annual_review ar
        WHERE ar.vehicle_id = v.id AND ar.is_deleted = 0
        ORDER BY ar.review_date DESC LIMIT 1), INTERVAL 1 YEAR) AS inspectionValidDate,
        (SELECT CONCAT('一年 (', mc.inspection_times, '次)')
        FROM hot_vehicle_secondary_maintenance_config mc
        WHERE mc.company_id = v.company_id AND mc.is_deleted = 0 AND mc.is_enabled = 1
        LIMIT 1) AS maintenanceCycle,
        (SELECT m.finish_time
        FROM hot_vehicle_maintenance m
        WHERE m.vehicle_id = v.id AND m.is_deleted = 0
        ORDER BY m.finish_time DESC LIMIT 1) AS maintenanceDate,
        v.remark AS remark
        FROM hot_vehicle v
        LEFT JOIN sys_company c ON v.company_id = c.id
        <where>
            v.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code LIKE CONCAT(#{districtCode}, '%')
            </if>
            <if test="bo.companyId != null">
                AND v.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY v.first_register_date DESC
    </select>

    <select id="selectGovGpsViolationPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovGpsViolationLedgerVo">
        SELECT t.id,
        c.company_name AS companyName,
        t.driver_name AS driverName,
        d.id_card_no AS idCardNo,
        t.plate_number AS plateNumber,
        t.create_time AS createTime,
        t.occur_time AS occurTime
        FROM hot_violation_info t
        LEFT JOIN sys_company c ON t.company_id = c.id
        LEFT JOIN hot_driver d ON t.driver_id = d.id
        <where>
            t.is_deleted = 0
            AND t.violation_type = 2
            <if test="bo.needInterview != null">
                AND t.need_interview = #{bo.needInterview}
            </if>
            <if test="bo.processStatus != null">
                AND t.process_status = #{bo.processStatus}
            </if>
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code LIKE CONCAT(#{districtCode}, '%')
            </if>
            <if test="bo.companyId != null">
                AND t.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND t.driver_name LIKE CONCAT('%',
                #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo},
                '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND t.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectGovAccidentPage" resultType="com.hotwj.platform.reportStatistics.domain.vo.GovAccidentLedgerVo">
        SELECT a.id,
        c.company_name AS companyName,
        c.responsible_name AS responsibleName,
        c.responsible_mobile AS responsibleMobile,
        a.accident_time AS accidentTime,
        a.driver_name AS driverName,
        a.plate_number AS plateNumber,
        a.occur_location AS occurLocation,
        a.death_toll AS deathToll,
        a.number_seriously_injured AS numberSeriouslyInjured,
        a.number_minor_injuries AS numberMinorInjuries,
        a.economic_loss AS economicLoss,
        a.main_cause AS mainCause,
        a.division_responsibilities AS divisionResponsibilities,
        a.treatment_result AS treatmentResult,
        a.accident_level AS accidentLevel,
        a.case_closing_time AS caseClosingTime,
        a.remark AS remark
        FROM hot_accident_archive a
        LEFT JOIN sys_company c ON a.company_id = c.id
        <where>
            a.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code LIKE CONCAT(#{districtCode}, '%')
            </if>
            <if test="bo.companyId != null">
                AND a.company_id = #{bo.companyId}
            </if>
            <if test="bo.isRectified != null">
                AND a.is_rectified = #{bo.isRectified}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND a.driver_name LIKE CONCAT('%',
                #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND a.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%')
            </if>
            <if test="bo.accidentLevel != null and bo.accidentLevel != ''">
                AND (
                a.accident_level = #{bo.accidentLevel}
                OR a.accident_level LIKE CONCAT('%\"levelName\":\"', #{bo.accidentLevel}, '\"%')
                )
            </if>
            <if test="bo.accidentDate != null">
                AND DATE(a.accident_time) = #{bo.accidentDate}
            </if>
            <if test="bo.caseClosingDate != null">
                AND DATE(a.case_closing_time) = #{bo.caseClosingDate}
            </if>
            <if test="bo.startDate != null">
                AND a.accident_time &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND a.accident_time &lt; DATE_ADD(#{bo.endDate}, INTERVAL 1 DAY)
            </if>
        </where>
        ORDER BY a.accident_time DESC
    </select>

    <select id="selectGovAccidentList" resultType="com.hotwj.platform.reportStatistics.domain.vo.GovAccidentLedgerVo">
        SELECT a.id,
        c.company_name AS companyName,
        c.responsible_name AS responsibleName,
        c.responsible_mobile AS responsibleMobile,
        a.accident_time AS accidentTime,
        a.driver_name AS driverName,
        a.plate_number AS plateNumber,
        a.occur_location AS occurLocation,
        a.death_toll AS deathToll,
        a.number_seriously_injured AS numberSeriouslyInjured,
        a.number_minor_injuries AS numberMinorInjuries,
        a.economic_loss AS economicLoss,
        a.main_cause AS mainCause,
        a.division_responsibilities AS divisionResponsibilities,
        a.treatment_result AS treatmentResult,
        a.accident_level AS accidentLevel,
        a.case_closing_time AS caseClosingTime,
        a.remark AS remark
        FROM hot_accident_archive a
        LEFT JOIN sys_company c ON a.company_id = c.id
        <where>
            a.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code LIKE CONCAT(#{districtCode}, '%')
            </if>
            <if test="bo.companyId != null">
                AND a.company_id = #{bo.companyId}
            </if>
            <if test="bo.isRectified != null">
                AND a.is_rectified = #{bo.isRectified}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND a.driver_name LIKE CONCAT('%',
                #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND a.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%')
            </if>
            <if test="bo.accidentLevel != null and bo.accidentLevel != ''">
                AND (
                a.accident_level = #{bo.accidentLevel}
                OR a.accident_level LIKE CONCAT('%\"levelName\":\"', #{bo.accidentLevel}, '\"%')
                )
            </if>
            <if test="bo.accidentDate != null">
                AND DATE(a.accident_time) = #{bo.accidentDate}
            </if>
            <if test="bo.caseClosingDate != null">
                AND DATE(a.case_closing_time) = #{bo.caseClosingDate}
            </if>
            <if test="bo.startDate != null">
                AND a.accident_time &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND a.accident_time &lt; DATE_ADD(#{bo.endDate}, INTERVAL 1 DAY)
            </if>
        </where>
        ORDER BY a.accident_time DESC
    </select>

    <select id="selectAccidentPage" resultType="com.hotwj.platform.reportStatistics.domain.vo.AccidentLedgerVo">
        SELECT a.accident_time AS accidentTime,
        a.driver_name AS driverName,
        a.plate_number AS plateNumber,
        a.occur_location AS occurLocation,
        a.death_toll AS deathToll,
        a.number_seriously_injured AS numberSeriouslyInjured,
        a.number_minor_injuries AS numberMinorInjuries,
        a.economic_loss AS economicLoss,
        a.main_cause AS mainCause,
        a.division_responsibilities AS divisionResponsibilities,
        a.treatment_result AS treatmentResult,
        a.accident_level AS accidentLevel,
        a.case_closing_time AS caseClosingTime,
        a.remark AS remark
        FROM hot_accident_archive a
        <where>
            a.is_deleted = 0
            <if test="bo.companyId != null">
                AND a.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND a.driver_name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND a.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
            <if test="bo.accidentLevel != null and bo.accidentLevel != ''">
                AND (
                a.accident_level = #{bo.accidentLevel}
                OR a.accident_level LIKE CONCAT('%\"levelName\":\"', #{bo.accidentLevel}, '\"%')
                )
            </if>
            <if test="bo.accidentDate != null">
                AND DATE(a.accident_time) = #{bo.accidentDate}
            </if>
            <if test="bo.caseClosingDate != null">
                AND DATE(a.case_closing_time) = #{bo.caseClosingDate}
            </if>
            <if test="bo.startDate != null">
                AND a.accident_time &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND a.accident_time &lt;= #{bo.endDate}
            </if>
        </where>
        ORDER BY a.accident_time DESC
    </select>

    <select id="selectAccidentList" resultType="com.hotwj.platform.reportStatistics.domain.vo.AccidentLedgerVo">
        SELECT a.accident_time AS accidentTime,
        a.driver_name AS driverName,
        a.plate_number AS plateNumber,
        a.occur_location AS occurLocation,
        a.death_toll AS deathToll,
        a.number_seriously_injured AS numberSeriouslyInjured,
        a.number_minor_injuries AS numberMinorInjuries,
        a.economic_loss AS economicLoss,
        a.main_cause AS mainCause,
        a.division_responsibilities AS divisionResponsibilities,
        a.accident_level AS accidentLevel,
        a.case_closing_time AS caseClosingTime,
        a.remark AS remark
        FROM hot_accident_archive a
        <where>
            a.is_deleted = 0
            <if test="bo.companyId != null">
                AND a.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND a.driver_name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND a.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
            <if test="bo.accidentLevel != null and bo.accidentLevel != ''">
                AND (
                a.accident_level = #{bo.accidentLevel}
                OR a.accident_level LIKE CONCAT('%\"levelName\":\"', #{bo.accidentLevel}, '\"%')
                )
            </if>
            <if test="bo.accidentDate != null">
                AND DATE(a.accident_time) = #{bo.accidentDate}
            </if>
            <if test="bo.caseClosingDate != null">
                AND DATE(a.case_closing_time) = #{bo.caseClosingDate}
            </if>
            <if test="bo.startDate != null">
                AND a.accident_time &gt;= #{bo.startDate}
            </if>
            <if test="bo.endDate != null">
                AND a.accident_time &lt;= #{bo.endDate}
            </if>
        </where>
        ORDER BY a.accident_time DESC
    </select>

    <select id="selectPreJobTrainingPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.PreJobTrainingLedgerVo">
        SELECT
        d.id AS driverId,
        d.company_id AS companyId,
        d.name AS driverName,
        d.gender AS gender,
        d.id_card_no AS idCardNo,
        d.entry_date AS entryDate,
        CASE
        WHEN tr.has_completed = 1 THEN '是'
        ELSE '否'
        END AS isCompletedTraining,
        tr.training_id AS trainingId,
        CASE
        WHEN d.status = 1 THEN '是'
        ELSE '否'
        END AS isEmployed
        FROM hot_driver d
        LEFT JOIN (
        SELECT
        tmp.user_id,
        tmp.training_id,
        tmp.has_completed
        FROM (
        SELECT
        tp.user_id,
        t.id AS training_id,
        tp.is_completed,
        t.start_time,
        MAX(CASE WHEN tp.is_completed = 1 THEN 1 ELSE 0 END)
        OVER (PARTITION BY tp.user_id) AS has_completed,
        ROW_NUMBER() OVER (PARTITION BY tp.user_id ORDER BY t.start_time DESC) AS rn
        FROM hot_training t
        INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE t.is_deleted = 0
        AND tp.is_deleted = 0
        AND t.training_type = 'pre-job'
        ) tmp
        WHERE tmp.rn = 1
        ) tr ON tr.user_id = d.id
        <where>
            d.is_deleted = 0
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            <if test="bo.ids != null and bo.ids != ''">
                <foreach collection="bo.ids.split(',')" item="id" open="AND d.id IN (" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="bo.isCompletedTraining != null">
                <if test="bo.isCompletedTraining == 1">
                    AND tr.has_completed = 1
                </if>
                <if test="bo.isCompletedTraining == 0">
                    AND (tr.has_completed IS NULL OR tr.has_completed = 0)
                </if>
            </if>
        </where>
        ORDER BY d.entry_date DESC
    </select>

    <select id="selectPreJobTrainingList"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.PreJobTrainingLedgerVo">
        SELECT
        d.id AS driverId,
        d.company_id AS companyId,
        d.name AS driverName,
        d.gender AS gender,
        d.id_card_no AS idCardNo,
        d.birth_date AS birthDate,
        d.education AS education,
        d.start_work_date AS startWorkDate,
        d.current_address AS currentAddress,
        d.entry_date AS entryDate,
        CASE
        WHEN tr.has_completed = 1 THEN '是'
        ELSE '否'
        END AS isCompletedTraining,
        tr.training_id AS trainingId,
        CASE
        WHEN d.status = 1 THEN '是'
        ELSE '否'
        END AS isEmployed
        FROM hot_driver d
        LEFT JOIN (
        SELECT
        tmp.user_id,
        tmp.training_id,
        tmp.has_completed
        FROM (
        SELECT
        tp.user_id,
        t.id AS training_id,
        tp.is_completed,
        t.start_time,
        MAX(CASE WHEN tp.is_completed = 1 THEN 1 ELSE 0 END)
        OVER (PARTITION BY tp.user_id) AS has_completed,
        ROW_NUMBER() OVER (PARTITION BY tp.user_id ORDER BY t.start_time DESC) AS rn
        FROM hot_training t
        INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE t.is_deleted = 0
        AND tp.is_deleted = 0
        AND t.training_type = 'pre-job'
        ) tmp
        WHERE tmp.rn = 1
        ) tr ON tr.user_id = d.id
        <where>
            d.is_deleted = 0
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo}, '%')
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
            <if test="bo.ids != null and bo.ids != ''">
                <foreach collection="bo.ids.split(',')" item="id" open="AND d.id IN (" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="bo.isCompletedTraining != null">
                <if test="bo.isCompletedTraining == 1">
                    AND tr.has_completed = 1
                </if>
                <if test="bo.isCompletedTraining == 0">
                    AND (tr.has_completed IS NULL OR tr.has_completed = 0)
                </if>
            </if>
        </where>
        ORDER BY d.entry_date DESC
    </select>

    <select id="selectGovPreJobTrainingPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovPreJobTrainingLedgerVo">
        SELECT c.id AS companyId,
        c.company_name AS companyName,
        c.responsible_name AS managerName,
        c.responsible_mobile AS managerPhone,
        IFNULL(d.total_count, 0) AS totalTrainingCount,
        IFNULL(t.completed_count, 0) AS completedTrainingCount,
        (IFNULL(d.total_count, 0) - IFNULL(t.completed_count, 0)) AS uncompletedTrainingCount
        FROM sys_company c
        LEFT JOIN (
        SELECT company_id, COUNT(*) as total_count
        FROM hot_driver
        WHERE is_deleted = 0 AND status = 1
        <if test="bo.auditStatus != null">
            AND audit_status = #{bo.auditStatus}
        </if>
        GROUP BY company_id
        ) d ON c.id = d.company_id
        LEFT JOIN (
        SELECT t.company_id, COUNT(DISTINCT tp.user_id) as completed_count
        FROM hot_training t
        INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE t.training_type = 'pre-job'
        AND tp.is_completed = 1
        AND t.is_deleted = 0
        AND tp.is_deleted = 0
        GROUP BY t.company_id
        ) t ON c.id = t.company_id
        <where>
            c.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND c.id = #{bo.companyId}
            </if>
            <if test="bo.managerName != null and bo.managerName != ''">
                AND c.responsible_name LIKE CONCAT('%', #{bo.managerName}, '%')
            </if>
            <if test="bo.managerPhone != null and bo.managerPhone != ''">
                AND c.responsible_mobile LIKE CONCAT('%', #{bo.managerPhone}, '%')
            </if>
        </where>
        ORDER BY c.create_time DESC
    </select>

    <select id="selectGovPreJobTrainingList"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovPreJobTrainingLedgerVo">
        SELECT c.company_name AS companyName,
        c.responsible_name AS managerName,
        c.responsible_mobile AS managerPhone,
        IFNULL(d.total_count, 0) AS totalTrainingCount,
        IFNULL(t.completed_count, 0) AS completedTrainingCount,
        (IFNULL(d.total_count, 0) - IFNULL(t.completed_count, 0)) AS uncompletedTrainingCount
        FROM sys_company c
        LEFT JOIN (
        SELECT company_id, COUNT(*) as total_count
        FROM hot_driver
        WHERE is_deleted = 0 AND status = 1
        GROUP BY company_id
        ) d ON c.id = d.company_id
        LEFT JOIN (
        SELECT t.company_id, COUNT(DISTINCT tp.user_id) as completed_count
        FROM hot_training t
        INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE t.training_type = 'pre-job'
        AND tp.is_completed = 1
        AND t.is_deleted = 0
        AND tp.is_deleted = 0
        GROUP BY t.company_id
        ) t ON c.id = t.company_id
        <where>
            c.is_deleted = 0
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND c.id = #{bo.companyId}
            </if>
            <if test="bo.managerName != null and bo.managerName != ''">
                AND c.responsible_name LIKE CONCAT('%', #{bo.managerName}, '%')
            </if>
            <if test="bo.managerPhone != null and bo.managerPhone != ''">
                AND c.responsible_mobile LIKE CONCAT('%', #{bo.managerPhone}, '%')
            </if>
        </where>
        ORDER BY c.create_time DESC
    </select>

    <select id="selectGovPreJobTrainingDetailPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovPreJobTrainingDetailVo">
        SELECT d.name AS driverName,
        d.id_card_no AS idCardNo,
        (CASE WHEN EXISTS (SELECT 1 FROM hot_training t INNER JOIN hot_training_participant tp ON t.id = tp.training_id
        WHERE tp.user_id = d.id AND t.is_deleted = 0 AND tp.is_deleted = 0 AND t.training_type = 'pre-job' AND
        tp.is_completed = 1) THEN '是' ELSE '否' END) AS isCompletedTraining,
        d.gender AS gender,
        DATE_FORMAT(d.birth_date, '%Y-%m-%d') AS birthDate,
        d.education AS education,
        DATE_FORMAT(d.start_work_date, '%Y-%m-%d') AS startWorkDate,
        DATE_FORMAT(d.entry_date, '%Y-%m-%d') AS entryDate,
        '驾驶员' AS post,
        d.current_address AS currentAddress,
        t.start_time AS trainingTime,
        t.training_location AS trainingLocation,
        '' AS trainingContent,
        t.class_hours AS trainingHours,
        '' AS examScore,
        '' AS examineeSignature,
        '' AS examinerSignature,
        '' AS remark
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        LEFT JOIN hot_training_participant tp ON tp.user_id = d.id AND tp.is_deleted = 0 AND EXISTS (SELECT 1 FROM
        hot_training t2 WHERE t2.id = tp.training_id AND t2.training_type = 'pre-job' AND t2.is_deleted = 0)
        LEFT JOIN hot_training t ON tp.training_id = t.id AND t.is_deleted = 0 AND t.training_type = 'pre-job'
        <where>
            d.is_deleted = 0 AND d.status = 1
            <if test="districtCode != null and districtCode != ''">
                AND c.region_code = #{districtCode}
            </if>
            <if test="bo.companyId != null">
                AND d.company_id = #{bo.companyId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName},
                '%')
            </if>
            <if test="bo.idCardNo != null and bo.idCardNo != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCardNo},
                '%')
            </if>
            <if test="bo.isCompletedTraining != null">
                <if test="bo.isCompletedTraining == 1">
                    AND EXISTS (SELECT 1 FROM hot_training t2 INNER JOIN hot_training_participant tp2 ON t2.id =
                    tp2.training_id
                    WHERE tp2.user_id = d.id AND t2.is_deleted = 0 AND tp2.is_deleted = 0 AND t2.training_type =
                    'pre-job' AND tp2.is_completed = 1)
                </if>
                <if test="bo.isCompletedTraining == 0">
                    AND NOT EXISTS (SELECT 1 FROM hot_training t2 INNER JOIN hot_training_participant tp2 ON t2.id =
                    tp2.training_id
                    WHERE tp2.user_id = d.id AND t2.is_deleted = 0 AND tp2.is_deleted = 0 AND t2.training_type =
                    'pre-job' AND tp2.is_completed = 1)
                </if>
            </if>
            <if test="bo.auditStatus != null">
                AND d.audit_status = #{bo.auditStatus}
            </if>
            <if test="bo.status != null">
                AND d.status = #{bo.status}
            </if>
        </where>
        ORDER BY d.entry_date DESC
    </select>
    <select id="selectGovSafetyEducationTrainingPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovSafetyEducationTrainingVo">
        SELECT
        t.plan_name AS planName,
        t.id AS id,
        t.start_time AS planStartDate,
        t.end_time AS planEndDate,
        CASE
        WHEN t.end_time IS NOT NULL AND t.end_time &lt; NOW() THEN '已截止'
        ELSE '未截止'
        END AS isClosed,
        (SELECT COUNT(*) FROM hot_training_participant tp WHERE tp.training_id = t.id AND tp.is_deleted = 0) AS
        expectedCount,
        (SELECT COUNT(*) FROM hot_training_participant tp WHERE tp.training_id = t.id AND tp.is_completed = 1 AND
        tp.is_deleted = 0) AS actualCount,
        ((SELECT COUNT(*) FROM hot_training_participant tp WHERE tp.training_id = t.id AND tp.is_deleted = 0) -
        (SELECT COUNT(*) FROM hot_training_participant tp WHERE tp.training_id = t.id AND tp.is_completed = 1 AND
        tp.is_deleted = 0)) AS absentCount
        FROM hot_training t
        <where>
            t.is_deleted = 0
            AND t.training_type = 'daily'
            AND t.is_enabled = 1
            <if test="bo.companyId != null">
                AND t.company_id = #{bo.companyId}
            </if>
            <if test="bo.planName != null and bo.planName != ''">
                AND t.plan_name LIKE CONCAT('%', #{bo.planName}, '%')
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectGovSafetyEducationTrainingDetail"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovSafetyEducationTrainingDetailVo">
        SELECT t.plan_name                                 AS planName,
               CONCAT(DATE_FORMAT(t.start_time, '%Y-%m-%d'), '至',
                      DATE_FORMAT(t.end_time, '%Y-%m-%d')) AS trainingPeriod,
               t.training_location                         AS location,
               t.teacher_name                              AS host,
               t.recorder_name                             AS recorder,
               (SELECT COUNT(*)
                FROM hot_training_participant tp
                WHERE tp.training_id = t.id
                  AND tp.is_deleted = 0)                   AS expectedCount,
               (SELECT COUNT(*)
                FROM hot_training_participant tp
                WHERE tp.training_id = t.id
                  AND tp.is_completed = 1
                  AND tp.is_deleted = 0)                   AS actualCount,
               ((SELECT COUNT(*) FROM hot_training_participant tp WHERE tp.training_id = t.id AND tp.is_deleted = 0) -
                (SELECT COUNT(*)
                 FROM hot_training_participant tp
                 WHERE tp.training_id = t.id
                   AND tp.is_completed = 1
                   AND tp.is_deleted = 0))                 AS absentCount
        FROM hot_training t
        WHERE t.id = #{id}
    </select>

    <select id="selectGovSafetyEducationTrainingParticipants"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovSafetyEducationTrainingParticipantVo">
        SELECT tp.user_name          AS userName,
               d.plate_number        AS plateNumber,
               tp.gender,
               tp.id_card_no         AS idCardNo,
               tp.phone,
               tp.is_completed       AS isCompleted,
               tp.learn_progress     AS learnProgress,
               tp.is_pass            AS isPass,
               tp.complete_time      AS completeTime,
               tp.signature_oss_id   AS signatureOssId,
               tp.study_photo_oss_id AS studyPhotoOssId
        FROM hot_training_participant tp
                 LEFT JOIN hot_driver d ON tp.user_id = d.id
        WHERE tp.training_id = #{trainingId}
          AND tp.is_deleted = 0
    </select>

    <select id="selectGovSafetyEducationTrainingCourseNames" resultType="java.lang.String">
        SELECT course_name
        FROM hot_training_course_config
        WHERE training_id = #{trainingId}
          AND is_deleted = 0
        ORDER BY sort_no ASC
    </select>

    <select id="selectViolationCountByDateRange"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovViolationInfoLedgerVo">
        SELECT DATE_FORMAT(t.occur_time, '%Y-%c') AS planName,
        COUNT(t.id) AS violationCount
        FROM hot_violation_info t left join hot_driver d
        on t.driver_id = d.id
        WHERE t.company_id = #{bo.companyId}
        AND t.is_deleted = 0 AND d.is_deleted = 0
        AND t.occur_time &gt;= #{startTime}
        AND t.occur_time &lt;= #{endTime}
        <if test="bo.auditStatus != null">
            AND d.audit_status = #{bo.auditStatus}
        </if>
        <if test="bo.status != null">
            AND d.status = #{bo.status}
        </if>
        GROUP BY planName
    </select>

    <select id="selectGovViolationInfoDetailPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovViolationInfoDetailVo">
        SELECT
        t.id,
        t.plate_number,
        t.driver_name,
        t.violation_content,
        t.recorder_name AS recorderName,
        t.remark
        FROM hot_violation_info t left join hot_driver d
        on t.driver_id = d.id
        WHERE t.is_deleted = 0 AND d.is_deleted = 0
        <if test="bo.companyId != null">
            AND t.company_id = #{bo.companyId}
        </if>
        <if test="bo.startDate != null">
            AND t.occur_time &gt;= #{bo.startDate}
        </if>
        <if test="bo.endDate != null">
            AND t.occur_time &lt;= #{bo.endDate}
        </if>
        <if test="bo.auditStatus != null">
            AND d.audit_status = #{bo.auditStatus}
        </if>
        <if test="bo.status != null">
            AND d.status = #{bo.status}
        </if>
        ORDER BY t.occur_time DESC
    </select>

    <select id="selectGovViolationHandlingPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.GovViolationHandlingLedgerVo">
        SELECT
        t.id,
        c.company_name,
        t.driver_name,
        d.id_card_no,
        t.plate_number,
        t.create_time,
        t.occur_time
        FROM hot_violation_info t
        LEFT JOIN sys_company c ON t.company_id = c.id
        LEFT JOIN hot_driver d ON t.driver_id = d.id
        WHERE t.is_deleted = 0
        AND t.process_status = 2
        <if test="districtCode != null and districtCode != ''">
            AND c.region_code LIKE concat(#{districtCode}, '%')
        </if>
        <if test="bo.companyId != null">
            AND t.company_id = #{bo.companyId}
        </if>
        <if test="bo.driverName != null and bo.driverName != ''">
            AND t.driver_name LIKE concat('%', #{bo.driverName}, '%')
        </if>
        <if test="bo.idCardNo != null and bo.idCardNo != ''">
            AND d.id_card_no LIKE concat('%', #{bo.idCardNo}, '%')
        </if>
        <if test="bo.auditStatus != null">
            AND d.audit_status = #{bo.auditStatus}
        </if>
        <if test="bo.status != null">
            AND d.status = #{bo.status}
        </if>
        <if test="bo.plateNumber != null and bo.plateNumber != ''">
            AND t.plate_number LIKE concat('%', #{bo.plateNumber}, '%')
        </if>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectSafetyProductionMeetingPage"
            resultType="com.hotwj.platform.reportStatistics.domain.vo.SafetyProductionMeetingLedgerVo">
        SELECT
        hsm.id AS meetingId,
        hsm.meeting_topic AS meetingSubject,
        hsm.meeting_category AS meetingCategory,
        hsm.start_time AS meetingStartTime,
        COUNT(sma.id) AS expectedAttendees,
        COUNT(sma.finish_time) AS actualAttendees,
        (COUNT(sma.id) - COUNT(sma.finish_time)) AS absentAttendees
        FROM hot_security_meeting hsm
        LEFT JOIN hot_security_meeting_attendee sma ON hsm.id = sma.meeting_id AND sma.is_deleted = 0
        <where>
            hsm.is_deleted = 0
            AND hsm.meeting_type = 'produce'
            <if test="bo.companyId != null">
                AND hsm.company_id = #{bo.companyId}
            </if>
            <if test="bo.meetingSubject != null and bo.meetingSubject != ''">
                AND hsm.meeting_topic LIKE concat('%', #{bo.meetingSubject}, '%')
            </if>
            <if test="bo.meetingCategory != null and bo.meetingCategory != ''">
                AND hsm.meeting_category = #{bo.meetingCategory}
            </if>
        </where>
        GROUP BY hsm.id
        ORDER BY hsm.create_time DESC
    </select>
</mapper>
