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

  <!-- ========== 공통 검색 조건 ========== -->
  <sql id="riskAlertSearchCondition">
    <include refid="common.companyCodeFilter"/>
    <if test="keyword != null and keyword != ''">
      AND (TITLE ILIKE '%' || #{keyword} || '%'
           OR DESCRIPTION ILIKE '%' || #{keyword} || '%')
    </if>
    <if test="severity != null and severity != ''">
      AND SEVERITY = #{severity}
    </if>
    <if test="is_active != null and is_active != ''">
      AND IS_ACTIVE = #{is_active}
    </if>
  </sql>

  <!-- ========== 전체 알림 목록 ========== -->
  <select id="getAllAlerts" parameterType="map" resultType="map">
    SELECT
        ID
        , COMPANY_CODE
        , ALERT_TYPE
        , TITLE
        , DESCRIPTION
        , SEVERITY
        , REGION
        , START_DATE
        , END_DATE
        , IS_ACTIVE
        , CREATED_DATE
        , UPDATED_DATE
    FROM RISK_ALERT
    WHERE 1=1
    <include refid="riskAlertSearchCondition"/>
    <include refid="common.dynamicOrderBy"/>
    <include refid="common.pagination"/>
  </select>

  <select id="getAllAlertsCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM RISK_ALERT
    WHERE 1=1
    <include refid="riskAlertSearchCondition"/>
  </select>

  <!-- ========== 타입별 조회 ========== -->
  <select id="getAlertsByType" parameterType="map" resultType="map">
    SELECT
        ID
        , COMPANY_CODE
        , ALERT_TYPE
        , TITLE
        , DESCRIPTION
        , SEVERITY
        , REGION
        , START_DATE
        , END_DATE
        , IS_ACTIVE
        , CREATED_DATE
        , UPDATED_DATE
    FROM RISK_ALERT
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="alert_type != null and alert_type != ''">
      AND ALERT_TYPE = #{alert_type}
    </if>
    <if test="is_active != null and is_active != ''">
      AND IS_ACTIVE = #{is_active}
    </if>
    ORDER BY CREATED_DATE DESC
  </select>

</mapper>
