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

	<select id="selectTotalPushCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_PUSH AS MP
	INNER JOIN MU_USER AS MU
			ON MP.MU_USER_ID = MU.MU_USER_ID
		   AND MP.USE_YN = 'Y'
		   AND MU.USE_YN = 'Y'
		   <if test="pushSearchStartDate != null and pushSearchStartDate != '' and pushSearchEndDate != null and pushSearchEndDate != ''">
		   AND DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d') &gt;= #{pushSearchStartDate}
		   AND DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d') &lt;= #{pushSearchEndDate}
		   </if>
		   <if test="pushSearchKeywordParam != null and pushSearchKeywordParam != ''">
		   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{pushSearchKeywordParam}),'%')
		   </if>
	</select>
	
	<select id="selectListPush" parameterType="hashmap" resultType="hashmap">
		SELECT MP.*
		  FROM (
				SELECT MP.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MP.MU_PUSH_ID                                 AS "muPushId"
						      ,MU.USER_NAME                                   AS "userName"
							  ,(SELECT MCI.CATEGORY_ITEM_NAME
								  FROM MU_CATEGORY_ITEM AS MCI
								 WHERE MCI.USE_YN = 'Y'
								   AND MCI.MU_CATEGORY_ITEM_ID = MU.NATIONALITY
								 LIMIT 0, 1)                                  AS "nationality"
							  ,DATE_FORMAT(MU.BIRTHDAY, '%Y-%m-%d')           AS "birthday"
							  ,TIMESTAMPDIFF(YEAR, MU.BIRTHDAY, CURDATE()) - (DATE_FORMAT(CURDATE(), '%m%d') <![CDATA[ < ]]> DATE_FORMAT(MU.BIRTHDAY, '%m%d'))
                                                                              AS "age"
							  ,CASE
							   WHEN MU.GENDER = 'M'
							   THEN '남'
							   WHEN MU.GENDER = 'F'
							   THEN '여'
							   ELSE ''
							   END                                            AS "gender"
							  ,IFNULL((SELECT MCI.FILE_PATH
										 FROM MU_CATEGORY_ITEM AS MCI
										WHERE MCI.USE_YN = 'Y'
										  AND MCI.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
										LIMIT 0, 1), MU.USER_TYPE2)           AS "userType"
						      ,MP.TITLE                                       AS "title"
						      ,MP.CONTENT                                     AS "content"
							  ,CASE
							   WHEN MP.STATE = 'R'
							   THEN '대기'
							   WHEN MP.STATE = 'G'
							   THEN '진행'
							   WHEN MP.STATE = 'Y'
							   THEN '완료'
							   ELSE ''
							   END						                      AS "state"
							  ,IFNULL((SELECT MM.NAME
									     FROM MU_MEMBER AS MM
									    WHERE MM.USE_YN = 'Y'
									      AND MM.MU_MEMBER_ID = MP.REG_ID
									    LIMIT 0, 1), '시스템')                 AS "memberName"
							  ,DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d')          AS "sendDate"
						  FROM MU_PUSH AS MP
					INNER JOIN MU_USER AS MU
							ON MP.MU_USER_ID = MU.MU_USER_ID
						   AND MP.USE_YN = 'Y'
						   AND MU.USE_YN = 'Y'
						   <if test="pushSearchStartDate != null and pushSearchStartDate != '' and pushSearchEndDate != null and pushSearchEndDate != ''">
						   AND DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d') &gt;= #{pushSearchStartDate}
						   AND DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d') &lt;= #{pushSearchEndDate}
						   </if>
						   <if test="pushSearchKeywordParam != null and pushSearchKeywordParam != ''">
						   AND MU.USER_NAME LIKE CONCAT('%',TRIM(#{pushSearchKeywordParam}),'%')
						   </if>
						 <choose>
						 <when test="pushSort != null and pushSort != ''">
					  ORDER BY ${pushSort}
						 </when>
						 <otherwise>
					  ORDER BY MP.SEND_DATE DESC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				  ) MP, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
		  ) MP
		 WHERE 1 = 1
		 LIMIT ${pushStart}, ${pushLimit}
	</select>

	<select id="selectListPushTimeGroup" parameterType="hashmap" resultType="hashmap">
		SELECT MP.TITLE              AS "title"
		      ,MP.CONTENT            AS "content"
			  ,MP.LINK_URL           AS "linkUrl"
			  ,MP.T_ID               AS "tId"
          FROM MU_PUSH AS MP
		 WHERE MP.USE_YN    = 'Y'
	       AND MP.STATE     = #{state}
		   AND MP.PUSH_TYPE = #{pushType}
	  GROUP BY MP.TITLE
		      ,MP.CONTENT
			  ,MP.LINK_URL
			  ,MP.T_ID
	  ORDER BY MP.T_ID ASC
		 LIMIT 0, 1
	</select>

	<select id="selectListPushTimeGroup2" parameterType="hashmap" resultType="hashmap">
		SELECT MP.TITLE              AS "title"
		      ,MP.CONTENT            AS "content"
			  ,MP.LINK_URL           AS "linkUrl"
			  ,MP.T_ID               AS "tId"
          FROM MU_PUSH AS MP
		 WHERE MP.USE_YN    = 'Y'
	       AND MP.STATE     = #{state}
		   AND MP.PUSH_TYPE = #{pushType}
	  GROUP BY MP.TITLE
		      ,MP.CONTENT
			  ,MP.LINK_URL
			  ,MP.T_ID
	  ORDER BY MP.T_ID ASC
		 LIMIT 0, 100
	</select>

	<select id="selectListPushTime" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PUSH_ID         AS "muPushId"
		      ,MP.MU_USER_ID         AS "muUserId"
		      ,MP.MU_DEVICE_ID       AS "muDeviceId"
		      ,MP.DEVICE_UUID        AS "deviceUuid"
		      ,MP.PUSH_KEY           AS "pushKey"
		      ,MP.PUSH_TYPE          AS "pushType"
		      ,MP.PHONE_TYPE         AS "phoneType"
		      ,MP.TITLE              AS "title"
		      ,MP.CONTENT            AS "content"
			  ,MP.LINK_URL           AS "linkUrl"
			  ,MP.T_ID               AS "tId"
          FROM MU_PUSH AS MP
		 WHERE MP.USE_YN    = 'Y'
	       AND MP.STATE     = #{state}
		   AND MP.PUSH_TYPE = #{pushType}
		   AND MP.TITLE     = #{title}
		   AND MP.CONTENT   = #{content}
		   AND MP.LINK_URL  = #{linkUrl}
		   AND MP.T_ID      = #{tId}
	  ORDER BY MP.REG_DATE ASC
		 LIMIT 0, 1000
	</select>

	<select id="selectListPushTime2" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PUSH_ID         AS "muPushId"
		      ,MP.MU_USER_ID         AS "muUserId"
		      ,MP.MU_DEVICE_ID       AS "muDeviceId"
		      ,MP.DEVICE_UUID        AS "deviceUuid"
		      ,MP.PUSH_KEY           AS "pushKey"
		      ,MP.PUSH_TYPE          AS "pushType"
		      ,MP.PHONE_TYPE         AS "phoneType"
		      ,MP.TITLE              AS "title"
		      ,MP.CONTENT            AS "content"
			  ,MP.LINK_URL           AS "linkUrl"
			  ,MP.T_ID               AS "tId"
          FROM MU_PUSH AS MP
		 WHERE MP.USE_YN    = 'Y'
	       AND MP.STATE     = #{state}
		   AND MP.PUSH_TYPE = #{pushType}
		   AND TIMESTAMPDIFF(MINUTE, MP.REG_DATE, NOW()) >= 1
	  ORDER BY MP.REG_DATE ASC
		 LIMIT 0, 1000
	</select>

	<select id="selectPush" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PUSH_ID                        AS "muPushId"
			  ,MP.TITLE                               AS "title"
			  ,MP.CONTENT                             AS "content"
			  ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_PUSH AS MP
		 WHERE MP.USE_YN        = 'Y'
		   AND MP.MU_PUSH_ID  = #{muPushId}
		 LIMIT 0, 1
	</select>

	<insert id="insertPush" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
        	SELECT CONCAT(#{muPushId},LPAD((SELECT NEXTVAL(MU_PUSH_SEQ)), 11, 0))
        </selectKey>
		INSERT INTO MU_PUSH(
					 MU_PUSH_ID
					,MU_APP_EVENT_ID
					,MU_USER_ID
					,USER_NAME
					,MU_DEVICE_ID
					,DEVICE_UUID
					,PUSH_KEY
					,PUSH_TYPE
					,PHONE_TYPE
					,TITLE
					,CONTENT
					,LINK_URL
					,STATE
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muAppEventId, jdbcType=VARCHAR}
					,#{muUserId, jdbcType=VARCHAR}
					,#{userName, jdbcType=VARCHAR}
					,#{muDeviceId, jdbcType=VARCHAR}
		   			,#{deviceUuid, jdbcType=VARCHAR}
		   			,#{pushKey, jdbcType=VARCHAR}
		   			,#{pushType, jdbcType=VARCHAR}
		   			,#{phoneType, jdbcType=VARCHAR}
		   			,#{title, jdbcType=VARCHAR}
					,#{content, jdbcType=VARCHAR}
					,#{linkUrl, jdbcType=VARCHAR}
					,#{state, jdbcType=VARCHAR}
					,NOW()
					,NOW()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>

	<update id="updatePushState" parameterType="hashmap">
		UPDATE MU_PUSH
	  	   SET USE_YN	= 'Y'
		      ,MOD_ID	= #{modId}
		      ,MOD_DATE	= NOW()
		      ,CUD_FLAG	= 'U'
		      ,STATE    = #{chgState, jdbcType=VARCHAR}
		      <choose>
			  <when test="sendDateYn eq 'Y'.toString()">
			  ,SEND_DATE = NOW()
			  </when>
		      <otherwise>
			  </otherwise>
			  </choose>
		WHERE USE_YN	     = 'Y'
		  AND MU_PUSH_ID    = #{muPushId}
		  AND STATE          = #{oldState}
	</update>

</mapper>