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

	<select id="selectListDevice" parameterType="hashmap" resultType="hashmap">
		SELECT ND.MU_DEVICE_ID 	AS "muDeviceId"
			  ,ND.PHONE_TYPE 	AS "phoneType"
			  ,ND.DEVICE_UUID	AS "deviceUuid"
			  ,ND.MODEL_NAME	AS "modelName"
		  FROM MU_DEVICE AS ND
		 WHERE ND.USE_YN      = 'Y'
		   AND ND.STATUS      = 'Y'
		   AND ND.MODEL_NAME  = #{modelName}
		   AND ND.PHONE_TYPE  = #{phoneType}
		   AND ND.DEVICE_UUID = #{deviceUuid}
		   AND ND.MU_USER_ID  = #{muUserId}
	</select>

	<select id="selectListDevice2" parameterType="hashmap" resultType="hashmap">
		SELECT MD.MU_DEVICE_ID  AS "muDeviceId"
			  ,MD.PHONE_TYPE    AS "phoneType"
			  ,MD.DEVICE_UUID   AS "deviceUuid"
			  ,MD.PUSH_KEY      AS "pushKey"
		      ,MU.MU_USER_ID    AS "muUserId"
		      ,MU.USER_NAME     AS "userName"
		  FROM MU_DEVICE AS MD
    INNER JOIN MU_USER AS MU
		  	ON MD.MU_USER_ID = MU.MU_USER_ID
		   AND MD.USE_YN      = 'Y'
		   AND MU.USE_YN      = 'Y'
		   AND MD.MU_USER_ID = #{muUserId}
		   AND MD.AUTO_PUSH_YN = 'Y'
	</select>

	<!-- device 정보 조회 -->
	<select id="selectDevice" parameterType="hashmap" resultType="hashmap">
		SELECT MD.MU_DEVICE_ID 	AS "muDeviceId"
			  ,MD.PHONE_TYPE 	AS "phoneType"
			  ,MD.DEVICE_UUID	AS "deviceUuid"
			  ,MD.PUSH_KEY		AS "pushKey"
		  FROM MU_DEVICE AS MD
		 WHERE MD.USE_YN      = 'Y'
		   AND MD.STATUS      = 'Y'
		   AND MD.MU_USER_ID  = #{muUserId}
	  ORDER BY MD.MOD_DATE DESC
	</select>

	<!-- 디바이스 푸시 알림 여부 조회 -->
	<select id="selectDevicePushYn" parameterType="hashmap" resultType="hashmap">
		SELECT MD.MU_DEVICE_ID 	AS "muDeviceId"
			  ,MD.PHONE_TYPE 	AS "phoneType"
			  ,MD.DEVICE_UUID	AS "deviceUuid"
			  ,MD.PUSH_KEY		AS "pushKey"
		      ,MD.AUTO_PUSH_YN       AS "autoPushYn"
		      ,MD.MARKETING_PUSH_YN  AS "marketingPushYn"
		  FROM MU_DEVICE AS MD
		 WHERE MD.USE_YN      = 'Y'
		   AND MD.STATUS      = 'Y'
		   AND MD.MU_USER_ID  = #{muUserId}
		   AND MD.DEVICE_UUID  = #{deviceUuid}
	</select>

	<!-- 디바이스 정보 등록 -->
	<insert id="insertDevice" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muDeviceId},LPAD((SELECT NEXTVAL(MU_DEVICE_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_DEVICE(
					MU_DEVICE_ID
					,MU_USER_ID
					,PHONE_TYPE
					,APP_VERSION
					,DEVICE_UUID
					,PUSH_KEY
					,MODEL_NAME
					,OS_VERSION
					,STATUS
					,WRITE_DATE
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
		)VALUES(
					#{id}
					,#{muUserId}
					,#{phoneType}
					,#{appVersion, jdbcType=VARCHAR}
					,#{deviceUuid, jdbcType=VARCHAR}
					,#{pushKey,    jdbcType=VARCHAR}
					,#{modelName,  jdbcType=VARCHAR}
					,#{osVersion,  jdbcType=VARCHAR}
					,#{status}
					,NOW()
					,'C'
					,#{useYn}
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
		)
	</insert>

	<update id="updateDevice" parameterType="hashmap">
		UPDATE MU_DEVICE
		   SET MOD_ID          = #{modId}
			  ,MOD_DATE        = NOW()
			  ,CUD_FLAG        = 'U'
			  ,PHONE_TYPE      = #{phoneType}
			  ,APP_VERSION     = #{appVersion}
			  ,PUSH_KEY        = #{pushKey}
			  ,MODEL_NAME      = #{modelName}
			  ,OS_VERSION      = #{osVersion}
		 WHERE USE_YN      = 'Y'
		   AND MU_DEVICE_ID = #{muDeviceId}
	</update>

	<update id="updateDevicePushYn" parameterType="hashmap">
		UPDATE MU_DEVICE
		   SET MOD_ID          = #{modId}
			  ,MOD_DATE        = NOW()
			  ,CUD_FLAG        = 'U'
		      <choose>
			  <when test="type eq 'auto'.toString()">
			  ,AUTO_PUSH_YN    = #{pushYn}
			  </when>
			  <when test="type eq 'marketing'.toString()">
			  ,MARKETING_PUSH_YN    = #{pushYn}
			  </when>
			  </choose>
		 WHERE USE_YN      = 'Y'
		   AND MU_USER_ID = #{muUserId}
		   AND DEVICE_UUID = #{deviceUuid}
	</update>

	<update id="updateDeviceStatus" parameterType="hashmap">
		UPDATE MU_DEVICE
		   SET MOD_ID          = #{modId}
			  ,MOD_DATE        = NOW()
			  ,CUD_FLAG        = 'U'
			  ,STATUS          = #{status}
		 WHERE USE_YN      = 'Y'
		   AND MU_USER_ID = #{muUserId}
		   AND DEVICE_UUID = #{deviceUuid}
	</update>
</mapper>