<?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.weather.modules.sys.dao.SysAlertDao">

    <sql id="activeCondition">
        is_active = 1
        AND (expire_time IS NULL OR expire_time > NOW())
    </sql>

    <select id="selectActiveAlerts" resultType="com.weather.modules.sys.entity.SysAlertEntity">
        SELECT * FROM sys_alert
        WHERE <include refid="activeCondition"/>
        ORDER BY publish_time DESC
    </select>

    <select id="selectActiveAlertsSince" resultType="com.weather.modules.sys.entity.SysAlertEntity">
        SELECT * FROM sys_alert
        WHERE <include refid="activeCondition"/>
        AND id > #{sinceId}
        ORDER BY publish_time DESC
    </select>

    <select id="countBySource" resultType="int">
        SELECT COUNT(1) FROM sys_alert
        WHERE source_type = #{sourceType}
        AND source_id = #{sourceId}
        AND <include refid="activeCondition"/>
    </select>

</mapper>
