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

    <!-- 查询本人查获数量 -->
    <select id="selectSelfSeizureCount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS seizure_count
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status=3 AND isr.inspect_user_id = #{userId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询班组查获总数 -->
    <select id="selectTeamAverage" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS total_team_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_team_id = #{deptId}
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 科室查获总数 -->
    <select id="selectDepartmentAverage" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS department_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status=3 AND isr.inspect_department_id = #{deptId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 大队查获总数 -->
    <select id="selectBrigadeAverage" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS brigade_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status=3 AND isr.inspect_brigade_id = #{deptId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 全站查获总数 -->
    <select id="selectStationAverage" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS station_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status=3 AND isr.inspect_station_id = #{deptId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询待处理数据数量 -->
    <select id="selectPendingCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM approval_instance ai
        JOIN approval_task at ON ai.id = at.instance_id
        WHERE ai.workflow_id IN (4, 5) -- 查获审批流程
        AND at.status = 'PENDING' -- 待审批状态
        AND at.assignee_id = #{userId} -- 分配给当前用户的任务
        <if test="startDate != null and endDate != null">
            AND ai.submit_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询排名信息 - -->
    <select id="selectRankingInfo" resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$RankingInfo">
        SELECT
        ranking AS current_rank,
        total_users AS total_items,
        CASE
        WHEN ranking &lt;= 3 THEN 'GREEN'
        ELSE 'RED'
        END AS color
        FROM (
        SELECT
        user_id,
        total_seizure,
        @rank := @rank + 1 AS ranking,
        @total_users AS total_items
        FROM (
        SELECT
        isr.inspect_user_id AS user_id,
        SUM(isi.quantity) AS total_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <if test="level == 'team'">
            isr.inspect_team_id = #{deptId}
        </if>
        <if test="level == 'department'">
            isr.inspect_department_id = #{deptId}
        </if>
        <if test="level == 'station'">
            isr.inspect_station_id = #{deptId}
        </if>
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY isr.inspect_user_id
        ORDER BY total_seizure DESC
        ) ranked_users
        CROSS JOIN (SELECT @rank := 0) r1,
        (SELECT @total_users := (
        SELECT COUNT(DISTINCT isr.inspect_user_id)
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <if test="level == 'team'">
            isr.inspect_team_id = #{deptId}
        </if>
        <if test="level == 'department'">
            isr.inspect_department_id = #{deptId}
        </if>
        <if test="level == 'station'">
            isr.inspect_station_id = #{deptId}
        </if>
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        )) r2
        ) ranked_result
        WHERE user_id = #{userId}
    </select>

    <!-- 查询个人排名-->
    <select id="selectSelfRanking" resultType="java.lang.Integer">
        SELECT ranking
        FROM (
        SELECT
        user_id,
        total_seizure,
        @rank := @rank + 1 AS ranking
        FROM (
        SELECT
        isr.inspect_user_id AS user_id,
        SUM(isi.quantity) AS total_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE
        <if test="level == 'team'">
            isr.inspect_team_id = #{deptId}
        </if>
        <if test="level == 'department'">
            isr.inspect_department_id = #{deptId}
        </if>
        <if test="level == 'brigade'">
            isr.inspect_brigade_id = #{deptId}
        </if>
        <if test="level == 'station'">
            isr.inspect_station_id = #{deptId}
        </if>
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY isr.inspect_user_id
        ORDER BY total_seizure DESC
        ) ranked_users
        CROSS JOIN (SELECT @rank := 0) r
        ) ranked_result
        WHERE user_id = #{userId}
    </select>

    <!-- 查询班组部门ID（根据用户ID） -->
    <select id="selectSectionDeptIdByUserId" resultType="java.lang.Long">
        SELECT dept_id
        FROM sys_user
        WHERE user_id = #{userId}
    </select>

    <!-- 查询科室部门ID（根据班组ID） -->
    <select id="selectSectionDeptIdByTeamId" resultType="java.lang.Long">
        SELECT parent_id
        FROM sys_dept
        WHERE dept_id = #{teamId}
    </select>

    <!-- 查询大队部门ID（根据科室ID） -->
    <select id="selectBrigadeDeptIdByDepartmentId" resultType="java.lang.Long">
        SELECT parent_id
        FROM sys_dept
        WHERE dept_id = #{departmentId}
    </select>

    <!-- 查询站点部门ID（根据部门ID） -->
    <select id="selectStationDeptIdByDeptId" resultType="java.lang.Long">
        SELECT parent_id
        FROM sys_dept
        WHERE dept_id = #{deptId}
    </select>

    <!-- 查询全站查获总数 -->
    <select id="selectTotalStationSeizure" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS total_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status=3 AND isr.inspect_station_id = #{deptId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询科室排名 -->
    <select id="selectDepartmentRankings"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$DepartmentRankingItem">
        SELECT
        sd.dept_name AS departmentName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_department_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id = #{deptId}
        AND del_flag = '0'
        AND dept_type = 'BRIGADE')
        AND sd.del_flag = '0'
        AND sd.dept_type = 'MANAGER' -- 确保是科室
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询科室排名 -->
    <select id="selectBrigadeRankings"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$BrigadeRankingItem">
        SELECT sd.dept_name AS brigadeName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_brigade_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{deptId}
        AND sd.del_flag = '0'
        AND sd.dept_type = 'BRIGADE'
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询科室排名（根据大队 ID） -->
    <select id="selectDepartmentRankingsByBrigadeId"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$DepartmentRankingItem">
        SELECT
        sd.dept_name AS departmentName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{brigadeId} -- 大队 ID
        AND sd.del_flag = '0'
        AND sd.dept_type = 'MANAGER' -- 确保是科室
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询班组排名（根据科室 ID） -->
    <select id="selectTeamRankingsByDepartmentId"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TeamRankingItem">
        SELECT
        sd.dept_name AS teamName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{departmentId} -- 科室 ID
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询用户排名（根据班组 ID） -->
    <select id="selectUserRankingsByTeamId"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$UserRankingItem">
        SELECT
        su.nick_name AS userName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        from sys_user su
        LEFT JOIN item_seizure_record isr ON su.user_id = isr.inspect_user_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        where su.dept_id = #{teamId}
        and su.del_flag = '0'
        GROUP BY su.user_id
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询排名前三的班组 -->
    <select id="selectTopThreeTeams" resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TopThreeTeamItem">
        SELECT
        sd.dept_name AS teamName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{deptId} -- 假设parent_id是科室ID
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
        LIMIT 3
    </select>

    <!-- 查询排名前三的科室 -->
    <select id="selectTopThreeDepartment"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TopThreeDepartmentItem">
        SELECT
        sd.dept_name AS departmentName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_department_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{deptId}
        AND sd.del_flag = '0'
        AND sd.dept_type = 'MANAGER'
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
        LIMIT 3
    </select>

    <!-- 查询前三名的班组排名 -->
    <select id="selectTopThreeTeamRankings"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TeamRankingItem">
        SELECT CONCAT(sd3.dept_name, sd2.dept_name, sd.dept_name) AS teamName, -- 大队名称+科室名称+班组名称
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN sys_dept sd2 ON sd.parent_id = sd2.dept_id -- 获取科室名称
        LEFT JOIN sys_dept sd3 ON sd2.parent_id = sd3.dept_id -- 获取大队名称
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id = #{deptId}
        AND del_flag = '0'
        AND dept_type = 'BRIGADE')
        AND del_flag = '0'
        AND dept_type = 'MANAGER')
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name, sd2.dept_name, sd3.dept_name
        ORDER BY seizureCount
        <choose>
            <when test="order == 'desc' or order == null">
                DESC
            </when>
            <otherwise>
                ASC
            </otherwise>
        </choose>
        LIMIT 3
    </select>

    <!-- 查询前三名的科室排名 -->
    <select id="selectTopThreeDepartmentRankings"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$DepartmentRankingItem">
        SELECT CONCAT(sd2.dept_name, sd.dept_name) AS departmentName, -- 科室名称+班组名称
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN sys_dept sd2 ON sd.parent_id = sd2.dept_id
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_department_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id = #{deptId}
        AND del_flag = '0'
        AND dept_type = 'BRIGADE')
        AND sd.del_flag = '0'
        AND sd.dept_type = 'MANAGER'
        GROUP BY sd.dept_id, sd.dept_name, sd2.dept_name
        ORDER BY seizureCount
        <choose>
            <when test="order == 'desc' or order == null">
                DESC
            </when>
            <otherwise>
                ASC
            </otherwise>
        </choose>
        LIMIT 3
    </select>

    <!-- 查询班组长查获总数 -->
    <select id="selectTeamSeizure" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS team_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_team_id = #{deptId}
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询科室查获总数 -->
    <select id="selectDepartmentSeizure" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS department_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_department_id = #{deptId}
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询大队查获总数 -->
    <select id="selectBrigadeSeizure" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS brigade_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_brigade_id = #{deptId}
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询科室下的班组总数 -->
    <select id="selectTeamCountByDepartmentId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS team_count
        FROM sys_dept
        WHERE parent_id = #{deptId} -- 科室ID
          AND del_flag = '0'
          AND dept_type = 'TEAMS' -- 确保是班组
    </select>

    <!-- 查询大队下的班组总数 -->
    <select id="selectTeamCountByBrigadeId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS team_count
        FROM sys_dept
        WHERE parent_id IN (SELECT dept_id
                            FROM sys_dept
                            WHERE parent_id = #{deptId}
                              AND del_flag = '0'
                              AND dept_type = 'MANAGER')
          AND del_flag = '0'
          AND dept_type = 'TEAMS'
    </select>

    <!-- 查询全站下的班组总数 -->
    <select id="selectTeamCountByStationId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS team_count
        FROM sys_dept sd
        WHERE sd.parent_id IN (SELECT dept_id
                               FROM sys_dept
                               WHERE parent_id IN (SELECT dept_id
                                                   FROM sys_dept
                                                   WHERE parent_id = #{deptId}
                                                     AND del_flag = '0'
                                                     AND dept_type = 'BRIGADE')
                                 AND del_flag = '0'
                                 AND dept_type = 'MANAGER')
          AND sd.del_flag = '0'
          AND sd.dept_type = 'TEAMS'
    </select>

    <!-- 查询科室下所有班组的排名 -->
    <select id="selectAllTeamRankingsInDepartment"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TeamRankingItem">
        SELECT
        sd.dept_name AS teamName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{deptId} -- 科室ID
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询大队下所有班组的排名 -->
    <select id="selectAllTeamRankingsInBrigade"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TeamRankingItem">
        SELECT CONCAT(sd2.dept_name, sd.dept_name) AS teamName, -- 科室名称+班组名称
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN sys_dept sd2 ON sd.parent_id = sd2.dept_id -- 获取科室名称
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id = #{deptId}
        AND del_flag = '0'
        AND dept_type = 'MANAGER')
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name, sd2.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询全站下所有班组的排名 -->
    <select id="selectAllTeamRankingsInStation"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$TeamRankingItem">
        SELECT CONCAT(sd3.dept_name, sd2.dept_name, sd.dept_name) AS teamName, -- 大队名称+科室名称+班组名称
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN sys_dept sd2 ON sd.parent_id = sd2.dept_id -- 获取科室名称
        LEFT JOIN sys_dept sd3 ON sd2.parent_id = sd3.dept_id -- 获取大队名称
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_team_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id IN (SELECT dept_id
        FROM sys_dept
        WHERE parent_id = #{deptId}
        AND del_flag = '0'
        AND dept_type = 'BRIGADE')
        AND del_flag = '0'
        AND dept_type = 'MANAGER')
        AND sd.del_flag = '0'
        AND sd.dept_type = 'TEAMS' -- 确保是班组
        GROUP BY sd.dept_id, sd.dept_name, sd2.dept_name, sd3.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询班组下用户总数 -->
    <select id="selectUserCountByTeamId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT user_id) AS user_count
        FROM sys_user
        WHERE dept_id = #{deptId}
          AND del_flag = '0'
    </select>

    <!-- 查询科室下用户总数（包括科室下所有班组的用户） -->
    <select id="selectUserCountByDepartmentId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT user_id) AS user_count
        FROM sys_user
        WHERE dept_id IN
              (SELECT dept_id
               FROM sys_dept
               WHERE parent_id = #{deptId}
                 AND del_flag = '0'
                 AND dept_type = 'TEAMS')
          AND del_flag = '0'
    </select>

    <!-- 查询大队下用户总数（包括大队下所有科室下所有班组的用户） -->
    <select id="selectUserCountByBrigadeId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT user_id) AS user_count
        FROM sys_user
        WHERE dept_id IN (SELECT dept_id
                          FROM sys_dept
                          WHERE parent_id IN (SELECT dept_id
                                              FROM sys_dept
                                              WHERE parent_id = #{deptId}
                                                AND del_flag = '0'
                                                AND dept_type = 'MANAGER')
                            AND del_flag = '0'
                            AND dept_type = 'TEAMS')
          AND del_flag = '0'
    </select>

    <!-- 查询全站下用户总数（包括全站下所有科室和班组的用户） -->
    <select id="selectUserCountByStationId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT user_id) AS user_count
        FROM sys_user
        WHERE dept_id IN (SELECT dept_id
                          FROM sys_dept
                          WHERE parent_id IN (SELECT dept_id
                                              FROM sys_dept
                                              WHERE parent_id in
                                                    (SELECT dept_id
                                                     FROM sys_dept
                                                     WHERE parent_id = #{deptId}
                                                       AND del_flag = '0'
                                                       AND dept_type = 'BRIGADE')
                                                AND del_flag = '0'
                                                AND dept_type = 'MANAGER')
                            AND del_flag = '0'
                            AND dept_type = 'TEAMS')
          AND del_flag = '0'
    </select>

    <!-- 查询全站下科室总数 -->
    <select id="selectDepartmentCountByStationId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS department_count
        FROM sys_dept
        WHERE parent_id in (SELECT dept_id
                            FROM sys_dept
                            WHERE parent_id = #{deptId}
                              AND del_flag = '0'
                              AND dept_type = 'BRIGADE')
          AND del_flag = '0'
          AND dept_type = 'MANAGER'
    </select>

    <!-- 查询全站下大队总数 -->
    <select id="selectBrigadeCountByStationId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS brigade_count
        FROM sys_dept
        WHERE parent_id = #{deptId}
          AND del_flag = '0'
          AND dept_type = 'BRIGADE'
    </select>

    <!-- 查询全大队下科室总数 -->
    <select id="selectDepartmentCountByBrigadeId" resultType="java.lang.Integer">
        SELECT COUNT(*) AS department_count
        FROM sys_dept
        WHERE parent_id = #{deptId} -- 大队ID下的科室
          AND del_flag = '0'
          AND dept_type = 'MANAGER' -- 确保是科室
    </select>

    <!-- 查询全站下所有科室的排名 -->
    <select id="selectAllDepartmentRankingsInStation"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$DepartmentRankingItem">
        SELECT sd.dept_name                   AS departmentName,
               COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
                 LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_department_id
                 LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
            AND isr.process_status = 3
        WHERE sd.parent_id in (SELECT dept_id
                               FROM sys_dept
                               WHERE parent_id = #{deptId}
                                 AND del_flag = '0'
                                 AND dept_type = 'BRIGADE')
          AND sd.del_flag = '0'
          AND sd.dept_type = 'MANAGER'
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询全站下所有大队的排名 -->
    <select id="selectAllBrigadeRankingsInStation"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$BrigadeRankingItem">
        SELECT sd.dept_name                   AS brigadeName,
               COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
                 LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_brigade_id
                 LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
            AND isr.process_status = 3
        WHERE sd.parent_id = #{deptId}
          AND sd.del_flag = '0'
          AND sd.dept_type = 'BRIGADE'
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询全站下所有科室的排名 -->
    <select id="selectAllDepartmentRankingsInBrigade"
            resultType="com.sundot.airport.item.domain.home.SeizureReportDTO$DepartmentRankingItem">
        SELECT
        sd.dept_name AS departmentName,
        COALESCE(SUM(isi.quantity), 0) AS seizureCount
        FROM sys_dept sd
        LEFT JOIN item_seizure_record isr ON sd.dept_id = isr.inspect_department_id
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        AND isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        WHERE sd.parent_id = #{deptId}
        AND sd.del_flag = '0'
        AND sd.dept_type = 'MANAGER'
        GROUP BY sd.dept_id, sd.dept_name
        ORDER BY seizureCount DESC
    </select>

    <!-- 查询全站范围内查获数量的最大值 -->
    <select id="selectMaxSeizureCountInStation" resultType="java.math.BigDecimal">
        SELECT COALESCE(MAX(user_seizure_count), 0) AS max_seizure_count
        FROM (
        SELECT SUM(isi.quantity) AS user_seizure_count
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.process_status = 3
        AND isr.inspect_station_id = #{deptId}
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY isr.inspect_user_id
        ) AS user_totals
    </select>

    <!-- 查询草稿箱中数据数量 -->
    <select id="selectDraftItemCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM item_seizure_record isr
        WHERE isr.inspect_user_id = #{userId}
        AND isr.process_status = '0' -- 草稿状态
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <!-- 查询查获总数 -->
    <select id="selectAverage" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(isi.quantity), 0) AS total_team_seizure
        FROM item_seizure_record isr
        LEFT JOIN item_seizure_items isi ON isr.id = isi.record_id
        WHERE isr.inspect_user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND isr.process_status=3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <select id="homePageSeizureReport"
            resultType="com.sundot.airport.item.domain.ItemLargeScreenHomePageSeizureReportSqlDto">
        select isr.inspect_user_id userId,
        isr.inspect_user_name userName,
        isi.quantity quantity
        from item_seizure_record isr
        left join item_seizure_items isi on isr.id = isi.record_id
        where 1 = 1
        and isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
    </select>

    <select id="homePageSeizureReportBrigadeId"
            resultType="com.sundot.airport.item.domain.ItemLargeScreenHomePageSeizureReportSqlDto">
        select isr.inspect_user_id userId,
        isr.inspect_user_name userName,
        isi.quantity quantity
        from item_seizure_record isr
        left join item_seizure_items isi on isr.id = isi.record_id
        where 1 = 1
        and isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="inspectBrigadeId != null">
            and isr.inspect_brigade_id = #{inspectBrigadeId}
        </if>
    </select>

    <select id="homePageSeizureReportDepartmentId"
            resultType="com.sundot.airport.item.domain.ItemLargeScreenHomePageSeizureReportSqlDto">
        select isr.inspect_user_id userId,
        isr.inspect_user_name userName,
        isi.quantity quantity
        from item_seizure_record isr
        left join item_seizure_items isi on isr.id = isi.record_id
        where 1 = 1
        and isr.process_status = 3
        <if test="startDate != null and endDate != null">
            AND isr.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="inspectDepartmentId != null">
            and isr.inspect_department_id = #{inspectDepartmentId}
        </if>
    </select>
</mapper>