<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sundot.airport.blocked.mapper.BlockedDashboardMapper">
    
    <resultMap type="java.lang.Integer" id="BlockedCountResult">
        <result property="count" column="total_count"/>
    </resultMap>

    <!-- 查询查堵总数-->
    <select id="selectTotalBlockedCountWithoutBrigade" resultType="java.lang.Integer">
        SELECT COALESCE(SUM(total_blocked_count), 0) as total_count
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
    </select>

    <!-- 查询查堵总数-->
    <select id="selectTotalBlockedCountWithBrigade" resultType="java.lang.Integer">
        SELECT COUNT(1) as total_count
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND brigade_id = #{brigadeId}
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
    </select>

    <!-- 查询各大队查堵总数(用于柱状图) -->
    <select id="selectBlockedCountByBrigade" resultType="com.sundot.airport.blocked.dto.BlockedBrigadeStatsDTO">
        SELECT 
            brigade_id as brigadeId,
            brigade_name as brigadeName,
            COALESCE(SUM(total_blocked_count), 0) as totalBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY brigade_id, brigade_name
        ORDER BY totalBlockedCount DESC
    </select>

    <!-- 查询总查堵万分率(所有大队平均值) -->
    <select id="selectTotalBlockedRate" resultType="java.math.BigDecimal">
        SELECT COALESCE(AVG(daily_block_rate), 0) as totalBlockedRate
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
    </select>

    <!-- 查询各大队平均查堵万分率(用于条形图) -->
    <select id="selectBlockedRateByBrigade" resultType="com.sundot.airport.blocked.dto.BlockedBrigadeRateStatsDTO">
        SELECT 
            brigade_id as brigadeId,
            brigade_name as brigadeName,
            COALESCE(AVG(daily_block_rate), 0) as avgBlockedRate
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY brigade_id, brigade_name
        ORDER BY avgBlockedRate DESC
    </select>

    <!-- 查询每日查堵合计件数(用于趋势图) -->
    <select id="selectBlockedCountByDate" resultType="com.sundot.airport.blocked.dto.BlockedDailyStatsDTO">
        SELECT 
            stat_date as statDate,
            COALESCE(SUM(total_blocked_count), 0) as totalBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询每日各大队查堵合计件数(用于大队对比表) -->
    <select id="selectBlockedCountByDateAndBrigade" resultType="com.sundot.airport.blocked.dto.BlockedDailyBrigadeStatsDTO">
        SELECT 
            stat_date as statDate,
            brigade_id as brigadeId,
            brigade_name as brigadeName,
            COALESCE(SUM(total_blocked_count), 0) as totalBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date, brigade_id, brigade_name
        ORDER BY stat_date ASC, brigade_id ASC
    </select>

    <!-- 查询每日平均查堵万分率(用于趋势图) -->
    <select id="selectBlockedRateByDate" resultType="com.sundot.airport.blocked.dto.BlockedDailyRateStatsDTO">
        SELECT 
            stat_date as statDate,
            COALESCE(AVG(daily_block_rate), 0) as avgBlockedRate
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询每日各大队平均查堵万分率(用于大队对比表) -->
    <select id="selectBlockedRateByDateAndBrigade" resultType="com.sundot.airport.blocked.dto.BlockedDailyBrigadeRateStatsDTO">
        SELECT 
            stat_date as statDate,
            brigade_id as brigadeId,
            brigade_name as brigadeName,
            COALESCE(AVG(daily_block_rate), 0) as avgBlockedRate
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date, brigade_id, brigade_name
        ORDER BY stat_date ASC, brigade_id ASC
    </select>

    <!-- 查询每日过检行李合计(用于趋势图) -->
    <select id="selectTotalLuggageCountByDate" resultType="com.sundot.airport.blocked.dto.BlockedDailyLuggageStatsDTO">
        SELECT 
            stat_date as statDate,
            COALESCE(SUM(total_luggage_count), 0) as totalLuggageCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询每日各大队过检行李合计(用于大队对比表) -->
    <select id="selectTotalLuggageCountByDateAndBrigade" resultType="com.sundot.airport.blocked.dto.BlockedDailyBrigadeLuggageStatsDTO">
        SELECT 
            stat_date as statDate,
            brigade_id as brigadeId,
            brigade_name as brigadeName,
            COALESCE(SUM(total_luggage_count), 0) as totalLuggageCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY stat_date, brigade_id, brigade_name
        ORDER BY stat_date ASC, brigade_id ASC
    </select>

    <!-- 查询查堵物品分布(用于扇形图) -->
    <select id="selectItemDistribution" resultType="com.sundot.airport.blocked.dto.BlockedItemDistributionDTO">
        SELECT 
            miss_check_item as missCheckItem,
            COUNT(1) as count,
            ROUND(COUNT(1) * 100.0 / SUM(COUNT(1)) OVER(), 2) as percentage
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND miss_check_item IS NOT NULL
        AND miss_check_item != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY miss_check_item
        ORDER BY count DESC
    </select>

    <!-- 查询区域查堵数据分布(用于双轴图表) -->
    <select id="selectAreaDistribution" resultType="com.sundot.airport.blocked.dto.BlockedAreaDistributionDTO">
        SELECT 
            COALESCE(SUM(t1_travel_luggage_count), 0) as t1PassengerLuggageCount,
            COALESCE(SUM(t2_travel_luggage_count), 0) as t2PassengerLuggageCount,
            COALESCE(SUM(t1_walk_luggage_count), 0) as t1CargoLuggageCount,
            COALESCE(SUM(t2_walk_luggage_count), 0) as t2CargoLuggageCount,
            COALESCE(SUM(t1_travel_blocked_count), 0) as t1PassengerBlockedCount,
            COALESCE(SUM(t2_travel_blocked_count), 0) as t2PassengerBlockedCount,
            COALESCE(SUM(t1_walk_blocked_count), 0) as t1CargoBlockedCount,
            COALESCE(SUM(t2_walk_blocked_count), 0) as t2CargoBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        AND brigade_id IS NOT NULL
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
    </select>

    <!-- 查询查堵类型分布(用于分组柱状图) -->
    <select id="selectDiscriminationDistribution" resultType="com.sundot.airport.blocked.dto.BlockedDiscriminationDistributionDTO">
        SELECT 
            miss_check_item as missCheckItem,
            discrimination_type as discriminationType,
            COUNT(1) as count
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND miss_check_item IS NOT NULL
        AND miss_check_item != ''
        AND discrimination_type IS NOT NULL
        AND discrimination_type != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY miss_check_item, discrimination_type
        ORDER BY miss_check_item, count DESC
    </select>

    <!-- 查询查堵时间段过检行李数及万分率(用于双轴图表) -->
    <select id="selectTimePeriodStats" resultType="com.sundot.airport.blocked.dto.BlockedTimePeriodStatsDTO">
        SELECT 
            time_period as timePeriod,
            COALESCE(SUM(total_luggage_count), 0) as totalLuggageCount,
            COALESCE(SUM(total_blocked_count), 0) as totalBlockedCount,
            COALESCE(AVG(blocked_rate), 0) as blockRate
        FROM blocked_luggage_piece_daily
        WHERE del_flag = '0'
        AND time_period IS NOT NULL
        AND time_period != ''
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY time_period
        ORDER BY MIN(time_period)
    </select>

    <!-- 查询每日过检行李数及万分率(用于双轴图表) -->
    <select id="selectDailyLuggageAndRate" resultType="com.sundot.airport.blocked.dto.BlockedTimePeriodStatsDTO">
        SELECT 
            DATE_FORMAT(stat_date, '%Y-%m-%d') as timePeriod,
            COALESCE(SUM(total_luggage_count), 0) as totalLuggageCount,
            COALESCE(SUM(total_blocked_count), 0) as totalBlockedCount,
            COALESCE(AVG(daily_block_rate), 0) as blockRate
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询查堵-AI复查图像总数(用于双系列图表) -->
    <select id="selectAiImageStats" resultType="com.sundot.airport.blocked.dto.BlockedDailyStatsDTO">
        SELECT 
            DATE_FORMAT(stat_date, '%Y-%m-%d') as statDate,
            COALESCE(SUM(ai_review_image_total), 0) as totalBlockedCount,
            COALESCE(SUM(ai_mark_total), 0) as dailyBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询查堵-AI漏判图像总数(用于单系列图表) -->
    <select id="selectAiMissImageStats" resultType="com.sundot.airport.blocked.dto.BlockedDailyStatsDTO">
        SELECT 
            DATE_FORMAT(stat_date, '%Y-%m-%d') as statDate,
            COALESCE(SUM(ai_miss_image_total), 0) as totalBlockedCount,
            0 as dailyBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询查堵-AI误判图像总数(用于单系列图表) -->
    <select id="selectAiErrorImageStats" resultType="com.sundot.airport.blocked.dto.BlockedDailyStatsDTO">
        SELECT 
            DATE_FORMAT(stat_date, '%Y-%m-%d') as statDate,
            COALESCE(SUM(ai_error_image_total), 0) as totalBlockedCount,
            0 as dailyBlockedCount
        FROM blocked_luggage_statistics_daily
        WHERE del_flag = '0'
        <if test="startTime != null and endTime != null">
            AND stat_date &gt;= #{startTime}
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        GROUP BY stat_date
        ORDER BY stat_date ASC
    </select>

    <!-- 查询查堵-主管排行榜 -->
    <select id="selectSupervisorRanking" resultType="com.sundot.airport.blocked.dto.BlockedRankingDTO">
        SELECT 
            supervisor_name as name,
            supervisor_id as id,
            COUNT(1) as totalCount
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND supervisor_name IS NOT NULL
        AND supervisor_name != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        <if test="supervisorId != null">
            AND supervisor_id = #{supervisorId}
        </if>
        GROUP BY supervisor_id, supervisor_name
        ORDER BY totalCount DESC
        LIMIT 30
    </select>

    <!-- 查询查堵-班组排行榜 -->
    <select id="selectTeamLeaderRanking" resultType="com.sundot.airport.blocked.dto.BlockedRankingDTO">
        SELECT 
            team_leader_name as name,
            team_leader_id as id,
            COUNT(1) as totalCount
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND team_leader_name IS NOT NULL
        AND team_leader_name != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        <if test="teamLeaderId != null">
            AND team_leader_id = #{teamLeaderId}
        </if>
        GROUP BY team_leader_id, team_leader_name
        ORDER BY totalCount DESC
        LIMIT 30
    </select>

    <!-- 查询查堵-人员排行榜 -->
    <select id="selectReviewedUserRanking" resultType="com.sundot.airport.blocked.dto.BlockedRankingDTO">
        SELECT 
            reviewed_user_name as name,
            reviewed_user_id as id,
            COUNT(1) as totalCount
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND reviewed_user_name IS NOT NULL
        AND reviewed_user_name != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        <if test="reviewedUserId != null">
            AND reviewed_user_id = #{reviewedUserId}
        </if>
        GROUP BY reviewed_user_id, reviewed_user_name
        ORDER BY totalCount DESC
        LIMIT 30
    </select>

    <!-- 查询查堵-物品位置分布(用于扇形图) -->
    <select id="selectItemLocationDistribution" resultType="com.sundot.airport.blocked.dto.BlockedItemLocationDistributionDTO">
        SELECT 
            item_location as itemLocation,
            COUNT(1) as count,
            ROUND(COUNT(1) * 100.0 / SUM(COUNT(1)) OVER(), 2) as percentage
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND item_location IS NOT NULL
        AND item_location != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        <if test="itemLocation != null and itemLocation != ''">
            AND item_location = #{itemLocation}
        </if>
        GROUP BY item_location
        ORDER BY count DESC
    </select>

    <!-- 查询查堵-原因分类分布(用于扇形图) -->
    <select id="selectMissCheckReasonDistribution" resultType="com.sundot.airport.blocked.dto.BlockedMissCheckReasonDistributionDTO">
        SELECT 
            miss_check_reason_category as missCheckReasonCategory,
            COUNT(1) as count,
            ROUND(COUNT(1) * 100.0 / SUM(COUNT(1)) OVER(), 2) as percentage
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND area_id IS NOT NULL
        AND miss_check_reason_category IS NOT NULL
        AND miss_check_reason_category != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND brigade_id = #{brigadeId}
        </if>
        <if test="terminalId != null">
            AND terminal_id = #{terminalId}
        </if>
        GROUP BY miss_check_reason_category
        ORDER BY count DESC
    </select>

    <!-- 查询查堵-图像难易程度分布(用于扇形图) -->
    <select id="selectDifficultyDistribution" resultType="com.sundot.airport.blocked.dto.BlockedDifficultyDistributionDTO">
        SELECT 
            difficulty_level as difficultyLevel,
            COUNT(1) as count,
            ROUND(COUNT(1) * 100.0 / SUM(COUNT(1)) OVER(), 2) as percentage
        FROM blocked_miss_check_statistics
        WHERE del_flag = '0'
        AND difficulty_level IS NOT NULL
        AND difficulty_level != ''
        <if test="startTime != null and endTime != null">
            AND review_date &gt;= #{startTime}
            AND review_date &lt;= #{endTime}
        </if>
        GROUP BY difficulty_level
        ORDER BY count DESC
    </select>

    <!-- 查询查堵-开机人员持证比例(用于扇形图) -->
    <select id="selectCertificateDistribution" resultType="com.sundot.airport.blocked.dto.BlockedCertificateDistributionDTO">
        SELECT 
            u.qualification_level as certificateLevel,
            COUNT(DISTINCT bms.reviewed_user_id) as count,
            ROUND(COUNT(DISTINCT bms.reviewed_user_id) * 100.0 / SUM(COUNT(DISTINCT bms.reviewed_user_id)) OVER(), 2) as percentage
        FROM blocked_miss_check_statistics bms
        INNER JOIN sys_user u ON bms.reviewed_user_id = u.user_id
        WHERE bms.del_flag = '0'
        AND u.del_flag = '0'
        AND u.status = '0'
        AND u.qualification_level IS NOT NULL
        AND u.qualification_level != ''
        <if test="startTime != null and endTime != null">
            AND bms.review_date &gt;= #{startTime}
            AND bms.review_date &lt;= #{endTime}
        </if>
        <if test="brigadeId != null">
            AND bms.brigade_id = #{brigadeId}
        </if>
        <if test="terminalId != null">
            AND bms.terminal_id = #{terminalId}
        </if>
        GROUP BY u.qualification_level
        ORDER BY count DESC
    </select>

    <!-- 查询大队开机人员证书分布(用于柱状图) -->
    <select id="selectBrigadeCertificateDistribution" resultType="com.sundot.airport.blocked.dto.BlockedBrigadeCertificateDistributionDTO">
        SELECT 
            d.dept_name as brigadeName,
            d.dept_id as brigadeId,
            COUNT(DISTINCT CASE WHEN u.qualification_level = 'LEVEL_ONE' THEN u.user_id END) as levelOneCount,
            COUNT(DISTINCT CASE WHEN u.qualification_level = 'LEVEL_TWO' THEN u.user_id END) as levelTwoCount,
            COUNT(DISTINCT CASE WHEN u.qualification_level = 'LEVEL_THREE' THEN u.user_id END) as levelThreeCount,
            COUNT(DISTINCT CASE WHEN u.qualification_level = 'LEVEL_FOUR' THEN u.user_id END) as levelFourCount,
            COUNT(DISTINCT CASE WHEN u.qualification_level = 'LEVEL_FIVE' THEN u.user_id END) as levelFiveCount,
            COUNT(DISTINCT u.user_id) as totalCount
        FROM sys_user u
        INNER JOIN sys_dept d ON u.dept_id = d.dept_id
        WHERE u.del_flag = '0'
        AND u.status = '0'
        AND u.qualification_level IS NOT NULL
        AND u.qualification_level != ''
        AND d.dept_type = 'BRIGADE'
        <if test="terminalId != null">
            AND d.terminal_id = #{terminalId}
        </if>
        GROUP BY d.dept_id, d.dept_name
        ORDER BY totalCount DESC
    </select>

</mapper>
