<?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.workbench.mapper.GovWorkbenchMapper">

    <!-- 统计当前辖区内的所有企业总数 -->
    <select id="selectEnterpriseCount" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM sys_company
        WHERE is_deleted = 0
          AND region_code LIKE CONCAT(#{districtCode}, '%')
    </select>

    <!-- 查询当前辖区内的所有企业列表 -->
    <select id="selectEnterpriseList"
            resultType="com.hotwj.platform.workbench.domain.vo.GovHomeStatisticsVo$EnterpriseInfo">
        SELECT id, company_name
        FROM sys_company
        WHERE is_deleted = 0
          AND region_code LIKE CONCAT(#{districtCode}, '%')
        <if test="companyName != null and companyName != ''">
            AND company_name LIKE CONCAT('%', #{companyName}, '%')
        </if>
    </select>

    <!-- 统计总运营车辆总数 -->
    <select id="selectVehicleCount" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
          AND c.region_code LIKE CONCAT(#{districtCode}, '%')
    </select>

    <!-- 统计当前总驾驶员总数 -->
    <select id="selectDriverCount" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
          AND d.audit_status = 1
          AND c.region_code LIKE CONCAT(#{districtCode}, '%')
    </select>

    <!-- ================= 企业隐患 ================= -->

    <!-- 统计营业执照隐患数 -->
    <select id="countBusinessLicense" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM sys_company
        WHERE is_deleted = 0
        <if test="companyId != null">
            AND id = #{companyId}
        </if>
        <if test="companyId == null">
            AND region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (business_long_term IS NULL OR business_long_term &lt;&gt; 1)
          AND (business_term IS NULL OR business_term &lt; NOW())
    </select>

    <!-- 查询营业执照隐患列表 -->
    <select id="listBusinessLicense" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT id                  AS id,
               company_name        AS title,
               CASE
                   WHEN business_term IS NULL THEN '未填写营业执照有效期'
                   ELSE CONCAT('营业执照已过期 ', DATEDIFF(NOW(), business_term), ' 天')
                   END             AS subTitle,
               'expired'           AS status,
               business_term       AS deadline,
               id                  AS relatedId
        FROM sys_company
        WHERE is_deleted = 0
        <if test="companyId != null">
            AND id = #{companyId}
        </if>
        <if test="companyId == null">
            AND region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (business_long_term IS NULL OR business_long_term &lt;&gt; 1)
          AND (business_term IS NULL OR business_term &lt; NOW())
    </select>

    <!-- 统计道路经营许可证隐患数 -->
    <select id="countRoadPermit" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM sys_company
        WHERE is_deleted = 0
        <if test="companyId != null">
            AND id = #{companyId}
        </if>
        <if test="companyId == null">
            AND region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (road_expire_date IS NULL OR road_expire_date &lt; NOW())
    </select>

    <!-- 查询道路经营许可证隐患列表 -->
    <select id="listRoadPermit" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT id                      AS id,
               company_name            AS title,
               CASE
                   WHEN road_expire_date IS NULL THEN '未填写道路运输经营许可证有效期'
                   ELSE CONCAT('道路运输经营许可证已过期 ', DATEDIFF(NOW(), road_expire_date), ' 天')
                   END                 AS subTitle,
               'expired'               AS status,
               road_expire_date        AS deadline,
               id                      AS relatedId
        FROM sys_company
        WHERE is_deleted = 0
        <if test="companyId != null">
            AND id = #{companyId}
        </if>
        <if test="companyId == null">
            AND region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (road_expire_date IS NULL OR road_expire_date &lt; NOW())
    </select>

    <!-- 统计上岗资格证隐患数 -->
    <select id="countManagerQualification" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_company_safety_manager m
                 LEFT JOIN sys_company c ON m.company_id = c.id
        WHERE m.is_deleted = 0
          AND m.status = 1
          AND (m.company_head IS NULL OR m.company_head &lt;&gt; 1)
          AND (m.is_resigned IS NULL OR m.is_resigned &lt;&gt; 1)
        <if test="companyId != null">
            AND m.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (m.certificate_expire_date IS NULL OR m.certificate_expire_date &lt; NOW())
    </select>

    <!-- 查询上岗资格证隐患列表 -->
    <select id="listManagerQualification" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT m.id                      AS id,
               m.name                    AS title,
               CASE
                   WHEN m.certificate_expire_date IS NULL THEN '未填写上岗资格证有效期'
                   ELSE CONCAT('上岗资格证已过期 ', DATEDIFF(NOW(), m.certificate_expire_date), ' 天')
                   END                   AS subTitle,
               'expired'                 AS status,
               m.certificate_expire_date AS deadline,
               c.company_name            AS relatedId
        FROM hot_company_safety_manager m
                 LEFT JOIN sys_company c ON m.company_id = c.id
        WHERE m.is_deleted = 0
          AND m.status = 1
          AND (m.company_head IS NULL OR m.company_head &lt;&gt; 1)
          AND (m.is_resigned IS NULL OR m.is_resigned &lt;&gt; 1)
        <if test="companyId != null">
            AND m.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (m.certificate_expire_date IS NULL OR m.certificate_expire_date &lt; NOW())
    </select>

    <!-- ================= 车辆隐患 ================= -->

    <!-- 统计行驶证隐患数 -->
    <select id="countVehicleLicense" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (v.inspection_valid_month IS NULL OR
               LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
    </select>

    <!-- 查询行驶证隐患列表 -->
    <select id="listVehicleLicense" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT v.id                  AS id,
               v.plate_number        AS title,
               CASE
                   WHEN v.inspection_valid_month IS NULL THEN '未填写行驶证有效期'
                   ELSE CONCAT('行驶证已过期 ', DATEDIFF(NOW(),
                                                         LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d'))),
                               ' 天')
                   END               AS subTitle,
               'expired'             AS status,
               LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) AS deadline,
               c.company_name        AS relatedId
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (v.inspection_valid_month IS NULL OR
               LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
    </select>

    <!-- 统计运输证隐患数 -->
    <select id="countVehicleTransportPermit" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (v.transport_valid_month IS NULL OR
               LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
    </select>

    <!-- 查询运输证隐患列表 -->
    <select id="listVehicleTransportPermit" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT v.id                           AS id,
               v.plate_number                 AS title,
               CASE
                   WHEN v.transport_valid_month IS NULL THEN '未填写道路运输证有效期'
                   ELSE CONCAT('道路运输证已过期 ', DATEDIFF(NOW(),
                                                             LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d'))),
                               ' 天')
                   END                        AS subTitle,
               'expired'                      AS status,
               LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) AS deadline,
               c.company_name                 AS relatedId
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (v.transport_valid_month IS NULL OR
               LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
    </select>

    <!-- 查询用于计算年审和二级维护隐患的车辆基础信息 -->
    <select id="selectVehicleForRiskCalc" resultType="com.hotwj.platform.workbench.domain.vo.VehicleRiskCalcVo">
        SELECT v.id           AS id,
               v.plate_number AS plateNumber,
               v.company_id   AS companyId,
               c.company_name AS companyName,
               v.certificate_issue_date AS issueDate
        FROM hot_vehicle v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.operation_status = 1
        AND v.vehicle_status = 1
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
    </select>

    <!-- ================= 驾驶员隐患 ================= -->

    <!-- 统计驾驶员身份证隐患数 -->
    <select id="countDriverIdCard" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (d.id_card_long_term IS NULL OR d.id_card_long_term &lt;&gt; 1)
          AND (d.id_card_expire_date IS NULL OR d.id_card_expire_date &lt; NOW())
    </select>

    <!-- 查询驾驶员身份证隐患列表 -->
    <select id="listDriverIdCard" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT d.id                  AS id,
               d.name                AS title,
               CASE
                   WHEN d.id_card_expire_date IS NULL THEN '未填写身份证有效期'
                   ELSE CONCAT('身份证已过期 ', DATEDIFF(NOW(), d.id_card_expire_date), ' 天')
                   END               AS subTitle,
               'expired'             AS status,
               d.id_card_expire_date AS deadline,
               c.company_name        AS relatedId
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (d.id_card_long_term IS NULL OR d.id_card_long_term &lt;&gt; 1)
          AND (d.id_card_expire_date IS NULL OR d.id_card_expire_date &lt; NOW())
    </select>

    <!-- 统计驾驶员驾驶证隐患数 -->
    <select id="countDriverLicense" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (d.driver_license_long_term IS NULL OR d.driver_license_long_term &lt;&gt; 1)
          AND (d.driver_license_expire_date IS NULL OR d.driver_license_expire_date &lt; NOW())
    </select>

    <!-- 查询驾驶员驾驶证隐患列表 -->
    <select id="listDriverLicense" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT d.id                         AS id,
               d.name                       AS title,
               CASE
                   WHEN d.driver_license_expire_date IS NULL THEN '未填写驾驶证有效期'
                   ELSE CONCAT('驾驶证已过期 ', DATEDIFF(NOW(), d.driver_license_expire_date), ' 天')
                   END                      AS subTitle,
               'expired'                    AS status,
               d.driver_license_expire_date AS deadline,
               c.company_name               AS relatedId
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        AND (d.driver_license_long_term IS NULL OR d.driver_license_long_term &lt;&gt; 1)
          AND (d.driver_license_expire_date IS NULL OR d.driver_license_expire_date &lt; NOW())
    </select>

    <!-- 统计驾驶员从业资格证隐患数 -->
    <select id="countDriverQualification" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (d.qualification_valid_end_date IS NULL OR d.qualification_valid_end_date &lt; NOW())
    </select>

    <!-- 查询驾驶员从业资格证隐患列表 -->
    <select id="listDriverQualification" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT d.id                           AS id,
               d.name                         AS title,
               CASE
                   WHEN d.qualification_valid_end_date IS NULL THEN '未填写从业资格证有效期'
                   ELSE CONCAT('从业资格证已过期 ', DATEDIFF(NOW(), d.qualification_valid_end_date), ' 天')
                   END                        AS subTitle,
               'expired'                      AS status,
               d.qualification_valid_end_date AS deadline,
               c.company_name                 AS relatedId
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND (d.qualification_valid_end_date IS NULL OR d.qualification_valid_end_date &lt; NOW())
    </select>

    <!-- 统计驾驶员未参加岗前培训隐患数 -->
    <select id="countDriverPreJobTraining" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND NOT EXISTS (SELECT 1
                          FROM hot_training_participant tp
                                   INNER JOIN hot_training t ON tp.training_id = t.id
                          WHERE tp.user_id = d.id
                            AND t.training_type IN ('pre-job')
                            AND tp.is_completed = 1
                            AND tp.is_pass = 1
                            AND t.is_deleted = 0
                            AND t.is_enabled = 1
                            AND tp.is_deleted = 0)
          AND NOT EXISTS (SELECT 1
                          FROM hot_training t
                          WHERE t.training_type = 'offline'
                            AND t.meeting_type = 'pre-job'
                            AND t.is_deleted = 0
                            AND FIND_IN_SET(d.id, t.participant_ids))
    </select>

    <!-- 查询驾驶员未参加岗前培训隐患列表 -->
    <select id="listDriverPreJobTraining" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT d.id             AS id,
               d.name           AS title,
               '未参加岗前培训' AS subTitle,
               'warning'        AS status,
               NULL             AS deadline,
               c.company_name   AS relatedId
        FROM hot_driver d
                 LEFT JOIN sys_company c ON d.company_id = c.id
        WHERE d.is_deleted = 0
          AND d.status = 1
        AND d.audit_status = 1
        <if test="companyId != null">
            AND d.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
          AND NOT EXISTS (SELECT 1
                          FROM hot_training_participant tp
                                   INNER JOIN hot_training t ON tp.training_id = t.id
                          WHERE tp.user_id = d.id
                            AND t.training_type IN ('pre-job')
                            AND tp.is_completed = 1
                            AND tp.is_pass = 1
                            AND t.is_deleted = 0
                            AND t.is_enabled = 1
                            AND tp.is_deleted = 0)
          AND NOT EXISTS (SELECT 1
                          FROM hot_training t
                          WHERE t.training_type = 'offline'
                            AND t.meeting_type = 'pre-job'
                            AND t.is_deleted = 0
                            AND FIND_IN_SET(d.id, t.participant_ids))
    </select>

    <!-- 统计驾驶员违法违规隐患数 -->
    <select id="countDriverViolation" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM hot_violation_info v
                 LEFT JOIN sys_company c ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.process_status in (0, 1)
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
    </select>

    <!-- 查询驾驶员违法违规隐患列表 -->
    <select id="listDriverViolation" resultType="com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo">
        SELECT v.id           AS id,
               CONCAT('驾驶员（', IFNULL(v.driver_name, '-'), '/', IFNULL(d.id_card_no, '-'), '）') AS title,
               CONCAT('违章\\违规：(',
                      IFNULL(dict.dict_label, '未知类型'),
                      ')',
                      CASE v.process_status
                          WHEN 0 THEN '待处理'
                          WHEN 1 THEN '处理中'
                          WHEN 2 THEN '已处理'
                          ELSE '未知状态'
                          END) AS subTitle,
               'warning'      AS status,
               v.occur_time   AS deadline,
               c.company_name AS relatedId
        FROM hot_violation_info v
                 LEFT JOIN sys_company c ON v.company_id = c.id
                 LEFT JOIN hot_driver d ON v.driver_id = d.id
                 LEFT JOIN sys_dict_data dict ON dict.dict_type = 'hot_violation_type'
            AND CAST(dict.dict_value AS UNSIGNED) = v.violation_type
        WHERE v.is_deleted = 0
        AND v.process_status in (0, 1)
        <if test="companyId != null">
            AND v.company_id = #{companyId}
        </if>
        <if test="companyId == null">
            AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
    </select>

    <!-- 按日统计隐患数量 -->
    <select id="selectHiddenDangerCountByDate" resultType="com.hotwj.platform.workbench.domain.vo.HiddenDangerCountVo">
        SELECT DATE_FORMAT(create_time, '%Y-%m-%d') as date, COUNT(*) as count
        FROM (
        -- 1. 营业执照隐患
        SELECT id, business_term as create_time, region_code, NULL as company_id FROM sys_company WHERE is_deleted = 0
        AND (business_term IS NULL OR business_term &lt; NOW())
        UNION ALL
        -- 2. 道路经营许可证隐患
        SELECT id, road_expire_date as create_time, region_code, NULL as company_id FROM sys_company WHERE is_deleted =
        0 AND (road_expire_date IS NULL OR road_expire_date &lt; NOW())
        UNION ALL
        -- 3. 上岗资格证隐患
        SELECT m.id, m.certificate_expire_date as create_time, c.region_code, m.company_id FROM
        hot_company_safety_manager m LEFT JOIN sys_company c ON m.company_id = c.id WHERE m.is_deleted = 0 AND m.status
        = 1 AND (m.company_head IS NULL OR m.company_head &lt;&gt; 1) AND (m.is_resigned IS NULL OR m.is_resigned &lt;&gt;
        1) AND (m.certificate_expire_date IS NULL OR m.certificate_expire_date &lt; NOW())
        UNION ALL
        -- 4. 行驶证隐患
        SELECT v.id, LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) as create_time,
        c.region_code, v.company_id FROM hot_vehicle v LEFT JOIN sys_company c ON v.company_id = c.id WHERE v.is_deleted
        = 0 AND v.operation_status = 1 AND (v.inspection_valid_month IS NULL OR
        LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
        UNION ALL
        -- 5. 运输证隐患
        SELECT v.id, LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) as create_time,
        c.region_code, v.company_id FROM hot_vehicle v LEFT JOIN sys_company c ON v.company_id = c.id WHERE v.is_deleted
        = 0 AND v.operation_status = 1 AND (v.transport_valid_month IS NULL OR
        LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
        UNION ALL
        -- 6. 驾驶员身份证隐患
        SELECT d.id, d.id_card_expire_date as create_time, c.region_code, d.company_id FROM hot_driver d LEFT JOIN
        sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.id_card_long_term IS NULL OR d.id_card_long_term &lt;&gt; 1) AND
        (d.id_card_expire_date IS NULL
        OR d.id_card_expire_date &lt; NOW())
        UNION ALL
        -- 7. 驾驶员驾驶证隐患
        SELECT d.id, d.driver_license_expire_date as create_time, c.region_code, d.company_id FROM hot_driver d LEFT
        JOIN sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.driver_license_long_term IS NULL OR d.driver_license_long_term &lt;&gt; 1) AND
        (d.driver_license_expire_date IS NULL OR d.driver_license_expire_date &lt; NOW())
        UNION ALL
        -- 8. 驾驶员从业资格证隐患
        SELECT d.id, d.qualification_valid_end_date as create_time, c.region_code, d.company_id FROM hot_driver d LEFT
        JOIN sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.qualification_valid_end_date IS NULL OR d.qualification_valid_end_date &lt; NOW())
        UNION ALL
        -- 9. 驾驶员违规隐患
        SELECT v.id, v.occur_time as create_time, c.region_code, v.company_id FROM hot_violation_info v LEFT JOIN
        sys_company c ON v.company_id = c.id WHERE v.is_deleted = 0 AND v.process_status = 0
        ) AS all_risks
        WHERE region_code LIKE CONCAT(#{districtCode}, '%')
        <if test="companyId != null">
            AND company_id = #{companyId}
        </if>
        <if test="startDate != null and startDate != ''">
            AND create_time &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND create_time &lt;= #{endDate}
        </if>
        GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')
        ORDER BY date ASC
    </select>

    <!-- 统计简单隐患（按企业和分类分组） -->
    <select id="selectSimpleRiskCountsGroupedByCompany" resultType="com.hotwj.platform.workbench.domain.vo.CompanyRiskCountVo">
        SELECT company_id as companyId, category, region_code as regionCode, COUNT(*) as count
        FROM (
        -- 1. 营业执照隐患 (Enterprise)
        SELECT id, region_code, id as company_id, 'enterprise' as category FROM sys_company WHERE is_deleted = 0
        AND (business_long_term IS NULL OR business_long_term &lt;&gt; 1)
        AND (business_term IS NULL OR business_term &lt; NOW())
        UNION ALL
        -- 2. 道路经营许可证隐患 (Enterprise)
        SELECT id, region_code, id as company_id, 'enterprise' as category FROM sys_company WHERE is_deleted = 0
        AND (road_expire_date IS NULL OR road_expire_date &lt; NOW())
        UNION ALL
        -- 3. 上岗资格证隐患 (Enterprise)
        SELECT m.id, c.region_code, m.company_id, 'enterprise' as category FROM
        hot_company_safety_manager m LEFT JOIN sys_company c ON m.company_id = c.id WHERE m.is_deleted = 0 AND m.status
        = 1 AND (m.company_head IS NULL OR m.company_head &lt;&gt; 1) AND (m.is_resigned IS NULL OR m.is_resigned &lt;&gt;
        1) AND (m.certificate_expire_date IS NULL OR m.certificate_expire_date &lt; NOW())
        UNION ALL
        -- 4. 行驶证隐患 (Vehicle)
        SELECT v.id, c.region_code, v.company_id, 'vehicle' as category FROM hot_vehicle v LEFT JOIN sys_company c ON v.company_id = c.id WHERE v.is_deleted
        = 0 AND v.operation_status = 1 AND (v.inspection_valid_month IS NULL OR
        LAST_DAY(STR_TO_DATE(CONCAT(v.inspection_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
        UNION ALL
        -- 5. 运输证隐患 (Vehicle)
        SELECT v.id, c.region_code, v.company_id, 'vehicle' as category FROM hot_vehicle v LEFT JOIN sys_company c ON v.company_id = c.id WHERE v.is_deleted
        = 0 AND v.operation_status = 1 AND (v.transport_valid_month IS NULL OR
        LAST_DAY(STR_TO_DATE(CONCAT(v.transport_valid_month, '-01'), '%Y-%m-%d')) &lt; NOW())
        UNION ALL
        -- 6. 驾驶员身份证隐患 (Personnel)
        SELECT d.id, c.region_code, d.company_id, 'personnel' as category FROM hot_driver d LEFT JOIN
        sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.id_card_long_term IS NULL OR d.id_card_long_term &lt;&gt; 1) AND
        (d.id_card_expire_date IS NULL
        OR d.id_card_expire_date &lt; NOW())
        UNION ALL
        -- 7. 驾驶员驾驶证隐患 (Personnel)
        SELECT d.id, c.region_code, d.company_id, 'personnel' as category FROM hot_driver d LEFT
        JOIN sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.driver_license_long_term IS NULL OR d.driver_license_long_term &lt;&gt; 1) AND
        (d.driver_license_expire_date IS NULL OR d.driver_license_expire_date &lt; NOW())
        UNION ALL
        -- 8. 驾驶员从业资格证隐患 (Personnel)
        SELECT d.id, c.region_code, d.company_id, 'personnel' as category FROM hot_driver d LEFT
        JOIN sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND
        (d.qualification_valid_end_date IS NULL OR d.qualification_valid_end_date &lt; NOW())
        UNION ALL
        -- 9. 驾驶员违规隐患 (Personnel)
        SELECT v.id, c.region_code, v.company_id, 'personnel' as category FROM hot_violation_info v LEFT JOIN
        sys_company c ON v.company_id = c.id WHERE v.is_deleted = 0 AND v.process_status IN (0, 1)
        UNION ALL
        -- 10. 驾驶员未参加岗前培训隐患 (Personnel)
        SELECT d.id, c.region_code, d.company_id, 'personnel' as category FROM hot_driver d LEFT JOIN
        sys_company c ON d.company_id = c.id WHERE d.is_deleted = 0 AND d.status = 1 AND d.audit_status = 1 AND NOT
        EXISTS (SELECT 1
                          FROM hot_training_participant tp
                                   INNER JOIN hot_training t ON tp.training_id = t.id
                          WHERE tp.user_id = d.id
                            AND t.training_type IN ('pre-job')
                            AND tp.is_completed = 1
                            AND tp.is_pass = 1
                            AND t.is_deleted = 0
                            AND t.is_enabled = 1
                            AND tp.is_deleted = 0)
        AND NOT EXISTS (SELECT 1
                          FROM hot_training t
                          WHERE t.training_type = 'offline'
                            AND t.meeting_type = 'pre-job'
                            AND t.is_deleted = 0
                            AND FIND_IN_SET(d.id, t.participant_ids))
        ) AS all_risks
        WHERE company_id IS NOT NULL
        <if test="districtCode != null and districtCode != ''">
            AND region_code LIKE CONCAT(#{districtCode}, '%')
        </if>
        GROUP BY company_id, category, region_code
    </select>

    <select id="selectCompanyRegionCodes" resultType="com.hotwj.platform.workbench.domain.vo.CompanyRiskCountVo">
        SELECT id as companyId, region_code as regionCode
        FROM sys_company
        WHERE id IN
        <foreach collection="companyIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectTrainingParticipants" resultType="com.hotwj.platform.securityManagement.trainingParticipant.domain.HotTrainingParticipant">
        SELECT p.create_time, p.complete_time, p.is_completed
        FROM hot_training_participant p
        INNER JOIN sys_company c ON p.company_id = c.id
        WHERE p.is_deleted = 0
        AND c.is_deleted = 0
        AND c.region_code LIKE CONCAT(#{districtCode}, '%')
        AND p.create_time &lt;= #{endDate}
    </select>

    <!-- ================= 事故统计 ================= -->
    <select id="selectAccidentLevelStats" resultType="com.hotwj.platform.workbench.domain.vo.GovAccidentStatsVo">
        SELECT CASE
                   WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '轻微%' THEN '轻微事故'
                   WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '一般%' THEN '一般事故'
                   WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '重大%' THEN '重大事故'
                   WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '特大%' THEN '特大事故'
                   ELSE '其他'
                   END AS levelName,
               COUNT(*) AS count
        FROM hot_accident_archive a
            LEFT JOIN sys_company c
        ON a.company_id = c.id
        WHERE a.is_deleted = 0
          AND c.is_deleted = 0
          AND c.region_code LIKE CONCAT(#{districtCode}
            , '%')
          AND accident_level IS NOT NULL
        GROUP BY
            CASE
            WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '轻微%' THEN '轻微事故'
            WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '一般%' THEN '一般事故'
            WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '重大%' THEN '重大事故'
            WHEN JSON_UNQUOTE(JSON_EXTRACT(accident_level, '$.levelName')) LIKE '特大%' THEN '特大事故'
            ELSE '其他'
        END
        HAVING levelName IN ('轻微事故', '一般事故', '重大事故', '特大事故')
    </select>

</mapper>
