<?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.item.mapper.ItemCategoryStatsMapper">

    <!-- 按一级分类统计全站查获数据 -->
    <select id="selectStationCategoryStats"
            resultType="com.sundot.airport.item.domain.dto.ItemCategoryStatsDTO$CategoryStat">
        SELECT isi.category_code_one AS categoryCodeOne,
        isi.category_name_one AS categoryNameOne,
        SUM(isi.quantity) AS quantity
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        AND isi.category_code_one IS NOT NULL
        AND isi.category_name_one IS NOT NULL
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
        GROUP BY isi.category_code_one, isi.category_name_one
        ORDER BY quantity DESC
    </select>

    <!-- 统计全站查获总量 -->
    <select id="selectStationTotalQuantity" resultType="java.lang.Long">
        SELECT IFNULL(SUM(isi.quantity), 0)
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
    </select>

    <!-- 按部位统计隐匿夹带查获数据 -->
    <select id="selectStationConcealmentPositionStats"
            resultType="com.sundot.airport.item.domain.dto.ItemCategoryStatsDTO$CategoryStat">
        SELECT isi.check_position_code_two AS categoryCodeOne,
        isi.check_position_name_two AS categoryNameOne,
        SUM(isi.quantity) AS quantity
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        AND isi.is_active_concealment = 1 -- 是否隐匿夹带为"是"
        AND isi.check_position_code_two IS NOT NULL
        AND isi.check_position_name_two IS NOT NULL
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
        GROUP BY isi.check_position_code_two, isi.check_position_name_two
        ORDER BY quantity DESC
    </select>

    <!-- 统计全站隐匿夹带查获总量 -->
    <select id="selectStationConcealmentTotalQuantity" resultType="java.lang.Long">
        SELECT IFNULL(SUM(isi.quantity), 0)
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        AND isi.is_active_concealment = 1 -- 是否隐匿夹带为"是"
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
    </select>

    <!-- 按岗位第二级分类统计查获数据 -->
    <select id="selectStationPostCategoryStats"
            resultType="com.sundot.airport.item.domain.dto.ItemCategoryStatsDTO$CategoryStat">
        SELECT isr.check_method AS categoryCodeOne,
        isr.check_method_desc AS categoryNameOne,
        SUM(isi.quantity) AS quantity
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        AND isr.check_method IS NOT NULL
        AND isr.check_method_desc IS NOT NULL
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
        GROUP BY isr.check_method, isr.check_method_desc
        ORDER BY quantity DESC
    </select>

    <!-- 统计全站岗位分类查获总量 -->
    <select id="selectStationPostCategoryTotalQuantity" resultType="java.lang.Long">
        SELECT IFNULL(SUM(isi.quantity), 0)
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
    </select>

    <!-- 查询全站通道排名前五数据 -->
    <select id="selectTopFiveChannelRankings"
            resultType="com.sundot.airport.item.domain.dto.ChannelRankingStatsDTO$ChannelRankingItem">
        SELECT t.channelName,
        t.seizureQuantity,
        t.regionalName,
        (@row_number := @row_number + 1) AS ranking
        FROM (SELECT isr.channel_name AS channelName,
        SUM(isi.quantity) AS seizureQuantity,
        isr.regional_name AS regionalName
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_station_id = #{stationId}
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
        AND isr.channel_name IS NOT NULL
        AND isr.regional_name IS NOT NULL
        <if test="brigadeId != null">
            AND isr.inspect_brigade_id = #{brigadeId}
        </if>
        <if test="departmentId != null">
            AND isr.inspect_department_id = #{departmentId}
        </if>
        <if test="teamId != null">
            AND isr.inspect_team_id = #{teamId}
        </if>
        <if test="userId != null">
            AND isr.inspect_user_id = #{userId}
        </if>
        GROUP BY isr.channel_name, isr.regional_name
        ORDER BY seizureQuantity DESC LIMIT 5) t
        CROSS JOIN (SELECT @row_number := 0) r
    </select>
</mapper>