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

    <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>
