<?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.style24.front.biz.dao.TsfNoticeDao">

	<!-- Paging -->
	<sql id="pageSql">
		LIMIT #{pageable.startRow}, #{pageable.pageSize}
	</sql>
	
	<!-- 공지사항 전체 건수 조회 -->
	<select id="getNoticeTotalCount" parameterType="Notice" resultType="int">
		/* TsfNotice.getNoticeTotalCount */
		SELECT COUNT(*)
		FROM   TB_NOTICE A
		WHERE  NOTICE_TYPE = #{noticeType}
		AND    USE_YN = 'Y'
		AND    NOW() BETWEEN NOTICE_STDT AND NOTICE_EDDT
		<choose>
		    <when test="urgentYn == null or urgentYn == ''">
		        <if test="searchTxt != null and searchTxt != ''">
		            <choose>
		                <when test="searchGb == 'TITLE'">
		AND    NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                </when>
		                <when test="searchGb == 'CONTENT'">
		AND    NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                </when>
		                <otherwise>
		AND    (
		        NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		        OR
		        NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		       )
		                </otherwise>
		            </choose>
		        </if>
		    </when>
		    <otherwise>
		        <if test='urgentYn == "Y"'> <!-- 최상단 공지글인 경우 -->
		AND    URGENT_YN = 'Y'
		        </if>
		    </otherwise>
		</choose>
		AND    EXISTS (SELECT 1
		               FROM   TB_NOTICE_RECEIVER
		               WHERE  NOTICE_SQ = A.NOTICE_SQ
		               AND    RECEIVER_ID = #{siteCd}
		              )
	</select>
	
	<!-- 공지사항 목록 -->
	<select id="getNoticeList" parameterType="Notice" resultType="Notice">
		/* TsfNotice.getNoticeList */
		SELECT NOTICE_SQ                              /*공지사항일련번호*/
		     , NOTICE_TITLE                           /*공지제목*/
		     , READ_CNT                               /*조회수*/
		     , DATE_FORMAT(REG_DT,'%Y%m%d') AS REG_DT /*등록일자*/
		     , (SELECT COUNT(1)
		        FROM   TB_NOTICE_FILE
		        WHERE  NOTICE_SQ = A.NOTICE_SQ
		       )                          AS FILE_CNT /*첨부파일건수*/
		FROM   TB_NOTICE A
		WHERE  NOTICE_TYPE = #{noticeType}
		AND    USE_YN =  'Y'
		AND    NOW() BETWEEN NOTICE_STDT AND NOTICE_EDDT
		<choose>
		    <when test="urgentYn == null or urgentYn == ''">
		        <if test="searchTxt != null and searchTxt != ''">
		            <choose>
		                <when test="searchGb == 'TITLE'">
		AND    NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                </when>
		                <when test="searchGb == 'CONTENT'">
		AND    NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                </when>
		                <otherwise>
		AND    (
		        NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		        OR
		        NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		       )
		                </otherwise>
		            </choose>
		        </if>
		    </when>
		    <otherwise>
		        <if test='urgentYn == "Y"'> <!-- 최상단 공지글인 경우 -->
		AND    URGENT_YN = 'Y'
		        </if>
		    </otherwise>
		</choose>
		AND    EXISTS (SELECT 1
		               FROM   TB_NOTICE_RECEIVER
		               WHERE  NOTICE_SQ = A.NOTICE_SQ
		               AND    RECEIVER_ID = #{siteCd}
		              )
		ORDER  BY URGENT_YN DESC, REG_DT DESC
		<include refid="pageSql"></include>
	</select>

	<!-- 공지사항 상세 -->
	<select id="getNoticeDetail" parameterType="Notice" resultType="Notice">
		/* TsfNotice.getNoticeDetail */
		SELECT NOTICE_SQ      /*공지사항일련번호*/
		     , NOTICE_TITLE   /*공지사항제목*/
		     , NOTICE_CONTENT /*공지사항내용*/
		     , READ_CNT       /*조회수*/
		     , REG_DT         /*등록일시*/
		FROM   (
		        SELECT NOTICE_SQ
		             , NOTICE_TITLE
		             , NOTICE_CONTENT
		             , READ_CNT
		             , DATE_FORMAT(REG_DT,'%Y.%m.%d') AS REG_DT
		        FROM   TB_NOTICE A
		        WHERE  NOTICE_TYPE = #{noticeType}
		        AND    USE_YN =  'Y'
		        AND    NOW() BETWEEN NOTICE_STDT AND NOTICE_EDDT
		        AND    NOTICE_SQ = #{noticeSq}
		        <choose>
		            <when test="urgentYn == null or urgentYn == ''">
		                <if test="searchTxt != null and searchTxt != ''">
		                    <choose>
		                        <when test="searchGb == 'TITLE'">
		        AND    NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                        </when>
		                        <when test="searchGb == 'CONTENT'">
		        AND    NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                        </when>
		                        <otherwise>
		        AND    (
		                NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                OR
		                NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		               )
		                        </otherwise>
		                    </choose>
		                </if>
		            </when>
		            <otherwise>
		                <if test='urgentYn == "Y"'> <!-- 최상단 공지글인 경우 -->
		        AND    URGENT_YN = 'Y'
		                </if>
		            </otherwise>
		        </choose>
		        AND    EXISTS (SELECT 1
		                       FROM   TB_NOTICE_RECEIVER
		                       WHERE  NOTICE_SQ = A.NOTICE_SQ
		                       AND    RECEIVER_ID = #{siteCd}
		                      )
		       )
	</select>
	
	<!-- 공지사항 이전글 조회 -->
	<select id="getPreviousNotice" parameterType="Notice" resultType="Notice">
		/* TsfNotice.getPreviousNotice */
		SELECT NOTICE_SQ    AS PREV_NOTICE_SQ
		     , NOTICE_TITLE AS PREV_NOTICE_TITLE
		FROM   TB_NOTICE
		WHERE  NOTICE_SQ = (SELECT MAX(NOTICE_SQ)
		                    FROM   TB_NOTICE N
		                    WHERE  NOTICE_SQ <![CDATA[<]]> #{noticeSq}
		                    AND    NOTICE_TYPE = #{noticeType}
		                    AND    USE_YN = 'Y'
		                    AND    NOW() BETWEEN NOTICE_STDT AND NOTICE_EDDT
		                    <choose>
		                        <when test="urgentYn == null or urgentYn == ''">
		                            <if test="searchTxt != null and searchTxt != ''">
		                                <choose>
		                                    <when test="searchGb == 'TITLE'">
		                    AND    NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                                    </when>
		                                    <when test="searchGb == 'CONTENT'">
		                    AND    NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                                    </when>
		                                    <otherwise>
		                    AND    (
		                            NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                            OR
		                            NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                           )
		                                    </otherwise>
		                                </choose>
		                            </if>
		                        </when>
		                        <otherwise>
		                            <if test='urgentYn == "Y"'> <!-- 최상단 공지글인 경우 -->
		                    AND    URGENT_YN = 'Y'
		                            </if>
		                        </otherwise>
		                    </choose>
		                    AND    EXISTS (SELECT 1
		                                   FROM   TB_NOTICE_RECEIVER
		                                   WHERE  NOTICE_SQ = N.NOTICE_SQ
		                                   AND    RECEIVER_ID = #{siteCd}
		                                  )
		                   )
	</select>

	<!-- 공지사항 다음글 조회 -->
	<select id="getNextNotice" parameterType="Notice" resultType="Notice">
		/* TsfNotice.getNextNotice */
		SELECT NOTICE_SQ    AS NEXT_NOTICE_SQ
		     , NOTICE_TITLE AS NEXT_NOTICE_TITLE
		FROM   TB_NOTICE
		WHERE  NOTICE_SQ = (SELECT MIN(NOTICE_SQ)
		                    FROM   TB_NOTICE N
		                    WHERE  NOTICE_SQ <![CDATA[>]]> #{noticeSq}
		                    AND    NOTICE_TYPE = #{noticeType}
		                    AND    USE_YN = 'Y'
		                    AND    NOW() BETWEEN NOTICE_STDT AND NOTICE_EDDT
		                    <choose>
		                        <when test="urgentYn == null or urgentYn == ''">
		                            <if test="searchTxt != null and searchTxt != ''">
		                                <choose>
		                                    <when test="searchGb == 'TITLE'">
		                    AND    NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                                    </when>
		                                    <when test="searchGb == 'CONTENT'">
		                    AND    NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                                    </when>
		                                    <otherwise>
		                    AND    (
		                            NOTICE_TITLE LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                            OR
		                            NOTICE_CONTENT LIKE CONCAT('%',TRIM(#{searchTxt}),'%')
		                           )
		                                    </otherwise>
		                                </choose>
		                            </if>
		                        </when>
		                        <otherwise>
		                            <if test='urgentYn == "Y"'> <!-- 최상단 공지글인 경우 -->
		                    AND    URGENT_YN = 'Y'
		                            </if>
		                        </otherwise>
		                    </choose>
		                    AND    EXISTS (SELECT 1
		                                   FROM   TB_NOTICE_RECEIVER
		                                   WHERE  NOTICE_SQ = N.NOTICE_SQ
		                                   AND    RECEIVER_ID = #{siteCd}
		                                  )
		                   )
	</select>

	<!-- 공지사항 파일 목록 -->
	<select id="getNoticeFileList" parameterType="Integer" resultType="Notice">
		/* TsfNotice.getNoticeFileList */
		SELECT NOTICE_SQ
		     , SEQ
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		FROM   TB_NOTICE_FILE
		WHERE  NOTICE_SQ = #{noticeSq}
	</select>
	
	<!-- 공지사항  조회건수 Update -->
	<update id="updateNoticeReadCount" parameterType="Notice">
		/* TsfNotice.updateNoticeReadCount */
		UPDATE TB_NOTICE
		SET    READ_CNT = READ_CNT + 1
		WHERE  NOTICE_SQ = #{noticeSq}
	</update>

</mapper>