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

  <!-- 공지사항 목록 조회 -->
  <select id="getSystemNoticeList" parameterType="map" resultType="map">
    SELECT
      ID,
      COMPANY_CODE,
      TITLE,
      CONTENT,
      IS_ACTIVE,
      CREATED_BY,
      CREATED_DATE,
      UPDATED_DATE
    FROM SYSTEM_NOTICE
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="is_active != null">
      AND IS_ACTIVE = #{is_active}
    </if>
    ORDER BY CREATED_DATE DESC
    <include refid="common.pagination"/>
  </select>

  <!-- 공지사항 카운트 -->
  <select id="getSystemNoticeListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM SYSTEM_NOTICE
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="is_active != null">
      AND IS_ACTIVE = #{is_active}
    </if>
  </select>

  <!-- 공지사항 단건 조회 -->
  <select id="getSystemNoticeInfo" parameterType="map" resultType="map">
    SELECT
      ID,
      COMPANY_CODE,
      TITLE,
      CONTENT,
      IS_ACTIVE,
      CREATED_BY,
      CREATED_DATE,
      UPDATED_DATE
    FROM SYSTEM_NOTICE
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <!-- 공지사항 등록 -->
  <insert id="insertSystemNotice" parameterType="map" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO SYSTEM_NOTICE (COMPANY_CODE, TITLE, CONTENT, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_DATE)
    VALUES (#{company_code}, #{title}, #{content},
            COALESCE(#{is_active}, TRUE),
            #{created_by}, NOW(), NOW())
  </insert>

  <!-- 공지사항 수정 -->
  <update id="updateSystemNotice" parameterType="map">
    UPDATE SYSTEM_NOTICE
    <set>
      <if test="title != null">
        TITLE = #{title},
      </if>
      <if test="content != null">
        CONTENT = #{content},
      </if>
      <if test="is_active != null">
        IS_ACTIVE = #{is_active},
      </if>
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <!-- 공지사항 삭제 -->
  <delete id="deleteSystemNotice" parameterType="map">
    DELETE FROM SYSTEM_NOTICE
    WHERE ID = #{id}
    <include refid="common.companyCodeFilter"/>
  </delete>

</mapper>
