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

	<select id="selectTotalNoticeCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_NOTICE AS MN
		 WHERE MN.USE_YN = 'Y'
		   <if test="noticeSearchStartDate != null and noticeSearchStartDate != '' and noticeSearchEndDate != null and noticeSearchEndDate != ''">
		   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d') &gt;= #{noticeSearchStartDate}
		   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d') &lt;= #{noticeSearchEndDate}
		   </if>
		   <if test="noticeSearchKeywordParam != null and noticeSearchKeywordParam != ''">
		   AND ((MN.TITLE LIKE CONCAT('%',TRIM(#{noticeSearchKeywordParam}),'%'))
			OR (MN.CONTENT LIKE CONCAT('%',TRIM(#{noticeSearchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListNotice" parameterType="hashmap" resultType="hashmap">
		SELECT MN.*
		  FROM (
					SELECT MN.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MN.MU_NOTICE_ID                        AS "muNoticeId"
									  ,MN.MU_MEMBER_ID                        AS "muMemberId"
									  ,IFNULL((SELECT MM.NAME
												 FROM MU_MEMBER AS MM
												WHERE MM.USE_YN       = 'Y'
												  AND MM.MU_MEMBER_ID = MN.MU_MEMBER_ID
												LIMIT 0, 1
									   ),'')                                  AS "writeName"
									  ,MN.TITLE                               AS "title"
									  ,MN.CONTENT                             AS "content"
									  ,DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d')  AS "openDate"
									  ,DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
								  FROM MU_NOTICE MN
								 WHERE MN.USE_YN = 'Y'
								   <if test="noticeSearchStartDate != null and noticeSearchStartDate != '' and noticeSearchEndDate != null and noticeSearchEndDate != ''">
								   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d') &gt;= #{noticeSearchStartDate}
								   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d') &lt;= #{noticeSearchEndDate}
								   </if>
								   <if test="noticeSearchKeywordParam != null and noticeSearchKeywordParam != ''">
								   AND ((MN.TITLE LIKE CONCAT('%',TRIM(#{noticeSearchKeywordParam}),'%'))
									OR (MN.CONTENT LIKE CONCAT('%',TRIM(#{noticeSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="noticeSort != null and noticeSort != ''">
							  ORDER BY ${noticeSort}
								 </when>
								 <otherwise>
							  ORDER BY MN.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MN, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MN
		 WHERE 1 = 1
		 LIMIT ${noticeStart}, ${noticeLimit}
	</select>

	<select id="selectNotice" parameterType="hashmap" resultType="hashmap">
		SELECT MN.MU_NOTICE_ID                        AS "muNoticeId"
			  ,MN.MU_MEMBER_ID                        AS "muMemberId"
			  ,MN.TITLE                               AS "title"
			  ,MN.CONTENT                             AS "content"
			  ,DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d')  AS "openDate"
			  ,DATE_FORMAT(MN.OPEN_DATE, '%H:%i')     AS "openTime"
			  ,DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_NOTICE AS MN
		 WHERE MN.USE_YN        = 'Y'
		   AND MN.MU_NOTICE_ID  = #{muNoticeId}
		 LIMIT 0, 1
	</select>

	<select id="selectListTodayNotice" parameterType="hashmap" resultType="hashmap">
		SELECT MN.MU_NOTICE_ID                        AS "muNoticeId"
			  ,MN.MU_MEMBER_ID                        AS "muMemberId"
			  ,MN.TITLE                               AS "title"
			  ,MN.CONTENT                             AS "content"
			  ,DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d')  AS "openDate"
			  ,DATE_FORMAT(MN.OPEN_DATE, '%H:%i')     AS "openTime"
			  ,DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_NOTICE AS MN
		 WHERE MN.USE_YN        = 'Y'
		   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d')           = #{searchDate}
		   AND DATE_FORMAT(MN.OPEN_DATE, '%Y-%m-%d %H:%i') &lt;= DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i')
	</select>

	<insert id="insertNotice" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muNoticeId},LPAD((SELECT NEXTVAL(MU_NOTICE_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_NOTICE(
					 MU_NOTICE_ID
					,MU_MEMBER_ID
					,TITLE
					,CONTENT
					,OPEN_DATE
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muMemberId}
					,#{title}
					,#{content}
					,#{openDate}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateNotice" parameterType="hashmap">
		UPDATE MU_NOTICE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,TITLE               = #{title}
			  ,CONTENT             = #{content}
			  ,OPEN_DATE           = #{openDate}
		 WHERE USE_YN        = 'Y'
		   AND MU_NOTICE_ID  = #{muNoticeId}
	</update>
	
	<update id="deleteNotice" parameterType="hashmap">
		UPDATE MU_NOTICE
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN        = 'Y'
		   AND MU_NOTICE_ID  = #{muNoticeId}
	</update>
</mapper>