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

    <!-- 个人有效查获数量 -->
    <select id="selectPersonalEffectiveSeizureCount" 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_user_id = #{userId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 个人故意隐匿查获数量 -->
    <select id="selectPersonalWillfulConcealmentCount" 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_user_id = #{userId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.is_active_concealment = 1
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 个人移交公安查获数量 -->
    <select id="selectPersonalReferToPoliceCount" 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_user_id = #{userId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织有效查获总量 -->
    <select id="selectOrgEffectiveSeizureCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT
        IFNULL(SUM(isi.quantity), 0) AS totalCount,
        COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <choose>
            <when test="userType.name() == 'TEAM'">isr.inspect_team_id = #{deptId}</when>
            <when test="userType.name() == 'DEPARTMENT'">isr.inspect_department_id = #{deptId}</when>
            <when test="userType.name() == 'STATION'">isr.inspect_station_id = #{deptId}</when>
            <otherwise>isr.inspect_team_id = #{deptId}</otherwise>
        </choose>
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织故意隐匿查获总量 -->
    <select id="selectOrgWillfulConcealmentCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT
        IFNULL(SUM(isi.quantity), 0) AS totalCount,
        COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <choose>
            <when test="userType.name() == 'TEAM'">isr.inspect_team_id = #{deptId}</when>
            <when test="userType.name() == 'DEPARTMENT'">isr.inspect_department_id = #{deptId}</when>
            <when test="userType.name() == 'STATION'">isr.inspect_station_id = #{deptId}</when>
            <otherwise>isr.inspect_team_id = #{deptId}</otherwise>
        </choose>
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isi.is_active_concealment = 1
        AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织移交公安查获总量 -->
    <select id="selectOrgReferToPoliceCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT
        IFNULL(SUM(isi.quantity), 0) AS totalCount,
        COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <choose>
            <when test="userType.name() == 'TEAM'">isr.inspect_team_id = #{deptId}</when>
            <when test="userType.name() == 'DEPARTMENT'">isr.inspect_department_id = #{deptId}</when>
            <when test="userType.name() == 'STATION'">isr.inspect_station_id = #{deptId}</when>
            <otherwise>isr.inspect_team_id = #{deptId}</otherwise>
        </choose>
        AND isr.seizure_time >= #{startTime}
        AND isr.seizure_time &lt;= #{endTime}
        AND isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
        AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织有效查获总量 - 班组级别 -->
    <select id="selectTeamEffectiveSeizureCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_team_id = #{teamId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织有效查获总量 - 科室级别 -->
    <select id="selectDepartmentEffectiveSeizureCount"
            resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_department_id = #{departmentId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织有效查获总量 - 大队级别 -->
    <select id="selectBrigadeEffectiveSeizureCount"
            resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_brigade_id = #{brigadeId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织有效查获总量 - 站点级别 -->
    <select id="selectStationEffectiveSeizureCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        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' -- 已归档状态
    </select>

    <!-- 组织故意隐匿查获总量 - 班组级别 -->
    <select id="selectTeamWillfulConcealmentCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_team_id = #{teamId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.is_active_concealment = 1
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织故意隐匿查获总量 - 科室级别 -->
    <select id="selectDepartmentWillfulConcealmentCount"
            resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_department_id = #{departmentId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.is_active_concealment = 1
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织故意隐匿查获总量 - 大队级别 -->
    <select id="selectBrigadeWillfulConcealmentCount"
            resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_brigade_id = #{brigadeId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.is_active_concealment = 1
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织故意隐匿查获总量 - 站点级别 -->
    <select id="selectStationWillfulConcealmentCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        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 isi.is_active_concealment = 1
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织移交公安查获总量 - 班组级别 -->
    <select id="selectTeamReferToPoliceCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_team_id = #{teamId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织移交公安查获总量 - 科室级别 -->
    <select id="selectDepartmentReferToPoliceCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_department_id = #{departmentId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织移交公安查获总量 - 大队级别 -->
    <select id="selectBrigadeReferToPoliceCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        FROM item_seizure_record isr
            LEFT JOIN item_seizure_items isi
        ON isr.id = isi.record_id
        WHERE isr.inspect_brigade_id = #{brigadeId}
          AND isr.seizure_time >= #{startTime}
          AND isr.seizure_time &lt;= #{endTime}
          AND isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
          AND isr.process_status = '3' -- 已归档状态
    </select>

    <!-- 组织移交公安查获总量 - 站点级别 -->
    <select id="selectStationReferToPoliceCount" resultType="com.sundot.airport.item.domain.portrait.SeizureStats">
        SELECT IFNULL(SUM(isi.quantity), 0) AS totalCount,
               COUNT(DISTINCT isr.inspect_user_id) AS count
        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 isi.handling_method = 'TRANSFER_TO_AIRPORT_POLICE'
          AND isr.process_status = '3' -- 已归档状态
    </select>

</mapper>