<?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="WebUserPushHistory">
	
	<select id="selectTotalUserPushHistoryCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM (
				SELECT MS.MU_SMS_ID                                   AS PUSH_ID
					  ,MS.CONTENT                                     AS CONTENT
					  ,MS.POPBILL_STATE						          AS POPBILL_STATE
					  ,MS.POPBILL_RESULT                              AS POPBILL_RESULT
					  ,MS.REG_ID                                      AS REG_ID
					  ,MS.SEND_DATE                                   AS SEND_DATE
					  ,'SMS'                                          AS TYPE
				  FROM MU_SMS AS MS
				 WHERE MS.USE_YN = 'Y'
				   AND MS.MU_USER_ID = #{muUserId}

			 UNION ALL

				SELECT MK.MU_KAKAO_ID                                 AS PUSH_ID
					  ,(SELECT MKT.TITLE
						  FROM MU_KAKAO_TEMPLATE AS MKT
						 WHERE MKT.USE_YN = 'Y'
						   AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
						 LIMIT 0, 1)                                  AS CONTENT
					  ,MK.POPBILL_STATE						          AS POPBILL_STATE
					  ,MK.POPBILL_RESULT                              AS POPBILL_RESULT
					  ,MK.REG_ID                                      AS REG_ID
					  ,MK.SEND_DATE                                   AS SEND_DATE
					  ,'KAKAO'                                        AS TYPE
				  FROM MU_KAKAO AS MK
				 WHERE MK.USE_YN = 'Y'
				   AND MK.MU_USER_ID = #{muUserId}

			 UNION ALL

				SELECT MUPH.MU_USER_PUSH_HISTORY_ID                   AS PUSH_ID
					  ,MESSAGE                                        AS CONTENT
					  ,CASE
					   WHEN MUPH.STATUS = 'R'
					   THEN '1'
					   WHEN MUPH.STATUS = 'G'
					   THEN '2'
					   WHEN MUPH.STATUS = 'Y'
					   THEN '3'
					   WHEN MUPH.STATUS = 'N'
					   THEN '4'
					   END                                            AS POPBILL_STATE
					  ,''                                             AS POPBILL_RESULT
					  ,MUPH.REG_ID                                    AS REG_ID
					  ,MUPH.PUSH_DATE                                 AS SEND_DATE
					  ,'PUSH'                                         AS TYPE
				  FROM MU_USER_PUSH_HISTORY AS MUPH
				 WHERE MUPH.USE_YN = 'Y'
				   AND MUPH.MU_USER_ID = #{muUserId}
		  ) AS MSKP
		 WHERE 1 = 1
		   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
		   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
		   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
		   </if>
		   <if test="searchKeywordParam != null and searchKeywordParam != ''">
		   AND (MSKP.CONTENT LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
		   </if>
		   <if test="searchType != null and searchType != ''">
		   AND MSKP.TYPE = #{searchType}
		   </if>
	</select>
	
	<select id="selectListUserPushHistory" parameterType="hashmap" resultType="hashmap">
		SELECT MSKP.*
		  FROM (
				SELECT MSKP.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MSKP.PUSH_ID                                   AS "pushId"
							  ,MSKP.CONTENT                                   AS "content"
							  ,CASE
							   WHEN MSKP.POPBILL_STATE = '0'
							   THEN '접수'
							   WHEN MSKP.POPBILL_STATE = '1'
							   THEN '대기중'
							   WHEN MSKP.POPBILL_STATE = '2'
							   THEN '처리중'
							   WHEN MSKP.POPBILL_STATE = '3'
							   THEN '완료'
							   WHEN MSKP.POPBILL_STATE = '4'
							   THEN '취소'
							   ELSE ''
							   END						                      AS "popbillState"
							  ,MSKP.POPBILL_RESULT                            AS "popbillResult"
							  ,(SELECT MM.NAME
								  FROM MU_MEMBER AS MM
								 WHERE MM.USE_YN = 'Y'
								   AND MM.MU_MEMBER_ID = MSKP.REG_ID
								 LIMIT 0, 1)                                  AS "memberName"
							  ,DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d')        AS "sendDate"
							  ,MSKP.TYPE                                      AS "type"
						  FROM (
								SELECT MS.MU_SMS_ID                                   AS PUSH_ID
									  ,MS.CONTENT                                     AS CONTENT
									  ,MS.POPBILL_STATE						          AS POPBILL_STATE
									  ,MS.POPBILL_RESULT                              AS POPBILL_RESULT
									  ,MS.REG_ID                                      AS REG_ID
									  ,MS.SEND_DATE                                   AS SEND_DATE
									  ,'SMS'                                          AS TYPE
								  FROM MU_SMS AS MS
								 WHERE MS.USE_YN = 'Y'
								   AND MS.MU_USER_ID = #{muUserId}
		
							 UNION ALL
		
								SELECT MK.MU_KAKAO_ID                                 AS PUSH_ID
									  ,(SELECT MKT.TITLE
										  FROM MU_KAKAO_TEMPLATE AS MKT
										 WHERE MKT.USE_YN = 'Y'
										   AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
										 LIMIT 0, 1)                                  AS CONTENT
									  ,MK.POPBILL_STATE						          AS POPBILL_STATE
									  ,MK.POPBILL_RESULT                              AS POPBILL_RESULT
									  ,MK.REG_ID                                      AS REG_ID
									  ,MK.SEND_DATE                                   AS SEND_DATE
									  ,'KAKAO'                                        AS TYPE
								  FROM MU_KAKAO AS MK
								 WHERE MK.USE_YN = 'Y'
								   AND MK.MU_USER_ID = #{muUserId}
		
							 UNION ALL
		
								SELECT MUPH.MU_USER_PUSH_HISTORY_ID                   AS PUSH_ID
									  ,MESSAGE                                        AS CONTENT
									  ,CASE
									   WHEN MUPH.STATUS = 'R'
									   THEN '1'
									   WHEN MUPH.STATUS = 'G'
									   THEN '2'
									   WHEN MUPH.STATUS = 'Y'
									   THEN '3'
									   WHEN MUPH.STATUS = 'N'
									   THEN '4'
									   END                                            AS POPBILL_STATE
									  ,''                                             AS POPBILL_RESULT
									  ,MUPH.REG_ID                                    AS REG_ID
									  ,MUPH.PUSH_DATE                                 AS SEND_DATE
									  ,'PUSH'                                         AS TYPE
								  FROM MU_USER_PUSH_HISTORY AS MUPH
								 WHERE MUPH.USE_YN = 'Y'
								   AND MUPH.MU_USER_ID = #{muUserId}
						  ) AS MSKP
						 WHERE 1 = 1
						   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
						   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
						   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
						   </if>
						   <if test="searchKeywordParam != null and searchKeywordParam != ''">
						   AND (MSKP.CONTENT LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
						   </if>
						   <if test="searchType != null and searchType != ''">
						   AND MSKP.TYPE = #{searchType}
						   </if>
						 <choose>
						 <when test="sort != null and sort != ''">
					  ORDER BY ${sort}
						 </when>
						 <otherwise>
					  ORDER BY MSKP.SEND_DATE DESC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				  ) MSKP, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
		  ) MSKP
		 WHERE 1 = 1
		 LIMIT ${start}, ${limit}
	</select>
	
	<select id="selectListUserPushHistoryExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MSKP.*
		  FROM (
				SELECT MSKP.*
					  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
				  FROM (
						SELECT MSKP.PUSH_ID                                   AS "pushId"
							  ,MSKP.CONTENT                                   AS "content"
							  ,CASE
							   WHEN MSKP.POPBILL_STATE = '0'
							   THEN '접수'
							   WHEN MSKP.POPBILL_STATE = '1'
							   THEN '대기중'
							   WHEN MSKP.POPBILL_STATE = '2'
							   THEN '처리중'
							   WHEN MSKP.POPBILL_STATE = '3'
							   THEN '완료'
							   WHEN MSKP.POPBILL_STATE = '4'
							   THEN '취소'
							   ELSE ''
							   END						                      AS "popbillState"
							  ,MSKP.POPBILL_RESULT                            AS "popbillResult"
							  ,(SELECT MM.NAME
								  FROM MU_MEMBER AS MM
								 WHERE MM.USE_YN = 'Y'
								   AND MM.MU_MEMBER_ID = MSKP.REG_ID
								 LIMIT 0, 1)                                  AS "memberName"
							  ,DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d')        AS "sendDate"
							  ,MSKP.TYPE                                      AS "type"
						  FROM (
								SELECT MS.MU_SMS_ID                                   AS PUSH_ID
									  ,MS.CONTENT                                     AS CONTENT
									  ,MS.POPBILL_STATE						          AS POPBILL_STATE
									  ,MS.POPBILL_RESULT                              AS POPBILL_RESULT
									  ,MS.REG_ID                                      AS REG_ID
									  ,MS.SEND_DATE                                   AS SEND_DATE
									  ,'SMS'                                          AS TYPE
								  FROM MU_SMS AS MS
								 WHERE MS.USE_YN = 'Y'
								   AND MS.MU_USER_ID = #{muUserId}

							 UNION ALL

								SELECT MK.MU_KAKAO_ID                                 AS PUSH_ID
									  ,(SELECT MKT.TITLE
										  FROM MU_KAKAO_TEMPLATE AS MKT
										 WHERE MKT.USE_YN = 'Y'
										   AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
										 LIMIT 0, 1)                                  AS CONTENT
									  ,MK.POPBILL_STATE						          AS POPBILL_STATE
									  ,MK.POPBILL_RESULT                              AS POPBILL_RESULT
									  ,MK.REG_ID                                      AS REG_ID
									  ,MK.SEND_DATE                                   AS SEND_DATE
									  ,'KAKAO'                                        AS TYPE
								  FROM MU_KAKAO AS MK
								 WHERE MK.USE_YN = 'Y'
								   AND MK.MU_USER_ID = #{muUserId}

							 UNION ALL

								SELECT MUPH.MU_USER_PUSH_HISTORY_ID                   AS PUSH_ID
									  ,MESSAGE                                        AS CONTENT
									  ,CASE
									   WHEN MUPH.STATUS = 'R'
									   THEN '1'
									   WHEN MUPH.STATUS = 'G'
									   THEN '2'
									   WHEN MUPH.STATUS = 'Y'
									   THEN '3'
									   WHEN MUPH.STATUS = 'N'
									   THEN '4'
									   END                                            AS POPBILL_STATE
									  ,''                                             AS POPBILL_RESULT
									  ,MUPH.REG_ID                                    AS REG_ID
									  ,MUPH.PUSH_DATE                                 AS SEND_DATE
									  ,'PUSH'                                         AS TYPE
								  FROM MU_USER_PUSH_HISTORY AS MUPH
								 WHERE MUPH.USE_YN = 'Y'
								   AND MUPH.MU_USER_ID = #{muUserId}
						  ) AS MSKP
						 WHERE 1 = 1
						   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
						   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
						   AND DATE_FORMAT(MSKP.SEND_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
						   </if>
						   <if test="searchKeywordParam != null and searchKeywordParam != ''">
						   AND (MSKP.CONTENT LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
						   </if>
						   <if test="searchType != null and searchType != ''">
						   AND MSKP.TYPE = #{searchType}
						   </if>
						 <choose>
						 <when test="sort != null and sort != ''">
					  ORDER BY ${sort}
						 </when>
						 <otherwise>
					  ORDER BY MSKP.SEND_DATE DESC
						 </otherwise>
						 </choose>
						 LIMIT 18446744073709551615
				   ) MSKP, (SELECT @RNUM:=0) R
				 WHERE 1 = 1
		   ) MSKP
		 WHERE 1 = 1
	</select>
	
	<insert id="insertUserPushHistory" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muUserPushHistoryChartId},LPAD((SELECT NEXTVAL(MU_USER_PUSH_HISTORY_SEQ)), 9, 0))
		</selectKey>
		INSERT INTO MU_USER_PUSH_HISTORY(
					 MU_USER_PUSH_HISTORY_ID
					,MU_USER_ID
					,MU_DEVICE_ID
					,MESSAGE
					,STATUS
					,READ_YN
					,TYPE
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muUserId}
					,#{muDeviceId}
					,#{message}
					,'R'
					,'N'
					,#{type}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
</mapper>