<?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.batch.biz.dao.TsbGoodsDao">

	<!-- 상품 기본정보 이력 생성 -->
	<insert id="createGoodsHst" parameterType="Goods">
		/* TsbGoods.createGoodsHst */
		INSERT INTO TB_GOODS_HST
		(       GOODS_CD
		      , BRAND_CD
		      , ITEMKIND_CD
		      , GOODS_NM
		      , GOODS_TNM
		      , GOODS_SNM
		      , GOODS_SNM1
		      , MAIN_COLOR_CD
		      , STYLE_YEAR
		      , SEASON_CD
		      , SEX_GB
		      , GOODS_NUM
		      , GOODS_TYPE
		      , LIST_PRICE
		      , CURR_PRICE
		      , CURR_BPRICE
		      , PRICE_UPD_DT
		      , COST_PRICE
		      , DC_RATE
		      , GOODS_STAT
		      , SELL_STDT
		      , SELL_EDDT
		      , SELF_MALL_YN
		      , GOODS_GB
		      , FOREIGN_BUY_YN
		      , PARALLEL_IMPORT_YN
		      , ORDER_MADE_YN
		      , DISTRIBUTION_GB
		      , SELF_GOODS_YN
		      , SUPPLY_COMP_CD
		      , SUPPLY_GOODS_CD
		      , AGE_GRP_CD
		      , DELV_LOC_CD
		      , DELV_FEE_CD
		      , PNT_PRATE
		      , PNT_MRATE
		      , SELL_FEE_RATE
		      , FORMAL_GB
		      , CHANGEABLE_YN
		      , RETURNABLE_YN
		      , CHANGE_FEE_FREE_YN
		      , RETURN_FEE_FREE_YN
		      , PRE_PPNT_USABLE_YN
		      , PRE_MPNT_USABLE_YN
		      , MIN_ORD_QTY
		      , MAX_ORD_QTY
		      , DAY_MAX_ORD_QTY
		      , GIFT_PACK_YN
		      , FRST_CFRM_DT
		      , ORIGIN_CD
		      , MAKE_YMD
		      , TAX_GB
		      , ERP_PRICE_LINK_YN
		      , ERP_STOCK_LINK_YN
		      , NEW_CUST_ORD_YN
		      , NEW_CUST_ORD_STDT
		      , NEW_CUST_ORD_EDDT
		      , ADULT_YN
		      , TOBE_FORM_YN
		      , REG_NO
		      , REG_DT
		      , UPD_NO
		      , UPD_DT
		)
		SELECT GOODS_CD
		     , BRAND_CD
		     , ITEMKIND_CD
		     , GOODS_NM
		     , GOODS_TNM
		     , GOODS_SNM
		     , GOODS_SNM1
		     , MAIN_COLOR_CD
		     , STYLE_YEAR
		     , SEASON_CD
		     , SEX_GB
		     , GOODS_NUM
		     , GOODS_TYPE
		     , LIST_PRICE
		     , CURR_PRICE
		     , CURR_BPRICE
		     , PRICE_UPD_DT
		     , COST_PRICE
		     , DC_RATE
		     , GOODS_STAT
		     , SELL_STDT
		     , SELL_EDDT
		     , SELF_MALL_YN
		     , GOODS_GB
		     , FOREIGN_BUY_YN
		     , PARALLEL_IMPORT_YN
		     , ORDER_MADE_YN
		     , DISTRIBUTION_GB
		     , SELF_GOODS_YN
		     , SUPPLY_COMP_CD
		     , SUPPLY_GOODS_CD
		     , AGE_GRP_CD
		     , DELV_LOC_CD
		     , DELV_FEE_CD
		     , PNT_PRATE
		     , PNT_MRATE
		     , SELL_FEE_RATE
		     , FORMAL_GB
		     , CHANGEABLE_YN
		     , RETURNABLE_YN
		     , CHANGE_FEE_FREE_YN
		     , RETURN_FEE_FREE_YN
		     , PRE_PPNT_USABLE_YN
		     , PRE_MPNT_USABLE_YN
		     , MIN_ORD_QTY
		     , MAX_ORD_QTY
		     , DAY_MAX_ORD_QTY
		     , GIFT_PACK_YN
		     , FRST_CFRM_DT
		     , ORIGIN_CD
		     , MAKE_YMD
		     , TAX_GB
		     , ERP_PRICE_LINK_YN
		     , ERP_STOCK_LINK_YN
		     , NEW_CUST_ORD_YN
		     , NEW_CUST_ORD_STDT
		     , NEW_CUST_ORD_EDDT
		     , ADULT_YN
		     , TOBE_FORM_YN
		     , #{regNo}
		     , NOW()
		     , UPD_NO
		     , UPD_DT
		FROM TB_GOODS
		WHERE 1 = 1
		<choose>
		      <when test='procJob != null and procJob == "G"'>
		AND GOODS_CD = #{goodsCd}
		      </when>
		      <when test='procJob != null and procJob == "GTE"'>
		AND GOODS_CD IN (
		                 SELECT G.GOODS_CD
		                 FROM TB_GOODS G
		                     ,(
		                       SELECT GOODS_CD
		                            , GOODS_TNM
		                       FROM (
		                             SELECT GT.GOODS_CD
		                                  , GT.GOODS_TNM
		                                  , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_TNM_RES_SQ DESC) RK
		                             FROM TB_GOODS_TNM_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                            ) Z
		                       WHERE RK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND G.GOODS_TNM = S.GOODS_TNM
		                )
		      </when>
		      <when test='procJob != null and procJob == "GTS"'>
		AND GOODS_CD IN (
		                 SELECT G.GOODS_CD
		                 FROM TB_GOODS G
		                     ,(
		                       SELECT GOODS_CD
		                            , GOODS_TNM
		                       FROM (
		                             SELECT GT.GOODS_CD
		                                  , GT.GOODS_TNM
		                                  , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_TNM_RES_SQ DESC) RK
		                             FROM TB_GOODS_TNM_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                            ) Z
		                       WHERE RK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND (G.GOODS_TNM IS NULL OR G.GOODS_TNM <![CDATA[<>]]> S.GOODS_TNM)
		                )
		      </when>
		      <when test='procJob != null and procJob == "GPE"'>
		AND GOODS_CD IN (
		                 SELECT G.GOODS_CD
		                 FROM TB_GOODS G
		                     ,(
		                       SELECT GOODS_CD
		                            , RES_GOODS_PRICE
		                       FROM (
		                             SELECT GT.GOODS_CD
		                                  , GT.RES_GOODS_PRICE
		                                  , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_PRICE_RES_SQ DESC) RK
		                             FROM TB_GOODS_PRICE_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                            ) Z
		                       WHERE RK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND G.CURR_PRICE = S.RES_GOODS_PRICE

		                )
		      </when>
		      <when test='procJob != null and procJob == "GPS"'>
		AND GOODS_CD IN (
		                 SELECT G.GOODS_CD
		                 FROM TB_GOODS G
		                     ,(
		                       SELECT GOODS_CD
		                            , RES_GOODS_PRICE
		                       FROM (
		                             SELECT GT.GOODS_CD
		                                  , GT.RES_GOODS_PRICE
		                                  , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_PRICE_RES_SQ DESC) RK
		                             FROM TB_GOODS_PRICE_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                            ) Z
		                       WHERE RK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND (G.CURR_PRICE <![CDATA[<>]]> S.RES_GOODS_PRICE)
		                )
		      </when>
		      <otherwise>
		AND 1 = 2
		      </otherwise>
		    </choose>

	</insert>

	<!-- 상품 타이틀 예약건 초기화 -->
	<update id="updateGoodTnmInit">
		/* TsbGoods.updateGoodTnmInit */
		WITH TAB AS (
		   SELECT P.GOODS_CD
		   FROM TB_GOODS P
		       ,(SELECT GOODS_CD
		              , GOODS_TNM
		         FROM (
		              SELECT RS.GOODS_CD
		                   , RS.GOODS_TNM
		                   , RANK() OVER(PARTITION BY RS.GOODS_CD ORDER BY RS.GOODS_TNM_RES_SQ DESC) RK
		              FROM TB_GOODS_TNM_RES RS
		              WHERE RS.APPLY_EDDT <![CDATA[<]]> NOW()
		              AND RS.APPLY_YN = 'N'
		              ) Z
		         WHERE RK = 1
		        ) TS
		   WHERE  P.GOODS_CD = TS.GOODS_CD
		)
		UPDATE TB_GOODS A
		SET A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		  , A.GOODS_TNM = (SELECT S.GOODS_TNM
		                   FROM (
		                       SELECT GT.GOODS_CD
		                            , GT.GOODS_TNM
		                            , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_TNM_RES_SQ DESC) RK
		                       FROM TB_GOODS_TNM_RES GT
		                       WHERE GT.APPLY_YN = 'N'
		                       AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                      ) S
		                   WHERE S.RK = 1
		                   AND S.GOODS_CD = A.GOODS_CD
		                   )
		WHERE A.GOODS_CD IN (SELECT GOODS_CD FROM TAB)
	</update>

	<!-- 상품예약 종료처리 -->
	<update id="updateGoodsTnmRes">
		/* TsbGoods.updateGoodsTnmRes */
		UPDATE TB_GOODS_TNM_RES
		   SET APPLY_YN = 'Y'
		     , UPD_NO = 99999
		     , UPD_DT = NOW()
		WHERE APPLY_EDDT <![CDATA[<]]> NOW()
		AND APPLY_YN = 'N'
	</update>

	<!-- 상품타이틀 변경 -->
	<update id="updateGoodsTnm" timeout="600">
		/* TsbGoods.updateGoodsTnm */
		WITH TAB AS (
		    SELECT RS.GOODS_CD
		         , RS.GOODS_TNM
		    FROM   (
		            SELECT RS.GOODS_CD
		                 , RS.GOODS_TNM
		                 , RANK() OVER(PARTITION BY RS.GOODS_CD ORDER BY RS.GOODS_TNM_RES_SQ DESC) RK
		            FROM   TB_GOODS_TNM_RES RS
		            WHERE  1 = 1
		                AND    NOW() BETWEEN RS.APPLY_STDT AND RS.APPLY_EDDT
		            AND    RS.APPLY_YN = 'N'
		           ) RS
		         , TB_GOODS G
		    WHERE  RS.GOODS_CD = G.GOODS_CD
		    AND    RS.RK = 1
		    AND    (RS.GOODS_TNM <![CDATA[<>]]> G.GOODS_TNM OR G.GOODS_TNM IS NULL)
		)
		UPDATE TB_GOODS A
		SET    GOODS_TNM = (SELECT GOODS_TNM FROM TAB WHERE GOODS_CD = A.GOODS_CD)
		     , A.UPD_NO = 99999
		     , A.UPD_DT = NOW()
		WHERE  GOODS_CD IN (SELECT GOODS_CD FROM TAB)
	</update>

	<!-- 상품 가격 예약건 초기화 -->
	<update id="updateGoodPriceInit">
		/* TsbGoods.updateGoodPriceInit */
		UPDATE TB_GOODS A, (SELECT P.GOODS_CD
		                         , TS.END_GOODS_PRICE
		                         , P.CURR_PRICE
		                         , 100 - ROUND((TS.END_GOODS_PRICE / P.LIST_PRICE) * 100 ,0) AS DC_RATE
		                    FROM TB_GOODS P
		                      , (SELECT GOODS_CD
		                              , END_GOODS_PRICE
		                         FROM (SELECT GT.GOODS_CD
		                                    , GT.END_GOODS_PRICE
		                                    , RANK() OVER(PARTITION BY GT.GOODS_CD ORDER BY GT.GOODS_PRICE_RES_SQ DESC) RK
		                               FROM TB_GOODS_PRICE_RES GT
		                               WHERE GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                               AND GT.APPLY_YN = 'N'
		                              ) S
		                         WHERE S.RK = 1
		                         ) TS
		                    WHERE P.GOODS_CD = TS.GOODS_CD
		                    ) B
		SET A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		  , A.CURR_PRICE = B.END_GOODS_PRICE
		  , A.CURR_BPRICE = B.CURR_PRICE
		  , A.DC_RATE = B.DC_RATE
		  , A.PRICE_UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품 가격 예약 종료처리 -->
	<update id="updateGoodsPriceRes">
		/* TsbGoods.updateGoodsPriceRes */
		UPDATE TB_GOODS_PRICE_RES
		   SET APPLY_YN = 'Y'
		     , UPD_NO = 99999
		     , UPD_DT = NOW()
		WHERE APPLY_EDDT <![CDATA[<]]> NOW()
		AND APPLY_YN = 'N'
	</update>

	<!-- 상품 가격 변경 -->
	<update id="updateGoodsPrice">
		/* TsbGoods.updateGoodsPrice */
		UPDATE TB_GOODS A, (SELECT P.GOODS_CD
		                         , S.RES_GOODS_PRICE
		                         , P.CURR_PRICE
		                         , 100 - ROUND((S.RES_GOODS_PRICE / P.LIST_PRICE) * 100 ,0) AS DC_RATE
		                    FROM TB_GOODS P
		                       , (
		                          SELECT GOODS_CD
		                               , RES_GOODS_PRICE
		                          FROM (
		                                SELECT RS.GOODS_CD
		                                     , RS.RES_GOODS_PRICE
		                                     , RANK() OVER(PARTITION BY RS.GOODS_CD ORDER BY RS.GOODS_PRICE_RES_SQ DESC) RK
		                                FROM TB_GOODS_PRICE_RES RS
		                                WHERE NOW() BETWEEN RS.APPLY_STDT AND RS.APPLY_EDDT
		                                AND RS.APPLY_YN = 'N'
		                                ) Z
		                          WHERE RK = 1
		                         ) S
		                    WHERE P.GOODS_CD = S.GOODS_CD
		                    AND P.CURR_PRICE <![CDATA[<>]]> S.RES_GOODS_PRICE
		                 ) B
		SET A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		  , A.CURR_PRICE = B.RES_GOODS_PRICE
		  , A.CURR_BPRICE = B.CURR_PRICE
		  , A.DC_RATE = B.DC_RATE
		  , A.PRICE_UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!--상품 검색어 작업 - 사용자검색어 없는 상품 일괄 -->
	<update id="updateGoodsSnmByGoodsSnm1Null" timeout="360">
		/* TsbGoods.updateGoodsSnmByGoodsSnm1Null */
		UPDATE TB_GOODS A, (
		                    WITH TMP_COLOR_GOODS AS (
		                       SELECT GOODS_CD
		                            , REPLACE(CONCAT(GROUP_CONCAT(COLOR_GRP_CD),';',GROUP_CONCAT(COLOR_ENM),';',GROUP_CONCAT(CD_NM)),',',';') AS COLOR_INFO
		                       FROM (
		                           SELECT DISTINCT G.GOODS_CD, C.COLOR_GRP_CD, C.COLOR_ENM, D.CD_NM
		                           FROM TB_GOODS G
		                           LEFT OUTER JOIN TB_OPTION E ON G.GOODS_CD = E.GOODS_CD
		                           LEFT OUTER JOIN TB_COLOR C ON E.OPT_CD1 = C.COLOR_CD
		                                                      AND C.USE_YN= 'Y'
		                           LEFT OUTER JOIN TB_COMMON_CODE D ON C.COLOR_GRP_CD = D.CD
		                                                      AND D.USE_YN= 'Y'
		                           WHERE G.GOODS_SNM1 IS NULL
		                       ) Z
		                       GROUP BY GOODS_CD
		                   )
		                   SELECT GOODS_CD
		                        , GOODS_SNM_COMP
		                   FROM (
		                       SELECT DISTINCT Y.GOODS_CD
		                            , Y.GOODS_SNM_COMP
		                            , Y.GOODS_SNM
		                       FROM (
		                           SELECT G.GOODS_CD
		                                , G.GOODS_SNM
		                                , UPPER(CONCAT(
		                                  G.GOODS_CD,';',
		                                  REPLACE(G.GOODS_NM,' ',''),';',
		                                  B.BRAND_ENM,';',
		                                  B.BRAND_KNM,';',
		                                  (CASE WHEN D.DISP_NM_LANG = 'EN' THEN D.BRAND_GROUP_ENM ELSE D.BRAND_GROUP_KNM END),';',
		                                  IF(NULLIF(G.STYLE_YEAR,'') IS NULL, '' ,CONCAT(G.STYLE_YEAR,';')),
		                                  IF(NULLIF(G.SEASON_CD, '') IS NULL, '' ,CONCAT(FN_GET_CODE_NM('G006',G.SEASON_CD),';')) ,
		                                  IF(NULLIF( G.SEX_GB,'') IS NULL, '' ,CONCAT(FN_GET_CODE_NM('G007',G.SEX_GB),';')) ,
		                                  IFNULL(COALESCE(D.COLOR_INFO,CONCAT(D.COLOR_INFO,';'),''),''),
		                                  IFNULL(REPLACE(I.ITEMKIND_NM,'>',';'),'')
		                                  )) AS GOODS_SNM_COMP
		                           FROM TB_GOODS G
		                           INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                           INNER JOIN TB_BRAND_GROUP D ON B.BRAND_GROUP_NO  = D.BRAND_GROUP_NO
		                           LEFT OUTER JOIN TB_ITEMKIND I ON G.ITEMKIND_CD = I.ITEMKIND_CD
		                           LEFT OUTER JOIN TMP_COLOR_GOODS D ON G.GOODS_CD = D.GOODS_CD
		                           WHERE G.GOODS_SNM1 IS NULL
		                            ) Y
		                        )X
		                        <![CDATA[
		                        WHERE X.GOODS_SNM_COMP <> IFNULL(X.GOODS_SNM,'1')
		                        ]]>
		                    ) B
		SET A.GOODS_SNM = B.GOODS_SNM_COMP
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 사용자검색어가 있는 상품 조회  -->
	<select id="getGoodsByGooodsSnm1List"  resultType="Goods" timeout="360">
		/* TsbGoods.getGoodsByGooodsSnm1List */
		WITH TMP_COLOR_GOODS AS (
		    SELECT Z.GOODS_CD
		         , REPLACE(CONCAT(GROUP_CONCAT(COLOR_GRP_CD),';',GROUP_CONCAT(COLOR_ENM),';',GROUP_CONCAT(CD_NM)),',',';') AS COLOR_INFO
		    FROM (
		        SELECT DISTINCT G.GOODS_CD, C.COLOR_GRP_CD, C.COLOR_ENM, D.CD_NM
		        FROM TB_GOODS G
		        LEFT OUTER JOIN TB_OPTION E ON G.GOODS_CD = E.GOODS_CD
		        LEFT OUTER JOIN TB_COLOR C ON E.OPT_CD1 = C.COLOR_CD
		                                   AND C.USE_YN= 'Y'
		        LEFT OUTER JOIN TB_COMMON_CODE D ON C.COLOR_GRP_CD = D.CD
		                                   AND D.USE_YN= 'Y'
		        WHERE G.GOODS_SNM1 IS NOT NULL
		        ) Z
		        GROUP BY Z.GOODS_CD
		)
		SELECT G.GOODS_CD
		     , G.GOODS_SNM1
		     , UPPER(CONCAT(
		       G.GOODS_CD,';',
		       REPLACE(G.GOODS_NM,' ',''),';',
		       B.BRAND_ENM,';',
		       B.BRAND_KNM,';',
		       (CASE WHEN D.DISP_NM_LANG = 'EN' THEN D.BRAND_GROUP_ENM ELSE D.BRAND_GROUP_KNM END),';',
		       IF(NULLIF(G.STYLE_YEAR,'') IS NULL, '' ,CONCAT(G.STYLE_YEAR,';')),
		       IF(NULLIF(G.SEASON_CD, '') IS NULL, '' ,CONCAT(FN_GET_CODE_NM('G006',G.SEASON_CD),';')) ,
		       IF(NULLIF( G.SEX_GB,'') IS NULL, '' ,CONCAT(FN_GET_CODE_NM('G007',G.SEX_GB),';')) ,
		       IFNULL(COALESCE(D.COLOR_INFO,CONCAT(D.COLOR_INFO,';'),''),''),
		       REPLACE(I.ITEMKIND_NM,'>',';')
		       )) AS GOODS_SNM
		FROM TB_GOODS G
		INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		INNER JOIN TB_BRAND_GROUP D ON B.BRAND_GROUP_NO  = D.BRAND_GROUP_NO
		LEFT OUTER JOIN TB_ITEMKIND I ON G.ITEMKIND_CD = I.ITEMKIND_CD
		LEFT OUTER JOIN TMP_COLOR_GOODS D ON G.GOODS_CD = D.GOODS_CD
		WHERE G.GOODS_SNM1 IS NOT NULL
	</select>

	<!--상품 검색어 작업-   -->
	<update id="updateGoodsSnm" parameterType="Goods">
		/* TsbGoods.updateGoodsSnm */
		UPDATE TB_GOODS A
		SET GOODS_SNM = #{goodsSnm}
		WHERE GOODS_CD = #{goodsCd}
	</update>

	<!-- 상품 혜택가 목록 -->
	<sql id="getGoodsBenefitList_sql">
		WITH CTE AS (
		SELECT CPN_REFVAL_SQ
		FROM TB_COUPON CP
		INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_ID=CPR.CPN_ID
		WHERE  CP.SITE_CD = 'G000_10'
		AND    CP.CPN_STAT = 'G232_11'   -- 쿠폰인 진행중인 쿠폰만
		AND    NOW() BETWEEN CP.AVAIL_STDT AND CP.AVAIL_EDDT
		AND    CP.CPN_TYPE = 'G230_10' /*즉시할인쿠폰*/
		AND    CP.DC_CD_GB = 'G233_00'		         -- 일반유형(할인쿠폰구분)
		AND    CP.APPLY_SCOPE = 'I'
		AND 	CPR.DEL_YN = 'N'
		)
		, CTE_DETAIL AS (
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.GOODS_CD GKEY, G.GOODS_CD
		        FROM CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.GOODS_CD
		        WHERE CPR.CPN_TARGET = 'G260_10'

		        UNION

		        /* 적용대상:카테고리*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.GOODS_CD GKEY, G.GOODS_CD
		        FROM  CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN
		                    (SELECT LEAF_CATE_NO
		                          , CATE_LVL
		                          , CATE_NO
		                     FROM (
		                    	   SELECT LEAF_CATE_NO ,
		                                CASE WHEN X = 1 THEN 'CATE1_NO'  -- 컬럼과 매핑(컬럼명)
		                                     WHEN X = 2 THEN 'CATE2_NO'
		                                     WHEN X = 3 THEN 'CATE3_NO'
		                                     WHEN X = 4 THEN 'CATE4_NO'
		                                     ELSE  'CATE5_NO'
		                                END CATE_LVL,
		                                CASE WHEN X = 1 THEN CATE1_NO  -- 컬럼과 매핑(컬럼 데이터)
		                                     WHEN X = 2 THEN CATE2_NO
		                                     WHEN X = 3 THEN CATE3_NO
		                                     WHEN X = 4 THEN CATE4_NO
		                                     ELSE  CATE5_NO
		                                 END CATE_NO
		                           FROM (
		                                  ( SELECT LEAF_CATE_NO
		                                         , CATE1_NO
		                                         , CATE2_NO
		                                         , CATE3_NO
		                                         , CATE4_NO
		                                         , CATE5_NO
		                                    FROM  TB_CATE_4SRCH
		                                    WHERE SITE_CD = 'G000_10'
		                                    AND CATE_TYPE = 'G031_10') A, -- 상품타입
		                                  (SELECT 1 AS X		-- UNPIVOT 컬럼 수 만큼 선언
		                                   UNION ALL SELECT 2 AS X
		                                   UNION ALL SELECT 3 AS X
		                                   UNION ALL SELECT 4 AS X
		                                   UNION ALL SELECT 5 AS X
		                                 ) B
		                                )
		                           ORDER BY LEAF_CATE_NO, CATE_LVL
		                          ) K
		                          WHERE CATE_NO IS NOT NULL
		                                ) X ON CPR.REF_VAL=X.CATE_NO
		        INNER JOIN TB_CATE_GOODS Y  ON X.LEAF_CATE_NO = Y.CATE_NO
		        INNER JOIN TB_GOODS G ON G.GOODS_CD=Y.GOODS_CD
		        WHERE CPR.CPN_TARGET =  'G260_11'
		        AND IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		        AND IFNULL(REF_BRAND_CD,G.BRAND_CD) = G.BRAND_CD

		        UNION

		        /* 적용대상:브랜드*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.BRAND_CD GKEY, G.GOODS_CD
		        FROM  CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.BRAND_CD
		        WHERE CPR.CPN_TARGET = 'G260_12'
		        AND   IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB

		        UNION

		        /* 적용대상:공급업체*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.SUPPLY_COMP_CD GKEY, G.GOODS_CD
		        FROM CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.SUPPLY_COMP_CD
		        WHERE CPR.CPN_TARGET = 'G260_13'
		 )
		SELECT GOODS_CD
		    , 'G100_00' AS CUST_GB
		    , CURR_PRICE
		    , MIN(
		          ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(CURR_PRICE - DC_PVAL ,0)
		                 ELSE CURR_PRICE - (FLOOR((CURR_PRICE * DC_PVAL / 100) / 1) * 1)
		                 END) ) AS PC_CURR_PRICE
		    , MIN(
		          ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(CURR_PRICE - DC_MVAL ,0)
		                 ELSE CURR_PRICE - (FLOOR((CURR_PRICE * DC_MVAL / 100) / 1) * 1)
		            END) )AS MO_CURR_PRICE
		    , MIN(
		          ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(CURR_PRICE - DC_AVAL ,0)
		                 ELSE CURR_PRICE - (FLOOR((CURR_PRICE * DC_AVAL / 100) / 1) * 1)
		            END) )AS MO_CURR_PRICE
		    , STAFF_CURR_PRICE
		    , MIN(
		         ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(STAFF_CURR_PRICE - DC_PVAL ,0)
		                ELSE CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_PVAL / 100) / 1) * 1)
		           END) )AS STAFF_PC_CURR_PRICE
		    , MIN(
		          ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(STAFF_CURR_PRICE - DC_MVAL ,0)
		                 ELSE CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_MVAL / 100) / 1) * 1)
		            END) )AS STAFF_MO_CURR_PRICE
		    , MIN(
		          ( CASE WHEN DC_WAY = 'G240_10' THEN GREATEST(STAFF_CURR_PRICE - DC_AVAL ,0)
		                 ELSE CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_AVAL / 100) / 1) * 1)
		            END) )AS STAFF_APP_CURR_PRICE
		FROM (
		      SELECT CP.CPN_ID
		           , CP.CPN_NM
		           , CP.DC_WAY                                              /*할인방식*/
		           , CP.DC_PVAL                                             /*PC 할인값*/
		           , CP.DC_MVAL                                             /*MO 할인값*/
		           , CP.DC_AVAL                                             /*APP 할인값*/
		           , CP.CURR_PRICE                                          /*전체 판매가*/
		           , CP.STAFF_CURR_PRICE                                    /*임직원판매가*/
		           , CP.GOODS_CD
		      FROM (
		            SELECT CP.CPN_ID    /*쿠폰ID*/
		                 , CP.CPN_NM    /*쿠폰명*/
		                 , CP.DC_WAY    /*할인방식*/
		                 , CP.DC_PVAL   /*PC할인값*/
		                 , CP.DC_MVAL   /*모바일할인값*/
		                 , CP.DC_AVAL   /*APP할인값*/
		                 , G.CURR_PRICE  /*현재판매가*/
		                 , G.CURR_PRICE AS STAFF_CURR_PRICE  /*현재판매가*/
		                 , G.GOODS_CD
		            FROM   CTE_DETAIL CC
		            INNER JOIN TB_GOODS G ON CC.GOODS_CD = G.GOODS_CD
		            INNER JOIN TB_COUPON CP ON CC.CPN_ID = CP.CPN_ID
		            WHERE
		            NOT EXISTS (
		                         SELECT 1
		                         FROM   TB_COUPON_REFVAL SCPR
		                         WHERE  SCPR.CPN_ID = CP.CPN_ID
		                         AND    SCPR.REF_VAL = G.GOODS_CD
		                         AND    SCPR.DEL_YN = 'N' /*삭제안된넘*/
		                         AND    SCPR.CPN_TARGET = 'G260_14' /* 제외상품*/
		                       )
		      		AND NOT EXISTS (
		                            SELECT 1
		                            FROM TB_COUPON_BAN_GOODS
		                            WHERE GOODS_CD = G.GOODS_CD
		                            AND DEL_YN = 'N'
		                           )
		          ) CP
		) Y
		GROUP BY GOODS_CD ,CURR_PRICE, STAFF_CURR_PRICE, CUST_GB
	</sql>

	<!-- 상품 혜택가 목록 -->
	<sql id="getGoodsBenefitList_sql_org">
		SELECT GOODS_CD
		       , 'G100_00' AS CUST_GB
		       , CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(CURR_PRICE - DC_PVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS PC_CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(CURR_PRICE - DC_MVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS MO_CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(CURR_PRICE - DC_AVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((CURR_PRICE * DC_AVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS MO_CURR_PRICE
		       , STAFF_CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(STAFF_CURR_PRICE - DC_PVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS STAFF_PC_CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(STAFF_CURR_PRICE - DC_MVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS STAFF_MO_CURR_PRICE
		       , MIN(
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 GREATEST(STAFF_CURR_PRICE - DC_AVAL ,0)
		            ELSE
		                 CURR_PRICE - (FLOOR((STAFF_CURR_PRICE * DC_AVAL / 100) / #{pointUnit}) * #{pointUnit})
		            END) )AS STAFF_APP_CURR_PRICE
		FROM   (
		        SELECT CP.CPN_ID
		             , CP.CPN_NM
		             , CP.DC_WAY                                              /*할인방식*/
		             , CP.DC_PVAL                                             /*PC 할인값*/
		             , CP.DC_MVAL                                             /*MO 할인값*/
		             , CP.DC_AVAL                                             /*APP 할인값*/
		             , CP.CURR_PRICE                                          /*전체 판매가*/
		             , CP.STAFF_CURR_PRICE                                    /*임직원판매가*/
		             , CP.GOODS_CD
		        FROM   (
		                SELECT CP.CPN_ID    /*쿠폰ID*/
		                     , CP.CPN_NM    /*쿠폰명*/
		                     , CP.DC_WAY    /*할인방식*/
		                     , CP.DC_PVAL   /*PC할인값*/
		                     , CP.DC_MVAL   /*모바일할인값*/
		                     , CP.DC_AVAL   /*APP할인값*/
		                     , G.CURR_PRICE  /*현재판매가*/
		                     , G.CURR_PRICE AS STAFF_CURR_PRICE  /*현재판매가*/
		                     , G.GOODS_CD
		                FROM   TB_GOODS G
		                     , TB_COUPON CP
		                WHERE  CP.SITE_CD = 'G000_10'
		                AND    CP.CPN_STAT = 'G232_11'   -- 쿠폰인 진행중인 쿠폰만
		                AND    NOW() BETWEEN CP.AVAIL_STDT AND CP.AVAIL_EDDT
		                AND    CP.CPN_TYPE = 'G230_10' /*즉시할인쿠폰*/
		                AND    CP.DC_CD_GB = 'G233_00'                         -- 일반유형(할인쿠폰구분)
		                AND   (
		                        (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1) FROM TB_COUPON_REFVAL
		                                                   WHERE CPN_ID = CP.CPN_ID
		                                                   AND CPN_TARGET = 'G260_10'
		                                                   AND DEL_YN = 'N'
		                                                   AND REF_VAL = G.GOODS_CD
		                                                   ) > 0
		                        )
		                        OR /* 적용대상:카테고리*/
		                        (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1) FROM TB_COUPON_REFVAL
		                                                   WHERE CPN_ID = CP.CPN_ID
		                                                   AND CPN_TARGET = 'G260_11'
		                                                   AND DEL_YN = 'N'
		                                                   AND IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		                                                   AND IFNULL(REF_BRAND_CD,G.BRAND_CD) = G.BRAND_CD
		                                                   AND REF_VAL IN (SELECT DISTINCT X.CATE_NO
		                                                                   FROM
		                                                                        (SELECT LEAF_CATE_NO
		                                                                              , CATE_LVL
		                                                                              , CATE_NO
		                                                                         FROM (
		                                                                               SELECT LEAF_CATE_NO ,
		                                                                                      CASE WHEN X = 1 THEN 'CATE1_NO'  -- 컬럼과 매핑(컬럼명)
		                                                                                           WHEN X = 2 THEN 'CATE2_NO'
		                                                                                           WHEN X = 3 THEN 'CATE3_NO'
		                                                                                           WHEN X = 4 THEN 'CATE4_NO'
		                                                                                           ELSE  'CATE5_NO'
		                                                                                      END CATE_LVL,
		                                                                                      CASE WHEN X = 1 THEN CATE1_NO  -- 컬럼과 매핑(컬럼 데이터)
		                                                                                           WHEN X = 2 THEN CATE2_NO
		                                                                                           WHEN X = 3 THEN CATE3_NO
		                                                                                           WHEN X = 4 THEN CATE4_NO
		                                                                                           ELSE  CATE5_NO
		                                                                                      END CATE_NO
		                                                                               FROM (
		                                                                                      ( SELECT LEAF_CATE_NO
		                                                                                             , CATE1_NO
		                                                                                             , CATE2_NO
		                                                                                             , CATE3_NO
		                                                                                             , CATE4_NO
		                                                                                             , CATE5_NO
		                                                                                        FROM  TB_CATE_4SRCH
		                                                                                        WHERE SITE_CD = 'G000_10'
		                                                                                        AND CATE_TYPE = 'G031_10') A, -- 상품타입
		                                                                                      (SELECT 1 AS X                -- UNPIVOT 컬럼 수 만큼 선언
		                                                                                       UNION ALL SELECT 2 AS X
		                                                                                       UNION ALL SELECT 3 AS X
		                                                                                       UNION ALL SELECT 4 AS X
		                                                                                       UNION ALL SELECT 5 AS X
		                                                                                     ) B
		                                                                                    )
		                                                                               ORDER BY LEAF_CATE_NO, CATE_LVL
		                                                                              ) K
		                                                                              WHERE CATE_NO IS NOT NULL
		                                                                        ) X,
		                                                                        (
		                                                                         SELECT A.CATE_NO, A.GOODS_CD
		                                                                         FROM TB_CATE_GOODS A
		                                                                         WHERE GOODS_CD = G.GOODS_CD
		                                                                        ) Y
		                                                                        WHERE X.LEAF_CATE_NO = Y.CATE_NO
		                                                                  )
		                                                  ) >0
		                        )
		                        OR /* 적용대상:브랜드*/
		                        (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1) FROM TB_COUPON_REFVAL
		                                                   WHERE CPN_ID = CP.CPN_ID
		                                                   AND CPN_TARGET = 'G260_12'
		                                                   AND DEL_YN = 'N'
		                                                   AND REF_VAL = G.BRAND_CD
		                                                   AND IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		                                                  ) >0
		                        )
		                        OR /* 적용대상:공급업체*/
		                        (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1) FROM TB_COUPON_REFVAL
		                                                   WHERE CPN_ID = CP.CPN_ID
		                                                   AND CPN_TARGET = 'G260_13'
		                                                   AND DEL_YN = 'N'
		                                                   AND REF_VAL = G.SUPPLY_COMP_CD
		                                                   /*AND IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB*/
		                                                  ) >0
		                        )
		                      )
		                AND NOT EXISTS (
		                                SELECT 1
		                                FROM   TB_COUPON_REFVAL SCPR
		                                WHERE  SCPR.CPN_ID = CP.CPN_ID
		                                AND    SCPR.REF_VAL = G.GOODS_CD
		                                AND    SCPR.DEL_YN = 'N' /*삭제안된넘*/
		                                AND    SCPR.CPN_TARGET = 'G260_14' /* 제외상품*/
		                               )
		                AND NOT EXISTS (
		                                 SELECT 1
		                                FROM TB_COUPON_BAN_GOODS
		                                WHERE GOODS_CD = G.GOODS_CD
		                                AND DEL_YN = 'N'
		                               )
		               ) CP
		       ) Y
		GROUP BY GOODS_CD ,CURR_PRICE, STAFF_CURR_PRICE, CUST_GB
	</sql>

	<!-- 상품혜택가 temp 테이블 삭제 -->
	<delete id="deleteGoodsBenefitPriceTemp" timeout="600">
		/* TsbGoods.deleteGoodsBenefitPriceTemp */
		DELETE FROM  TB_GOODS_BENEFIT_PRICE_TMP
	</delete>


	<!-- 상품혜택가 temp 적용 등록 -->
	<insert id="crteateGoodsBenefitPriceTemp"  parameterType="Coupon"  timeout="600">
		/* TsbGoods.crteateGoodsBenefitPriceTemp */
		INSERT INTO TB_GOODS_BENEFIT_PRICE_TMP (
		     GOODS_CD
		   , CUST_GB
		   , CURR_PRICE
		   , PC_CURR_PRICE
		   , MO_CURR_PRICE
		   , APP_CURR_PRICE
		   , STAFF_CURR_PRICE
		   , STAFF_PC_CURR_PRICE
		   , STAFF_MO_CURR_PRICE
		   , STAFF_APP_CURR_PRICE
		   )
		    <include refid="getGoodsBenefitList_sql"/>
	</insert>

	<!-- 상품혜택가 temp 적용 변경 -->
	<update id="updateGoodsBenefitPriceTemp"  parameterType="Coupon"  timeout="600">
		/* TsbGoods.updateGoodsBenefitPriceTemp */
		INSERT INTO TB_GOODS_BENEFIT_PRICE_TMP (
		         GOODS_CD
		       , CUST_GB
		       , CURR_PRICE
		       , PC_CURR_PRICE
		       , MO_CURR_PRICE
		       , APP_CURR_PRICE
		       , STAFF_CURR_PRICE
		       , STAFF_PC_CURR_PRICE
		       , STAFF_MO_CURR_PRICE
		       , STAFF_APP_CURR_PRICE
		       , REG_DT
		  )
		  (
		    SELECT * FROM (
		       SELECT C.*, NOW() FROM (
		           <include refid="getGoodsBenefitList_sql"/>
		       ) C
		    ) B
		  )
		ON DUPLICATE KEY UPDATE
		     REG_DT = NOW()
		   , CURR_PRICE = B.CURR_PRICE
		   , STAFF_CURR_PRICE = B.STAFF_CURR_PRICE
		   , PC_CURR_PRICE = B.PC_CURR_PRICE
		   , MO_CURR_PRICE = B.MO_CURR_PRICE
		   , APP_CURR_PRICE = B.APP_CURR_PRICE
		   , STAFF_PC_CURR_PRICE = B.STAFF_PC_CURR_PRICE
		   , STAFF_MO_CURR_PRICE = B.STAFF_MO_CURR_PRICE
		   , STAFF_APP_CURR_PRICE = B.STAFF_APP_CURR_PRICE
	</update>

	<!-- 상품혜택가  테이블 삭제 -->
	<delete id="deleteGoodsBenefitPrice">
		/* TsbGoods.deleteGoodsBenefitPrice */
		DELETE FROM TB_GOODS_BENEFIT_PRICE
	</delete>

	<!-- 상품혜택가 생성 -->
	<insert id="createGoodsBenefitPrice"  timeout="300">
		/* TsbGoods.createGoodsBenefitPrice */
		INSERT INTO TB_GOODS_BENEFIT_PRICE
		    ( GOODS_CD
		    , CUST_GB
		    , CURR_PRICE
		    , PC_CURR_PRICE
		    , MO_CURR_PRICE
		    , APP_CURR_PRICE
		    , STAFF_CURR_PRICE
		    , STAFF_PC_CURR_PRICE
		    , STAFF_MO_CURR_PRICE
		    , STAFF_APP_CURR_PRICE
		    , REG_DT
		    )
		SELECT GOODS_CD
		     , CUST_GB
		     , CURR_PRICE
		     , PC_CURR_PRICE
		     , MO_CURR_PRICE
		     , APP_CURR_PRICE
		     , STAFF_CURR_PRICE
		     , STAFF_PC_CURR_PRICE
		     , STAFF_MO_CURR_PRICE
		     , STAFF_APP_CURR_PRICE
		     , NOW()
		FROM TB_GOODS_BENEFIT_PRICE_TMP
	</insert>

	<!-- 연관상품보기 스코어링 삭제 -->
	<delete id="deleteGoodsRelate" parameterType="String">
		/* TsbGoods.deleteGoodsRelate */
		DELETE FROM TB_RELATE_GOODS
		WHERE REG_YMD = #{regYmd}
	</delete>

	<!-- 연관상품보기 스코어링 생성 -->
	<insert id="createGoodsRelate" parameterType="String">
		/* TsbGoods.createGoodsRelate */
		INSERT INTO TB_RELATE_GOODS
		    ( REG_YMD
		    , GOODS_CD
		    , PREV_GOODS_CD
		    , RELATE_SCORE
		    , REG_NO
		    , REG_DT
		    )
		SELECT DISTINCT REG_YMD
		     , GOODS_CD
		     , PREV_GOODS_CD
		     , RELATE_SCORE
		     , 99999
		     , NOW()
		FROM(
		    SELECT REG_YMD
		         , JSESSION_ID
		         , REG_DT
		         , GOODS_CD
		         , PREV_GOODS_CD
		         , COUNT(*) OVER (PARTITION BY CONCAT(GOODS_CD,PREV_GOODS_CD)  ORDER BY CONCAT(GOODS_CD,PREV_GOODS_CD)) RELATE_SCORE
		    FROM(
		         SELECT REG_YMD
		              , JSESSION_ID
		              , REG_DT
		              , GOODS_CD
		              , PREV_GOODS_CD
		         FROM (
		               SELECT DATE_FORMAT(REG_DT,'%Y%m%d') AS REG_YMD
		                    , JSESSION_ID
		                    , REG_DT
		                    , GOODS_CD
		                    , LAG(GOODS_CD,1) OVER (ORDER BY DATE_FORMAT(REG_DT, '%Y%m%d'),JSESSION_ID,REG_DT) PREV_GOODS_CD
		               FROM TB_GOODS_VIEW_HST
		               WHERE REG_DT >= DATE_FORMAT(#{workDt}, '%Y-%m-%d %H:%i:%S')
		                <![CDATA[
		               AND   REG_DT < DATE_FORMAT(DATE_ADD(#{workDt}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		               ]]>
		              ) A
		         WHERE GOODS_CD <![CDATA[<>]]> PREV_GOODS_CD
		         AND PREV_GOODS_CD IS NOT NULL
		      ) B
		  ) C
	</insert>

	<!--  전시상품 통계 - temp 테이블 삭제 -->
	<delete id="deleteGoodsSummaryTemp">
		/* TsbGoods.deleteGoodsSummaryTemp */
		TRUNCATE TABLE TB_GOODS_SUMMARY_TMP
	</delete>

	<!-- 전시상품 통계 - 판매  2021.08.26 단품단위에서 상품단위로 설정(세트상품)-->
	<insert id="createGoodsSummaryBySale" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryBySale */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		    , ${colNm1}
		    , REG_NO
		    , REG_DT
		)
		(
		SELECT * FROM (
			SELECT A.GOODS_CD
			     , SUM(A.COL_DATA) AS COL_DATA
			     , 99999
			     , NOW()
			FROM (
				SELECT OD.GOODS_CD
				     , CASE WHEN ODIH.ORD_DTL_STAT IN ('G720_10')
				            THEN ODIH.ORD_QTY
				            ELSE CAST(ODIH.CNCL_RTN_QTY AS SIGNED) * -1
				            END  COL_DATA
				FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
				INNER  JOIN TB_ORDER_DETAIL OD
				ON     ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
				WHERE  1=1
				AND    ODIH.ORD_DTL_STAT IN ('G720_10','G720_30','G720_40','G720_50','G720_60') -- 판매-결제완료, 환입-취소완료, 환입-품절취소, 환입_반품완료, 환입-교환완료
				<if test="fromDay != null and fromDay != ''">
				AND ODIH.REG_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -#{fromDay} DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
				</if>
				AND ODIH.REG_DT <![CDATA[ < ]]> DATE_FORMAT(DATE_ADD(DATE_FORMAT(NOW(), '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
				GROUP  BY OD.GOODS_CD
				     , ODIH.ORD_DTL_STAT
				     , ODIH.ORD_QTY
				     , ODIH.CNCL_RTN_QTY
			) A
			WHERE  1=1
			GROUP  BY A.GOODS_CD
		) B
		) 
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA
	</insert>

	<insert id="createGoodsSummaryBySale_ORG" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryBySale_ORG */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		        SELECT GOODS_CD
		              ,SUM(USAC_QTY *  (CASE USAC_GB WHEN '10' THEN 1 ELSE -1 END) ) AS COL_DATA
		              , 99999
		              , NOW()
		        FROM TB_USAC
		        WHERE MALL_GB = '10'
		        <if test="fromDay != null and fromDay != ''">
		        AND OCCUR_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -#{fromDay} DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		        <![CDATA[
		        AND OCCUR_DT < DATE_FORMAT(DATE_ADD(DATE_FORMAT(NOW(), '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
		        ]]>
		        </if>
		        GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA
	</insert>

	<!--전시상품 통계 - 조회 -->
	<insert id="createGoodsSummaryByView" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByView */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,COUNT(GOODS_CD) AS COL_DATA
		             ,99999
		             ,NOW()
		       FROM TB_GOODS_VIEW_HST
		       WHERE 1 = 1
		       AND REG_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -#{fromDay} DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		       <![CDATA[
		       AND REG_DT < DATE_FORMAT(DATE_FORMAT(DATE_ADD(DATE_ADD(NOW(), INTERVAL -7 DAY), INTERVAL 7 DAY ), '%Y%m%d'), '%Y%m%d%H%i%S')
		       ]]>
		       GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA
	</insert>

	<!--전시상품 통계 - 상품평 건수 -->
	<insert id="createGoodsSummaryByReviewCount" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewCount */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,COUNT(GOODS_CD) AS COL_DATA
		             ,99999
		             ,NOW()
		       FROM TB_REVIEW A
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       AND CONFIRM_YN = 'Y'  -- 승인된 상품평만 조회
		       <if test='photoYn != null and photoYn == "Y"'>
		       AND EXISTS (SELECT 1
		                   FROM TB_REVIEW_ATTACH B
		                   WHERE B.DEL_YN = 'N'
		                   AND B.REVIEW_SQ = A.REVIEW_SQ
		                   AND (B.FILE_GB  = 'I'
		                        OR
		                        B.FILE_GB = 'M' AND B.KMC_KEY IS NOT NULL
		                       )
		                   )
		       </if>
		       <if test='photoYn != null and photoYn == "N"'>
		       AND NOT EXISTS (SELECT 1
		                       FROM TB_REVIEW_ATTACH B
		                        WHERE B.REVIEW_SQ = A.REVIEW_SQ)
		       </if>
		       GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA
	</insert>

	<!--전시상품 통계 - 상품평 건수 -->
	<insert id="createDealGoodsSummaryByReviewCount" parameterType="GoodsSummary">
		/* TsbGoods.createDealGoodsSummaryByReviewCount */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , REVIEW_REG_CNT
		  , REG_NO
		  , REG_DT
		)
		(
		SELECT GOODS_CD
		     , REVIEW_REG_CNT
		     , 99999
		     , NOW()
		FROM (
		    SELECT GOODS_CD
		         , SUM(REVIEW_REG_CNT) AS REVIEW_REG_CNT
		    FROM (
		             SELECT A.GOODS_CD
		                  , A.COMPS_GOODS_CD
		                  , C.REVIEW_REG_CNT
		             FROM TB_GOODS_COMPOSE A
		             INNER JOIN TB_GOODS B ON A.COMPS_GOODS_CD = B.GOODS_CD
		             INNER JOIN TB_GOODS_SUMMARY_TMP C ON A.COMPS_GOODS_CD = C.GOODS_CD
		             WHERE A.GOODS_CD IN (SELECT GOODS_CD
		                                  FROM TB_GOODS
		                                  WHERE SELF_GOODS_YN = 'Y'
		                                  AND GOODS_TYPE = 'G056_D'
		                                  AND GOODS_STAT != 'G008_99')
		               AND A.USE_YN = 'Y'
		         )Z
		    GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    REVIEW_REG_CNT = B.REVIEW_REG_CNT
	</insert>

	<!--전시상품 통계 - 상품평 점수 합계 -->
	<insert id="createGoodsSummaryByReviewSum" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewSum */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT GOODS_CD
		         , ROUND( COL_DATA /REVIEW_CNT,1)
		         , 99999
		         , NOW()
		    FROM (
		       SELECT GOODS_CD
		             ,SUM(${reviewColNm})AS COL_DATA
		             ,COUNT(*) AS REVIEW_CNT
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       AND CONFIRM_YN = 'Y'  -- 승인된 상품평만 조회
		       GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = ROUND(B.COL_DATA / B.REVIEW_CNT,1)
	</insert>

	<!--전시상품 통계 - 상품평 스코어합계-->
	<insert id="createGoodsSummaryByReviewScore" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewScore */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , ${colNm2}
		  , ${colNm3}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '1' THEN 1 ELSE 0 END),0) AS COL_DATA1
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '2' THEN 1 ELSE 0 END),0) AS COL_DATA2
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '3' THEN 1 ELSE 0 END),0) AS COL_DATA3
		             ,99999
		             ,NOW()
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       AND CONFIRM_YN = 'Y'  -- 승인된 상품평만 조회
		       <if test='photoYn != null and photoYn == "Y"'>
		       AND SYS_FILE_NM IS NOT NULL
		       </if>
		       <if test='photoYn != null and photoYn == "N"'>
		       AND SYS_FILE_NM IS NULL
		       </if>
		       GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA1
		   ,${colNm2} = B.COL_DATA2
		   ,${colNm3} = B.COL_DATA3
	</insert>

	<!--전시상품 통계 - 상품평 스코어합계-->
	<insert id="createGoodsSummaryByReviewScore_OLD" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewScore_OLD */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , ${colNm1}
		  , ${colNm2}
		  , ${colNm3}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT GOODS_CD
		         , ROUND( COL_DATA1 /REVIEW_CNT,1)  AS COL_DATA1
		         , ROUND( COL_DATA2 /REVIEW_CNT,1)  AS COL_DATA2
		         , ROUND( COL_DATA3 /REVIEW_CNT,1)  AS COL_DATA3
		         , 99999
		         , NOW()
		    FROM (
		       SELECT GOODS_CD
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '1' THEN 1 ELSE 0 END),0) AS COL_DATA1
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '2' THEN 1 ELSE 0 END),0) AS COL_DATA2
		             ,IFNULL(SUM(CASE WHEN ${reviewColNm} = '3' THEN 1 ELSE 0 END),0) AS COL_DATA3
		             ,COUNT(*) AS REVIEW_CNT
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       AND CONFIRM_YN = 'Y'  -- 승인된 상품평만 조회
		       <if test='photoYn != null and photoYn == "Y"'>
		       AND SYS_FILE_NM IS NOT NULL
		       </if>
		       <if test='photoYn != null and photoYn == "N"'>
		       AND SYS_FILE_NM IS NULL
		       </if>
		       GROUP BY GOODS_CD
		        ) B
		)
		ON DUPLICATE KEY UPDATE
		    ${colNm1} = B.COL_DATA1
		   ,${colNm2} = B.COL_DATA2
		   ,${colNm3} = B.COL_DATA3
	</insert>

	<!--전시상품 통계 - 상품문의 -->
	<insert id="createGoodsSummaryByCounsel" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByCounsel */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , GOODS_QNA_CNT
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT REL_GOODS_CD
		             ,COUNT(REL_GOODS_CD) AS COL_DATA
		             ,99999
		             ,NOW()
		       FROM TB_COUNSEL
		       WHERE COUNSEL_TYPE = 'G'
		       AND REL_GOODS_CD IS NOT NULL
		       GROUP BY REL_GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    GOODS_QNA_CNT = B.COL_DATA
	</insert>

	<!--전시상품 통계 - 딜상품 상품문의 -->
	<insert id="createDealGoodsSummaryByCounsel" parameterType="GoodsSummary">
		/* TsbGoods.createDealGoodsSummaryByCounsel */
		INSERT INTO TB_GOODS_SUMMARY_TMP (
		    GOODS_CD
		  , GOODS_QNA_CNT
		  , REG_NO
		  , REG_DT
		)
		(
		SELECT GOODS_CD
		     , GOODS_QNA_CNT
		     , 99999
		     , NOW()
		FROM (
		    SELECT GOODS_CD
		         , SUM(GOODS_QNA_CNT) AS GOODS_QNA_CNT
		    FROM (
		             SELECT A.GOODS_CD
		                  , A.COMPS_GOODS_CD
		                  , C.GOODS_QNA_CNT
		             FROM TB_GOODS_COMPOSE A
		             INNER JOIN TB_GOODS B ON A.COMPS_GOODS_CD = B.GOODS_CD
		             INNER JOIN TB_GOODS_SUMMARY_TMP C ON A.COMPS_GOODS_CD = C.GOODS_CD
		             WHERE A.GOODS_CD IN (SELECT GOODS_CD
		                                  FROM TB_GOODS
		                                  WHERE SELF_GOODS_YN = 'Y'
		                                  AND GOODS_TYPE = 'G056_D'
		                                  AND GOODS_STAT != 'G008_99')
		               AND A.USE_YN = 'Y'
		         )Z
		    GROUP BY GOODS_CD
		    ) B
		)
		ON DUPLICATE KEY UPDATE
		    GOODS_QNA_CNT = B.GOODS_QNA_CNT
	</insert>

	<!--전시상품 통계 - 저장-->
	<insert id="saveGoodsSummary">
		/* TsbGoods.saveGoodsSummary */
		UPDATE TB_GOODS_SUMMARY A, TB_GOODS_SUMMARY_TMP GS
		SET  A.SELL_TOT_QTY        = IFNULL(GS.SELL_TOT_QTY,0)
		   , A.SELL_WEEK_QTY       = IFNULL(GS.SELL_WEEK_QTY,0)
		   , A.SELL_MONTH_QTY      = IFNULL(GS.SELL_MONTH_QTY,0)
		   , A.SELL_YDAY_QTY       = IFNULL(GS.SELL_YDAY_QTY,0)
		   , A.SELL_DAY15_QTY      = IFNULL(GS.SELL_DAY15_QTY,0)
		   , A.READ_NWEEK_CNT      = IFNULL(GS.READ_NWEEK_CNT,0)
		   , A.READ_PWEEK_CNT      = IFNULL(GS.READ_PWEEK_CNT,0)
		   , A.GOODS_QNA_CNT       = IFNULL(GS.GOODS_QNA_CNT,0)
		   , A.REVIEW_REG_CNT      = IFNULL(GS.REVIEW_REG_CNT,0)
		   , A.RREVIEW_REG_CNT     = IFNULL(GS.RREVIEW_REG_CNT,0)
		   , A.PREVIEW_REG_CNT     = IFNULL(GS.PREVIEW_REG_CNT,0)
		   , A.SCORE               = IFNULL(GS.SCORE,0)
		   , A.SCORE_SIZE1         = IFNULL(GS.SCORE_SIZE1,0)
		   , A.SCORE_SIZE2         = IFNULL(GS.SCORE_SIZE2,0)
		   , A.SCORE_SIZE3         = IFNULL(GS.SCORE_SIZE3,0)
		   , A.SCORE_COLOR1        = IFNULL(GS.SCORE_COLOR1,0)
		   , A.SCORE_COLOR2        = IFNULL(GS.SCORE_COLOR2,0)
		   , A.SCORE_COLOR3        = IFNULL(GS.SCORE_COLOR3,0)
		   , A.SCORE_FIT1          = IFNULL(GS.SCORE_FIT1,0)
		   , A.SCORE_FIT2          = IFNULL(GS.SCORE_FIT2,0)
		   , A.SCORE_FIT3          = IFNULL(GS.SCORE_FIT3,0)
		   , A.SCORE_THICK1        = IFNULL(GS.SCORE_THICK1,0)
		   , A.SCORE_THICK2        = IFNULL(GS.SCORE_THICK2,0)
		   , A.SCORE_THICK3        = IFNULL(GS.SCORE_THICK3,0)
		   , A.SCORE_WEIGHT1       = IFNULL(GS.SCORE_WEIGHT1,0)
		   , A.SCORE_WEIGHT2       = IFNULL(GS.SCORE_WEIGHT2,0)
		   , A.SCORE_WEIGHT3       = IFNULL(GS.SCORE_WEIGHT3,0)
		   , A.SCORE_BALL1         = IFNULL(GS.SCORE_BALL1,0)
		   , A.SCORE_BALL2         = IFNULL(GS.SCORE_BALL2,0)
		   , A.SCORE_BALL3         = IFNULL(GS.SCORE_BALL3,0)
		   , A.UPD_NO              = 99999
		   , A.UPD_DT              = NOW()
		WHERE A.GOODS_CD = GS.GOODS_CD
	</insert>

	<!--  안전인증대상 상품 조회 - 오픈후 1회성 작업 -->
	<select id="getGoodsSafeNoList_org2"   resultType="GoodsSafeNo">
		/* TsbGoods.getGoodsSafeNoList_org2 */
		SELECT GOODS_CD
		     , CERT_NUM
		FROM TEMP_GOODS_SAFE_NO
		WHERE CERT_TARGET_GB = 'G083_1'  -- 인증대상
		-- AND CERT_FORM_GB = 'G084_3'   -- 어린이제품
		AND CERT_TYPE IN ('G081_1', 'G081_2')  -- 안전인증, 안전확인
		AND NULLIF(CERT_NUM, '') IS NOT NULL
	</select>

	<!--  안전인증대상 상품 조회 - 오픈후 1회성 작업 -->
	<select id="getGoodsSafeNoList"   resultType="GoodsSafeNo">
		/* TsbGoods.getGoodsSafeNoList */
		SELECT GOODS_CD
		     , CERT_NUM
		FROM TEMP_GOODS_SAFE_NO
		WHERE 1 = 1
		AND NULLIF(CERT_NUM, '') IS NOT NULL
		AND UPD_NO != 99999
		-- AND REG_NO = 6
		LIMIT 10000
	</select>

	<!--  안전인증대상 상품 조회 -->
	<select id="getGoodsSafeNoList_org"   resultType="GoodsSafeNo">
		/* TsbGoods.getGoodsSafeNoList_org */
		SELECT B.GOODS_CD
		     , B.GOODS_NUM
		     , A.ITEMKIND_CD
		     , C.CERT_NUM
		     , C.CERT_STATE
		     , C.CERT_DIV
		     , C.CERT_DT
		FROM TB_ITEMKIND A
		INNER JOIN TB_GOODS B ON A.ITEMKIND_CD = B.ITEMKIND_CD AND B.SELF_GOODS_YN = 'Y'  /* 자사상품 */
		INNER JOIN TB_BRAND D ON B.BRAND_CD = D.BRAND_CD AND D.USE_YN = 'Y'
		LEFT OUTER JOIN TB_GOODS_SAFE_NO C ON B.GOODS_CD = C.GOODS_CD
		                                   AND (CERT_NUM IS NULL
		                                        <![CDATA[
		                                        OR CERT_DT < DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -5 YEAR), '%Y%m%d')  /*인증유효 5년 경과*/
		                                        OR CERT_STATE <> '적합'
		                                        ]]>
		                                       )
		WHERE A.NI_CLSF_CD = 'G004_23'  /*고시 유아용품*/
	</select>

	<!--  상품 안전인증번호 저장 -->
	<insert id="saveGoodsSafeNo"  parameterType="GoodsSafeNo">
		/* TsbGoods.saveGoodsSafeNo */
		INSERT INTO TEMP_GOODS_SAFE_NO (
		    GOODS_CD
		  , CERT_NUM
		  , CERT_STATE
		  , CERT_DIV
		  , CERT_ORGAN_NAME
		  , CERT_DT
		  , REG_NO
		  , REG_DT
		  , UPD_NO
		  , UPD_DT
		)
		VALUES (
		    #{goodsCd}
		  , #{certNum}
		  , #{certState}
		  , #{certDiv}
		  , #{certOrganName}
		  , #{certDt}
		  , #{regNo}
		  , NOW()
		  , #{updNo}
		  , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       CERT_NUM = #{certNum}
		     , CERT_DIV = #{certDiv}
		     , CERT_STATE = #{certState}
		     , CERT_ORGAN_NAME = #{certOrganName}
		     , CERT_DT = #{certDt}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!--  상품 안전인증번호수정 -->
	<insert id="updateGoodsSafeNo"  parameterType="GoodsSafeNo">
		/* TsbGoods.updateGoodsSafeNo */
		UPDATE TEMP_GOODS_SAFE_NO
		SET    UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE GOODS_CD = #{goodsCd}
	</insert>

	<!--실측사이즈 저장-->
	<insert id="saveGoodsIfMeasurement" parameterType="GoodsIfMeasurement">
		/* TsbGoods.saveGoodsIfMeasurement */
		INSERT INTO TB_IF_MEASUREMENT (
		       SKUCODE
		     , PRODUCTNO
		     , PRODUCTCODE
		     , TYPECD
		     , WASHINGMETHOD
		     , VALUE1
		     , VALUE2
		     , VALUE3
		     , VALUE4
		     , VALUE5
		     , MEMO
		     , DATEINSERTED
		     , DATELASTMODIFIED
		     , ISUSE
		     , USERLASTMODIFIED
		     , UPD_DT
		)
		VALUES(
		       #{skucode}
		     , #{productno}
		     , #{productcode}
		     , #{typecd}
		     , #{washingmethod}
		     , IFNULL(#{value1},0)
		     , IFNULL(#{value2},0)
		     , IFNULL(#{value3},0)
		     , IFNULL(#{value4},0)
		     , IFNULL(#{value5},0)
		     , #{memo}
		     , DATE_FORMAT(#{dateinserted}, '%Y-%m-%d %H:%i:%S')
		     , DATE_FORMAT(#{datelastmodified}, '%Y-%m-%d %H:%i:%S')
		     , IFNULL(#{isuse},'1')
		     , #{userlastmodified}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       WASHINGMETHOD = #{washingmethod}
		     , VALUE1 = IFNULL(#{value1},0)
		     , VALUE2 = IFNULL(#{value2},0)
		     , VALUE3 = IFNULL(#{value3},0)
		     , VALUE4 = IFNULL(#{value4},0)
		     , VALUE5 = IFNULL(#{value5},0)
		     , MEMO = #{memo}
		     , DATEINSERTED = DATE_FORMAT(#{dateinserted}, '%Y-%m-%d %H:%i:%S')
		     , DATELASTMODIFIED = DATE_FORMAT(#{datelastmodified}, '%Y-%m-%d %H:%i:%S')
		     , ISUSE = IFNULL(#{isuse},'1')
		     , USERLASTMODIFIED = #{userlastmodified}
		     , UPD_DT = NOW()
	</insert>

	<!--입고 저장-->
	<insert id="saveGoodsIfIncomelot" parameterType="GoodsIfIncomelot">
		/* TsbGoods.saveGoodsIfIncomelot */
		INSERT INTO TB_IF_INCOMELOT (
		       LOTNO
		     , PURCHASENO
		     , PROVIDERNO
		     , PROVIDERNAME
		     , BRANDNO
		     , BRANDNAME
		     , DATEINCOME
		     , UPD_DT
		)
		VALUES (
		       #{lotno}
		     , #{purchaseno}
		     , #{providerno}
		     , #{providername}
		     , #{brandno}
		     , #{brandname}
		     , DATE_FORMAT(#{dateincome}, '%Y-%m-%d %H:%i:%S')
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       PURCHASENO = #{purchaseno}
		     , PROVIDERNO = #{providerno}
		     , PROVIDERNAME = #{providername}
		     , BRANDNO = #{brandno}
		     , BRANDNAME = #{brandname}
		     , DATEINCOME = DATE_FORMAT(#{dateincome}, '%Y-%m-%d %H:%i:%S')
		     , UPD_DT = NOW()
	</insert>

	<!--입고상품 저장-->
	<insert id="saveGoodsIfIncomelotitem" parameterType="GoodsIfIncomelotitem">
		/* TsbGoods.saveGoodsIfIncomelotitem */
		INSERT INTO TB_IF_INCOMELOTITEM (
		       LOTNO
		     , ITEMNO
		     , WMSITEMNO
		     , DATEINCOME
		     , PRODUCTNO
		     , PRODUCTCODE
		     , PRODUCTNAME
		     , SKUCODE
		     , NORMALQTY
		     , BROKENQTY
		     , TOTALQTY
		     , SKUMODELNO
		     , UPD_DT
		)
		VALUES (
		       #{lotno}
		     , #{itemno}
		     , #{wmsitemno}
		     , DATE_FORMAT(#{dateincome}, '%Y-%m-%d %H:%i:%S')
		     , #{productno}
		     , #{productcode}
		     , #{productname}
		     , #{skucode}
		     , #{normalqty}
		     , #{brokenqty}
		     , #{totalqty}
		     , #{skumodelno}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       WMSITEMNO = #{wmsitemno}
		     , DATEINCOME = DATE_FORMAT(#{dateincome}, '%Y-%m-%d %H:%i:%S')
		     , PRODUCTNO = #{productno}
		     , PRODUCTCODE = #{productcode}
		     , PRODUCTNAME = #{productname}
		     , SKUCODE = #{skucode}
		     , NORMALQTY = #{normalqty}
		     , BROKENQTY = #{brokenqty}
		     , TOTALQTY = #{totalqty}
		     , SKUMODELNO = #{skumodelno}
		     , UPD_DT = NOW()
	</insert>

	<!--WMS 상품 저장-->
	<insert id="saveWmsGoods" parameterType="GoodsIfIncomelot" >
		/* TsbGoods.saveWmsGoods */
		INSERT INTO TB_WMS_GOODS (
		       PRODUCT_NO
		     , PRODUCT_CODE
		     , PRODUCT_NAME
		     , MODEL_NO
		     , PROVIDER_NO
		     , PROVIDER_NAME
		     , BRAND_NO
		     , BRAND_NAME
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT Z.PRODUCTNO
		     , Z.PRODUCTCODE
		     , Z.PRODUCTNAME
		     , Z.MODELNO
		     , Z.PROVIDERNO
		     , Z.PROVIDERNAME
		     , Z.BRANDNO
		     , Z.BRANDNAME
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		FROM (
		      SELECT B.PRODUCTNO
		           , B.PRODUCTCODE
		           , MAX(B.PRODUCTNAME) AS PRODUCTNAME
		           , MAX(CASE WHEN INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), 'F20') = 1 THEN LEFT(REPLACE(B.SKUMODELNO, ' ' , ''),12)
		                      WHEN INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') > 0 AND INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') <![CDATA[<]]> 10 THEN LEFT(REPLACE(B.SKUMODELNO, ' ' , ''),11)
		                      ELSE LEFT(REPLACE(B.SKUMODELNO, ' ' , ''),10) END) AS MODELNO
		           , MAX(A.PROVIDERNO) AS PROVIDERNO
		           , MAX(A.PROVIDERNAME) AS PROVIDERNAME
		           , MAX(A.BRANDNO) AS BRANDNO
		           , MAX(A.BRANDNAME) AS BRANDNAME
		      FROM TB_IF_INCOMELOT  A
		      INNER JOIN TB_IF_INCOMELOTITEM B ON A.LOTNO = B.LOTNO
		      WHERE 1 = 1
		      <choose>
		          <when test='jobdate != null and jobdate != ""'>
		      AND A.DATEINCOME >= DATE_FORMAT(DATE_FORMAT(#{jobdate}, '%Y%m%d'), '%Y%m%d%H%i%S')
		      <![CDATA[
		      AND A.DATEINCOME < DATE_FORMAT(DATE_ADD(DATE_FORMAT(#{jobdate}, '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
		      ]]>
		          </when>
		          <otherwise>
		      AND A.DATEINCOME >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		      <![CDATA[
		      AND A.DATEINCOME < DATE_FORMAT(DATE_ADD(DATE_FORMAT(NOW(), '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
		      ]]>
		          </otherwise>
		      </choose>
		      GROUP BY B.PRODUCTNO , B.PRODUCTCODE
		) Z
		WHERE NOT EXISTS (SELECT 1
		                FROM TB_WMS_GOODS
		                WHERE MODEL_NO = Z.MODELNO /* 같은모델번호에 PRODUCT_NO 여러개 존재*/
		                )
		AND NOT EXISTS (SELECT 1
		                FROM TB_GOODS
		                WHERE GOODS_CD = Z.MODELNO)  /* 마이그된 상품 제외*/
	</insert>


	<!-- 공급업체 목록 -->
	<select id="getSupplyCompanyList" resultType="IfProvider">
		/* TsbGoods.getSupplyCompanyList */
		SELECT A.PROVIDER_NO AS PROVIDERNO	         /*WMS업체코드*/
		     , A.SUPPLY_COMP_NM AS PROVIDERNAME      /*온라인공급업체명*/
		     , (SELECT C.CATE1_NM
		        FROM TB_GOODS G
		            ,TB_CATE_GOODS B
		            ,TB_CATE_4SRCH C
		        WHERE G.GOODS_CD  = B.GOODS_CD
		        AND G.SUPPLY_COMP_CD  = A.SUPPLY_COMP_CD
		        AND G.GOODS_STAT ='G008_90'
		        AND B.CATE_NO  = C.LEAF_CATE_NO
		        AND C.CATE_TYPE = 'G031_10'          /*상품분류카테고리*/
		        AND C.CATE_GB = 'G032_101'           /*BY ITEM*/
		        AND C.SITE_CD  = 'G000_10'           /*사이트*/
		        LIMIT 1) AS CATEGORYTYPECD           /*분류코드*/
		     , CASE WHEN A.DISTRIBUTION_GB = 'G065_10' THEN '제조'
		            WHEN A.DISTRIBUTION_GB = 'G065_11' THEN '사입'
                    ELSE '위탁' END  AS DISTRIBUTIONCD              /*유통구분*/
		     , A.CS_CHARGE_NM AS CHARGENAME          /*CS담당자명*/
		     , A.CS_CHARGE_TELNO AS CHARGECELLNUM    /*CS담당자전화번호*/
		     , CASE WHEN A.USE_YN = 'Y' THEN '정상'  /*상태*/
		            ELSE '사용중지'
		            END AS STATUSCD
		     , 'WMS' AS STOCKMGMTTYPECD              /*재고관리유형*/
		     , (CASE WHEN A.ECONTRACT_YN = 'Y' THEN 1 ELSE 0 END) AS ISBILLTRANS     /*전자계약여부*/
		     , A.SUPPLY_COMP_CD                      /*온라인업체코드*/
		FROM  TB_SUPPLY_COMPANY A
		WHERE  A.DISTRIBUTION_GB  IN ('G065_10', 'G065_11', 'G065_12')  /* 자사 유통구분*/
		-- AND UPD_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -7 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		<![CDATA[
		-- AND UPD_DT < DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
		]]>
	</select>

	<!-- 브랜드 목록 -->
	<select id="getBrandList" resultType="IfBrand">
		/* TsbGoods.getBrandList */
		SELECT A.BRAND_NO AS BRANDNO
		     , A.BRAND_ENM AS BRANDNAME
		     , CASE WHEN A.USE_YN = 'Y' THEN '정상'
		            ELSE '사용안함'
		       END AS STATUSCD
		     , A.REG_DT AS DATECREATED
		     , A.BRAND_CD
		FROM   TB_BRAND A
		WHERE  A.SELF_YN  = 'Y'  /* 자사 */
		-- AND A.UPD_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -7 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')  -- eskim
		<![CDATA[
		-- AND A.UPD_DT < DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
		]]>
	</select>

	<!-- 브랜드 업체 정보 목록 -->
	<select id="getBrandProviderXrefList" resultType="IfBrand">
		/* TsbGoods.getBrandProviderXrefList */
		SELECT A.BRAND_NO AS BRANDNO
		     , C.PROVIDER_NO  AS PROVIDERNO
		     , A.BRAND_CD
		     , C.SUPPLY_COMP_CD
		     , A.BRAND_ENM AS BRANDNAME
		     , CASE WHEN A.USE_YN = 'Y' THEN '정상'
		            ELSE '사용안함'
		       END AS STATUSCD
		FROM TB_BRAND A
		INNER JOIN TB_BRAND_SUPPLY B ON A.BRAND_CD = B.BRAND_CD
		--                             AND B.REG_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -7 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		                             <![CDATA[
		--                             AND B.REG_DT < DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
		                             ]]>
		INNER JOIN TB_SUPPLY_COMPANY  C ON  B.SUPPLY_COMP_CD  = C.SUPPLY_COMP_CD
		WHERE A.SELF_YN = 'Y'  /* 자사 */
	</select>

	<!-- 상품 목록 -->
	<select id="getGoodsList" parameterType="String" resultType="IfProduct" timeout="300">
		/* TsbGoods.getGoodsList */
		SELECT A.PRODUCT_NO AS PRODUCTNO                            /*WMS상품번호*/
		     , A.PRODUCT_CODE AS PRODUCTCODE                        /*WMS상품코드*/
		     , G.GOODS_NM AS PRODUCTNAME                            /*상품명*/
		     , G.GOODS_NUM AS MODELNO                               /*상품품번*/
		     , B.BRAND_NO AS BRANDNO                                /*브랜드번호*/
		     , B.BRAND_ENM AS BRANDNAME                             /*브랜드명*/
		     , C.PROVIDER_NO AS PROVIDERNO                          /*업체번호*/
		     , (CASE G.GOODS_STAT WHEN 'G008_20' THEN '승인보류'
		                         WHEN 'G008_40' THEN '승인대기'
		                         WHEN 'G008_70' THEN '품절'
		                         WHEN 'G008_99' THEN '삭제'
		                         WHEN 'G008_95' THEN '판매중지'
		                         ELSE '판매중'
		        END) AS STATUSCD                                        /*상품상태*/
		     , FN_GET_CODE_NM('G056', G.GOODS_STAT) AS PRODUCTTYPECD    /*상품유형*/
		     , G.SUPPLY_GOODS_CD AS VENDORPRODUCTCODE                   /*업체상품코드*/
		     , G.GOODS_CD                                               /*온라인상품코드*/
		     , G.BRAND_CD                                               /*온라인브랜드코드*/
		     , G.SUPPLY_COMP_CD                                         /*온라인업체코드*/
		     , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = G.GOODS_CD AND COLOR_CD = G.MAIN_COLOR_CD AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM /*상품대표이미지*/
		FROM   TB_GOODS G
		INNER JOIN TB_OPTION A ON G.GOODS_CD = A.GOODS_CD
		INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		INNER JOIN TB_SUPPLY_COMPANY C ON G.SUPPLY_COMP_CD = C.SUPPLY_COMP_CD
		WHERE  G.SELF_GOODS_YN  = 'Y'  /* 자사 */
		<choose>
		    <when test='jobdate != null and jobdate == "20210901"'>
				AND ((SELECT COUNT(1)
				  FROM TB_GOODS_IMG
				  WHERE GOODS_CD = G.GOODS_CD
			    ) > 0)
			    
		    </when>
		    <otherwise>
				AND ((SELECT COUNT(1)
				  FROM TB_GOODS_IMG
				  WHERE GOODS_CD = G.GOODS_CD
					AND REG_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
			     <![CDATA[
					AND REG_DT < DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
			     ]]>
			    ) > 0)
			    OR
				(G.UPD_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
			     <![CDATA[
			     AND G.UPD_DT < DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
			     ]]>
			    )
		    </otherwise>
		</choose>
		
	</select>

	<!-- TB_IF_PRODUCTSKU 삭제 -->
	<delete id="deleteIfProductSku"  timeout="300">
		/* TsbGoods.deleteIfProductSku */
		DELETE FROM TB_IF_PRODUCTSKU
	</delete>

	<!-- TB_IF_PRODUCTSKU 저장 -->
	<insert id="createIfProductSku"  parameterType="java.util.HashMap" >
		/* TsbGoods.createIfProductSku */
		INSERT INTO TB_IF_PRODUCTSKU(
		       SKUCODE
		     , SKUMODELNO
		     , PRODUCTNO
		     , PRODUCTCODE
		     , OPTION1
		     , OPTION2
		     , OPTION3
		     , SELLINGSTOCKAMOUNT
		     , SELLINGSTOCKTYPECD
		     , LIMITSTOCKAMOUNT
		     , VENDORSKUMODELNO
		     , VENDORSKUCODE
		     , ISVIRTUALSTOCK
		     , DATEVIRTUALSTOCK
		     , SKUCODE88
		     , UPD_DT
		)
		VALUES
		<foreach collection="productSukList" item="item" index="index"  separator=",">
		(
		       #{item.skucode}
		     , #{item.skumodelno}
		     , #{item.productno}
		     , #{item.productcode}
		     , #{item.option1}
		     , #{item.option2}
		     , #{item.option3}
		     , #{item.sellingstockamount}
		     , #{item.sellingstocktypecd}
		     , #{item.limitstockamount}
		     , #{item.vendorskumodelno}
		     , #{item.vendorskucode}
		     , #{item.isvirtualstock}
		     , #{item.datevirtualstock}
		     , #{item.skucode88}
		     , NOW()
		)
		</foreach>

	</insert>

	<!-- TB_IF_PRODUCTSKU 저장 -->
	<insert id="createIfProductSkuByRow"  parameterType="IfProduct">
		/* TsbGoods.createIfProductSkuByRow */
		INSERT INTO TB_IF_PRODUCTSKU(
		       SKUCODE
		     , SKUMODELNO
		     , PRODUCTNO
		     , PRODUCTCODE
		     , OPTION1
		     , OPTION2
		     , OPTION3
		     , SELLINGSTOCKAMOUNT
		     , SELLINGSTOCKTYPECD
		     , LIMITSTOCKAMOUNT
		     , VENDORSKUMODELNO
		     , VENDORSKUCODE
		     , ISVIRTUALSTOCK
		     , DATEVIRTUALSTOCK
		     , SKUCODE88
		     , UPD_DT
		)
		VALUES(
		       #{skucode}
		     , #{skumodelno}
		     , #{productno}
		     , #{productcode}
		     , #{option1}
		     , #{option2}
		     , #{option3}
		     , #{sellingstockamount}
		     , #{sellingstocktypecd}
		     , #{limitstockamount}
		     , #{vendorskumodelno}
		     , #{vendorskucode}
		     , #{isvirtualstock}
		     , #{datevirtualstock}
		     , #{skucode88}
		     , NOW()
		)
	</insert>

	<!-- TB_IF_PRODUCTSKU_HST 삭제 -->
	<delete id="deleteWmsProductSkuHst"  parameterType="String" timeout="300">
		/* TsbGoods.deleteWmsProductSkuHst */
		DELETE FROM TB_IF_PRODUCTSKU_HST
		WHERE JOBDATE <![CDATA[<=]]> #{jobdate}
	</delete>

	<!-- TB_IF_PRODUCTSKU_HST 저장 -->
	<insert id="createWmsProductSkuHst"  parameterType="String" timeout="300">
		/* TsbGoods.createWmsProductSkuHst */
		INSERT INTO TB_IF_PRODUCTSKU_HST(
		       JOBDATE
		     , SKUCODE
		     , SKUMODELNO
		     , PRODUCTNO
		     , PRODUCTCODE
		     , OPTION1
		     , OPTION2
		     , OPTION3
		     , SELLINGSTOCKAMOUNT
		     , SELLINGSTOCKTYPECD
		     , LIMITSTOCKAMOUNT
		     , VENDORSKUMODELNO
		     , VENDORSKUCODE
		     , ISVIRTUALSTOCK
		     , DATEVIRTUALSTOCK
		     , SKUCODE88
		)
		SELECT #{jobdate}
		     , SKUCODE
		     , SKUMODELNO
		     , PRODUCTNO
		     , PRODUCTCODE
		     , OPTION1
		     , OPTION2
		     , OPTION3
		     , SELLINGSTOCKAMOUNT
		     , SELLINGSTOCKTYPECD
		     , LIMITSTOCKAMOUNT
		     , VENDORSKUMODELNO
		     , VENDORSKUCODE
		     , ISVIRTUALSTOCK
		     , DATEVIRTUALSTOCK
		     , SKUCODE88
		FROM TB_IF_PRODUCTSKU
	</insert>

	<!-- TB_WMS_STOCK 삭제 -->
	<delete id="deleteWmsStock"  timeout="300">
		/* TsbGoods.deleteWmsStock */
		DELETE FROM TB_WMS_STOCK
	</delete>

	<!-- TB_WMS_STOCK 저장 -->
	<insert id="createWmsStock"  parameterType="java.util.HashMap" >
		/* TsbGoods.createWmsStock */
		INSERT INTO TB_WMS_STOCK(
		      PRODUCT_NO
		      ,SKU_CODE
		      ,LOC_QTY
		      ,PICKING_QTY
		      ,EOUT_PICKING_QTY
		      ,UPD_DT
		)
		VALUES
		<foreach collection="productSukList" item="item" index="index"  separator=",">
		(
		       #{item.productNo}
		     , #{item.skuCode}
		     , #{item.locQty}
		     , #{item.pickingQty}
		     , #{item.eoutPickingQty}
		     , NOW()
		)
		</foreach>

	</insert>

	<!-- TB_IF_PRODUCTSKU 저장 -->
	<insert id="createWmsStockByRow"  parameterType="WmsStock">
		/* TsbGoods.createWmsStockByRow */
		INSERT INTO TB_WMS_STOCK(
		       PRODUCT_NO
		      ,SKU_CODE
		      ,LOC_QTY
		      ,PICKING_QTY
		      ,EOUT_PICKING_QTY
		      ,UPD_DT
		)
		VALUES(
		       #{productNo}
		     , #{skuCode}
		     , #{locQty}
		     , #{pickingQty}
		     , #{eoutPickingQty}
		     , NOW()
		)
	</insert>

	<!-- TB_IF_PRODUCTSKU_HST 삭제 -->
	<delete id="deleteWmsStockHst"  parameterType="String" timeout="300">
		/* TsbGoods.deleteWmsStockHst */
		DELETE FROM TB_WMS_STOCK_HST
		WHERE JOBDATE <![CDATA[<=]]> #{jobdate}
	</delete>

	<!-- TB_IF_PRODUCTSKU_HST 저장 -->
	<insert id="createWmsStockHst"  parameterType="String" timeout="300">
		/* TsbGoods.createWmsStockHst */
		INSERT INTO TB_WMS_STOCK_HST(
		       JOBDATE
		      ,PRODUCT_NO
		      ,SKU_CODE
		      ,LOC_QTY
		      ,PICKING_QTY
		      ,EOUT_PICKING_QTY
		)
		SELECT #{jobdate}
		      ,PRODUCT_NO
		      ,SKU_CODE
		      ,LOC_QTY
		      ,PICKING_QTY
		      ,EOUT_PICKING_QTY
		FROM TB_WMS_STOCK
	</insert>

	<!-- TB_GOODS_STOCK 삭제 -->
	<delete id="deleteGoodsDispStock" timeout="300">
		/* TsbGoods.deleteGoodsDispStock */
		DELETE FROM TB_GOODS_STOCK
	</delete>

	<!-- TB_GOODS_STOCK 저장 -->
	<insert id="createGoodsDispStock"  parameterType="String" timeout="300">
		/* TsbGoods.createGoodsDispStock */
		INSERT INTO TB_GOODS_STOCK (
		       GOODS_CD
		     , STOCK_QTY
		     , REG_DT
		)
		SELECT G.GOODS_CD
		     , SUM(S.CURR_STOCK_QTY - S.BASE_STOCK_QTY) AS STOCK_QTY
		     , NOW()                                    AS REG_DT
		FROM   TB_GOODS G
		     , VW_STOCK S
		WHERE  G.GOODS_CD = S.GOODS_CD
		AND    G.GOODS_TYPE = 'G056_N' /*일반상품*/
		AND    G.GOODS_STAT = 'G008_90' /*승인된상품*/
		AND    G.SELF_MALL_YN = 'Y' /*자사몰에노출하는넘만*/
		AND    NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT /*유효한판매기간*/
		AND    S.SOLDOUT_YN = 'N' /*품절이아닌넘*/
		AND    S.DISP_YN = 'Y' /*사이즈노출하는넘만*/
		AND    S.CURR_STOCK_QTY - S.BASE_STOCK_QTY > 0/*재고있는넘만*/
		GROUP  BY G.GOODS_CD
		UNION ALL
		SELECT G.GOODS_CD
		     , SUM(S.CURR_STOCK_QTY - S.BASE_STOCK_QTY) AS STOCK_QTY
		     , NOW()                                    AS REG_DT
		FROM   TB_GOODS G
		     , TB_GOODS_COMPOSE GC
		     , VW_STOCK S
		WHERE  G.GOODS_CD = GC.GOODS_CD
		AND    GC.COMPS_GOODS_CD = S.GOODS_CD
		AND    G.GOODS_TYPE != 'G056_N' /*일반상품이 아닌넘*/
		AND    G.GOODS_STAT = 'G008_90' /*승인된상품*/
		-- AND    G.SELF_GOODS_YN = 'Y' /*자사상품만(세트상품과 딜상품은 자사상품만)*/
		AND    G.SELF_MALL_YN = 'Y' /*자사몰에노출하는넘만*/
		AND    NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT /*유효한판매기간*/
		AND    GC.USE_YN = 'Y' /*사용하는구성상품*/
		AND    S.SOLDOUT_YN = 'N' /*품절이아닌넘*/
		AND    S.DISP_YN = 'Y' /*사이즈노출하는넘만*/
		AND    S.CURR_STOCK_QTY - S.BASE_STOCK_QTY > 0 /*재고있는넘만*/
		GROUP  BY G.GOODS_CD
	</insert>

	<!-- WMS 재고 임시 테이블 삭제 -->
	<delete id="deleteTbOptionSyncTemp"   timeout="300">
		/* TsbGoods.deleteTbOptionSyncTemp */
		DELETE FROM TB_OPTION_SYNC_TMP
	</delete>

	<!--WMS 재고 임시 테이블 이관  -->
	<insert id="createTbOptionSyncTemp"   timeout="300">
		/* TsbGoods.createTbOptionSyncTemp */
		INSERT INTO TB_OPTION_SYNC_TMP
		(
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , STORE_MAX_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , ERP_STOCK_LINK_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , 0 AS BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , 0 AS STORE_STOCK_QTY
		     , 0 AS STORE_MAX_QTY
		     , 0 AS ADD_PRICE
		     , 'N' AS SOLDOUT_YN
		     , DISP_ORD
		     , 'Y' AS DISP_YN
		     , ERP_STOCK_LINK_YN
		     , 99999 AS REG_NO
		     , NOW() AS REG_DT
		     , 99999 AS UPD_NO
		     , NOW() AS UPD_DT
		FROM (
		      SELECT B.GOODS_CD
		           , A.SKU_CODE AS OPT_CD
		           , E.OPT_CD1
		           , E.OPT_CD2
		           , E.SKU_MODEL_NO
		           , E.PRODUCT_NO
		           , E.PRODUCT_CODE
		           , ROUND((GREATEST(A.LOC_QTY, 0) -  GREATEST(A.PICKING_QTY, 0)  - GREATEST(A.EOUT_PICKING_QTY, 0)) * (IFNULL(D.STOCK_APPL_RATE, 0)/100)) AS CURR_STOCK_QTY
		           , E.STORE_STOCK_QTY
		           , E.DISP_ORD
		           , B.ERP_STOCK_LINK_YN
		      FROM TB_WMS_STOCK A
		      INNER JOIN TB_OPTION E ON A.PRODUCT_NO = E.PRODUCT_NO
		                             AND A.SKU_CODE = E.OPT_CD
		      INNER JOIN TB_GOODS B ON E.GOODS_CD = B.GOODS_CD
		                            AND B.SELF_GOODS_YN = 'Y'
		                            AND B.ERP_STOCK_LINK_YN = 'Y'
		      INNER JOIN TB_STOCK_SYNC_BASE C ON B.BRAND_CD = C.BRAND_CD
		                                      AND C.STOCK_SYNC_YN = 'Y'
		                                      AND C.DELV_LOC_CD = 'ST0001'    -- WMS 매장코드
		      INNER JOIN TB_DELIVERY_LOC D ON B.SUPPLY_COMP_CD = D.SUPPLY_COMP_CD
		                                   AND C.DELV_LOC_CD = D.DELV_LOC_CD
		                                   AND D.USE_YN = 'Y'
		                                   AND D.DELV_LOC_CD = 'ST0001'    -- WMS 매장코드
		     ) Z
	</insert>

	<!--매장 재고 임시 테이블 이관  -->
	<insert id="saveTbOptionSyncTemp"   timeout="300">
		/* TsbGoods.saveTbOptionSyncTemp */
		INSERT INTO TB_OPTION_SYNC_TMP
		(
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , STORE_MAX_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , ERP_STOCK_LINK_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , 0 AS BASE_STOCK_QTY
		     , 0 AS CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , 0 AS STORE_MAX_QTY
		     , 0 AS ADD_PRICE
		     , 'N' AS SOLDOUT_YN
		     , DISP_ORD
		     , 'Y' AS DISP_YN
		     , ERP_STOCK_LINK_YN
		     , 99999 AS REG_NO
		     , NOW() AS REG_DT
		     , 99999 AS UPD_NO
		     , NOW() AS UPD_DT
		FROM (
		      SELECT E.GOODS_CD
		           , E.OPT_CD
		           , E.OPT_CD1
		           , E.OPT_CD2
		           , E.SKU_MODEL_NO
		           , E.PRODUCT_NO
		           , E.PRODUCT_CODE
		           , E.CURR_STOCK_QTY
		           , SUM(A.STORE_STOCK_QTY) AS STORE_STOCK_QTY
		           , B.ERP_STOCK_LINK_YN
		           , E.DISP_ORD
		      FROM TB_SHOP_STOCK A
		      INNER JOIN TB_OPTION E ON A.CD_STYLE = E.GOODS_CD
		                             AND A.OPT_CD = E.OPT_CD
		      INNER JOIN TB_GOODS B ON E.GOODS_CD = B.GOODS_CD
		                            AND B.SELF_GOODS_YN = 'Y'
		                            AND B.ERP_STOCK_LINK_YN = 'Y'
		      GROUP BY GOODS_CD , OPT_CD , OPT_CD1 , OPT_CD2 , SKU_MODEL_NO , PRODUCT_NO , PRODUCT_CODE , CURR_STOCK_QTY , DISP_ORD , ERP_STOCK_LINK_YN
		  ) Z
		ON DUPLICATE KEY UPDATE
		     STORE_STOCK_QTY = Z.STORE_STOCK_QTY
	</insert>

	<!-- 입점상품 SELL_QTY 목록 조회 -->
	<select id="getGoodsSelfNoStockList" resultType="Option"   timeout="300">
		/* TsbGoods.getGoodsSelfNoStockList */
		SELECT A.GOODS_CD
		     , A.OPT_CD
		     , SUM(
		           (CASE A.SELL_GB WHEN '10' THEN 1
		                          WHEN '20' THEN 1
		                          ELSE -1 END) * A.SELL_QTY
		           ) AS SALE_STOCK_QTY
		FROM TB_SELL_QTY A
		   , TB_GOODS B
		WHERE A.GOODS_CD = B.GOODS_CD
		AND B.SELF_GOODS_YN = 'N'  -- 입점상품
		GROUP BY A.GOODS_CD, A.OPT_CD
	</select>

	<!-- 상품 옵션/재고 정보 이력생성 -->
	<insert id="createStockHst" parameterType="Option"   timeout="300">
		/* TsaGoods.createStockHst */
		INSERT INTO TB_OPTION_HST
		(     OPTION_HST_SQ
		    , GOODS_CD
		    , OPT_CD
		    , OPT_CD1
		    , OPT_CD2
		    , SKU_MODEL_NO
		    , PRODUCT_NO
		    , PRODUCT_CODE
		    , BASE_STOCK_QTY
		    , CURR_STOCK_QTY
		    , STORE_STOCK_QTY
		    , STORE_MAX_QTY
		    , ADD_PRICE
		    , SOLDOUT_YN
		    , DISP_ORD
		    , DISP_YN
		    , REG_NO
		    , REG_DT
		    , UPD_NO
		    , UPD_DT
		)
		SELECT NULL
		     , GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , STORE_MAX_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , #{regNo}
		     , NOW()
		     , UPD_NO
		     , UPD_DT
		FROM TB_OPTION
		WHERE GOODS_CD = #{goodsCd}
		AND OPT_CD = #{optCd}
	</insert>

	<!-- 상품 재고 정보 저장 -->
	<update id="updateStock" parameterType="Option"  timeout="300">
		/* TsbGoods.updateStock */
		UPDATE TB_OPTION
		SET CURR_STOCK_QTY = GREATEST(CURR_STOCK_QTY - #{saleStockQty},0)
		  , UPD_NO = #{updNo}
		  , UPD_DT = NOW()
		WHERE GOODS_CD = #{goodsCd}
		AND OPT_CD = #{optCd}
	</update>

	<!-- WMS 재고 임시 테이블 삭제 -->
	<delete id="deleteGoodsOptionSync"  timeout="300">
		/* TsbGoods.deleteGoodsOptionSync */
		TRUNCATE TABLE TB_OPTION_SYNC
	</delete>

	<!--TB_OPTION = > TB_OPTION_SYNC 적용 -->
	<insert id="createGoodsOptionSync"  timeout="300">
		/* TsbGoods.createGoodsOptionSync */
		INSERT INTO TB_OPTION_SYNC
		(
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , STORE_MAX_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , ERP_STOCK_LINK_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT B.GOODS_CD
		     , B.OPT_CD
		     , B.OPT_CD1
		     , B.OPT_CD2
		     , B.SKU_MODEL_NO
		     , B.PRODUCT_NO
		     , B.PRODUCT_CODE
		     , B.BASE_STOCK_QTY
		     , 0 AS CURR_STOCK_QTY
		     , 0 AS STORE_STOCK_QTY
		     , 0 AS STORE_MAX_QTY
		     , B.ADD_PRICE
		     , B.SOLDOUT_YN
		     , B.DISP_ORD
		     , B.DISP_YN
		     , A.ERP_STOCK_LINK_YN
		     , B.REG_NO
		     , B.REG_DT
		     , B.UPD_NO
		     , B.UPD_DT
		FROM   TB_GOODS A, TB_OPTION B
		WHERE  A.ERP_STOCK_LINK_YN = 'Y'
		AND    A.SELF_GOODS_YN ='Y'
		AND    A.GOODS_CD = B.GOODS_CD
	</insert>

	<!-- TB_OPTION_SYNC_TMP => TB_OPTION_SYNC 변경적용-->
	<update id="saveGoodsOptionSyncSync"  timeout="300">
		/* TsbGoods.saveGoodsOptionSyncSync */
		UPDATE TB_OPTION_SYNC A, (SELECT GOODS_CD
		                               , OPT_CD
		                               , CURR_STOCK_QTY
		                               , STORE_STOCK_QTY
		                               , ERP_STOCK_LINK_YN
		                          FROM TB_OPTION_SYNC_TMP
		                          WHERE ERP_STOCK_LINK_YN = 'Y'
		                          ) B
		SET A.CURR_STOCK_QTY = B.CURR_STOCK_QTY
		  , A.STORE_STOCK_QTY = B.STORE_STOCK_QTY
		  , A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD = B.OPT_CD
		<![CDATA[
		AND (A.CURR_STOCK_QTY <> B.CURR_STOCK_QTY
		    OR
		    A.STORE_STOCK_QTY <> B.STORE_STOCK_QTY
		    )
		]]>
	</update>

	<!-- TB_OPTION_SYNC 매장별 최대구매수량 적용-->
	<update id="updateGoodsOptionSyncSync"  timeout="300">
		/* TsbGoods.updateGoodsOptionSyncSync */
		UPDATE TB_OPTION_SYNC A, (SELECT GOODS_CD
		                               , OPT_CD
		                               , STORE_MAX_QTY
		                          FROM (
		                                SELECT GOODS_CD
		                                     , OPT_CD
		                                     , GREATEST(CURR_STOCK_QTY, QT_STOCK) AS STORE_MAX_QTY
		                                FROM (
		                                         SELECT OS.GOODS_CD
		                                              , OS.OPT_CD
		                                              , GREATEST(OS.CURR_STOCK_QTY, 0)  AS CURR_STOCK_QTY
		                                              , GREATEST(IFNULL(SS.QT_STOCK, 0), 0) AS QT_STOCK
		                                         FROM TB_OPTION_SYNC OS
		                                         LEFT OUTER JOIN TB_SHOP_STOCK SS ON OS.GOODS_CD = SS.CD_STYLE
		                                                                       AND OS.OPT_CD = SS.OPT_CD
		                                         WHERE OS.ERP_STOCK_LINK_YN = 'Y'
		                                      ) Z
		                               ) Y WHERE STORE_MAX_QTY > 0
		                           ) B
		SET A.STORE_MAX_QTY = B.STORE_MAX_QTY
		  , A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD = B.OPT_CD
		<![CDATA[
		AND A.STORE_MAX_QTY <> B.STORE_MAX_QTY
		]]>
	</update>

	<!-- WMS 임시 테이블에서 실테이블로 변경 -->
	<update id="saveGoodsOptionNew"  timeout="300">
		/* TsbGoods.saveGoodsOptionNew */
		UPDATE TB_OPTION A, (SELECT GOODS_CD
		                          , OPT_CD
		                          , CURR_STOCK_QTY
		                          , STORE_STOCK_QTY
		                          , STORE_MAX_QTY
		                          , ERP_STOCK_LINK_YN
		                      FROM TB_OPTION_SYNC
		                      WHERE ERP_STOCK_LINK_YN = 'Y'
		                      ) B
		SET A.CURR_STOCK_QTY = B.CURR_STOCK_QTY
		  , A.STORE_STOCK_QTY = B.STORE_STOCK_QTY
		  , A.STORE_MAX_QTY = B.STORE_MAX_QTY
		  , A.UPD_NO = 99999
		  , A.UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD = B.OPT_CD
		<![CDATA[
		AND (A.CURR_STOCK_QTY <> B.CURR_STOCK_QTY
		    OR
		    A.STORE_STOCK_QTY <> B.STORE_STOCK_QTY
		    OR
		    A.STORE_MAX_QTY <> B.STORE_MAX_QTY
		    )
		]]>
	</update>

	<!-- TB_SELL_QTY_TMP 테이블 삭제 -->
	<delete id="deleteTtbSellQtyTemp"  timeout="300">
		/* TsbGoods.deleteTtbSellQtyTemp */
		TRUNCATE TABLE TB_SELL_QTY_TMP
	</delete>

	<!-- TB_SELL_QTY_TMP 테이블 삭제 -->
	<insert id="createTtbSellQtyTemp"  timeout="300">
		/* TsbGoods.createTtbSellQtyTemp */
		INSERT INTO TB_SELL_QTY_TMP (
		      GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , ORD_CHG_SQ
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		)
		SELECT GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , ORD_CHG_SQ
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		FROM TB_SELL_QTY
	</insert>

	<!-- 주문적재 테이블 삭제 -->
	<delete id="deleteSellQty"  timeout="300">
		/* TsbGoods.deleteSellQty */
		DELETE FROM TB_SELL_QTY
	</delete>

	<!-- TB_SELL_QTY 테이블 생성 -->
	<insert id="createTtbSellQty"  timeout="300">
		/* TsbGoods.createTtbSellQty */
		<![CDATA[
		INSERT INTO TB_SELL_QTY (
		      GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , ORD_CHG_SQ
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		)
		SELECT  GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , ORD_CHG_SQ
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		FROM TB_SELL_QTY_TMP
		WHERE ORD_DTL_NO IN (
		                     SELECT OD.ORD_DTL_NO
		                     FROM TB_ORDER_DETAIL OD
		                        , TB_ORDER_DETAIL_ITEM ODI
		                        , TB_GOODS G
		                     WHERE OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		                     AND OD.ORD_DTL_STAT IN ('G013_10','G013_20','G013_25','G013_30','G013_35','G013_40') -- 입금대기, 결제완료, 교환대기, 상품준비중,출고처지정,배송준비중
		                     AND OD.GOODS_CD = G.GOODS_CD
		                     AND G.SELF_GOODS_YN ='Y'
		                     AND OD.REG_DT >= DATE_ADD(DATE_FORMAT('20210831','%Y%m%d%H%i%S') , INTERVAL 0 DAY) 
		                    )
		]]>
	</insert>

	<!-- TB_SELL_QTY 테이블 생성 -->
	<insert id="createTtbSellQtyByOrderDetail"  timeout="300">
		/* TsbGoods.createTtbSellQtyByOrderDetail */
		<![CDATA[
		INSERT INTO TB_SELL_QTY (
		      GOODS_CD
		    , OPT_CD
		    , SELL_GB
		    , ORD_DTL_NO
		    , AGENT_ORDER_ID
		    , EXTMALL_ORDER_ID
		    , SELL_QTY
		    , REG_NO
		    , REG_DT
		)
		SELECT ITEM_CD
		     , OPT_CD
		     , SELL_GB
		     , ORD_DTL_NO
		     , AGENT_ORDER_ID
		     , EXTMALL_ORDER_ID
		     , SELL_QTY
		     , 0
		     , NOW()
		FROM (
		      SELECT ODI.ITEM_CD
		           , ODI.OPT_CD
		           , '10' AS SELL_GB -- 주문건
		           , OD.ORD_DTL_NO
		           , OD.AGENT_ORDER_ID
		           , OD.EXTMALL_ORDER_ID
		           , SUM((OD.ORD_QTY - OD.CNCL_RTN_QTY) * ODI.ITEM_QTY) AS SELL_QTY
		      FROM TB_ORDER_DETAIL OD
		         , TB_ORDER_DETAIL_ITEM ODI
		      WHERE OD.ORD_DTL_NO = ODI.ORD_DTL_NO
		      AND OD.ORD_DTL_STAT IN ('G013_10','G013_20','G013_25','G013_30','G013_35','G013_40') -- 입금대기, 결제완료, 교환대기, 상품준비중,출고처지정,배송준비중
		      AND EXISTS (SELECT 1 FROM TB_OPTION X WHERE X.GOODS_CD = ODI.ITEM_CD AND X.OPT_CD = ODI.OPT_CD)
		      AND OD.REG_DT >= DATE_ADD(DATE_FORMAT('20210831','%Y%m%d%H%i%S') , INTERVAL 0 DAY) 
		      GROUP BY ODI.ITEM_CD, ODI.OPT_CD, OD.ORD_DTL_NO, OD.AGENT_ORDER_ID, OD.EXTMALL_ORDER_ID
		     ) A
		WHERE NOT EXISTS (SELECT 1
		                  FROM TB_SELL_QTY Y
		                  WHERE Y.GOODS_CD = A.ITEM_CD
		                  AND Y.OPT_CD = A.OPT_CD
		                  AND Y.SELL_GB = A.SELL_GB
		                  AND Y.ORD_DTL_NO = A.ORD_DTL_NO
		                  )
		]]>
	</insert>

	<!-- 입고상품중 온라인 옵션이 없는 상품옵션 추가  -->
	<insert id="createGoodsOptionList" parameterType="String" timeout="300">
		/* TsbGoods.createGoodsOptionList */
		INSERT INTO TB_OPTION(
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , SKU_MODEL_NO
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , BASE_STOCK_QTY
		     , CURR_STOCK_QTY
		     , STORE_STOCK_QTY
		     , STORE_MAX_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT E.GOODS_CD
		     , E.SKUCODE AS OPT_CD
		     , F.COLOR_CD AS OPT_CD1
		     , E.OPTION2 AS OPT_CD2
		     , E.SKUMODELNO AS  SKU_MODEL_NO
		     , E.PRODUCTNO AS PRODUCT_NO
		     , E.PRODUCTCODE AS PRODUCT_CODE
		     , 0 AS BASE_STOCK_QTY
		     , E.NORMALQTY AS CURR_STOCK_QTY
		     , 0 AS STORE_STOCK_QTY
		     , E.NORMALQTY AS STORE_MAX_QTY
		     , 0 AS ADD_PRICE
		     , 'N' AS SOLDOUT_YN
		     , IFNULL((SELECT (MAX(DISP_ORD) + 1)
			        FROM TB_OPTION WHERE GOODS_CD = E.GOODS_CD) , ROW_NUMBER() OVER(PARTITION BY GOODS_CD ORDER BY OPT_CD)) AS DISP_ORD
		     , 'Y' AS DISP_YN
		     , 99999 AS REG_NO
		     , NOW() AS REG_DT
		     , 99999 AS UPD_NO
		     , NOW() AS UPD_DT
		FROM (
		      SELECT A.SKUCODE
		           , A.NORMALQTY
		           , B.SKUMODELNO
		           , MAX(CASE WHEN INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), 'F20') = 1 THEN MID(REPLACE(B.SKUMODELNO, ' ' , ''),13,2)
		                      WHEN INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') > 0 AND INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') <![CDATA[<]]> 10 THEN MID(REPLACE(B.SKUMODELNO, ' ' , ''),12,2)
		                      ELSE MID(REPLACE(B.SKUMODELNO, ' ' , ''),11,2) END) AS WMS_COLOR_CD
		          -- , RIGHT(LEFT(REPLACE(B.SKUMODELNO,'-',''),12),2) AS WMS_COLOR_CD
		           , B.PRODUCTNO
		           , B.PRODUCTCODE
		           , B.OPTION1
		           , B.OPTION2
		           , C.SUPPLY_COMP_CD
		           , C.BRAND_CD
		           , C.GOODS_CD
		      FROM TB_IF_INCOMELOTITEM A                                              -- 입고상품
		      INNER JOIN TB_IF_PRODUCTSKU B ON A.PRODUCTNO = B.PRODUCTNO              -- wms 상품코드확인
		                                    AND A.SKUCODE = B.SKUCODE
		      LEFT OUTER JOIN TB_OPTION D ON B.PRODUCTNO = D.PRODUCT_NO
		                                  AND A.SKUCODE = D.OPT_CD
		                                  AND D.OPT_CD IS NULL                        -- 온라인 상품 옵션 미등록건 추출
		      INNER JOIN TB_GOODS C ON D.GOODS_CD = C.GOODS_CD                        -- 온라인 상품등록 확인
		      WHERE 1 = 1
		      AND LENGTH(REPLACE(B.SKUMODELNO, ' ' , '')) >= 13                       -- 품번+색상+사이즈 최소길이
		      <choose>
		      <when test='jobdate != null and jobdate != ""'>
		      AND DATEINCOME >= DATE_FORMAT(#{jobdate}, '%Y-%m-%d %H:%i:%S')
		      <![CDATA[
		      AND DATEINCOME < DATE_FORMAT(DATE_ADD(#{jobdate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
		      ]]>
		      </when>
		      <otherwise>
		      AND DATEINCOME >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY),'%Y%m%d' ), '%Y-%m-%d %H:%i:%S')
		      <![CDATA[
		      AND DATEINCOME < DATE_FORMAT(DATE_FORMAT(NOW() ,'%Y%m%d'), '%Y-%m-%d %H:%i:%S')
		      ]]>
		      </otherwise>
		      </choose>
		      ) E
		      INNER JOIN TB_WMS_COLOR_MAPPING F ON E.SUPPLY_COMP_CD = F.SUPPLY_COMP_CD
		                                        AND F.USE_YN ='Y'

	</insert>

	<!-- 상품혜택임시 테이블 truncate -->
	<delete id="truncateTemporaryGoodsBenefit">
		/* TsbGoods.truncateTemporaryGoodsBenefit */
		TRUNCATE TABLE TB_GOODS_BENEFIT_TMP
	</delete>

	<!-- 상품혜택임시- 쿠폰할인 생성(적용범위 개별) -->
	<insert id="createTemporaryGoodsBenefitScopeCouponDiscount" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitScopeCouponDiscount */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		WITH CTE AS (
		    SELECT CP.CPN_ID , CP.BUY_LIMIT_AMT, CPR.CPN_REFVAL_SQ
		    FROM TB_COUPON CP
		    INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_ID=CPR.CPN_ID
		    WHERE  CP.SITE_CD = 'G000_10' /*전시사이트*/
		    AND    CP.CPN_STAT = 'G232_11' /*쿠폰인 진행중인 쿠폰만*/
		    AND    CP.DOWN_ABL_YN = 'Y' /*상품상세 다운로드 가능여부*/
		    AND    NOW() <![CDATA[<=]]> IF(CP.PD_GB = 'D',DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 DAY),'%Y-%m-%d %H:%i:%S'),CP.AVAIL_EDDT)
		    AND    NOW() BETWEEN CP.DOWN_STDT AND CP.DOWN_EDDT
		    AND    CP.CPN_TYPE IN ('G230_11','G230_20','G230_30') /*상품쿠폰,주문서쿠폰,배송비쿠폰*/
		    AND    CP.DC_CD_GB = 'G233_00'                         -- 일반유형(할인쿠폰구분)
		    AND    (SELECT COUNT(1)
		            FROM   TB_COUPON_CUST_GBN
		            WHERE  CPN_ID = CP.CPN_ID
		            AND    USABLE_CUST_GB IN ('G100_10') /*사용가능고객구분:일반*/
		           ) >= 1
		    AND    CP.APPLY_SCOPE = 'I'   -- 개별
		    AND    CP.DC_PVAL > 0
		    AND    IF(CP.TOT_PUB_LIMIT_QTY = 0,9999999999,CP.TOT_PUB_LIMIT_QTY) > (SELECT COUNT(1)
		                                                                           FROM   TB_CUST_COUPON
		                                                                           WHERE  CPN_ID = CP.CPN_ID
		                                                                          ) /*총발행제한수*/
		)
		  , CTE_DETAIL AS (

		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.GOODS_CD GKEY, G.GOODS_CD
		        FROM CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.GOODS_CD  AND CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		        WHERE CPR.CPN_TARGET = 'G260_10'

		        UNION

		        /* 적용대상:카테고리*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.GOODS_CD GKEY, G.GOODS_CD
		        FROM  CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN
		                    (SELECT LEAF_CATE_NO
		                          , CATE_LVL
		                          , CATE_NO
		                     FROM (
		                    	   SELECT LEAF_CATE_NO ,
		                                CASE WHEN X = 1 THEN 'CATE1_NO'  -- 컬럼과 매핑(컬럼명)
		                                     WHEN X = 2 THEN 'CATE2_NO'
		                                     WHEN X = 3 THEN 'CATE3_NO'
		                                     WHEN X = 4 THEN 'CATE4_NO'
		                                     ELSE  'CATE5_NO'
		                                END CATE_LVL,
		                                CASE WHEN X = 1 THEN CATE1_NO  -- 컬럼과 매핑(컬럼 데이터)
		                                     WHEN X = 2 THEN CATE2_NO
		                                     WHEN X = 3 THEN CATE3_NO
		                                     WHEN X = 4 THEN CATE4_NO
		                                     ELSE  CATE5_NO
		                                 END CATE_NO
		                           FROM (
		                                  ( SELECT LEAF_CATE_NO
		                                         , CATE1_NO
		                                         , CATE2_NO
		                                         , CATE3_NO
		                                         , CATE4_NO
		                                         , CATE5_NO
		                                    FROM  TB_CATE_4SRCH
		                                    WHERE SITE_CD = 'G000_10'
		                                    AND CATE_TYPE = 'G031_10') A, -- 상품타입
		                                  (SELECT 1 AS X		-- UNPIVOT 컬럼 수 만큼 선언
		                                   UNION ALL SELECT 2 AS X
		                                   UNION ALL SELECT 3 AS X
		                                   UNION ALL SELECT 4 AS X
		                                   UNION ALL SELECT 5 AS X
		                                 ) B
		                                )
		                           ORDER BY LEAF_CATE_NO, CATE_LVL
		                          ) K
		                          WHERE CATE_NO IS NOT NULL
		                                ) X ON CPR.REF_VAL=X.CATE_NO
		        INNER JOIN TB_CATE_GOODS Y  ON X.LEAF_CATE_NO = Y.CATE_NO
		        INNER JOIN TB_GOODS G ON G.GOODS_CD=Y.GOODS_CD AND CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		        WHERE CPR.CPN_TARGET =  'G260_11'
		        AND IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		        AND IFNULL(REF_BRAND_CD,G.BRAND_CD) = G.BRAND_CD

		        UNION

		        /* 적용대상:브랜드*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.BRAND_CD GKEY, G.GOODS_CD
		        FROM  CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.BRAND_CD AND CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		        WHERE CPR.CPN_TARGET = 'G260_12'
		        AND   IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB

		        UNION

		        /* 적용대상:공급업체*/
		        SELECT CP.CPN_REFVAL_SQ, CPR.CPN_ID, CPR.REF_VAL, G.SUPPLY_COMP_CD GKEY, G.GOODS_CD
		        FROM CTE CP
		        INNER JOIN TB_COUPON_REFVAL CPR ON CP.CPN_REFVAL_SQ=CPR.CPN_REFVAL_SQ
		        INNER JOIN TB_GOODS G ON CPR.REF_VAL=G.SUPPLY_COMP_CD AND CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		        WHERE CPR.CPN_TARGET = 'G260_13'
		)
		SELECT G.GOODS_CD
		     , '10'       AS BENEFIT_GB /*쿠폰할인*/
		FROM   (
		SELECT CP.CPN_ID
		      ,G.GOODS_CD
		FROM   CTE_DETAIL CC
		INNER JOIN TB_GOODS G ON CC.GOODS_CD = G.GOODS_CD
		INNER JOIN TB_COUPON CP ON CC.CPN_ID = CP.CPN_ID
		WHERE NOT EXISTS (
		                  SELECT 1
		                  FROM   TB_COUPON_REFVAL SCPR
		                  WHERE  SCPR.CPN_ID = CP.CPN_ID
		                  AND    SCPR.REF_VAL = G.GOODS_CD
		                  AND    SCPR.DEL_YN = 'N' /*삭제안된넘*/
		                  AND    SCPR.CPN_TARGET = 'G260_14' /* 제외상품*/
		                 )
		AND NOT EXISTS (
		                SELECT 1
		               FROM TB_COUPON_BAN_GOODS
		               WHERE GOODS_CD = G.GOODS_CD
		               AND DEL_YN = 'N'
		                )
		AND NOT EXISTS (
		                SELECT 1
		               FROM TB_GOODS_BENEFIT_TMP
		               WHERE GOODS_CD = G.GOODS_CD
		               AND BENEFIT_GB = '10'
		                )
		   ) G
		GROUP  BY G.GOODS_CD
		HAVING COUNT(*) <![CDATA[>]]> 0
	</insert>

	<!-- 상품혜택임시- 쿠폰할인 생성(적용범위 전체) -->
	<insert id="createTemporaryGoodsBenefitAllCouponDiscount" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitAllCouponDiscount */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		WITH CTE AS (
 		    SELECT CPN_ID , BUY_LIMIT_AMT
		    FROM TB_COUPON CP
		    WHERE  CP.SITE_CD = 'G000_10' /*전시사이트*/
		    AND    CP.CPN_STAT = 'G232_11' /*쿠폰인 진행중인 쿠폰만*/
		    AND    CP.DOWN_ABL_YN = 'Y' /*상품상세 다운로드 가능여부*/
		    AND    NOW() <![CDATA[<=]]> IF(CP.PD_GB = 'D',DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 DAY),'%Y-%m-%d %H:%i:%S'),CP.AVAIL_EDDT)
		    AND    NOW() BETWEEN CP.DOWN_STDT AND CP.DOWN_EDDT
		    AND    CP.CPN_TYPE IN ('G230_11','G230_20','G230_30') /*상품쿠폰,주문서쿠폰,배송비쿠폰*/
		    AND    CP.DC_CD_GB = 'G233_00'                         -- 일반유형(할인쿠폰구분)
		    AND    (SELECT COUNT(1)
		            FROM   TB_COUPON_CUST_GBN
		            WHERE  CPN_ID = CP.CPN_ID
		            AND    USABLE_CUST_GB IN ('G100_10') /*사용가능고객구분:일반*/
		           ) >= 1
		    AND    CP.APPLY_SCOPE = 'A'   -- 전체
		    AND    CP.DC_PVAL > 0
		    AND    IF(CP.TOT_PUB_LIMIT_QTY = 0,9999999999,CP.TOT_PUB_LIMIT_QTY) > (SELECT COUNT(1)
		                                                                           FROM   TB_CUST_COUPON
		                                                                           WHERE  CPN_ID = CP.CPN_ID
		                                                                          ) /*총발행제한수*/
		      )
		      , CTE_DETAIL AS (
		                       SELECT CP.CPN_ID, G.GOODS_CD
		                       FROM  CTE CP
		                       INNER JOIN TB_GOODS G ON CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		                      )
		SELECT G.GOODS_CD
		    , '10'       AS BENEFIT_GB /*쿠폰할인*/
		FROM (
		    SELECT CP.CPN_ID
		          ,G.GOODS_CD
		    FROM   CTE_DETAIL CP
		    INNER JOIN TB_GOODS G ON CP.GOODS_CD = G.GOODS_CD
		    WHERE NOT EXISTS (
		                      SELECT 1
		                      FROM   TB_COUPON_REFVAL SCPR
		                      WHERE  SCPR.CPN_ID = CP.CPN_ID
		                      AND    SCPR.REF_VAL = G.GOODS_CD
		                      AND    SCPR.DEL_YN = 'N' /*삭제안된넘*/
		                      AND    SCPR.CPN_TARGET = 'G260_14' /* 제외상품*/
		                     )
		    AND NOT EXISTS (
		                    SELECT 1
		                    FROM TB_COUPON_BAN_GOODS
		                    WHERE GOODS_CD = G.GOODS_CD
		                    AND DEL_YN = 'N'
		                   )

		   ) G
		GROUP  BY G.GOODS_CD
		HAVING COUNT(*) > 0
	</insert>

	<!-- 상품혜택임시-쿠폰할인 생성 -->
	<insert id="createTemporaryGoodsBenefitCouponDiscount_org" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitCouponDiscount_org */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		SELECT G.GOODS_CD
		     , '10'       AS BENEFIT_GB /*쿠폰할인*/
		FROM   TB_GOODS G
		     , TB_COUPON CP
		WHERE  CP.SITE_CD = 'G000_10' /*전시사이트*/
		AND    CP.CPN_STAT = 'G232_11' /*쿠폰인 진행중인 쿠폰만*/
		AND    CP.DOWN_ABL_YN = 'Y' /*상품상세 다운로드 가능여부*/
		AND    NOW() <![CDATA[<=]]> IF(CP.PD_GB = 'D',DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 DAY),'%Y-%m-%d %H:%i:%S'),CP.AVAIL_EDDT)
		AND    NOW() BETWEEN CP.DOWN_STDT AND CP.DOWN_EDDT
		AND    CP.CPN_TYPE IN ('G230_11','G230_20','G230_30') /*상품쿠폰,주문서쿠폰,배송비쿠폰*/
		AND    CP.DC_CD_GB = 'G233_00'                         -- 일반유형(할인쿠폰구분)
		AND    (SELECT COUNT(1)
		        FROM   TB_COUPON_CUST_GBN
		        WHERE  CPN_ID = CP.CPN_ID
		        AND    USABLE_CUST_GB IN ('G100_10') /*사용가능고객구분:일반*/
		       ) <![CDATA[>=]]> 1
		AND    CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		AND    CP.DC_PVAL <![CDATA[>]]> 0
		AND    IF(CP.TOT_PUB_LIMIT_QTY = 0,9999999999,CP.TOT_PUB_LIMIT_QTY) <![CDATA[>]]> (SELECT COUNT(1)
		                                                                                   FROM   TB_CUST_COUPON
		                                                                                   WHERE  CPN_ID = CP.CPN_ID
		                                                                                  ) /*총발행제한수*/
		AND   (
		       (CP.APPLY_SCOPE = 'A' )
		       OR  /*적용대상:상품*/
		       (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1)
		                                  FROM   TB_COUPON_REFVAL
		                                  WHERE  CPN_ID = CP.CPN_ID
		                                  AND    CPN_TARGET = 'G260_10'
		                                  AND    DEL_YN = 'N'
		                                  AND    REF_VAL = G.GOODS_CD
		                                 ) <![CDATA[>]]> 0
		       )
		       OR /*적용대상:카테고리*/
		       (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1)
		                                  FROM   TB_COUPON_REFVAL
		                                  WHERE  CPN_ID = CP.CPN_ID
		                                  AND    CPN_TARGET = 'G260_11'
		                                  AND    DEL_YN = 'N'
		                                  AND    IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		                                  AND    IFNULL(REF_BRAND_CD,G.BRAND_CD) = G.BRAND_CD
		                                  AND    REF_VAL IN (SELECT DISTINCT
		                                                            X.CATE_NO
		                                                     FROM   (
		                                                             SELECT LEAF_CATE_NO
		                                                                  , CATE_LVL
		                                                                  , CATE_NO
		                                                             FROM   (
		                                                                     SELECT LEAF_CATE_NO
		                                                                          , CASE WHEN X = 1 THEN 'CATE1_NO' /*컬럼과 매핑(컬럼명)*/
		                                                                                 WHEN X = 2 THEN 'CATE2_NO'
		                                                                                 WHEN X = 3 THEN 'CATE3_NO'
		                                                                                 WHEN X = 4 THEN 'CATE4_NO'
		                                                                                 ELSE  'CATE5_NO'
		                                                                            END AS CATE_LVL
		                                                                          , CASE WHEN X = 1 THEN CATE1_NO /*컬럼과 매핑(컬럼 데이터)*/
		                                                                                 WHEN X = 2 THEN CATE2_NO
		                                                                                 WHEN X = 3 THEN CATE3_NO
		                                                                                 WHEN X = 4 THEN CATE4_NO
		                                                                                 ELSE  CATE5_NO
		                                                                            END AS CATE_NO
		                                                                     FROM   (SELECT LEAF_CATE_NO
		                                                                                  , CATE1_NO
		                                                                                  , CATE2_NO
		                                                                                  , CATE3_NO
		                                                                                  , CATE4_NO
		                                                                                  , CATE5_NO
		                                                                             FROM   TB_CATE_4SRCH
		                                                                             WHERE  SITE_CD = 'G000_10'
		                                                                             AND    CATE_TYPE = 'G031_10'
		                                                                            ) A
		                                                                          , (
		                                                                             /*UNPIVOT 컬럼 수 만큼 선언*/
		                                                                             SELECT 1 AS X
		                                                                             UNION ALL SELECT 2 AS X
		                                                                             UNION ALL SELECT 3 AS X
		                                                                             UNION ALL SELECT 4 AS X
		                                                                             UNION ALL SELECT 5 AS X
		                                                                            ) B
		                                                                     ORDER  BY LEAF_CATE_NO, CATE_LVL
		                                                                    ) K
		                                                             WHERE  CATE_NO IS NOT NULL
		                                                            ) X
		                                                          , (
		                                                             SELECT A.CATE_NO
		                                                                  , A.GOODS_CD
		                                                             FROM   TB_CATE_GOODS A
		                                                             WHERE  GOODS_CD = G.GOODS_CD
		                                                            ) Y
		                                                     WHERE  X.LEAF_CATE_NO = Y.CATE_NO
		                                                    )
		                                 ) <![CDATA[>]]> 0
		       )
		       OR /*적용대상:브랜드*/
		       (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1)
		                                  FROM   TB_COUPON_REFVAL
		                                  WHERE  CPN_ID = CP.CPN_ID
		                                  AND    CPN_TARGET = 'G260_12'
		                                  AND    DEL_YN = 'N'
		                                  AND    REF_VAL = G.BRAND_CD
		                                  AND    IFNULL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		                                 ) <![CDATA[>]]> 0
		       )
		       OR /*적용대상:공급업체*/
		       (CP.APPLY_SCOPE = 'I' AND (SELECT COUNT(1)
		                                  FROM   TB_COUPON_REFVAL
		                                  WHERE  CPN_ID = CP.CPN_ID
		                                  AND    CPN_TARGET = 'G260_13'
		                                  AND    DEL_YN = 'N'
		                                  AND    REF_VAL = G.SUPPLY_COMP_CD
		                                 ) <![CDATA[>]]> 0
		       )
		      )
		AND   NOT EXISTS (SELECT 1
		                  FROM   TB_COUPON_REFVAL SCPR
		                  WHERE  SCPR.CPN_ID = CP.CPN_ID
		                  AND    SCPR.REF_VAL = G.GOODS_CD
		                  AND    SCPR.DEL_YN = 'N' /*삭제안된넘*/
		                  AND    SCPR.CPN_TARGET = 'G260_14' /* 제외상품*/
		                 )
		AND   NOT EXISTS (SELECT 1
		                  FROM   TB_COUPON_BAN_GOODS
		                  WHERE  GOODS_CD = G.GOODS_CD
		                  AND    DEL_YN = 'N'
		                 )
		GROUP  BY G.GOODS_CD
		HAVING COUNT(*) <![CDATA[>]]> 0
	</insert>

	<!-- 상품혜택임시-무료배송 생성 -->
	<insert id="createTemporaryGoodsBenefitFreeDelivery" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitFreeDelivery */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		SELECT GOODS_CD
		     , '20'     AS BENEFIT_GB /*무료배송*/
		FROM   (
        SELECT G.GOODS_CD
             , DFP.MIN_ORD_AMT
             , FN_GET_BENEFIT_PRICE('P',G.GOODS_CD,G.CURR_PRICE,'G100_10') AS CURR_PRICE
        FROM   TB_DELV_FEE_POLICY DFP
		INNER  JOIN TB_GOODS G
		ON     G.DELV_FEE_CD = DFP.DELV_FEE_CD
		AND    G.SUPPLY_COMP_CD = DFP.SUPPLY_COMP_CD
		AND    DFP.USE_YN = 'Y'
		INNER  JOIN TB_GOODS_STOCK GS
		ON     G.GOODS_CD = GS.GOODS_CD
		AND    GS.STOCK_QTY > 0 /*재고있는상품*/
		WHERE  1=1
		AND    G.GOODS_STAT = 'G008_90'
		AND    G.SELF_GOODS_YN = 'Y'
		AND    NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT /*판매기간*/
       ) Z
		WHERE  MIN_ORD_AMT <![CDATA[<=]]> CURR_PRICE
	</insert>

	<!-- 상품혜택임시-사은품 생성 -->
	<insert id="createTemporaryGoodsBenefitFreegift" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitFreegift */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		SELECT GOODS_CD
		     , '30'       AS BENEFIT_GB /*사은품*/
		FROM   (
		        SELECT G.GOODS_CD
		        FROM   TB_FREEGIFT F
		        INNER  JOIN TB_FREEGIFT_SECTION FS
		        ON     F.FREEGIFT_SQ = FS.FREEGIFT_SQ
		        INNER  JOIN TB_FREEGIFT_GOODS FGA
		        ON     F.FREEGIFT_SQ = FGA.FREEGIFT_SQ
		        AND    FGA.GOODS_GB = 'G800_20'
		        AND    FGA.TARGET_GB = 'G260_10'
		        INNER  JOIN TB_GOODS G
		        ON     FGA.TARGET_VAL = G.GOODS_CD
		        LEFT   OUTER JOIN TB_FREEGIFT_GOODS NOT_APPLY
		        ON     F.FREEGIFT_SQ = NOT_APPLY.FREEGIFT_SQ
		        AND    G.GOODS_CD = NOT_APPLY.TARGET_VAL
		        AND    NOT_APPLY.GOODS_GB = 'G800_30'
		        AND    NOT_APPLY.TARGET_GB = 'G260_10'
		        WHERE  NOT_APPLY.TARGET_VAL IS NULL
		        AND    F.DEL_YN = 'N'
		        AND    FS.DEL_YN = 'N'
		        AND    FGA.DEL_YN = 'N'
		        AND    NOW() BETWEEN F.FREEGIFT_STDT AND F.FREEGIFT_EDDT
		        AND    F.SELF_YN = 'Y'
		        AND    F.FREEGIFT_STAT = 'G232_11'
		        GROUP  BY G.GOODS_CD
		        HAVING COUNT(*) <![CDATA[>]]> 0

		        UNION ALL

		        SELECT G.GOODS_CD
		        FROM   TB_FREEGIFT F
		        INNER  JOIN TB_FREEGIFT_SECTION FS
		        ON     F.FREEGIFT_SQ = FS.FREEGIFT_SQ
		        INNER  JOIN TB_FREEGIFT_GOODS_APPLY BRAND_FGA
		        ON     F.FREEGIFT_SQ = BRAND_FGA.FREEGIFT_SQ
		        AND    BRAND_FGA.TARGET_GB = 'G260_12' /*브랜드*/
		        INNER  JOIN TB_FREEGIFT_GOODS_APPLY COMPANY_FGA
		        ON     F.FREEGIFT_SQ = COMPANY_FGA.FREEGIFT_SQ
		        AND    COMPANY_FGA.TARGET_GB = 'G260_13' /*업체*/
		        INNER  JOIN TB_GOODS G
		        ON     BRAND_FGA.TARGET_VAL = G.BRAND_CD
		        AND    COMPANY_FGA.TARGET_VAL = G.SUPPLY_COMP_CD
		        LEFT   OUTER JOIN TB_FREEGIFT_GOODS NOT_APPLY
		        ON     F.FREEGIFT_SQ = NOT_APPLY.FREEGIFT_SQ
		        AND    G.GOODS_CD = NOT_APPLY.TARGET_VAL
		        AND    NOT_APPLY.GOODS_GB = 'G800_30'
		        WHERE  NOT_APPLY.TARGET_VAL IS NULL
		        AND    F.DEL_YN = 'N'
		        AND    FS.DEL_YN = 'N'
		        AND    NOW() BETWEEN F.FREEGIFT_STDT AND F.FREEGIFT_EDDT
		        AND    F.SELF_YN = 'Y'
		        AND    F.FREEGIFT_STAT = 'G232_11'
		        GROUP  BY G.GOODS_CD
		        HAVING COUNT(*) <![CDATA[>]]> 0
		       ) Z
		GROUP  BY GOODS_CD
	</insert>

	<!-- 상품혜택임시-신상 생성 -->
	<insert id="createTemporaryGoodsBenefitNew" timeout="600">
		/* TsbGoods.createTemporaryGoodsBenefitNew */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		SELECT G.GOODS_CD
		     , '40'       AS BENEFIT_GB /*신상(=정상상품)*/
		FROM   TB_GOODS G
		     , TB_GOODS_STOCK GS
		WHERE  G.GOODS_CD = GS.GOODS_CD
		AND    G.GOODS_STAT = 'G008_90' /*승인완료상품*/
		AND    G.SELF_MALL_YN = 'Y' /*몰노출상품*/
		AND    NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT /*판매기간*/
		AND    GS.STOCK_QTY <![CDATA[>]]> 0 /*재고있는상품*/
		AND    G.FORMAL_GB = 'G009_10' /*정상상품(=신상)*/
	</insert>

	<!-- 상품혜택임시-총알배송 생성 -->
	<insert id="createTemporaryGoodsShotDelivery" timeout="600">
		/* TsbGoods.createTemporaryGoodsShotDelivery */
		INSERT INTO TB_GOODS_BENEFIT_TMP (
		       GOODS_CD
		     , BENEFIT_GB
		)
		SELECT G.GOODS_CD
		     , '50'       AS BENEFIT_GB /*총알배송*/
		FROM   TB_GOODS G
		     , TB_GOODS_STOCK GS
		WHERE  G.GOODS_CD = GS.GOODS_CD
		AND    G.GOODS_STAT = 'G008_90' /*승인완료상품*/
		AND    G.SELF_MALL_YN = 'Y' /*몰노출상품*/
		AND    NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT /*판매기간*/
		AND    G.SELF_GOODS_YN = 'Y' /*자사상품만*/
		AND    GS.STOCK_QTY <![CDATA[>]]> 0 /*재고있는상품*/
		AND    NOT EXISTS (SELECT 1
		                   FROM   TB_GOODS_SHOT_DELV_SKIP
		                   WHERE  GOODS_CD = G.GOODS_CD
		                  )
	</insert>

	<!-- 상품혜택 삭제 -->
	<delete id="deleteGoodsBenefit">
		/* TsbGoods.deleteGoodsBenefit */
		DELETE FROM TB_GOODS_BENEFIT
	</delete>

	<!-- 상품혜택 생성 -->
	<insert id="createGoodsBenefit"  timeout="300">
		/* TsbGoods.createGoodsBenefit */
		INSERT INTO TB_GOODS_BENEFIT (
		       GOODS_CD
		     , BENEFIT_GB
		     , REG_NO
		     , REG_DT
		)
		SELECT DISTINCT
		       GOODS_CD
		     , BENEFIT_GB
		     , 99999      AS REG_NO
		     , NOW()      AS REG_DT
		FROM   (
		        SELECT GOODS_CD
		             , BENEFIT_GB
		        FROM   TB_GOODS_BENEFIT_TMP
		        UNION ALL
		        SELECT GC.GOODS_CD
		             , G.BENEFIT_GB
		        FROM   TB_GOODS_BENEFIT_TMP G
		             , TB_GOODS_COMPOSE GC
		        WHERE  G.GOODS_CD = GC.COMPS_GOODS_CD
		       ) Z
	</insert>

	<!-- TB_SHOP_STOCK 삭제 -->
	<delete id="deleteShopStock"  timeout="300">
		/* TsbGoods.deleteShopStock */
		DELETE FROM TB_SHOP_STOCK
	</delete>

	<!-- TB_SHOP_STOCK 저장 -->
	<insert id="createShopStock"  timeout="300">
		/* TsbGoods.createShopStock */
		INSERT INTO TB_SHOP_STOCK(
		      SUPPLY_COMP_CD
		    , CD_SHOP
		    , CD_STYLE
		    , CD_COLOR
		    , CD_SIZE
		    , QT_STOCK
		    , OPT_CD
		    , OPT_CD1
		    , STORE_STOCK_QTY
		    , UPD_DT
		)
		SELECT A.SUPPLY_COMP_CD
		     , A.CD_SHOP
		     , A.CD_STYLE
		     , A.CD_COLOR
		     , A.CD_SIZE
		     , A.QT_STOCK
		     , E.OPT_CD
		     , E.OPT_CD1
		     , ROUND(GREATEST(A.QT_STOCK, 0) * (IFNULL(D.STOCK_APPL_RATE, 0)/100)) AS STORE_STOCK_QTY
		     , NOW()
		FROM (SELECT S.SUPPLY_COMP_CD
		          , S.CD_SHOP
		          , S.CD_STYLE
		          , S.CD_COLOR
		          , S.CD_SIZE
		          , S.QT_STOCK
		          , M.COLOR_CD AS OPT_CD1
		     FROM TB_SHOP_GOODS_STOCK S
		     INNER JOIN TB_WMS_COLOR_MAPPING M ON S.SUPPLY_COMP_CD = M.SUPPLY_COMP_CD
		                                       AND S.CD_COLOR = M.WMS_COLOR_CD
		                                       AND M.USE_YN = 'Y') A
		INNER JOIN TB_OPTION E USE INDEX(PRIMARY) ON A.CD_STYLE = E.GOODS_CD
		                       AND A.OPT_CD1 = E.OPT_CD1
		                       AND A.CD_SIZE = E.OPT_CD2
		INNER JOIN TB_GOODS B ON A.CD_STYLE = B.GOODS_CD
		                      AND B.SELF_GOODS_YN = 'Y'
		                      AND B.ERP_STOCK_LINK_YN = 'Y'
		INNER JOIN TB_STOCK_SYNC_BASE C ON B.BRAND_CD = C.BRAND_CD
		                                AND C.STOCK_SYNC_YN = 'Y'
		                                AND A.CD_SHOP = C.DELV_LOC_CD
		INNER JOIN TB_DELIVERY_LOC D ON A.SUPPLY_COMP_CD = D.SUPPLY_COMP_CD
		                             AND A.CD_SHOP = D.DELV_LOC_CD
		                             AND D.USE_YN = 'Y'
	</insert>

	<!-- TB_SHOP_STOCK_HST 삭제 -->
	<delete id="deleteShopStockHst"  parameterType="String" timeout="300">
		/* TsbGoods.deleteShopStockHst */
		DELETE FROM TB_SHOP_STOCK_HST
		WHERE JOBDATE <![CDATA[<=]]> #{jobdate}
	</delete>

	<!-- TB_SHOP_STOCK_HST 저장 -->
	<insert id="createShopStockHst"  parameterType="String" timeout="300">
		/* TsbGoods.createShopStockHst */
		INSERT INTO TB_SHOP_STOCK_HST(
		       JOBDATE
		     , SUPPLY_COMP_CD
		     , CD_SHOP
		     , CD_STYLE
		     , CD_COLOR
		     , CD_SIZE
		     , QT_STOCK
		     , OPT_CD
		     , OPT_CD1
		     , STORE_STOCK_QTY
		)
		SELECT #{jobdate}
		      SUPPLY_COMP_CD
		     , CD_SHOP
		     , CD_STYLE
		     , CD_COLOR
		     , CD_SIZE
		     , QT_STOCK
		     , OPT_CD
		     , OPT_CD1
		     , STORE_STOCK_QTY
		FROM TB_SHOP_STOCK
	</insert>

	<!-- 재입고알림 목록 -->
	<select id="getReinboundInformList"  resultType="ReinboundInform">
		/* TsbGoods.getReinboundInformList */
		SELECT IA.RINBD_INFO_SQ
		       , IA.GOODS_CD
		       , IA.OPT_CD
		       , IA.OPT_CD1
		       , IA.OPT_CD2
		       , IA.REQ_DT
		       , IA.CUST_NO
		       , C.CUST_ID
		       , C.CUST_NM
		       , C.CELL_PHNNO
		       , G.GOODS_NM
		       , CO.COLOR_KNM AS OPT_CD1_NM
		       , VS.CURR_STOCK_QTY
		  FROM TB_REINBOUND_INFORM IA
		  INNER JOIN TB_GOODS G ON IA.GOODS_CD = G.GOODS_CD
		                        AND G.GOODS_STAT = 'G008_90'                    /* 승인완료*/
		                        AND NOW() BETWEEN G.SELL_STDT AND G.SELL_EDDT   /*판매기간 확인*/
		                        AND G.SELF_MALL_YN = 'Y'                        /*자사몰 노출*/
		  INNER JOIN VW_STOCK VS ON IA.GOODS_CD = VS.GOODS_CD
		                         AND IA.OPT_CD = VS.OPT_CD
		                         AND VS.SOLDOUT_YN = 'N'
		                         AND VS.CURR_STOCK_QTY > 0         /* 상품재고 */
		  INNER JOIN TB_CUSTOMER C ON IA.CUST_NO = C.CUST_NO
		                           AND C.CUST_STAT = 'G104_10'     /* 활동회원 */
		  INNER JOIN TB_COLOR CO ON IA.OPT_CD1 = CO.COLOR_CD
		  WHERE IA.INFORM_DT IS NULL                               /*미발송건*/
		  AND DATEDIFF(CURRENT_DATE(),IA.REQ_DT) <![CDATA[<=]]> 15 /*경과일수*/
	</select>

	<!-- 재입고 알림톡 발송처리 -->
	<update id="updateReinboundInform" parameterType="ReinboundInform">
		/* TsbGoods.updateReinboundInform */
		UPDATE TB_REINBOUND_INFORM
		SET INFORM_DT = NOW()
		  , UPD_NO = #{updNo}
		  , UPD_DT = NOW()
		WHERE RINBD_INFO_SQ = #{rinbdInfoSq}
	</update>
	
	<!-- VW_STOCK임시 테이블 truncate -->
	<delete id="truncateVwStockTemporary">
		/* TsbGoods.truncateVwStockTemporary */
		TRUNCATE TABLE VW_STOCK_TMP
	</delete>
	
	<!-- VW_STOCK임시 테이블 생성 -->
	<insert id="createVwStockTemporary" timeout="900">
		/* TsbGoods.createVwStockTemporary */
		INSERT INTO VW_STOCK_TMP (
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , CURR_STOCK_QTY
		     , BASE_STOCK_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		)
		SELECT A.GOODS_CD
		     , A.OPT_CD
		     , A.OPT_CD1
		     , A.OPT_CD2
		     , A.CURR_STOCK_QTY
		     , 0                AS BASE_STOCK_QTY
		     , A.ADD_PRICE
		     , A.SOLDOUT_YN
		     , A.DISP_ORD
		     , A.DISP_YN
		FROM   (
		        SELECT S.GOODS_CD
		             , S.OPT_CD
		             , S.OPT_CD1
		             , S.OPT_CD2
		             , GREATEST((GREATEST(((GREATEST(S.CURR_STOCK_QTY,0) + GREATEST(S.STORE_STOCK_QTY,0)) - S.BASE_STOCK_QTY),0) - IFNULL(SQ.STOCK_QTY, 0)),0) AS CURR_STOCK_QTY
		             , S.SOLDOUT_YN
		             , S.ADD_PRICE
		             , S.DISP_ORD
		             , S.DISP_YN
		        FROM   TB_OPTION S
		        LEFT OUTER JOIN (
		                         SELECT GOODS_CD
		                              , OPT_CD
		                              , SUM((CASE SELL_GB
		                                          WHEN '10' THEN 1
		                                          WHEN '20' THEN 1
		                                          ELSE -1
		                                     END) * SELL_QTY) AS STOCK_QTY
		                         FROM   TB_SELL_QTY
		                         GROUP  BY GOODS_CD, OPT_CD
		                        ) SQ ON S.GOODS_CD = SQ.GOODS_CD
		                            AND S.OPT_CD = SQ.OPT_CD
		       ) A
	</insert>
	
	<!-- VW_STOCK 테이블 삭제 -->
	<delete id="deleteVwStock" timeout="300">
		/* TsbGoods.deleteVwStock */
		DELETE FROM VW_STOCK
	</delete>
	
	<!-- VW_STOCK 테이블 생성 -->
	<insert id="createVwStock" timeout="900">
		/* TsbGoods.createVwStock */
		INSERT INTO VW_STOCK (
		       GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , CURR_STOCK_QTY
		     , BASE_STOCK_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , REG_DT
		)
		SELECT GOODS_CD
		     , OPT_CD
		     , OPT_CD1
		     , OPT_CD2
		     , CURR_STOCK_QTY
		     , BASE_STOCK_QTY
		     , ADD_PRICE
		     , SOLDOUT_YN
		     , DISP_ORD
		     , DISP_YN
		     , NOW()          AS REG_DT
		FROM   VW_STOCK_TMP
	</insert>

</mapper>