<?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.scm.biz.dao.TssNoticeDao">

	<!-- 공지사항 저장 -->
	<insert id="createNotice" parameterType="Notice" keyProperty="noticeSq">
		/* TssNotice.createNotice */
		INSERT INTO TB_NOTICE (
		       NOTICE_SQ
		     , NOTICE_TYPE
		     , NOTICE_TITLE
		     , NOTICE_STDT
		     , NOTICE_EDDT
		     , NOTICE_CONTENT
		     , URGENT_YN
		     , USE_YN
		     , READ_CNT
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       NULL
		     , #{noticeType}
		     , #{noticeTitle}
		     , STR_TO_DATE(#{noticeStdt},'%Y-%m-%d')
		     , STR_TO_DATE(#{noticeEddt},'%Y-%m-%d')
		     , #{noticeContent}
		     , IFNULL(#{urgentYn},'N')
		     , #{useYn}
		     , IFNULL(#{readCnt},0)
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
	</insert>

	<!-- 공지사항 수정 -->
	<update id="updateNotice" parameterType="Notice">
		/* TssNotice.updateNotice */
		UPDATE TB_NOTICE
		SET    NOTICE_TITLE = #{noticeTitle}
		     , NOTICE_CONTENT = #{noticeContent}
		     , NOTICE_STDT = STR_TO_DATE(#{noticeStdt},'%Y-%m-%d')
		     , NOTICE_EDDT = STR_TO_DATE(#{noticeEddt},'%Y-%m-%d')
		     , URGENT_YN = IFNULL(#{urgentYn},'N')
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  NOTICE_SQ = #{noticeSq}
	</update>
	
	<!-- 공지사항 팝업 총건수 -->
	<select id="getNoticePopupTotalCount" parameterType="String" resultType="int">
		/* TssNotice.getNoticePopupTotalCount */
		SELECT COUNT(*) AS CNT
		FROM   TB_NOTICE A
		WHERE  A.NOTICE_TYPE = 'G047_20' /*내부공지*/
		AND    A.NOTICE_SQ IN (SELECT NOTICE_SQ
		                       FROM   TB_NOTICE_RECEIVER
		                       WHERE  1 = 1
		                       <choose>
		                           <when test="roleCd == 'G001_B000'">
		                       AND    RECEIVER_ID = 'G048_60' /*입점업체*/
		                           </when>
		                           <when test="roleCd == 'G001_C000'">
		                       AND    RECEIVER_ID = 'G048_40' /*외부몰벤더*/
		                           </when>
		                           <when test="roleCd == 'G001_D000'">
		                       AND    RECEIVER_ID = 'G048_50' /*제휴사이트*/
		                           </when>
		                       </choose>
		                      )
		AND    A.USE_YN = 'Y'
		AND    A.POPUP_YN = 'Y' /*팝업*/
		AND    CURRENT_DATE() BETWEEN A.POPUP_DISP_STDT AND A.POPUP_DISP_EDDT
	</select>
	
	<!-- 공지사항 팝업 목록 -->
	<select id="getNoticePopupList" parameterType="String" resultType="Notice">
		/* TssNotice.getNoticePopupList */
		SELECT A.NOTICE_SQ                                           /*공지번호*/
		     , A.NOTICE_TITLE                                        /*공지제목*/
		     , A.NOTICE_CONTENT                                      /*공지내용*/
		     , IFNULL((SELECT COUNT(*)
		               FROM   TB_NOTICE_FILE
		               WHERE  NOTICE_SQ = A.NOTICE_SQ
		              ),0)                               AS FILE_CNT /*파일건수*/
		     , DATE_FORMAT(A.REG_DT,'%Y-%m-%d %H:%i:%S') AS REG_DT   /*등록일시*/
		FROM   TB_NOTICE A
		WHERE  A.NOTICE_TYPE = 'G047_20' /*내부공지*/
		AND    A.NOTICE_SQ IN (SELECT NOTICE_SQ
		                       FROM   TB_NOTICE_RECEIVER
		                       WHERE  1 = 1
		                       <choose>
		                           <when test="roleCd == 'G001_B000'">
		                       AND    RECEIVER_ID = 'G048_60' /*입점업체*/
		                           </when>
		                           <when test="roleCd == 'G001_C000'">
		                       AND    RECEIVER_ID = 'G048_40' /*외부몰벤더*/
		                           </when>
		                           <when test="roleCd == 'G001_D000'">
		                       AND    RECEIVER_ID = 'G048_50' /*제휴사이트*/
		                           </when>
		                       </choose>
		                      )
		AND    A.USE_YN = 'Y'
		AND    A.POPUP_YN = 'Y' /*팝업*/
		AND    CURRENT_DATE() BETWEEN A.POPUP_DISP_STDT AND A.POPUP_DISP_EDDT
		ORDER  BY A.URGENT_YN DESC, A.NOTICE_SQ DESC
	</select>
	
	<!-- 공지사항 팝업 파일 목록 -->
	<select id="getNoticePopupFileList" parameterType="Integer" resultType="NoticeFileList">
		/* TsaNotice.getNoticePopupFileList */
		SELECT NOTICE_SQ
		     , SEQ
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		FROM   TB_NOTICE_FILE
		WHERE  NOTICE_SQ = #{noticeSq}
		ORDER  BY SEQ
	</select>

</mapper>