<?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.HeadquartersWorkbenchMapper">

    <select id="selectTrainingTypeStats" resultType="com.hotwj.platform.workbench.domain.vo.TrainingTypeStatRow">
        SELECT t.training_type AS type,
               COUNT(*) AS count
        FROM
            hot_training t
            LEFT JOIN sys_company c
        ON t.company_id = c.id
        WHERE
            t.is_deleted = 0
          AND t.is_enabled = 1
          AND c.is_deleted = 0
        GROUP BY
            t.training_type
    </select>

    <!-- 月度每日完成数量 -->
    <select id="selectMonthlyCompleted" resultType="com.hotwj.platform.workbench.domain.vo.DailyCountRow">
        SELECT DATE_FORMAT(tp.complete_time, '%Y-%m-%d') AS day,
            COUNT(*) AS cnt
        FROM hot_training_participant tp
            INNER JOIN hot_training t
        ON t.id = tp.training_id
            AND t.is_deleted = 0
            AND t.is_enabled = 1
            AND (t.training_type IS NULL OR t.training_type &lt;&gt; 'offline')
            LEFT JOIN sys_company c
            ON t.company_id = c.id
        WHERE tp.is_deleted = 0
          AND tp.is_completed = 1
          AND c.is_deleted = '0'
          AND tp.complete_time &gt;= #{startDate}
          AND tp.complete_time
         &lt; #{endDate}
        GROUP BY DATE_FORMAT(tp.complete_time, '%Y-%m-%d')
    </select>

    <!-- 月度每日未完成数量（按创建时间统计当日新增且未完成的记录） -->
    <select id="selectMonthlyUncompleted" resultType="com.hotwj.platform.workbench.domain.vo.DailyCountRow">
        SELECT DATE_FORMAT(tp.create_time, '%Y-%m-%d') AS day,
            COUNT(*) AS cnt
        FROM hot_training_participant tp
            INNER JOIN hot_training t
        ON t.id = tp.training_id
            AND t.is_deleted = 0
            AND t.is_enabled = 1
            AND (t.training_type IS NULL OR t.training_type &lt;&gt; 'offline')
            LEFT JOIN sys_company c
            ON t.company_id = c.id
        WHERE tp.is_deleted = 0
          AND (tp.is_completed IS NULL
           OR tp.is_completed = 0)
          AND c.is_deleted = '0'
          AND tp.create_time &gt;= #{startDate}
          AND tp.create_time
         &lt; #{endDate}
        GROUP BY DATE_FORMAT(tp.create_time, '%Y-%m-%d')
    </select>

    <select id="selectMonthlyStudyTrendFromDailyStats"
            resultType="com.hotwj.platform.workbench.domain.vo.MonthlyTrendRow">
        SELECT DATE_FORMAT(s.stats_date, '%Y-%m-%d') AS day,
            SUM(s.completed_count) AS completed,
            SUM(s.uncompleted_count) AS uncompleted
        FROM hot_training_daily_stats s
            INNER JOIN sys_company c
        ON c.id = s.company_id
            AND c.is_deleted = 0
            AND c.status = 1
        WHERE s.is_deleted = 0
          AND s.stats_date BETWEEN #{startDate}
          AND #{endDate}
        GROUP BY s.stats_date
        ORDER BY s.stats_date ASC
    </select>

    <select id="selectViolationMonthlyCounts" resultType="com.hotwj.platform.workbench.domain.vo.ViolationMonthlyRow">
        SELECT DATE_FORMAT(v.occur_time, '%m') AS month,
            COUNT(*) AS cnt
        FROM hot_violation_info v
            INNER JOIN sys_company c
        ON c.id = v.company_id
            AND c.is_deleted = 0
            AND c.status = 1
        WHERE v.is_deleted = 0
          AND v.occur_time BETWEEN #{startDate}
          AND #{endDate}
        GROUP BY DATE_FORMAT(v.occur_time, '%m')
        ORDER BY month ASC
    </select>

    <!-- 查询所有车辆创建年份及所属企业行业 -->
    <select id="selectAllVehicleCreateYears" resultType="java.util.HashMap">
        SELECT
            YEAR (v.create_time) AS year, c.industry AS industry
        FROM hot_vehicle v
            LEFT JOIN sys_company c
        ON v.company_id = c.id
        WHERE v.is_deleted = 0
          AND v.create_time IS NOT NULL
        ORDER BY year ASC
    </select>

    <select id="selectLatestHiddenDangerSummary"
            resultType="com.hotwj.platform.workbench.domain.vo.HeadquartersHiddenDangerSummaryVo">
        SELECT IFNULL(SUM(total_risk), 0)      AS totalCount,
               IFNULL(SUM(enterprise_risk), 0) AS enterpriseCount,
               IFNULL(SUM(vehicle_risk), 0)    AS vehicleCount,
               IFNULL(SUM(personnel_risk), 0)  AS personnelCount
        FROM hot_hidden_danger_daily_stats
        WHERE stats_date = (SELECT MAX(stats_date) FROM hot_hidden_danger_daily_stats)
        <if test="regionCode != null and regionCode != ''">
            AND region_code LIKE CONCAT(#{regionCode}, '%')
        </if>
    </select>

    <select id="selectCompanyIndustryCounts" resultType="java.util.Map">
        SELECT
        industry,
        COUNT(*) as count
        FROM sys_company
        WHERE is_deleted = 0
        <if test="regionCode != null and regionCode != ''">
            AND region_code LIKE CONCAT(#{regionCode}, '%')
        </if>
        GROUP BY industry
    </select>

    <select id="selectAllCompanyCreateYears" resultType="java.util.Map">
        SELECT YEAR (create_time) as year, industry
        FROM sys_company
        WHERE is_deleted = 0
          AND create_time IS NOT NULL
        ORDER BY year ASC
    </select>

    <select id="selectAllPersonnelCreateYears" resultType="java.util.Map">
        SELECT YEAR (create_time) as year, person_type as personType
        FROM hot_driver
        WHERE is_deleted = 0
          AND status = 1
          AND create_time IS NOT NULL
        ORDER BY year ASC
    </select>

    <select id="selectMonthlyAccidentCounts" resultType="java.util.Map">
        SELECT
            YEAR(accident_time) as year, MONTH(accident_time) as month, COUNT(*) as count
        FROM hot_accident_archive
        WHERE is_deleted = 0
          AND YEAR(accident_time) BETWEEN #{startYear}
          AND #{endYear}
        GROUP BY YEAR(accident_time), MONTH(accident_time)
    </select>

    <select id="selectMonthlyHiddenDangerCounts" resultType="java.util.Map">
        SELECT
            YEAR(stats_date) as year, MONTH(stats_date) as month, SUM(total_risk) as count
        FROM hot_hidden_danger_daily_stats
        WHERE YEAR(stats_date) BETWEEN #{startYear}
          AND #{endYear}
        GROUP BY YEAR(stats_date), MONTH(stats_date)
    </select>

    <select id="selectCompanyListWithManagerCount"
            resultType="com.hotwj.platform.workbench.domain.vo.HeadquartersCompanyListVo$CompanyRow">
        SELECT c.id,
               c.company_name       AS companyName,
               c.responsible_name   AS leaderName,
               c.legal_person_name  AS legalPerson,
               (SELECT COUNT(*)
                FROM hot_company_safety_manager m
                WHERE m.company_id = c.id
                  AND m.is_deleted = 0
                  AND m.status = 1) AS managerCount
        FROM sys_company c
        WHERE c.is_deleted = 0
        ORDER BY c.create_time DESC
    </select>
</mapper>
