<?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.TsaMorebetterDao">
	<!-- // CSB 진행 -->
	<select id="getMorebetterListCount" parameterType="MoreBetter" resultType="int">
		/* TsaMorebetter.getMorebetterListCount */
		SELECT  COUNT(*) AS TOTCNT
		FROM	TB_TMTB T
		WHERE	1=1
		<if test="tmtbStdt != null and tmtbStdt != '' and tmtbEddt != null and tmtbEddt != ''">
			AND T.TMTB_ST_DT BETWEEN STR_TO_DATE(#{tmtbStdt}, '%Y-%m-%d%H%i%S') AND STR_TO_DATE(CONCAT(#{tmtbEddt}, '235959'),'%Y-%m-%d%H%i%S')
		</if>
		<if test="searchTxt != null and searchTxt != ''">
			<if test="searchGubun != null and searchGubun == 'tmtbSq'">
				AND T.TMTB_SQ = #{searchTxt}
			</if>
			<if test="searchGubun != null and searchGubun == 'tmtbNm'">
				AND T.TMTB_NM = #{searchTxt}
			</if>
		</if>
	</select>

	<select id="getMorebetterList" parameterType="MoreBetter" resultType="MoreBetter">
		/* TsaMorebetter.getMorebetterList */
		SELECT Z.*
		FROM(
			SELECT @rownum := @rownum + 1 AS RNUM
					,A.*
			FROM (
				SELECT T.TMTB_SQ
					 , T.TMTB_NM
					 , FN_GET_CODE_NM('G232',T.TMTB_STAT) as TMTB_STAT
					 , DATE_FORMAT(T.TMTB_ST_DT, '%Y.%m.%d') AS TMTB_STDT
					 , DATE_FORMAT(T.TMTB_ED_DT, '%Y.%m.%d') AS TMTB_EDDT
					 , T.DEL_YN
					 , FN_GET_USER_NM(T.REG_NO) AS REG_NM
					 , DATE_FORMAT(T.REG_DT, '%Y.%m.%d %H:%i:%S') AS REG_DT
					 , FN_GET_USER_NM(T.UPD_NO) AS UPD_NM
					 , DATE_FORMAT(T.UPD_DT, '%Y.%m.%d %H:%i:%S') AS UPD_DT
				FROM TB_TMTB T
				JOIN ( SELECT @rownum := 0) R
				WHERE 1=1
				<if test="tmtbStdt != null and tmtbStdt != '' and tmtbEddt != null and tmtbEddt != ''">
					AND T.TMTB_ST_DT BETWEEN STR_TO_DATE(#{tmtbStdt}, '%Y-%m-%d%H%i%S') AND STR_TO_DATE(CONCAT(#{tmtbEddt}, '235959'),'%Y-%m-%d%H%i%S')
				</if>
				<if test="searchTxt != null and searchTxt != ''">
					<if test="searchGubun != null and searchGubun == 'tmtbSq'">
						AND T.TMTB_SQ = #{searchTxt}
					</if>
					<if test="searchGubun != null and searchGubun == 'tmtbNm'">
						AND T.TMTB_NM = #{searchTxt}
					</if>
				</if>
				ORDER BY T.REG_DT DESC
			<include refid="getListPagingCondition_sql"/>
	</select>

	<sql id="getListPagingCondition_sql">
		<choose>
			<when test="pageable != null">
				) A
				)Z
				WHERE RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
			</when>
			<otherwise>
				) A
				)Z
			</otherwise>
		</choose>
	</sql>

	<select id="getMorebetterMstInfo" parameterType="Integer" resultType="MoreBetter">
		/* TsaMorebetter.getMorebetterMstInfo */
		SELECT	   	   TT.TMTB_SQ
					 , TT.TMTB_NM
					 , TT.TMTB_STAT
					 , TT.TMTB_ST_DT
					 , TT.TMTB_ED_DT
					 , TT.MULTI_YN
					 , TT.DEL_YN
					 , TT.REG_NO
					 , FN_GET_USER_NM(TT.REG_NO) AS REG_NM
					 , TT.REG_DT
					 , TT.UPD_NO
					 , FN_GET_USER_NM(TT.UPD_NO) AS UPD_NM
					 , TT.UPD_DT
					 , TS.SECTION_GB
					 , TS.SECTION_VAL
					 , TV.DC_WAY
					 , TV.DC_VAL
					 , DATE_FORMAT(TT.TMTB_ST_DT , '%Y-%m-%d') AS EVENT_STDT
					 , DATE_FORMAT(TT.TMTB_ST_DT , '%H') AS EVENT_STHH
					 , DATE_FORMAT(TT.TMTB_ST_DT , '%i') AS EVENT_STMM
					 , DATE_FORMAT(TT.TMTB_ED_DT , '%Y-%m-%d') AS EVENT_EDDT
					 , DATE_FORMAT(TT.TMTB_ED_DT , '%H') AS EVENT_EDHH
					 , DATE_FORMAT(TT.TMTB_ED_DT , '%i') AS EVENT_EDMM
		FROM 	TB_TMTB TT
		INNER 	JOIN TB_TMTB_SECTION TS
		ON 		TT.TMTB_SQ = TS.TMTB_SQ
		INNER 	JOIN TB_TMTB_VAL TV
		ON 		TT.TMTB_SQ = TV.TMTB_SQ
		AND		TS.TMTB_SECTION_SQ = TV.TMTB_SECTION_SQ
		WHERE 	1=1
		  AND 	TT.TMTB_SQ = #{tmtbSq}
		LIMIT 1
	</select>

	<select id="getMorebetterSectionValList" parameterType="Integer" resultType="MoreBetterSection">
		/* TsaMorebetter.getMorebetterSectionValList */
		SELECT    TS.TMTB_SQ
				 ,TS.TMTB_SECTION_SQ
				 ,TV.TMTB_VAL_SQ
				 ,TS.SECTION_GB
				 ,TS.SECTION_VAL
				 ,TV.DC_WAY
				 ,TV.DC_VAL
				 ,TS.DEL_YN
				 ,TS.REG_NO
				 ,TS.REG_DT
				 ,TS.UPD_NO
				 ,TS.UPD_DT
		FROM	TB_TMTB_SECTION TS
		INNER 	JOIN TB_TMTB_VAL TV
		ON 		TS.TMTB_SQ = TV.TMTB_SQ
		AND		TS.TMTB_SECTION_SQ = TV.TMTB_SECTION_SQ
		WHERE 	1=1
		  AND 	TS.TMTB_SQ = #{tmtbSq}
		  AND   TS.DEL_YN = 'N'
		ORDER BY TS.TMTB_SQ, TS.TMTB_SECTION_SQ, TV.TMTB_VAL_SQ
	</select>

	<select id="getMorebetterSupplyCompList" parameterType="MoreBetterGoods" resultType="MoreBetterGoods">
		/* TsaMorebetter.getMorebetterSupplyCompList */
		SELECT 	 TMTB_GOODS_SQ
				 , TMTB_SQ
				 , GOODS_GB
				 , TARGET_GB
				 , TARGET_VAL
				 , TARGET_VAL AS SUPPLY_COMP_CD
				 , FN_GET_SUPPLY_COMP_NM(TARGET_VAL) AS SUPPLY_COMP_NM
				 , DEL_YN
				 , REG_NO
				 , REG_DT
				 , UPD_NO
				 , UPD_DT
		FROM TB_TMTB_GOODS
		WHERE 1=1
		  AND TMTB_SQ = #{tmtbSq}
		  AND TARGET_GB = 'G260_13'
		  AND DEL_YN = 'N'
	</select>

	<select id="getMorebetterBrandList" parameterType="MoreBetterGoods" resultType="MoreBetterGoods">
		/* TsaMorebetter.getMorebetterBrandList */
		SELECT 	 TMTB_GOODS_SQ
				 , TMTB_SQ
				 , GOODS_GB
				 , TARGET_GB
				 , TARGET_VAL
				 , TARGET_VAL AS BRAND_CD
				 , (SELECT   BRAND_ENM
					FROM  TB_BRAND
					WHERE BRAND_CD = TARGET_VAL) AS BRAND_ENM
				 , (SELECT   FN_GET_SUPPLY_COMP_NM(SUPPLY_COMP_CD)
					FROM  TB_BRAND
					WHERE BRAND_CD = TARGET_VAL) AS SUPPLY_COMP_NM
				 , DEL_YN
				 , REG_NO
				 , REG_DT
				 , UPD_NO
				 , UPD_DT
		FROM TB_TMTB_GOODS
		WHERE 1=1
		  AND TMTB_SQ = #{tmtbSq}
		  AND TARGET_GB = 'G260_12'
		  AND DEL_YN = 'N'
	</select>

	<select id="getMorebetterApplyGoodsList" parameterType="MoreBetterGoods" resultType="MoreBetterGoods">
		/* TsaMorebetter.getMorebetterApplyGoodsList */
		SELECT 	 A.TMTB_GOODS_SQ
		       , A.TMTB_SQ
		       , B.TMTB_APPLY_GOODS_SQ
		       , A.GOODS_GB
		       , A.TARGET_GB
		       , A.TARGET_VAL
		       , A.TARGET_VAL AS GOODS_CD
		       , (SELECT  GOODS_NM
		      	FROM  TB_GOODS
		      	WHERE  GOODS_CD = A.TARGET_VAL) AS GOODS_NM
		       , A.DEL_YN
		       , A.REG_NO
		       , A.REG_DT
		       , A.UPD_NO
		       , A.UPD_DT
		FROM 		TB_TMTB_GOODS A
		INNER JOIN 	TB_TMTB_APPLY_GOODS B
		      ON 	A.TMTB_SQ = B.TMTB_SQ
		      AND A.GOODS_GB = B.GOODS_GB
		      AND A.TARGET_VAL = B.GOODS_CD
		WHERE 1=1
		  AND A.TMTB_SQ = #{tmtbSq}
		  AND A.TARGET_GB = 'G260_10'
		  AND A.GOODS_GB IN ('G800_10','G800_20')
 		  AND A.DEL_YN = 'N'
		GROUP BY A.TMTB_GOODS_SQ
	</select>

	<select id="getMorebetterExceptGoodsList" parameterType="MoreBetterGoods" resultType="MoreBetterGoods">
		/* TsaMorebetter.getMorebetterExceptGoodsList */
		SELECT 	 A.TMTB_GOODS_SQ
				 , A.TMTB_SQ
				 , B.TMTB_APPLY_GOODS_SQ
				 , A.GOODS_GB
				 , A.TARGET_GB
				 , A.TARGET_VAL
				 , A.TARGET_VAL AS GOODS_CD
				 , (SELECT  GOODS_NM
					FROM  TB_GOODS
					WHERE  GOODS_CD = A.TARGET_VAL) AS GOODS_NM
				 , A.DEL_YN
				 , A.REG_NO
				 , A.REG_DT
				 , A.UPD_NO
				 , A.UPD_DT
			FROM 	TB_TMTB_GOODS A
		INNER JOIN 	TB_TMTB_APPLY_GOODS B
			 ON 	A.TMTB_SQ = B.TMTB_SQ
			AND 	A.GOODS_GB = B.GOODS_GB
			AND 	A.TARGET_VAL = B.GOODS_CD
		WHERE 1=1
		  AND A.TMTB_SQ = #{tmtbSq}
		  AND A.TARGET_GB = 'G260_10'
		  AND A.GOODS_GB IN ('G800_30')
		  AND A.DEL_YN = 'N'
		GROUP BY A.TMTB_GOODS_SQ
	</select>

	<select id="getMorebetterBurdenList" parameterType="Integer" resultType="MoreBetterBurden">
		/* TsaMorebetter.getMorebetterBurdenList */
		SELECT 		TB_TMTB_BURDEN_SQ
				   , TMTB_SQ
				   , TMTB_SUPPLY_CD
				   , TMTB_BURDEN_RATE
				   , USE_YN
				   , REG_NO
				   , REG_DT
				   , UPD_NO
				   , UPD_DT
		FROM TB_TMTB_BURDEN
		WHERE 1=1
		  AND TMTB_SQ = #{tmtbSq}
		  AND USE_YN = 'Y'
	</select>

	<insert id="saveMorebetterMst" parameterType="MoreBetter" keyProperty="tmtbSq">
		/* TsaMorebetter.saveMorebetterMst */
		INSERT INTO TB_TMTB(
		        TMTB_SQ
		        ,TMTB_NM
				,TMTB_STAT
				,TMTB_ST_DT
				,TMTB_ED_DT
				,APPLY_GB
				,MULTI_YN
				,DEL_YN
				,REG_NO
				,REG_DT
				,UPD_NO
				,UPD_DT
		) VALUES (
			#{tmtbSq}
		    ,#{tmtbNm}
			,#{tmtbStat}
			,DATE_FORMAT(#{tmtbStdt},'%Y-%m-%d %H:%i:%s')
			,DATE_FORMAT(#{tmtbEddt},'%Y-%m-%d %H:%i:%s')
			,#{applyGb}
			,'Y'
			,'N'
			,#{regNo}
			,NOW()
			,#{updNo}
			,NOW()
		)
		ON DUPLICATE KEY UPDATE
			TMTB_NM = #{tmtbNm}
			,TMTB_STAT = #{tmtbStat}
			,TMTB_ST_DT = #{tmtbStdt}
			,TMTB_ED_DT = #{tmtbEddt}
			,APPLY_GB = #{applyGb}
			,UPD_NO = #{updNo}
			,UPD_DT = NOW()
	</insert>

	<insert id="saveMorebetterGoods" parameterType="MoreBetterGoods" keyProperty="tmtbGoodsSq">
		/* TsaMorebetter.saveMorebetterGoods */
		INSERT INTO TB_TMTB_GOODS(
		         	 TMTB_GOODS_SQ
		        	,TMTB_SQ
					,GOODS_GB
					,TARGET_GB
					,TARGET_VAL
					,DEL_YN
					,REG_NO
					,REG_DT
					,UPD_NO
					,UPD_DT
			)
			VALUES (
			         #{tmtbGoodsSq}
			        ,#{tmtbSq}
				    ,#{goodsGb}
				    ,#{targetGb}
				    ,#{targetVal}
				    ,'N'
				    ,#{regNo}
				    ,NOW()
				    ,#{updNo}
				    ,NOW()
				   )
			ON DUPLICATE KEY UPDATE
						 GOODS_GB = #{goodsGb}
						,TARGET_GB = #{targetGb}
						,TARGET_VAL = #{targetVal}
			    		,DEL_YN = #{delYn}
						,UPD_NO = #{updNo}
						,UPD_DT = NOW()
	</insert>

	<insert id="saveMorebetterApplyGoods" parameterType="MoreBetterGoods" keyProperty="tmtbApplyGoodsSq">
		/* TsaMorebetter.saveMorebetterApplyGoods */
		INSERT INTO TB_TMTB_APPLY_GOODS(
					TMTB_APPLY_GOODS_SQ
					,TMTB_SQ
					,GOODS_GB
					,GOODS_CD
					,DEL_YN
					,REG_NO
					,REG_DT
					,UPD_NO
					,UPD_DT
		)
		VALUES (
				#{tmtbApplyGoodsSq}
			   ,#{tmtbSq}
			   ,#{goodsGb}
			   ,#{goodsCd}
			   ,'N'
			   ,#{regNo}
			   ,NOW()
			   ,#{updNo}
			   ,NOW()
			   )
			ON DUPLICATE KEY UPDATE
								 GOODS_GB = #{goodsGb}
								 ,GOODS_CD = #{goodsCd}
								 ,UPD_NO = #{updNo}
								 ,UPD_DT = NOW()
	</insert>

	<insert id="saveMorebetterSection" parameterType="MoreBetterSection" keyProperty="tmtbSectionSq">
		/* TsaMorebetter.saveMorebetterSection */
		INSERT INTO TB_TMTB_SECTION(
					 TMTB_SECTION_SQ
					,TMTB_SQ
					,SECTION_GB
					,SECTION_VAL
					,DEL_YN
					,REG_NO
					,REG_DT
					,UPD_NO
					,UPD_DT
			)
			VALUES (
					 #{tmtbSectionSq}
					,#{tmtbSq}
					,#{sectionGb}
				    ,#{sectionVal}
				    ,'N'
				    ,#{regNo}
				    ,NOW()
				    ,#{updNo}
				    ,NOW()
				   )
			ON DUPLICATE KEY UPDATE
						 SECTION_GB = #{sectionGb}
						,SECTION_VAL = #{sectionVal}
						,DEL_YN = #{delYn}
						,UPD_NO = #{updNo}
						,UPD_DT = NOW()
	</insert>

	<insert id="saveMorebetterVal" parameterType="MoreBetterSection" keyProperty="tmtbValSq">
		/* TsaMorebetter.saveMorebetterVal */
		INSERT INTO TB_TMTB_VAL(
					 TMTB_VAL_SQ
					,TMTB_SQ
					,TMTB_SECTION_SQ
					,DC_WAY
					,DC_VAL
					,DEL_YN
					,REG_NO
					,REG_DT
					,UPD_NO
					,UPD_DT
			)
			VALUES (
					#{tmtbValSq}
				   ,#{tmtbSq}
				   ,#{tmtbSectionSq}
				   ,#{dcWay}
				   ,#{dcVal}
				   ,'N'
				   ,#{regNo}
				   ,NOW()
				   ,#{updNo}
				   ,NOW()
				   )
			ON DUPLICATE KEY UPDATE
						 DC_WAY = #{dcWay}
						,DC_VAL = #{dcVal}
						,DEL_YN = #{delYn}
						,UPD_NO = #{updNo}
						,UPD_DT = NOW()
	</insert>

	<insert id="saveMorebetterBurden" parameterType="MoreBetterBurden" keyProperty="tbTmtbBurdenSq">
		/* TsaMorebetter.saveMorebetterBurden */
		INSERT INTO TB_TMTB_BURDEN(
								  TB_TMTB_BURDEN_SQ
		                          ,TMTB_SQ
								  ,TMTB_SUPPLY_CD
								  ,TMTB_BURDEN_RATE
								  ,USE_YN
								  ,REG_NO
								  ,REG_DT
								  ,UPD_NO
								  ,UPD_DT
		) VALUES (
			    #{tbTmtbBurdenSq}
		       ,#{tmtbSq}
			   ,#{tmtbSupplyCd}
			   ,#{tmtbBurdenRate}
			   ,#{useYn}
			   ,#{regNo}
			   ,NOW()
			   ,#{updNo}
			   ,NOW()
			   )
			ON DUPLICATE KEY UPDATE
								 TMTB_SUPPLY_CD = #{tmtbSupplyCd}
								 ,TMTB_BURDEN_RATE = #{tmtbBurdenRate}
								 ,USE_YN = #{useYn}
								 ,UPD_NO = #{updNo}
								 ,UPD_DT = NOW()
	</insert>

	<delete id="deleteTmtbSupplyCompanyList" parameterType="MoreBetterGoods">
		/* TsaMorebetter.deleteTmtbSupplyCompanyList */
		DELETE FROM TB_TMTB_GOODS
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrCompanySq != null and arrCompanySq.length>0">
			AND TMTB_GOODS_SQ IN
			<foreach collection="arrCompanySq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbBrandList" parameterType="MoreBetterGoods">
		/* TsaMorebetter.deleteTmtbBrandList */
		DELETE FROM TB_TMTB_GOODS
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrBrandSq != null and arrBrandSq.length>0">
			AND TMTB_GOODS_SQ IN
			<foreach collection="arrBrandSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbApplyGoodsList" parameterType="MoreBetterGoods">
		/* TsaMorebetter.deleteTmtbApplyGoodsList */
		DELETE FROM TB_TMTB_GOODS
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrApplyGoodsSq != null and arrApplyGoodsSq.length>0">
			AND TMTB_GOODS_SQ IN
			<foreach collection="arrApplyGoodsSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbExceptGoodsList" parameterType="MoreBetterGoods">
		/* TsaMorebetter.deleteTmtbExceptGoodsList */
		DELETE FROM TB_TMTB_GOODS
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrExceptGoodsSq != null and arrExceptGoodsSq.length>0">
			AND TMTB_GOODS_SQ IN
			<foreach collection="arrExceptGoodsSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbSectionList" parameterType="MoreBetterSection">
		/* TsaMorebetter.deleteTmtbSectionList */
		DELETE FROM TB_TMTB_SECTION
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrSectionSq != null and arrSectionSq.length>0">
			AND TMTB_SECTION_SQ IN
			<foreach collection="arrSectionSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbValList" parameterType="MoreBetterSection">
		/* TsaMorebetter.deleteTmtbValList */
		DELETE FROM TB_TMTB_VAL
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrSectionSq != null and arrSectionSq.length>0">
			AND TMTB_SECTION_SQ IN
			<foreach collection="arrSectionSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteTmtbBurdenList" parameterType="MoreBetterGoods">
		/* TsaMorebetter.deleteTmtbBurdenList */
		DELETE FROM TB_TMTB_BURDEN
		WHERE TMTB_SQ =  #{tmtbSq}
		<if test="arrBurdenSq != null and arrBurdenSq.length>0">
			AND TB_TMTB_BURDEN_SQ IN
			<foreach collection="arrBurdenSq" item="item" index="index"  open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<select id="getMorebetterDuplicateList" parameterType="MoreBetterGoods" resultType="MoreBetterGoods">
		/* TsaMorebetter.getMorebetterDuplicateList */
		SELECT 	REG_NO
				, GOODS_CD
				, GOODS_NM
		 FROM	TB_TMTB_APPLY_GOODS_TEMP
		WHERE	1=1
		  AND	REG_NO = #{regNo}
		  AND	GOODS_CD NOT IN (
							SELECT	GOODS_CD
							FROM	TB_TMTB_APPLY_GOODS
							WHERE	TMTB_SQ = #{tmtbSq}
							)
	</select>

	<update id="updateMorebetterStat" parameterType="MoreBetter">
		/* TsaMorebetter.updateMorebetterStat */
		UPDATE TB_TMTB SET
			TMTB_STAT = #{tmtbStat}
		<if test="tmtbStat == 'G232_14'">
			, DEL_YN = 'Y'
		</if>
			, UPD_NO = #{updNo}
			, UPD_DT = NOW()
		WHERE TMTB_SQ = #{tmtbSq}
	</update>

	<update id="deleteTmtbGoods" parameterType="MoreBetter">
		/* TsaMorebetter.deleteTmtbGoods */
		UPDATE TB_TMTB_GOODS SET
			DEL_YN = 'Y'
			, UPD_NO = #{updNo}
			, UPD_DT = CURRENT_TIMESTAMP
		WHERE TMTB_SQ = #{tmtbSq}
		  AND DEL_YN = 'N'
	</update>

	<update id="deleteTmtbBurden" parameterType="MoreBetter">
		/* TsaMorebetter.deleteTmtbBurden */
		UPDATE TB_TMTB_BURDEN SET
			USE_YN = 'N'
			, UPD_NO = #{updNo}
			, UPD_DT = CURRENT_TIMESTAMP
		WHERE TMTB_SQ = #{tmtbSq}
		  AND USE_YN = 'Y'
	</update>

	<update id="deleteTmtbSection" parameterType="MoreBetter">
		/* TsaMorebetter.deleteTmtbSection */
		UPDATE TB_TMTB_SECTION SET
			DEL_YN = 'Y'
			, UPD_NO = #{updNo}
			, UPD_DT = CURRENT_TIMESTAMP
		WHERE TMTB_SQ = #{tmtbSq}
		  AND DEL_YN = 'N'
	</update>

	<update id="deleteTmtbVal" parameterType="MoreBetter">
		/* TsaMorebetter.deleteTmtbVal */
		UPDATE TB_TMTB_VAL SET
			DEL_YN = 'Y'
			, UPD_NO = #{updNo}
			, UPD_DT = CURRENT_TIMESTAMP
		WHERE TMTB_SQ = #{tmtbSq}
		  AND DEL_YN = 'N'
	</update>

	<delete id="deleteTmtbApplyGoods" parameterType="MoreBetter">
		/* TsaMarketing.deleteTmtbApplyGoods */
		DELETE FROM TB_TMTB_APPLY_GOODS
		WHERE	TMTB_SQ = #{tmtbSq}
	</delete>

	<insert id="insertMorebetterApplyGoods" parameterType="MoreBetterGoods">
		/* TsaMarketing.insertMorebetterApplyGoods */
		<selectKey keyProperty="tmtbApplyGoodsSq" resultType="int" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		INSERT INTO TB_TMTB_APPLY_GOODS(
			  TMTB_SQ
			, GOODS_GB
			, GOODS_CD
			, DEL_YN
			, REG_NO
			, REG_DT
			, UPD_NO
			, UPD_DT
		)
		SELECT #{tmtbSq} AS TMTB_SQ
				, 'G800_10' AS GOODS_GB
				, Z.GOODS_CD AS GOODS_CD
				, 'N' AS DEL_YN
				, #{regNo} AS REG_NO
				, NOW() AS REG_DT
				, #{updNo} AS UPD_NO
				, NOW() AS UPD_DT
		FROM	(
			SELECT T.GOODS_CD
			FROM (
				<if test="multiSupplyCompCd!= null and multiSupplyCompCd.length>0">
					SELECT GOODS_CD
					FROM TB_GOODS
					WHERE SUPPLY_COMP_CD IN
					<foreach collection="multiSupplyCompCd" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
					UNION ALL
				</if>
				<if test="multiBrand!= null and multiBrand.length>0">
					SELECT	GOODS_CD
					FROM	TB_GOODS
					WHERE 	BRAND_CD IN
					<foreach collection="multiBrand" item="item" open="(" separator="," close=")">
						#{item}
					</foreach>
					UNION ALL
				</if>
				${applySql}
			) T
			WHERE	1=1
			<if test="multiExceptGoods!= null and multiExceptGoods.length>0">
			AND 	T.GOODS_CD NOT IN ( ${exceptSql} )
			</if>
		) Z
		WHERE	1=1
		AND 	Z.GOODS_CD NOT IN (
									SELECT	b.TARGET_VAL
									FROM 	TB_TMTB A
									LEFT OUTER JOIN TB_TMTB_GOODS B
									ON A.TMTB_SQ = B.TMTB_SQ
									WHERE	A.TMTB_ST_DT <![CDATA[<=]]> NOW()
									AND 	A.TMTB_ED_DT <![CDATA[>=]]> NOW()
									AND 	A.TMTB_STAT = 'G232_11'
		                            <if test="tmtbSq != null and tmtbSq != ''">
									AND     A.TMTB_SQ <![CDATA[<>]]> #{tmtbSq}
									</if>
									AND 	B.GOODS_GB <![CDATA[<>]]> 'G800_30'
								)
	</insert>

	<delete id="deleteMorebetterApplyGoodsTemp" parameterType="MoreBetter">
		/* TsaMorebetter.deleteMorebetterApplyGoodsTemp */
		DELETE FROM TB_TMTB_APPLY_GOODS_TEMP
		WHERE	REG_NO = #{regNo}
	</delete>

	<insert id="insertMorebetterApplyGoodsTemp" parameterType="MoreBetter">
		/* TsaMorebetter.insertMorebetterApplyGoodsTemp */
		INSERT INTO TB_TMTB_APPLY_GOODS_TEMP
		SELECT	T.*
		FROM (
			SELECT	#{regNo} AS REG_NO
					, GOODS_CD
					, GOODS_NM
			FROM	TB_GOODS
			WHERE	1=1
			<if test="multiApplyGoods != null and multiApplyGoods.length>0">
				AND GOODS_CD IN
				<foreach collection="multiApplyGoods" item="item" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
		) T
		WHERE	1=1
		<if test="multiExceptGoods != null and multiExceptGoods.length>0">
		AND 	T.GOODS_CD NOT IN (
		    						SELECT	GOODS_CD
		    						FROM	TB_GOODS
		    						WHERE	1=1
		    						AND 	GOODS_CD IN
									<foreach collection="multiExceptGoods" item="item" open="(" separator="," close=")">
										#{item}
									</foreach>
								)
		</if>
	</insert>
	<!--// CSB 진행 -->
</mapper>