<?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.zksy.base.warning.mapper.EarlyWarningMapper">

    <resultMap id="BaseResultMap" type="com.zksy.base.warning.domain.EarlyWarning">
        <id column="warning_id" property="warningId"/>
        <result column="warning_no" property="warningNo"/>
        <result column="warning_name" property="warningName"/>
        <result column="warning_type" property="warningType"/>
        <result column="warning_level" property="warningLevel"/>
        <result column="warning_special" property="warningSpecial"/>
        <result column="location" property="location"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="ownership_unit" property="ownershipUnit"/>
        <result column="publisher" property="publisher"/>
        <result column="publish_time" property="publishTime"/>
        <result column="handler" property="handler"/>
        <result column="status" property="status"/>
        <result column="warning_content" property="warningContent"/>
        <result column="process_instance_id" property="processInstanceId"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <sql id="Base_Column_List">
        warning_id, warning_no, warning_name, warning_type, warning_level, warning_special,
        location, longitude, latitude, ownership_unit, publisher, publish_time, handler,
        status, warning_content, process_instance_id, create_by, create_time, update_by,
        update_time, remark
    </sql>

    <!-- 统计今日预警核心指标 -->
    <select id="selectTodayWarningStats" resultType="java.util.Map">
        SELECT
        COUNT(CASE WHEN status != 'DRAFT' THEN 1 END) AS today_total,
        COUNT(CASE WHEN status = 'HANDLED' THEN 1 END) AS handled_today,
        COUNT(CASE WHEN status = 'CLOSED' THEN 1 END) AS closed_today
        FROM early_warning
        WHERE publish_time &gt;= CURRENT_DATE
        AND publish_time &lt; CURRENT_DATE + INTERVAL '1 day'
    </select>

    <!-- 查询今日预警列表 -->
    <select id="selectTodayWarningList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM early_warning
        WHERE publish_time &gt;= CURRENT_DATE
        AND publish_time &lt; CURRENT_DATE + INTERVAL '1 day'
        <choose>
            <when test="status != null and status != ''">
                AND status = #{status}
            </when>
            <otherwise>
                AND status != 'DRAFT'
            </otherwise>
        </choose>
        ORDER BY publish_time DESC
    </select>

    <!-- 按日统计预警发展趋势 -->
    <select id="selectWarningTrendByDay" resultType="java.util.Map">
        SELECT
        TO_CHAR(publish_time, 'YYYY-MM-DD') AS stat_date,
        COALESCE(warning_special, '其他') AS warning_special,
        COUNT(*) AS warning_count
        FROM early_warning
        WHERE status != 'DRAFT'
        <if test="startTime != null">
            AND publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND warning_special = #{warningSpecial}
        </if>
        GROUP BY stat_date, warning_special
        ORDER BY stat_date ASC, warning_special ASC
    </select>

    <!-- 按月统计预警发展趋势 -->
    <select id="selectWarningTrendByMonth" resultType="java.util.Map">
        SELECT
        TO_CHAR(publish_time, 'YYYY-MM') AS stat_month,
        COALESCE(warning_special, '其他') AS warning_special,
        COUNT(*) AS warning_count
        FROM early_warning
        WHERE status != 'DRAFT'
        <if test="startTime != null">
            AND publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND warning_special = #{warningSpecial}
        </if>
        GROUP BY stat_month, warning_special
        ORDER BY stat_month ASC, warning_special ASC
    </select>

    <!-- 处置状态分析：按预警状态分组统计数量 -->
    <select id="selectWarningGroupByStatus" resultType="java.util.Map">
        SELECT
        status,
        COUNT(warning_id) AS status_count
        FROM early_warning
        WHERE status != 'DRAFT'
        <if test="startTime != null">
            AND publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND warning_special = #{warningSpecial}
        </if>
        GROUP BY status
        ORDER BY status_count DESC
    </select>

    <!-- 处置时效分析-概览指标统计 -->
    <select id="selectDisposeEfficiencyOverview" resultType="java.util.Map">
        SELECT
        ROUND(AVG(dispose_hours), 2) AS avg_hours,
        ROUND(MAX(dispose_hours), 2) AS max_hours,
        ROUND(MIN(dispose_hours), 2) AS min_hours,
        COUNT(*) AS total_count
        FROM (
        SELECT
        EXTRACT(EPOCH FROM (MAX(d.create_time) - w.publish_time)) / 3600 AS dispose_hours
        FROM early_warning w
        LEFT JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status IN ('HANDLED', 'CLOSED')
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        GROUP BY w.warning_id
        HAVING dispose_hours IS NOT NULL
        ) AS temp
    </select>

    <!-- 处置时效分析-时效区间分布统计 -->
    <select id="selectDisposeEfficiencyRange" resultType="java.util.Map">
        SELECT
        CASE
        WHEN dispose_hours &lt;= 2 THEN '0-2小时'
        WHEN dispose_hours &lt;= 6 THEN '2-6小时'
        WHEN dispose_hours &lt;= 24 THEN '6-24小时'
        ELSE '24小时以上'
        END AS time_range,
        COUNT(*) AS warning_count
        FROM (
        SELECT
        EXTRACT(EPOCH FROM (MAX(d.create_time) - w.publish_time)) / 3600 AS dispose_hours
        FROM early_warning w
        LEFT JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status IN ('HANDLED', 'CLOSED')
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        GROUP BY w.warning_id
        HAVING dispose_hours IS NOT NULL
        ) AS temp
        GROUP BY time_range
        ORDER BY
        CASE time_range
        WHEN '0-2小时' THEN 1
        WHEN '2-6小时' THEN 2
        WHEN '6-24小时' THEN 3
        ELSE 4
        END ASC
    </select>

    <!-- 处置时效分析-按预警专项统计平均时效 -->
    <select id="selectDisposeEfficiencyBySpecial" resultType="java.util.Map">
        SELECT
        COALESCE(warning_special, '其他') AS warning_special,
        COUNT(*) AS warning_count,
        ROUND(AVG(dispose_hours), 2) AS avg_dispose_hours
        FROM (
        SELECT
        w.warning_special,
        EXTRACT(EPOCH FROM (MAX(d.create_time) - w.publish_time)) / 3600 AS dispose_hours
        FROM early_warning w
        LEFT JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status IN ('HANDLED', 'CLOSED')
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        GROUP BY w.warning_id
        HAVING dispose_hours IS NOT NULL
        ) AS temp
        GROUP BY warning_special
        ORDER BY avg_dispose_hours DESC
    </select>

    <!-- 处置时效分析-明细列表 -->
    <select id="selectDisposeEfficiencyList" resultType="java.util.Map">
        SELECT
        w.warning_id,
        w.warning_no,
        w.warning_name,
        COALESCE(w.warning_special, '其他') AS warning_special,
        w.publish_time,
        MAX(d.create_time) AS dispose_time,
        ROUND(EXTRACT(EPOCH FROM (MAX(d.create_time) - w.publish_time)) / 3600, 2) AS dispose_hours,
        w.status
        FROM early_warning w
        LEFT JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status IN ('HANDLED', 'CLOSED')
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        GROUP BY w.warning_id
        HAVING dispose_hours IS NOT NULL
        ORDER BY dispose_hours
        <if test="sortType != null and sortType == 'desc'">
            DESC
        </if>
        <if test="sortType == null or sortType == 'asc'">
            ASC
        </if>
    </select>

    <!-- 接警效率分析-概览指标统计 -->
    <select id="selectReceiveEfficiencyOverview" resultType="java.util.Map">
        SELECT
        ROUND(AVG(receive_hours), 2) AS avg_hours,
        ROUND(MAX(receive_hours), 2) AS max_hours,
        ROUND(MIN(receive_hours), 2) AS min_hours,
        COUNT(*) AS total_count
        FROM (
        SELECT
        EXTRACT(EPOCH FROM (MIN(d.create_time) - w.publish_time)) / 3600 AS receive_hours
        FROM early_warning w
        INNER JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status != 'DRAFT'
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningType != null and warningType != ''">
            AND w.warning_type = #{warningType}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        <if test="status != null and status != ''">
            AND w.status = #{status}
        </if>
        GROUP BY w.warning_id
        HAVING receive_hours IS NOT NULL
        ) AS temp
    </select>

    <!-- 接警效率分析-时效区间分布统计 -->
    <select id="selectReceiveEfficiencyRange" resultType="java.util.Map">
        SELECT
        CASE
        WHEN receive_hours &lt;= 0.5 THEN '0-30分钟'
        WHEN receive_hours &lt;= 2 THEN '30分钟-2小时'
        WHEN receive_hours &lt;= 6 THEN '2-6小时'
        ELSE '6小时以上'
        END AS time_range,
        COUNT(*) AS warning_count
        FROM (
        SELECT
        EXTRACT(EPOCH FROM (MIN(d.create_time) - w.publish_time)) / 3600 AS receive_hours
        FROM early_warning w
        INNER JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status != 'DRAFT'
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningType != null and warningType != ''">
            AND w.warning_type = #{warningType}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        <if test="status != null and status != ''">
            AND w.status = #{status}
        </if>
        GROUP BY w.warning_id
        HAVING receive_hours IS NOT NULL
        ) AS temp
        GROUP BY time_range
        ORDER BY
        CASE time_range
        WHEN '0-30分钟' THEN 1
        WHEN '30分钟-2小时' THEN 2
        WHEN '2-6小时' THEN 3
        ELSE 4
        END ASC
    </select>

    <!-- 接警效率分析-按预警类型统计平均时效 -->
    <select id="selectReceiveEfficiencyByType" resultType="java.util.Map">
        SELECT
        COALESCE(warning_type, '其他') AS warning_type,
        COUNT(*) AS warning_count,
        ROUND(AVG(receive_hours), 2) AS avg_receive_hours
        FROM (
        SELECT
        w.warning_type,
        EXTRACT(EPOCH FROM (MIN(d.create_time) - w.publish_time)) / 3600 AS receive_hours
        FROM early_warning w
        INNER JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status != 'DRAFT'
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        GROUP BY w.warning_id
        HAVING receive_hours IS NOT NULL
        ) AS temp
        GROUP BY warning_type
        ORDER BY avg_receive_hours DESC
    </select>

    <!-- 接警效率分析-明细列表 -->
    <select id="selectReceiveEfficiencyList" resultType="java.util.Map">
        SELECT
        w.warning_id,
        w.warning_no,
        w.warning_name,
        COALESCE(w.warning_type, '其他') AS warning_type,
        COALESCE(w.warning_special, '其他') AS warning_special,
        w.publish_time,
        MIN(d.create_time) AS receive_time,
        ROUND(EXTRACT(EPOCH FROM (MIN(d.create_time) - w.publish_time)) / 3600, 2) AS receive_hours,
        w.status
        FROM early_warning w
        INNER JOIN warning_disposal d ON w.warning_id = d.warning_id
        WHERE w.status != 'DRAFT'
        <if test="startTime != null">
            AND w.publish_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND w.publish_time &lt; #{endTime}
        </if>
        <if test="warningType != null and warningType != ''">
            AND w.warning_type = #{warningType}
        </if>
        <if test="warningSpecial != null and warningSpecial != ''">
            AND w.warning_special = #{warningSpecial}
        </if>
        <if test="status != null and status != ''">
            AND w.status = #{status}
        </if>
        GROUP BY w.warning_id
        HAVING receive_hours IS NOT NULL
        ORDER BY receive_hours
        <if test="sortType != null and sortType == 'desc'">
            DESC
        </if>
        <if test="sortType == null or sortType == 'asc'">
            ASC
        </if>
    </select>

    <!-- ————————————————————————————————————————————————————————————— -->

    <select id="selectWarningList" resultType="java.util.Map">
        SELECT w.*
        FROM early_warning w
        <where>
            <if test="warningName != null and warningName != ''">
                AND w.warning_name LIKE CONCAT('%', #{warningName}, '%')
            </if>
            <if test="warningType != null and warningType != ''">
                AND w.warning_type = #{warningType}
            </if>
            <if test="warningLevel != null and warningLevel != ''">
                AND w.warning_level = #{warningLevel}
            </if>
            <if test="status != null and status != ''">
                AND w.status = #{status}
            </if>
        </where>
        ORDER BY w.create_time DESC
    </select>

    <select id="searchWarnings" resultType="java.util.Map">
        SELECT w.*
        FROM early_warning w
        <where>
            <if test="warningName != null and warningName != ''">
                AND w.warning_name LIKE CONCAT('%', #{warningName}, '%')
            </if>
            <if test="warningNo != null and warningNo != ''">
                AND w.warning_no LIKE CONCAT('%', #{warningNo}, '%')
            </if>
            <if test="warningType != null and warningType != ''">
                AND w.warning_type = #{warningType}
            </if>
            <if test="warningLevel != null and warningLevel != ''">
                AND w.warning_level = #{warningLevel}
            </if>
            <if test="status != null and status != ''">
                AND w.status = #{status}
            </if>
            <if test="location != null and location != ''">
                AND w.location LIKE CONCAT('%', #{location}, '%')
            </if>
            <if test="publisher != null and publisher != ''">
                AND w.publisher LIKE CONCAT('%', #{publisher}, '%')
            </if>
            <if test="ownershipUnit != null and ownershipUnit != ''">
                AND w.ownership_unit LIKE CONCAT('%', #{ownershipUnit}, '%')
            </if>
            <if test="startTime != null">
                AND w.create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND w.create_time &lt;= #{endTime}
            </if>
        </where>
        ORDER BY w.create_time DESC
    </select>

    <select id="selectTodoList" resultType="java.util.Map">
        SELECT
            t.*,
            w.warning_name,
            w.warning_type,
            w.warning_level,
            w.handler,
            w.publisher,
            w.location,
            w.ownership_unit,
            w.publish_time
        FROM warning_todo t
        LEFT JOIN early_warning w ON t.warning_id = w.warning_id
        <where>
            t.user_id = #{userId}
            <if test="todoType != null and todoType != ''">
                AND t.todo_type = #{todoType}
            </if>
            <if test="warningName != null and warningName != ''">
                AND w.warning_name LIKE CONCAT('%', #{warningName}, '%')
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectProcessingWarnings" resultType="java.util.Map">
        SELECT w.*
        FROM early_warning w
        <where>
            AND w.status IN ('PENDING', 'PROCESSING', 'RELEASED')
            <if test="warningType != null and warningType != ''">
                AND w.warning_type = #{warningType}
            </if>
            <if test="warningLevel != null and warningLevel != ''">
                AND w.warning_level = #{warningLevel}
            </if>
        </where>
        ORDER BY w.publish_time DESC
    </select>

    <select id="selectCompletedWarnings" resultType="java.util.Map">
        SELECT w.*
        FROM early_warning w
        <where>
            AND w.status IN ('HANDLED', 'CLOSED')
            <if test="warningType != null and warningType != ''">
                AND w.warning_type = #{warningType}
            </if>
            <if test="warningLevel != null and warningLevel != ''">
                AND w.warning_level = #{warningLevel}
            </if>
            <if test="startTime != null">
                AND w.update_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND w.update_time &lt;= #{endTime}
            </if>
        </where>
        ORDER BY w.update_time DESC
    </select>

    <select id="selectWarningDetail" resultType="java.util.Map">
        SELECT w.*
        FROM early_warning w
        WHERE w.warning_id = #{warningId}
    </select>

    <select id="selectStatistics" resultType="java.util.Map">
        SELECT
            COUNT(*) AS total,
            SUM(CASE WHEN status = 'DRAFT' THEN 1 ELSE 0 END) AS draftCount,
            SUM(CASE WHEN status = 'PENDING' THEN 1 ELSE 0 END) AS pendingCount,
            SUM(CASE WHEN status = 'PROCESSING' THEN 1 ELSE 0 END) AS processingCount,
            SUM(CASE WHEN status = 'RELEASED' THEN 1 ELSE 0 END) AS releasedCount,
            SUM(CASE WHEN status = 'HANDLED' THEN 1 ELSE 0 END) AS handledCount,
            SUM(CASE WHEN status = 'CLOSED' THEN 1 ELSE 0 END) AS closedCount,
            SUM(CASE WHEN warning_level = '1' THEN 1 ELSE 0 END) AS level1Count,
            SUM(CASE WHEN warning_level = '2' THEN 1 ELSE 0 END) AS level2Count,
            SUM(CASE WHEN warning_level = '3' THEN 1 ELSE 0 END) AS level3Count,
            SUM(CASE WHEN warning_level = '4' THEN 1 ELSE 0 END) AS level4Count
        FROM early_warning
    </select>

</mapper>
