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

	<select id="selectTotalPushCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)    AS "totalCount"
		  FROM MU_PUSH AS MP
		 WHERE MP.USE_YN = 'Y'
		   AND MP.STATE = 'Y'
		   AND MP.MU_USER_ID = #{muUserId}
		   AND MP.DEVICE_UUID = #{deviceUuid}
		   AND MP.SEND_DATE IS NOT NULL
	</select>

	<select id="selectListPush" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PUSH_ID                           AS "muPushId"
			  ,IFNULL(MP.MU_APP_EVENT_ID, '')          AS "muAppEventId"
			  ,MP.MU_DEVICE_ID                         AS "muDeviceId"
			  ,MP.MU_USER_ID                           AS "muUserId"
			  ,MP.TITLE                                AS "title"
			  ,MP.CONTENT                              AS "content"
			  ,MP.USER_CHECK_YN                        AS "userCheckYn"
			  ,IFNULL(MP.LINK_URL, '')                 AS "linkUrl"
			  ,DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d')   AS "sendDate"
			  ,DATE_FORMAT(MP.SEND_DATE, '%H:%i')      AS "sendTime"
		  FROM MU_PUSH AS MP
		 WHERE MP.USE_YN = 'Y'
		   AND MP.STATE = 'Y'
		   AND MP.MU_USER_ID = #{muUserId}
		   AND MP.DEVICE_UUID = #{deviceUuid}
		   AND MP.SEND_DATE IS NOT NULL
	  ORDER BY MP.SEND_DATE DESC
		 LIMIT 0, 30
	</select>

	<select id="selectPush" parameterType="hashmap" resultType="hashmap">
		SELECT MP.MU_PUSH_ID                           AS "muPushId"
			  ,IFNULL(MP.MU_APP_EVENT_ID, '')          AS "muAppEventId"
			  ,MP.MU_DEVICE_ID                         AS "muDeviceId"
			  ,MP.MU_USER_ID                           AS "muUserId"
			  ,MP.TITLE                                AS "title"
			  ,MP.CONTENT                              AS "content"
			  ,MP.USER_CHECK_YN                        AS "userCheckYn"
			  ,IFNULL(MP.LINK_URL, '')                 AS "linkUrl"
			  ,DATE_FORMAT(MP.SEND_DATE, '%Y-%m-%d')   AS "sendDate"
			  ,DATE_FORMAT(MP.SEND_DATE, '%H:%I')      AS "sendTime"
		  FROM MU_PUSH AS MP
		 WHERE MP.USE_YN        = 'Y'
		   AND MP.MU_PUSH_ID  = #{muPushId}
		 LIMIT 0, 1
	</select>

	<select id="selectPushCountCheck" parameterType="hashmap" resultType="hashmap">
		SELECT IF(TOTAL_COUNT > CHECK_COUNT, 'Y', 'N')  AS "checkYn"
		  FROM (
		      	SELECT COUNT(*) AS TOTAL_COUNT
					  ,COUNT(CASE WHEN MP.USER_CHECK_YN = 'Y' THEN 1 END) AS CHECK_COUNT
				  FROM MU_PUSH AS MP
				 WHERE MP.USE_YN      = 'Y'
				   AND MP.MU_USER_ID  = #{muUserId}
				   AND MP.DEVICE_UUID = #{deviceUuid}
				   AND MP.SEND_DATE IS NOT NULL
			  ORDER BY MP.SEND_DATE DESC
				 LIMIT 0, 30
		  ) AS MP
	</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="updatePushUserCheckYn" parameterType="hashmap">
		UPDATE MU_PUSH
	  	   SET MOD_ID        = #{modId}
		      ,MOD_DATE      = NOW()
		      ,CUD_FLAG      = 'U'
		      ,USER_CHECK_YN = #{userCheckYn}
		 WHERE USE_YN	     = 'Y'
		   AND MU_PUSH_ID    = #{muPushId}
	</update>

</mapper>