<?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.admin.biz.dao.TsaMailTemplateDao">

		<select id="getmailHFList" parameterType="MailTemplate" resultType="MailTemplate">
		/* TsaMailTemplate.getmailHFList 메일헤더푸터리스트*/
		SELECT MAILHF_SQ                                             /*메일헤더푸터 일련번호*/
		   	 , MAILHF_NM                                             /*메일헤더푸터 명*/
		     , MAILHF_GB                                             /*메일 헤더푸터 구분 H:헤더 F:푸터*/
		     , MAILHF_CONTENT                                        /*메일 헤더푸터 내용*/
		     , USE_YN                                                /*사용여부*/
		     , FN_GET_USER_NM(REG_NO)             AS REG_NM
		     , DATE_FORMAT(REG_DT,'%Y%m%d%H%i%S') AS REG_DT
		     , FN_GET_USER_NM(UPD_NO)             AS UPD_NM
		     , DATE_FORMAT(UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
		FROM   TB_MAILHF
		WHERE  USE_YN = 'Y'
		<if test="mailhfNm != null and mailhfNm !='' ">
		AND    LOWER(MAILHF_NM) LIKE CONCAT('%',LOWER(#{mailhfNm}),'%')
		</if>
		<if test="mailhfGb !=null and mailhfGb !='' ">
		AND    MAILHF_GB = #{mailhfGb}
		</if> 
		<if test="searchTxt !=null and searchTxt !='' ">
		AND    FN_GET_USER_NM(REG_NO) LIKE CONCAT('%',LOWER(#{searchTxt}), '%')
		</if>
		ORDER  BY MAILHF_SQ DESC
		</select>
	
		<insert id="saveMailHF" parameterType="MailTemplate">
		/* TsaMailTemplate.saveMailHF 메일헤더푸터관리 등록/수정*/
		INSERT INTO TB_MAILHF (
			   MAILHF_SQ                                        /*메일헤더푸터 일련번호*/
		     , MAILHF_NM                                        /*메일헤더푸터 명*/
		     , MAILHF_GB                                        /*메일헤더푸터 구분 H:헤더 F:푸터*/
		     , MAILHF_CONTENT                                   /* 메일헤더푸터 내용*/
		     , USE_YN                                           /*사용여부*/
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
	     VALUES (
		       #{mailhfSq}
		     , #{mailhfNm}
		     , #{mailhfGb}
		     , #{mailhfContent}
		     , IFNULL(#{useYn},'Y')
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       MAILHF_SQ = #{mailhfSq}
		     , MAILHF_NM = #{mailhfNm}
		     , MAILHF_GB = #{mailhfGb}
		     , MAILHF_CONTENT = #{mailhfContent}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		</insert>
	
		<select id="getMailHFDetail" parameterType="Integer" resultType="MailTemplate">
		/* TsaMailTemplate.getMailHFDetail 메일헤더푸터 상세화면 */
		SELECT MAILHF_SQ
		     , MAILHF_NM
		     , MAILHF_GB
		     , MAILHF_CONTENT
		FROM   TB_MAILHF
		WHERE  MAILHF_SQ = #{mailhfSq}
		</select>
	
		<update id='deleteMailHFList' parameterType='MailTemplate'>
		/* TsaMailTemplate.deleteMailHFList 메일헤더푸터리스트삭제*/
		UPDATE TB_MAILHF
		SET    USE_YN = 'N'
		WHERE  MAILHF_SQ = #{mailhfSq}
		</update>
	
		<select id="getMailGoodsInfo" parameterType="Goods" resultType="Goods">
		/*TsaMailTemplate.getMailGoodsInfo 메일 템플릿 상품 전시*/
		SELECT GOODS_CD
		     , BRAND_ENM 
		     , GOODS_NM
	         , GOODS_STAT
	         , LIST_PRICE
	         , CURR_PRICE
		     , GROUP_CONCAT(OPT_CD2 ORDER BY DISP_ORD DESC, OPT_CD2 SEPARATOR ',')  AS SITE_CD
	      FROM (
		SELECT   GI.SYS_IMG_NM
			   , G.GOODS_CD
			   , G.GOODS_NM
			   , B.BRAND_ENM 
			   , (CASE WHEN G.GOODS_TYPE = 'G056_N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD)
		   ELSE  (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = G.GOODS_CD)
				 END) AS STOCK_QTY_SUM
			   , G.GOODS_STAT
			   , G.STYLE_YEAR
			   , G.SEASON_CD
			   , G.LIST_PRICE
			   , G.CURR_PRICE
			   , G.PRICE_UPD_DT
			   , G.ERP_STOCK_LINK_YN
			   , G.FORMAL_GB
			   , G.RETURNABLE_YN
			   , G.REG_DT
			   , FN_GET_USER_NM(G.REG_NO) AS REG_NM
			   , G.UPD_DT
			   , FN_GET_USER_NM(G.UPD_NO) AS UPD_NM
			   , IFNULL(GC.COMPS_GOODS_CD, G.GOODS_CD) AS REP_GOODS_CD
			   , OP.OPT_CD2
			   , OP.DISP_ORD
			FROM TB_GOODS G
			LEFT OUTER JOIN TB_GOODS_COMPOSE GC
			  ON G.GOODS_CD = GC.GOODS_CD
		   INNER JOIN TB_OPTION OP
			  ON IFNULL(GC.COMPS_GOODS_CD, G.GOODS_CD) = OP.GOODS_CD
			LEFT OUTER JOIN TB_GOODS_IMG GI
			  ON G.GOODS_CD = GI.GOODS_CD
		   INNER JOIN TB_BRAND B
			  ON G.BRAND_CD = B.BRAND_CD
		   WHERE G.GOODS_CD IN
		<foreach collection="arrGoodsCd" item="item" index="index"  open="(" close=")" separator=",">
			 #{item}
		</foreach>
		  ) Z
		    GROUP BY SYS_IMG_NM, GOODS_CD, GOODS_NM, STOCK_QTY_SUM, GOODS_STAT, STYLE_YEAR
				   , SEASON_CD, LIST_PRICE, CURR_PRICE, PRICE_UPD_DT, ERP_STOCK_LINK_YN, FORMAL_GB, RETURNABLE_YN, REG_DT, REG_NM
				   , UPD_DT, UPD_NM, REP_GOODS_CD
		</select>
	
		<insert id="saveMktmailform" parameterType="mktMailManagement" keyProperty="mailtSq">
		/* TsaMailTemplate.saveMktmailform mkt메일 등록 수정 */
		INSERT INTO TB_MAILT (
				    MAILT_SQ
				  , MAILT_NM
				  , MAILT_GB
				  , MAILH_SQ
				  , MAILF_SQ
				  , MAIL_CONTENT
				  , USE_YN
				  , REG_NO
				  , REG_DT
				  , UPD_NO
				  , UPD_DT
			)
		     VALUES (
				    #{mailtSq}
				  , #{mailtNm}
				  , #{mailtGb}
				  , #{mktHeader}
				  , IFNULL(#{mktFooter}, null )
				  , #{mailContent}
				  , IFNULL(#{useYn},'Y')
				  , #{regNo}
				  , NOW()
				  , #{updNo}
				  , NOW()
			)
		ON DUPLICATE KEY UPDATE
			        MAILT_SQ = #{mailtSq}
		          , MAILT_NM = #{mailtNm}
		          , MAILT_GB = #{mailtGb}
		          , MAILH_SQ = #{mktHeader}
		          , MAILF_SQ = #{mktFooter}
		          , MAIL_CONTENT = #{mailContent}
		          , UPD_NO = #{updNo}
		          , UPD_DT = NOW()
		</insert>
	
		<insert id="saveMailTc" parameterType="MktmailManagement" keyProperty="mailtcSq">
		/* TsaMailTemplate.saveMailTc mkt메일 구분자 등록 */
		INSERT INTO TB_MAILTC ( 
				    MAILTC_SQ
				  , MAILT_SQ
				  , MAILTC_NM
				  , GOODS_CPR
				  , MAILTC_LINK
				  , USE_YN
				  , REG_NO
				  , REG_DT
				  , UPD_NO
				  , UPD_DT
			)
		     VALUES ( 
				    #{mailtcSq}
				  , #{mailtSq}
				  , #{mailtcNm}
				  , #{goodsCpr}
				  , #{mailtcLink}
				  , IFNULL(#{useYn},'Y')
				  , #{regNo}
				  , NOW()
				  , #{updNo}
				  , NOW()
			)
		ON DUPLICATE KEY UPDATE
			        MAILTC_SQ = #{mailtcSq}
			      , MAILT_SQ = #{mailtSq}
				  , MAILTC_NM = #{mailtcNm}
				  , GOODS_CPR = #{goodsCpr}
				  , MAILTC_LINK = #{mailtcLink}
				  , UPD_NO = #{updNo}
				  , UPD_DT = NOW()
		</insert>
	
		<insert id="saveMailGoods" parameterType="MktmailManagement" keyProperty="mailtcSq">
		/* TsaMailTemplate.saveMailGoods mkt메일 상품등록 */
		INSERT INTO TB_MAILTC_GOODS (
				    MAILTC_SQ
				  , GOODS_CD
				  , DISP_ORD
				  , REG_NO
				  , REG_DT
				  , UPD_NO
				  , UPD_DT
			)
		     VALUES (
				    #{mailtcSq}
				  , #{goodsCd}
				  , #{dispOrd}
				  , #{regNo}
				  , NOW()
				  , #{updNo}
				  , NOW()
			)
		ON DUPLICATE KEY UPDATE
				    MAILTC_SQ = #{mailtcSq}
				  , GOODS_CD = #{goodsCd}
				  , DISP_ORD = #{dispOrd}
				  , UPD_NO = #{updNo}
				  , UPD_DT = NOW()
		</insert>
		
		<select id="getMktMailList" parameterType="MktmailManagement" resultType="MktmailManagement">
		/* TsaMailTemplate.getMktMailList mkt메일 리스트*/
		SELECT 		   TM.MAILT_SQ
					 , TM.MAILT_GB
					 , TM.MAILT_NM
					 , FN_GET_USER_NM(TM.REG_NO)             AS REG_NM
					 , DATE_FORMAT(TM.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
					 , FN_GET_USER_NM(TM.UPD_NO)             AS UPD_NM
					 , DATE_FORMAT(TM.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
		FROM    	   TB_MAILT TM
		WHERE  		   TM.USE_YN = 'Y'
		<if test="mktmailStdt != null and mktmailStdt != '' and mktmailEddt != null and mktmailEddt != ''">
		AND  TM.REG_DT BETWEEN STR_TO_DATE(#{mktmailStdt}, '%Y-%m-%d%H%i%S') AND STR_TO_DATE(CONCAT(#{mktmailEddt}, '235959'),'%Y-%m-%d%H%i%S')
		</if> 
		<if test="searchTxt != null and searchTxt != ''">
			<if test="searchGb != null and searchGb == 'mailtSq'">
				AND TM.MAILT_SQ = #{searchTxt}
			</if>
			<if test="searchGb != null and searchGb == 'mailtNm'">
				AND TM.MAILT_NM LIKE CONCAT('%',LOWER(#{searchTxt}), '%')
			</if>
		</if>
		ORDER BY TM.MAILT_SQ DESC
		</select>
		
		<select id="getMktmailManagementDetail" parameterType="Integer" resultType="MktmailManagement">
		/* TsaMailTemplate.getMktmailManagementDetail mkt메일 상세화면*/
		SELECT MAILT_SQ
			 , MAILT_GB
			 , MAILH_SQ
			 , MAILF_SQ
			 , MAILT_NM
			 , MAIL_CONTENT
		FROM   TB_MAILT
		WHERE  MAILT_SQ = #{mailtSq}
		</select>
		
		<select id="getMktmailManagementTCList" parameterType="MktmailManagementTc" resultType="MktmailManagementTc">
		/* TsaMailTemplate.getMktmailManagementTCDetail mkt메일 구분자 상세화면*/
		SELECT 		TC.MAILTC_SQ
			 	  , TC.MAILT_SQ	
			 	  , TC.MAILTC_NM
			  	  , TC.GOODS_CPR	
			 	  , TC.MAILTC_LINK
		FROM   		TB_MAILTC TC
		INNER JOIN 	TB_MAILT TM
		ON 			TM.MAILT_SQ = TC.MAILT_SQ 
		WHERE  		TC.MAILT_SQ = #{mailtSq}
		AND	  		TC.USE_YN = "Y"
		</select>
		
		<select id="getMktmailManagementGoodsList" parameterType="MktmailManagementTc" resultType="MktmailManagementGoods">
		/* TsaMailTemplate.getMktmailManagementGoodsList mkt메일 상품 상세화면*/
		SELECT		TG.MAILTC_SQ
				  , TG.GOODS_CD
			      , TBRAND.BRAND_ENM 
			      , tbg.GOODS_NM 
			      , TBG.GOODS_STAT
			      , TBG.LIST_PRICE 
			      , TBG.CURR_PRICE
			      , TG.DISP_ORD
		FROM 		TB_MAILTC_GOODS TG
		INNER JOIN  TB_MAILTC TC
		ON 			TC.MAILTC_SQ = TG.MAILTC_SQ 
		INNER JOIN  tb_goods TBG
		ON 			TBG.GOODS_CD = TG.GOODS_CD 
		INNER JOIN 	tb_brand TBRAND
		ON		    TBRAND.BRAND_CD = TBG.BRAND_CD
		WHERE 		TC.MAILTC_SQ = #{mailtcSq}
		ORDER  BY 	TG.DISP_ORD
		</select>
		
		<select id="getMktmailPreviewGoodsList" parameterType="MktmailManagementTc" resultType="MktmailManagementGoods">
		/* TsaMailTemplate.getMktmailPreviewGoodsList mkt메일 미리보기 상품정보 호출*/
		SELECT		TG.MAILTC_SQ
				  ,	IMG.SYS_IMG_NM
			      , TBRAND.BRAND_ENM 
			      , tbg.GOODS_NM 
			      , TBG.LIST_PRICE 
			      , TBG.CURR_PRICE
		FROM 		TB_MAILTC_GOODS TG
		INNER JOIN  TB_MAILTC TC
		ON 			TC.MAILTC_SQ = TG.MAILTC_SQ 
		INNER JOIN  tb_goods TBG
		ON 			TBG.GOODS_CD = TG.GOODS_CD 
		INNER JOIN 	tb_brand TBRAND
		ON		    TBRAND.BRAND_CD = TBG.BRAND_CD
		INNER JOIN  TB_GOODS_IMG IMG
		ON  		IMG.GOODS_CD = TG.GOODS_CD
		WHERE 		TC.MAILTC_SQ = #{mailtcSq}
		AND 		IMG.DEFAULT_IMG_YN = 'Y'
		ORDER  BY 	TG.DISP_ORD
		</select>
		
		<update id='deleteMktmailList' parameterType='MktmailManagement'>
		/* TsaMailTemplate.deleteMailHFList mkt메일 리스트 삭제*/
		UPDATE  TB_MAILT
	  	SET     USE_YN = 'N'
		WHERE   MAILT_SQ = #{mailtSq}
		</update>
		
		<delete id="deleteMktCorner" parameterType='MktmailManagementTc'>
		/* TsaMailTemplate.deleteMktCorner mkt메일 구분자 삭제*/
		UPDATE  TB_MAILTC
		SET		USE_YN = 'N'
		WHERE	MAILTC_SQ = #{mailtcSq}
		</delete>
		
		<delete id="deleteMktGoodsList" parameterType='MktmailManagementGoods'>
		/* TsaMailTemplate.deleteMktGoods mkt메일 상품 삭제*/
		DELETE 	
		FROM	TB_MAILTC_GOODS
		WHERE	MAILTC_SQ = #{mailtcSq}
		</delete>
		
		<select id="getMktmailPreviewHeader" parameterType="Integer" resultType="MktmailManagement">
		/* TsaMailTemplate.getMktmailPreviewHeader mkt메일 미리보기 헤더 호출*/
		SELECT 		HF.MAILHF_CONTENT
		FROM   		TB_MAILT TM
		INNER JOIN  TB_MAILHF HF
		ON 	  		TM.MAILH_SQ = HF.MAILHF_SQ
		WHERE 		HF.MAILHF_GB = 'H'
		AND 		TM.MAILT_SQ = #{mailtSq}
		</select>
		
		<select id="getMktmailPreviewFooter" parameterType="Integer" resultType="MktmailManagement">
		/* TsaMailTemplate.getMktmailPreviewFooter mkt메일 미리보기 푸터 호출*/
		SELECT 		HF.MAILHF_CONTENT
		FROM   		TB_MAILT TM
		INNER JOIN  TB_MAILHF HF
		ON 	  		TM.MAILF_SQ = HF.MAILHF_SQ
		WHERE 		HF.MAILHF_GB = 'F'
		AND 		TM.MAILT_SQ = #{mailtSq}
		</select>
		
		<select id="getMktmailPreviewContent" parameterType="MktmailManagement" resultType="MktmailManagement">
		/* TsaMailTemplate.getMktmailPreviewContent mkt메일 미리보기 내용 호출*/
		SELECT 		MAILT_NM
				  , MAIL_CONTENT
		FROM		TB_MAILT
		WHERE 		MAILT_SQ =   #{mailtSq}
		</select>
	
</mapper>