<?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="com.style24.core.biz.dao.TscPopupDao">

	<!-- 팝업 목록 건수 조회 -->
	<select id="getPopupListCount" parameterType="Popup" resultType="int">
		/* TscPopup.getPopupListCount */
		SELECT	COUNT(*) AS TOTCNT
		FROM	TB_POPUP_PREVIEW A
		LEFT OUTER JOIN TB_POPUP B
		ON 	A.POPUP_SQ = B.POPUP_SQ
		WHERE	1=1
		AND A.USE_YN = 'Y'
		<if test='siteCd != null and siteCd != ""'>
			AND A.SITE_CD = #{siteCd}
		</if>
		<if test='frontGb != null and frontGb != ""'>
			AND A.FRONT_GB = #{frontGb}
		</if>
		<if test="dispEddt != null and dispEddt !=''">
		<![CDATA[
		  AND A.DISP_STDT <= STR_TO_DATE(CONCAT(#{dispEddt},#{dispEdTime}),'%Y-%m-%d %H%i%S')
		]]>
		</if>
		<if test="dispStdt != null and dispStdt !=''">
		<![CDATA[
		  AND A.DISP_EDDT >= STR_TO_DATE(CONCAT(#{dispStdt},#{dispStTime}),'%Y-%m-%d %H%i%S')
		]]>
		</if>
	</select>

	<!-- 팝업 목록 조회 -->
	<select id="getPopupList" parameterType="Popup" resultType="Popup">
		/* TscPopup.getPopupList */
		SELECT	A.POPUP_SQ
			, A.SITE_CD
			, A.FRONT_GB
			, A.POPUP_GB
			, A.IMG_URL1
			, A.IMG_URL2
			, A.IMG_URL3
			, A.IMG_URL4
			, A.IMG_URL5
			, A.LINK_URL1
			, A.LINK_URL2
			, A.LINK_URL3
			, A.LINK_URL4
			, A.LINK_URL5
			, A.VIEW_PAGE
			, (SELECT CD_NM FROM TB_COMMON_CODE WHERE CD_GB = 'G037' AND CD = A.VIEW_PAGE) AS VIEW_PAGE_NM
			, A.CATE_NO
			, A.BRAND_CD
			, A.PLAN_SQ
			, A.DISP_ORD
			, A.TOP_LOC
			, A.LEFT_LOC
			, A.POPUP_WIDTH
			, A.POPUP_HEIGHT
			, A.UNEXP_DAYS
			, A.LINK_TARGET
			, DATE_FORMAT(A.DISP_STDT,'%Y-%m-%d') AS DISP_STDT
			, DATE_FORMAT(A.DISP_STDT,'%H:%i:%S') AS DISP_ST_TIME
			, DATE_FORMAT(A.DISP_EDDT,'%Y-%m-%d') AS DISP_EDDT
			, DATE_FORMAT(A.DISP_EDDT,'%H:%i:%S') AS DISP_ED_TIME
			, IFNULL(B.USE_YN, 'N') AS DISPLAY_YN
			, A.REG_NO
			, FN_GET_USER_NM(A.REG_NO) AS REG_NM
			, DATE_FORMAT(A.REG_DT,'%Y-%m-%d %H:%i:%S') AS REG_DT
		FROM	TB_POPUP_PREVIEW A
		LEFT OUTER JOIN TB_POPUP B
		ON 	A.POPUP_SQ = B.POPUP_SQ
		WHERE	1=1
		AND A.USE_YN = 'Y'
		<if test='siteCd != null and siteCd != ""'>
			AND A.SITE_CD = #{siteCd}
		</if>
		<if test='frontGb != null and frontGb != ""'>
			AND A.FRONT_GB = #{frontGb}
		</if>
		<if test="dispEddt != null and dispEddt !=''">
		<![CDATA[
		  AND A.DISP_STDT <= STR_TO_DATE(CONCAT(#{dispEddt},#{dispEdTime}),'%Y-%m-%d %H%i%S')
		]]>
		</if>
		<if test="dispStdt != null and dispStdt !=''">
		<![CDATA[
		  AND A.DISP_EDDT >= STR_TO_DATE(CONCAT(#{dispStdt},#{dispStTime}),'%Y-%m-%d %H%i%S')
		]]>
		</if>
		ORDER BY A.POPUP_SQ DESC
	</select>

	<!-- 팝업관리 등록/수정 -->
	<insert id="savePopup" parameterType="Popup" keyProperty="popupSq">
		/* TscPopup.savePopup */
		INSERT INTO TB_POPUP_PREVIEW(
			POPUP_SQ
			,SITE_CD
			,FRONT_GB
			,POPUP_GB
			,IMG_URL1
			,LINK_URL1
			,VIEW_PAGE
		<if test='cateNo != null and cateNo != ""'>
			,CATE_NO
		</if>
		<if test='brandCd != null and brandCd != ""'>
			,BRAND_CD
		</if>
		<if test='planSq != null and planSq != ""'>
			,PLAN_SQ
		</if>
			,DISP_STDT
			,DISP_EDDT
			,UNEXP_DAYS
			,LINK_TARGET 
			,DISP_ORD
			,USE_YN
			,REG_NO
			,REG_DT
			,UPD_NO
			,UPD_DT
		) VALUES (
			#{popupSq}
			,#{siteCd}
			,#{frontGb}
			,#{popupGb}
			,#{sysFileNm}
			,#{linkUrl1}
			,#{viewPage}
		<if test='cateNo != null and cateNo != ""'>
			,#{cateNo}
		</if>
		<if test='brandCd != null and brandCd != ""'>
			,#{brandCd}
		</if>
		<if test='planSq != null and planSq != ""'>
			,#{planSq}
		</if>
			,#{dispStdt}
			,#{dispEddt}
			,#{unexpDays}
			,#{linkTarget}
			,#{dispOrd}
			,#{useYn}
			,#{regNo}
			,NOW()
			,#{updNo}
		    ,NOW()
		) ON DUPLICATE KEY UPDATE
			 FRONT_GB	= #{frontGb}
			,POPUP_GB   = CASE WHEN #{frontGb} = 'P' THEN 'F' ELSE #{popupGb} END
			,IMG_URL1	= #{sysFileNm}
			,LINK_URL1	= #{linkUrl1}
			,VIEW_PAGE	= #{viewPage}
		<choose>
		<when test='cateNo != null and cateNo != ""'>
		    ,CATE_NO	= #{cateNo}
		    ,BRAND_CD	= null
		    ,PLAN_SQ	= null
		</when>
		<when test='brandCd != null and brandCd != ""'>
			,BRAND_CD	= #{brandCd}
			,CATE_NO	= null
			,PLAN_SQ	= null
		</when>
		<when test='planSq != null and planSq != ""'>
			,PLAN_SQ	= #{planSq}
			,CATE_NO	= null
			,BRAND_CD	= null
		</when>
		<otherwise>
			,PLAN_SQ	= null
			,CATE_NO	= null
			,BRAND_CD	= null
		</otherwise>
		</choose>	
			,DISP_STDT	= #{dispStdt}
			,DISP_EDDT	= #{dispEddt}
			,UNEXP_DAYS	= #{unexpDays}
			,LINK_TARGET = #{linkTarget} 
			,DISP_ORD	= #{dispOrd}
		    ,USE_YN = #{useYn}
			,UPD_NO = #{updNo}
			,UPD_DT = NOW()
	</insert>

	<!-- 팝업관리 팝업 삭제 -->
	<update id="deletePopup" parameterType="Popup">
		/* TscPopup.deletePopup */
		UPDATE TB_POPUP_PREVIEW
		SET UPD_NO = #{updNo}
		  , UPD_DT = NOW()
		  , USE_YN = 'N'
		WHERE POPUP_SQ = #{popupSq}
	</update>

	<!-- 팝업관리 프론트 삭제 -->
	<update id="deletePopupFront" parameterType="Popup">
		/* TscPopup.deletePopupFront */
		UPDATE TB_POPUP
		SET UPD_NO = #{updNo}
		  , UPD_DT = (SELECT UPD_DT FROM TB_POPUP_PREVIEW WHERE POPUP_SQ = #{popupSq})
		  , USE_YN = 'N'
		WHERE POPUP_SQ = #{popupSq}
	</update>

	<!-- 팝업관리 프론트 적용 -->
	<insert id="savePopupFront" parameterType="Popup">
		/* TscPopup.savePopupFront */
		INSERT INTO TB_POPUP ( POPUP_SQ
								   , SITE_CD
								   , FRONT_GB
								   , POPUP_GB
								   , IMG_URL1
								   , LINK_URL1
								   , VIEW_PAGE
								   , CATE_NO
								   , BRAND_CD
								   , PLAN_SQ
								   , DISP_STDT
								   , DISP_EDDT
								   , UNEXP_DAYS
								   , LINK_TARGET
								   , DISP_ORD
								   , USE_YN
								   , REG_NO
								   , REG_DT
								   , UPD_NO
								   , UPD_DT)
			(
				SELECT *
				FROM (
						 SELECT POPUP_SQ
							  , SITE_CD
							  , FRONT_GB
							  , POPUP_GB
							  , IMG_URL1
							  , LINK_URL1
							  , VIEW_PAGE
							  , CATE_NO
							  , BRAND_CD
							  , PLAN_SQ
							  , DISP_STDT
							  , DISP_EDDT
							  , UNEXP_DAYS
							  , LINK_TARGET
							  , DISP_ORD
							  , USE_YN
							  , #{regNo} 	AS REG_NO
							  , NOW()		AS REG_DT
							  , #{updNo}	AS UPD_NO
							  , NOW()		AS UPD_DT
						 FROM TB_POPUP_PREVIEW
						 WHERE POPUP_SQ = #{popupSq}
					 ) B
			) ON DUPLICATE KEY
		UPDATE
			SITE_CD = B.SITE_CD
			,FRONT_GB = B.FRONT_GB
			,POPUP_GB = B.POPUP_GB
			,IMG_URL1 = B.IMG_URL1
			,LINK_URL1 = B.LINK_URL1
			,VIEW_PAGE = B.VIEW_PAGE
			,CATE_NO = B.CATE_NO
			,BRAND_CD = B.BRAND_CD
			,PLAN_SQ = B.PLAN_SQ
			,DISP_STDT = B.DISP_STDT
			,DISP_EDDT = B.DISP_EDDT
			,UNEXP_DAYS = B.UNEXP_DAYS
			,LINK_TARGET = B.LINK_TARGET
			,DISP_ORD = B.DISP_ORD
			,USE_YN = B.USE_YN
		    ,REG_NO = B.REG_NO
		    ,REG_DT = B.REG_DT
			,UPD_NO = #{updNo}
			,UPD_DT = NOW()
	</insert>
	
</mapper>
