<?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.admin.biz.dao.TsaNoticeDao">

	<!-- 공지사항 목록 -->
	<select id="getNoticeList" parameterType="Notice" resultType="Notice">
		/* TsaNotice.getNoticeList */
		SELECT A.NOTICE_SQ                                         /*공지번호*/
		     , A.NOTICE_TYPE                                       /*공지유형*/
		     , A.URGENT_YN                                         /*긴급여부*/
		     , 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.NOTICE_STDT,'%Y%m%d')  AS NOTICE_STDT /*공지시작일자*/
		     , DATE_FORMAT(A.NOTICE_EDDT,'%Y%m%d')  AS NOTICE_EDDT /*공지종료일자*/
		     , A.USE_YN                                            /*사용여부*/
		     , A.READ_CNT                                          /*조회수*/
		     , FN_GET_USER_NM(A.REG_NO)             AS REG_NM      /*등록자*/
		     , DATE_FORMAT(A.REG_DT,'%Y%m%d%H%i%S') AS REG_DT      /*등록일시*/
		     , FN_GET_USER_NM(A.UPD_NO)             AS UPD_NM      /*수정자*/
		     , DATE_FORMAT(A.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT      /*수정일시*/
		FROM   TB_NOTICE A
		WHERE  A.NOTICE_TYPE = #{noticeType}
		<if test="startDt != null and startDt !=''">
		AND    A.REG_DT <![CDATA[>=]]> STR_TO_DATE(#{startDt},'%Y-%m-%d')
		</if>
		<if test="startDt != null and startDt !='' and endDt != null and endDt != ''">
		AND    A.REG_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{endDt},'%Y-%m-%d'),INTERVAL 1 DAY)
		</if>
		<if test='useYn != null and useYn !=""'>
		AND    A.USE_YN = #{useYn}
		</if>
		<if test="noticeTitle != null and noticeTitle !=''">
		AND    LOWER(A.NOTICE_TITLE) LIKE CONCAT('%',LOWER(#{noticeTitle}),'%')
		</if>
		<if test='receiverId != null and receiverId !=""'>
		AND    A.NOTICE_SQ IN (SELECT NOTICE_SQ
		                       FROM   TB_NOTICE_RECEIVER
		                       WHERE  RECEIVER_ID = #{receiverId}
		                      )
		</if>
		ORDER  BY A.URGENT_YN DESC, A.NOTICE_SQ DESC
	</select>

	<!-- 공지사항 수신자 목록 -->
	<select id="getNoticeReceiverList" parameterType="Integer" resultType="Notice">
		/* TsaNotice.getNoticeReceiverList */
		SELECT RECEIVER_ID
		FROM   TB_NOTICE_RECEIVER
		WHERE  NOTICE_SQ = #{noticeSq}
	</select>

	<!-- 공지사항 파일 목록 -->
	<select id="getNoticeFileList" parameterType="Integer" resultType="Notice">
		/* TsaNotice.getNoticeFileList */
		SELECT NOTICE_SQ
		     , SEQ
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		FROM   TB_NOTICE_FILE
		WHERE  NOTICE_SQ = #{noticeSq}
	</select>

	<!-- 공지사항 저장 -->
	<insert id="createNotice" parameterType="Notice" keyProperty="noticeSq">
		/* TsaNotice.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">
		/* TsaNotice.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>

	<!-- 공지사항 수신자 삭제 -->
	<delete id="deleteNoticeReceiver" parameterType="Notice">
		/* TsaNotice.deleteNoticeReceiver */
		DELETE FROM TB_NOTICE_RECEIVER
		WHERE  NOTICE_SQ = #{noticeSq}
	</delete>

	<!--공지사항 수신자 등록 -->
	<insert id="createNoticeReceiver" parameterType="Notice">
		/* TsaNotice.createNoticeReceiver */
		INSERT INTO TB_NOTICE_RECEIVER (
		       NOTICE_SQ
		     , RECEIVER_ID
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       #{noticeSq}
		     , #{receiverId}
		     , #{regNo}
		     , NOW()
		)
	</insert>

	<!-- 공지사항 파일 삭제 -->
	<delete id="deleteNoticeFile" parameterType="Notice">
		/* TsaNotice.deleteNoticeFile */
		DELETE FROM TB_NOTICE_FILE
		WHERE  NOTICE_SQ = #{noticeSq}
		<if test="seq != null and seq !=''">
		AND    SEQ = #{seq}
		</if>
	</delete>

	<!-- 공지사항 파일 저장 -->
	<insert id="createNoitceFlie" parameterType="Notice">
		/* TsaNotice.createNoitceFlie */
		INSERT INTO TB_NOTICE_FILE (
		       NOTICE_SQ
		     , SEQ
		     , ORG_FILE_NM
		     , SYS_FILE_NM
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       #{noticeSq}
		     , IFNULL((SELECT MAX(SEQ)
		               FROM   TB_NOTICE_FILE
		               WHERE  NOTICE_SQ = #{noticeSq}
		              ),0) + 1
		     , #{orgFileNm}
		     , #{sysFileNm}
		     , #{regNo}
		     , NOW()
		)
	</insert>

</mapper>