<?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.TsbGoodsSearchDao">

	<!-- 상품검색 temp 테이블 삭제 -->
	<select id="deleteGoodsSearchTemp" timeout="600" statementType="CALLABLE">
		/* TsbGoodsSearch.deleteGoodsSearchTemp */
		/* TRUNCATE TABLE TB_GOODS_SEARCH_TEMP */
		{ CALL FN_INIT_TABLE('TB_GOODS_SEARCH_TEMP') }
	</select>

	<!-- 상품검색 temp 상품기본정보 생성 -->
	<insert id="createGoodsSearchTempByGoods"  parameterType="String"  timeout="600">
		/* TsbGoods.createGoodsSearchTempByGoods */
		INSERT INTO TB_GOODS_SEARCH_TEMP (
		      GOODS_CD
		    , OPT_CD1
		    , GOODS_NM
		    , GOODS_TNM
		    , GOODS_GB
		    , FOREIGN_BUY_YN
		    , PARALLEL_IMPORT_YN
		    , ORDER_MADE_YN
		    , BRAND_CD
		    , ITEMKIND_CD
		    , FORMAL_GB
		    , LIST_PRICE
		    , CURR_PRICE
		    , SELF_GOODS_YN
		    , GOODS_STAT
		    , SEX_GB
		    , MAIN_COLOR_CD
		    , SUPPLY_COMP_CD
		    , SELF_MALL_YN
		    , AGE_GRP_CD
		    , SEASON_CD
		    , GOODS_TYPE
		    , REG_DT
		    , BRAND_GROUP_KNM
		    , BRAND_GROUP_ENM
		    , BRAND_GROUP_NM
		    , BRAND_GROUP_NO
		    , DEFAULT_CATE_YN
		    , BRAND_CATE_NO
		    , ORDER_CNT
		    , REVIEW_CNT
		    , SIZE_GB
		    , SIZE_GB_NM
		    , SELL_STDT
		    , SELL_EDDT
		    , STOCK_QTY
		    , PROC_GB
		)
		SELECT DISTINCT G.GOODS_CD
		     , IFNULL(O.OPT_CD1, 'XX') AS OPT_CD1
		     , G.GOODS_NM
		     , G.GOODS_TNM
		     , G.GOODS_GB
		     , G.FOREIGN_BUY_YN
		     , G.PARALLEL_IMPORT_YN
		     , G.ORDER_MADE_YN
		     , G.BRAND_CD
		     , G.ITEMKIND_CD
		     , G.FORMAL_GB
		     , G.LIST_PRICE
		     , G.CURR_PRICE
		     , G.SELF_GOODS_YN
		     , G.GOODS_STAT
		     , G.SEX_GB
		     , G.MAIN_COLOR_CD
		     , G.SUPPLY_COMP_CD
		     , G.SELF_MALL_YN
		     , G.AGE_GRP_CD
		     , G.SEASON_CD
		     , G.GOODS_TYPE
		     , G.REG_DT AS REG_DT
		     , BG.BRAND_GROUP_KNM
		     , BG.BRAND_GROUP_ENM
		     , (CASE WHEN BG.DISP_NM_LANG = 'EN' THEN BG.BRAND_GROUP_ENM ELSE BG.BRAND_GROUP_KNM END) AS BRAND_GROUP_NM
		     , BG.BRAND_GROUP_NO
		     , BG.DEFAULT_CATE_YN
		     , BG.BRAND_CATE_NO
		     , GS.SELL_WEEK_QTY AS ORDER_CNT
		     , GS.REVIEW_REG_CNT AS REVIEW_CNT
		     , I.SIZE_GB
		     , CASE WHEN I.SIZE_GB = 'T' THEN '상의'
		            WHEN I.SIZE_GB = 'B' THEN '하의'
		            WHEN I.SIZE_GB = 'S' THEN '신발'
		            ELSE NULL END AS SIZE_GB_NM
		     , DATE_FORMAT(G.SELL_STDT, '%Y-%m-%d %H:%i:%S') AS SELL_STDT
		     , DATE_FORMAT(G.SELL_EDDT, '%Y-%m-%d %H:%i:%S') AS SELL_EDDT
		     , (CASE WHEN G.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'Y' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD AND OPT_CD1 = O.OPT_CD1)
		     		 WHEN G.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD)
		            ELSE (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = G.GOODS_CD)
		            END) AS STOCK_QTY
		     , 'C' AS PROC_GB
		FROM   TB_GOODS G
		INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                      AND B.USE_YN = 'Y'
		INNER JOIN TB_BRAND_GROUP BG ON B.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                              AND BG.USE_YN = 'Y'
		INNER JOIN TB_GOODS_SUMMARY GS ON G.GOODS_CD = GS.GOODS_CD
		INNER JOIN TB_ITEMKIND I ON  G.ITEMKIND_CD = I.ITEMKIND_CD

		<if test='procGb != null and procGb == "S" '>
		INNER JOIN (SELECT GOODS_CD, OPT_CD1
		            FROM TB_GOODS_SEARCH_SECTION_TEMP
		            WHERE PROC_GB = 'C') ST ON G.GOODS_CD = ST.GOODS_CD
		LEFT OUTER JOIN  TB_OPTION O ON G.GOODS_CD = O.GOODS_CD
		                             AND G.SELF_GOODS_YN = 'Y'
		                             AND ST.GOODS_CD = O.GOODS_CD
		                             AND ST.OPT_CD1 = O.OPT_CD1
		</if>
		<if test='procGb != null and procGb == "A" '>
		LEFT OUTER JOIN  TB_OPTION O ON G.GOODS_CD = O.GOODS_CD
		                             AND G.SELF_GOODS_YN = 'Y'
		</if>
		WHERE G.SELF_MALL_YN = 'Y'                 -- 자사몰 노출
		AND G.GOODS_STAT = 'G008_90'               -- 상품상태 승인완료
		AND NOW() BETWEEN SELL_STDT AND SELL_EDDT  -- 현재 판매가능기간
	</insert>

	<!-- 상품검색 temp 상품키워드 생성 -->
	<update id="updateGoodsSearchTempByKeyword"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByKeyword */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT AKG.GOODS_CD
		                                     , GROUP_CONCAT(DISTINCT AK.AD_KEYWORD) AS  AD_KEYWORD
		                                FROM TB_AD_KEYWORD AK
		                                INNER JOIN TB_AD_KEYWORD_GOODS AKG ON AK.AD_KEYWORD_SQ  = AKG.AD_KEYWORD_SQ
		                                INNER JOIN TB_GOODS_SEARCH_TEMP ST ON AKG.GOODS_CD = ST.GOODS_CD
		                                WHERE DATE(NOW()) BETWEEN  DATE(AK.AD_KEYWORD_STDT) AND DATE(AK.AD_KEYWORD_EDDT)
		                                AND AK.USE_YN  = 'Y'
		                                GROUP BY AKG.GOODS_CD) B
		SET A.AD_KEYWORD = B.AD_KEYWORD
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 상품이미지 생성 -->
	<update id="updateGoodsSearchTempByImage"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByImage */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GOODS_CD
		                                     , COLOR_CD
		                                     , MAX(SYS_IMG_NM) AS SYS_IMG_NM
		                                     , MAX(SYS_IMG_NM2) AS SYS_IMG_NM2
		                                 FROM (
		                                    SELECT GI.GOODS_CD
		                                          ,GI.COLOR_CD
		                                          ,CASE WHEN GI.DEFAULT_IMG_YN  = 'Y' THEN CONCAT(#{goodsPath}, '/', GI.SYS_IMG_NM)  ELSE '' END AS SYS_IMG_NM
		                                          ,CASE WHEN GI.MOUSEOVER_IMG_YN  = 'Y' THEN CONCAT(#{goodsPath}, '/', GI.SYS_IMG_NM) ELSE '' END AS SYS_IMG_NM2
		                                    FROM TB_GOODS_IMG GI
		                                    INNER JOIN TB_GOODS_SEARCH_TEMP ST ON GI.GOODS_CD = ST.GOODS_CD
		                                                                       AND GI.COLOR_CD  = ST.OPT_CD1
		                                    WHERE ( GI.DEFAULT_IMG_YN = 'Y' OR GI.MOUSEOVER_IMG_YN  = 'Y' )
		                                        ) Z
		                                  GROUP BY GOODS_CD, COLOR_CD) B
		SET A.SYS_IMG_NM = B.SYS_IMG_NM
		  , A.SYS_IMG_NM2 = B.SYS_IMG_NM2
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD1 = B.COLOR_CD
	</update>

	<!-- 상품검색 temp 상품 동영상 생성 -->
	<update id="updateGoodsSearchTempByVideo"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByVideo */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GOODS_CD
		                                     , MAX(CASE WHEN  RNUM = 1 THEN  VIDEO_GB END) AS  VIDEO_GB_M
		                                     , MAX(CASE WHEN  RNUM = 1 THEN  KMC_KEY END) AS  VIDEO_VAL_M
		                                     , MAX(CASE WHEN  RNUM = 2 THEN  VIDEO_GB END) AS  VIDEO_GB_S
		                                     , MAX(CASE WHEN  RNUM = 2 THEN  KMC_KEY END) AS  VIDEO_VAL_S
		                               FROM (
		                                     SELECT A.GOODS_CD
		                                          , A.VIDEO_GB
		                                          , A.KMC_KEY
		                                          , A.REG_DT
		                                          , RANK() OVER(PARTITION BY A.GOODS_CD ORDER BY A.REG_DT, A.KMC_KEY ) RNUM
		                                     FROM TB_GOODS_VIDEO  A
		                                     INNER JOIN TB_GOODS_SEARCH_TEMP ST ON A.GOODS_CD = ST.GOODS_CD
		                                     WHERE A.DISP_YN = 'Y'
		                                     AND A.KMC_KEY IS NOT NULL
		                                    ) Z
		                               GROUP BY GOODS_CD) B
		SET A.VIDEO_GB_M = B.VIDEO_GB_M
		  , A.VIDEO_VAL_M = B.VIDEO_VAL_M
		  , A.VIDEO_GB_S = B.VIDEO_GB_S
		  , A.VIDEO_VAL_S = B.VIDEO_VAL_S
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 상품 즉시적용가 생성 -->
	<update id="updateGoodsSearchTempByPrice"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByPrice */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GBP.GOODS_CD
		                                     , GBP.PC_CURR_PRICE
		                                     , GBP.MO_CURR_PRICE
		                                     , GBP.APP_CURR_PRICE
		                                FROM TB_GOODS_BENEFIT_PRICE GBP
		                                INNER JOIN TB_GOODS_SEARCH_TEMP ST ON GBP.GOODS_CD = ST.GOODS_CD
		                                WHERE GBP.CUST_GB = 'G100_00') B
		SET A.PC_CURR_PRICE = B.PC_CURR_PRICE
		  , A.MO_CURR_PRICE = B.MO_CURR_PRICE
		  , A.APP_CURR_PRICE = B.APP_CURR_PRICE
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 상품 아이콘 생성 -->
	<update id="updateGoodsSearchTempByIcon"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByIcon */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GBP.GOODS_CD
		                                     , GROUP_CONCAT(DISTINCT GBP.BENEFIT_GB) AS BENEFIT_GB
		                                FROM TB_GOODS_BENEFIT GBP
		                                INNER JOIN TB_GOODS_SEARCH_TEMP ST ON GBP.GOODS_CD = ST.GOODS_CD
		                                GROUP BY GBP.GOODS_CD) B
		SET A.BENEFIT_GB = B.BENEFIT_GB
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 상품 전시카테 생성 -->
	<update id="updateGoodsSearchTempByCate"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempByCate */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GOODS_CD
		                                     , GROUP_CONCAT(DISTINCT BYITEM_CATE_NO) AS BYITEM_CATE_NO
		                                     , GROUP_CONCAT(DISTINCT  BYBRAND_CATE_NO) AS BYBRAND_CATE_NO
		                                FROM (
		                                         SELECT G.GOODS_CD
		                                              , CG.CATE_NO AS BYITEM_CATE_NO
		                                              , 0          AS BYBRAND_CATE_NO
		                                         FROM TB_GOODS_SEARCH_TEMP G
		                                         INNER JOIN TB_CATE_GOODS CG ON G.GOODS_CD = CG.GOODS_CD
		                                         INNER JOIN TB_CATE_4SRCH TCS ON CG.CATE_NO = TCS.LEAF_CATE_NO
		                                         AND TCS.SITE_CD = 'G000_10'  -- 사이트
		                                         AND TCS.CATE_GB = 'G032_101' -- BY ITEM
		                                         AND TCS.CATE_TYPE = 'G031_10' -- 카테고리유형
		                                         GROUP BY G.GOODS_CD, CG.CATE_NO
		                                         UNION ALL
		                                         SELECT G.GOODS_CD
		                                              , 0          AS BYITEM_CATE_NO
		                                              , CG.CATE_NO AS BYBRAND_CATE_NO
		                                         FROM TB_GOODS_SEARCH_TEMP G
		                                         INNER JOIN TB_CATE_GOODS CG ON G.GOODS_CD = CG.GOODS_CD
		                                         INNER JOIN TB_CATE_4SRCH TCS ON CG.CATE_NO = TCS.LEAF_CATE_NO
		                                         AND TCS.SITE_CD = 'G000_10'  -- 사이트
		                                         AND TCS.CATE_GB = 'G032_102' -- BY BRAND
		                                         AND TCS.CATE_TYPE = 'G031_10' -- 카테고리유형
		                                         GROUP BY G.GOODS_CD, CG.CATE_NO
		                                     ) Z
		                                GROUP BY GOODS_CD) B
		SET A.BYITEM_CATE_NO = B.BYITEM_CATE_NO
		  , A.BYBRAND_CATE_NO = B.BYBRAND_CATE_NO
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 자사 옵션정보 생성 -->
	<update id="updateGoodsSearchTempBySelfOpt"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempBySelfOpt */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT G.GOODS_CD
		                                     , VS.OPT_CD1
		                                     , C.COLOR_FILE
		                                     , CC.CD AS COLOR_CD
		                                     , CC.CD_DESC AS COLOR_CHIP /*RGB값*/
		                                     , GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                                 ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N' ELSE 'Y'  END END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		                                FROM  TB_GOODS_SEARCH_TEMP G
		                                INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                                       AND G.OPT_CD1 = VS.OPT_CD1
		                                                       AND VS.DISP_YN = 'Y'
		                                INNER JOIN TB_COLOR C ON VS.OPT_CD1 = C.COLOR_CD
		                                                      AND C.USE_YN = 'Y'
		                                INNER JOIN TB_COMMON_CODE CC ON C.COLOR_GRP_CD = CC.CD
		                                                             AND CC.USE_YN = 'Y'
		   	                                                         AND CC.CD_GB = 'G072'

		                                WHERE G.SELF_GOODS_YN = 'Y' /*자사상품*/
		                                GROUP BY G.GOODS_CD, VS.OPT_CD1, C.COLOR_CD, CC.CD_DESC) B
		SET A.COLOR_CHIP = B.COLOR_CHIP
		  , A.COLOR_CD = B.COLOR_CD
		  , A.SIZES = B.SIZES
		  , A.OPT_CD1_CHIP = COLOR_FILE
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD1 = B.OPT_CD1
	</update>

	<!-- 상품검색 temp 자사 옵션별생삭정보 생성 -->
	<update id="updateGoodsSearchTempBySelfColor"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempBySelfColor */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT GOODS_CD, GROUP_CONCAT(CONCAT(OPT_CD1,':', OPT_CD1_CHIP)) AS COLOR_CHIPS
		                                FROM TB_GOODS_SEARCH_TEMP
		                                WHERE SELF_GOODS_YN = 'Y'
		                                AND PROC_GB = 'C'
		                                GROUP BY  GOODS_CD) B
		SET A.COLOR_CHIPS = B.COLOR_CHIPS
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 입점 옵션정보 생성 -->
	<update id="updateGoodsSearchTempBySelfNoOpt"  parameterType="String" timeout="600">
		/* TsbGoods.updateGoodsSearchTempBySelfNoOpt */
		UPDATE TB_GOODS_SEARCH_TEMP A, (SELECT G.GOODS_CD
		                                     , GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                                 ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N' ELSE 'Y'  END END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		                                FROM  TB_GOODS_SEARCH_TEMP G
		                                INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                                       AND    VS.DISP_YN = 'Y'

		                                WHERE G.SELF_GOODS_YN = 'N' /*입점상품*/
		                                GROUP  BY G.GOODS_CD) B
		SET A.SIZES = B.SIZES
		WHERE A.GOODS_CD = B.GOODS_CD
	</update>

	<!-- 상품검색 temp 입점 옵션정보 생성 -->
	<insert id="updateSessionGroupConcat">
		set session group_concat_max_len = 102400;
	</insert>

	<!-- 상품검색 테이블 삭제 -->
	<delete id="deleteGoodsSearch" timeout="600">
		/* TsbGoodsSearch.deleteGoodsSearch */
		TRUNCATE TABLE TB_GOODS_SEARCH
	</delete>

	<!-- 상품검색 테이블 생성 -->
	<insert id="createGoodsSearch" timeout="600">
		/* TsbGoodsSearch.createGoodsSearch */
		INSERT INTO TB_GOODS_SEARCH (
		    GOODS_CD
		  , OPT_CD1
		  , GOODS_NM
		  , GOODS_TNM
		  , GOODS_GB
		  , FOREIGN_BUY_YN
		  , PARALLEL_IMPORT_YN
		  , ORDER_MADE_YN
		  , BRAND_CD
		  , ITEMKIND_CD
		  , FORMAL_GB
		  , LIST_PRICE
		  , CURR_PRICE
		  , SELF_GOODS_YN
		  , GOODS_STAT
		  , SEX_GB
		  , MAIN_COLOR_CD
		  , SUPPLY_COMP_CD
		  , SELF_MALL_YN
		  , AGE_GRP_CD
		  , SEASON_CD
		  , GOODS_TYPE
		  , REG_DT
		  , BRAND_GROUP_KNM
		  , BRAND_GROUP_ENM
		  , BRAND_GROUP_NM
		  , BRAND_GROUP_NO
		  , DEFAULT_CATE_YN
		  , BRAND_CATE_NO
		  , ORDER_CNT
		  , REVIEW_CNT
		  , SIZE_GB
		  , SIZE_GB_NM
		  , SIZES
		  , COLOR_CD
		  , COLOR_CHIP
		  , BENEFIT_GB
		  , PC_CURR_PRICE
		  , MO_CURR_PRICE
		  , APP_CURR_PRICE
		  , AD_KEYWORD
		  , VIDEO_GB_M
		  , VIDEO_VAL_M
		  , VIDEO_GB_S
		  , VIDEO_VAL_S
		  , BYITEM_CATE_NO
		  , BYBRAND_CATE_NO
		  , SYS_IMG_NM
		  , SYS_IMG_NM2
		  , SELL_STDT
		  , SELL_EDDT
		  , STOCK_QTY
		  , OPT_CD1_CHIP
		  , COLOR_CHIPS
		  , PROC_GB
		)
		SELECT GOODS_CD
		     , OPT_CD1
		     , GOODS_NM
		     , GOODS_TNM
		     , GOODS_GB
		     , FOREIGN_BUY_YN
		     , PARALLEL_IMPORT_YN
		     , ORDER_MADE_YN
		     , BRAND_CD
		     , ITEMKIND_CD
		     , FORMAL_GB
		     , LIST_PRICE
		     , CURR_PRICE
		     , SELF_GOODS_YN
		     , GOODS_STAT
		     , SEX_GB
		     , MAIN_COLOR_CD
		     , SUPPLY_COMP_CD
		     , SELF_MALL_YN
		     , AGE_GRP_CD
		     , SEASON_CD
		     , GOODS_TYPE
		     , REG_DT
		     , BRAND_GROUP_KNM
		     , BRAND_GROUP_ENM
		     , BRAND_GROUP_NM
		     , BRAND_GROUP_NO
		     , DEFAULT_CATE_YN
		     , BRAND_CATE_NO
		     , ORDER_CNT
		     , REVIEW_CNT
		     , SIZE_GB
		     , SIZE_GB_NM
		     , SIZES
		     , COLOR_CD
		     , COLOR_CHIP
		     , BENEFIT_GB
		     , PC_CURR_PRICE
		     , MO_CURR_PRICE
		     , APP_CURR_PRICE
		     , AD_KEYWORD
		     , VIDEO_GB_M
		     , VIDEO_VAL_M
		     , VIDEO_GB_S
		     , VIDEO_VAL_S
		     , BYITEM_CATE_NO
		     , BYBRAND_CATE_NO
		     , SYS_IMG_NM
		     , SYS_IMG_NM2
		     , DATE_FORMAT(SELL_STDT, '%Y-%m-%d %H:%i:%S') AS SELL_STDT
		     , DATE_FORMAT(SELL_EDDT, '%Y-%m-%d %H:%i:%S') AS SELL_EDDT
		     , STOCK_QTY
		     , OPT_CD1_CHIP
		     , COLOR_CHIPS
		     , PROC_GB
		FROM TB_GOODS_SEARCH_TEMP
	</insert>

	<!-- 상품검색 부분색인용 TEMP 상품 정보 삭제 -->
	<delete id="deleteGoodsSearchSectionTemp" timeout="300">
		/* TsbGoodsSearch.deleteGoodsSearchSectionTemp */
		TRUNCATE TABLE TB_GOODS_SEARCH_SECTION_TEMP
	</delete>

	<!-- 상품검색 부분색인용 상품정보 생성 - 상품추가건 -->
	<insert id="createGoodsSearchSectionTempByGoods"  timeout="300">
		/* TsbGoods.createGoodsSearchSectionTempByGoods */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT G.GOODS_CD
		         , IFNULL(O.OPT_CD1, 'XX') AS OPT_CD1
		    FROM   TB_GOODS G
		    INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                          AND B.USE_YN = 'Y'
		    INNER JOIN TB_BRAND_GROUP BG ON B.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                                  AND BG.USE_YN = 'Y'
		    INNER JOIN TB_GOODS_SUMMARY GS ON G.GOODS_CD = GS.GOODS_CD
		    INNER JOIN TB_ITEMKIND I ON  G.ITEMKIND_CD = I.ITEMKIND_CD

		    LEFT OUTER JOIN  TB_OPTION O ON G.GOODS_CD = O.GOODS_CD
		                                 AND G.SELF_GOODS_YN = 'Y'
		    WHERE G.SELF_MALL_YN = 'Y'                 -- 자사몰 노출
		    AND G.GOODS_STAT = 'G008_90'               -- 상품상태 승인완료
		    AND NOW() BETWEEN SELL_STDT AND SELL_EDDT  -- 현재 판매가능기간
		    AND G.PRICE_UPD_DT >= DATE_ADD(NOW(), INTERVAL -65 MINUTE)  -- 가격변경일 65분 이내
		) Z
		WHERE NOT EXISTS( SELECT 1
		                    FROM TB_GOODS_SEARCH ST
		                    WHERE ST.GOODS_CD = Z.GOODS_CD
		                    AND ST.OPT_CD1 = Z.OPT_CD1
		                    AND ST.PROC_GB = 'C'
		                  )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 판매가 변경건 -->
	<insert id="saveGoodsSearchSectionTempByPrice"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByPrice */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT G.GOODS_CD
		         , IFNULL(O.OPT_CD1, 'XX') AS OPT_CD1
		    FROM TB_GOODS G
		    LEFT OUTER JOIN  TB_OPTION O ON G.GOODS_CD = O.GOODS_CD
		                                 AND G.SELF_GOODS_YN = 'Y'
		    WHERE G.SELF_MALL_YN = 'Y'                  -- 자사몰 노출
		    AND G.GOODS_STAT = 'G008_90'                -- 상품상태 승인완료
		    AND NOW() BETWEEN SELL_STDT AND SELL_EDDT   -- 현재 판매가능기간
		    AND G.PRICE_UPD_DT >= DATE_ADD(NOW(), INTERVAL -65 MINUTE)  -- 가격변경일 65분 이내
		) Z
		WHERE NOT EXISTS( SELECT 1
		                    FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                    WHERE ST.GOODS_CD = Z.GOODS_CD
		                    AND ST.OPT_CD1 = Z.OPT_CD1
		                    AND ST.PROC_GB = 'D'
		                  )
		AND EXISTS( SELECT 1
		            FROM TB_GOODS_SEARCH ST
		            WHERE ST.GOODS_CD = Z.GOODS_CD
		            AND ST.OPT_CD1 = Z.OPT_CD1
		            AND ST.PROC_GB = 'C'
		          )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 즉시할인쿠폰 변경건 -->
	<insert id="saveGoodsSearchSectionTempByCoupon"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByCoupon */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.PC_CURR_PRICE             AS PC_CURR_PRICE_AS
		         , A.MO_CURR_PRICE             AS MO_CURR_PRICE_AS
		         , A.APP_CURR_PRICE            AS APP_CURR_PRICE_AS
		         , IFNULL(B.PC_CURR_PRICE, 0)  AS PC_CURR_PRICE
		         , IFNULL(B.MO_CURR_PRICE, 0)  AS MO_CURR_PRICE
		         , IFNULL(B.APP_CURR_PRICE, 0) AS APP_CURR_PRICE
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN TB_GOODS_BENEFIT_PRICE B ON A.GOODS_CD = B.GOODS_CD
		) Z
		WHERE (PC_CURR_PRICE_AS <![CDATA[<>]]> PC_CURR_PRICE
		      OR MO_CURR_PRICE_AS <![CDATA[<>]]> MO_CURR_PRICE
		      OR APP_CURR_PRICE_AS <![CDATA[<>]]> APP_CURR_PRICE)
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		AND EXISTS( SELECT 1
		            FROM TB_GOODS_SEARCH ST
		            WHERE ST.GOODS_CD = Z.GOODS_CD
		            AND ST.OPT_CD1 = Z.OPT_CD1
		            AND ST.PROC_GB = 'C'
		          )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 타이틀 변경건 -->
	<insert id="saveGoodsSearchSectionTempByTitle"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByTitle */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.GOODS_TNM             AS GOODS_TNM_AS
		         , B.GOODS_TNM             AS GOODS_TNM
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN TB_GOODS B ON A.GOODS_CD = B.GOODS_CD
		                               AND B.SELF_MALL_YN = 'Y'                  -- 자사몰 노출
		                               AND B.GOODS_STAT = 'G008_90'                -- 상품상태 승인완료
		                               AND NOW() BETWEEN B.SELL_STDT AND B.SELL_EDDT   -- 현재 판매가능기간
		                               AND B.UPD_DT >= DATE_ADD(NOW(), INTERVAL -65 MINUTE)  -- 가격변경일 65분 이내
		) Z
		WHERE GOODS_TNM_AS <![CDATA[<>]]> GOODS_TNM
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		AND EXISTS( SELECT 1
		            FROM TB_GOODS_SEARCH ST
		            WHERE ST.GOODS_CD = Z.GOODS_CD
		            AND ST.OPT_CD1 = Z.OPT_CD1
		            AND ST.PROC_GB = 'C'
		          )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 혜택(딱지) 변경건 -->
	<insert id="saveGoodsSearchSectionTempByBenefit"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByBenefit */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.BENEFIT_GB             AS BENEFIT_GB_AS
		         , B.BENEFIT_GB             AS BENEFIT_GB
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN (SELECT GBP.GOODS_CD
		                          , GROUP_CONCAT(BENEFIT_GB) AS BENEFIT_GB
		                     FROM TB_GOODS_BENEFIT GBP
		                     GROUP BY GBP.GOODS_CD) B ON A.GOODS_CD = B.GOODS_CD
		) Z
		WHERE BENEFIT_GB_AS <![CDATA[<>]]> BENEFIT_GB
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		AND EXISTS( SELECT 1
		            FROM TB_GOODS_SEARCH ST
		            WHERE ST.GOODS_CD = Z.GOODS_CD
		            AND ST.OPT_CD1 = Z.OPT_CD1
		            AND ST.PROC_GB = 'C'
		          )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 자사 옵션정보 변경건 -->
	<insert id="saveGoodsSearchSectionTempBySelfOpt"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempBySelfOpt */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END       AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.SIZES             AS SIZES_AS
		         , (SELECT GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                                      ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N'
		                                                                           ELSE 'Y'  END
		                                                                      END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		                   FROM  TB_GOODS_SEARCH G
		                   INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                          AND G.OPT_CD1 = VS.OPT_CD1
		                                          AND VS.DISP_YN = 'Y'

		                   WHERE G.GOODS_CD = A.GOODS_CD
		                   AND G.OPT_CD1 = A.OPT_CD1
		                   AND G.SELF_GOODS_YN = 'Y' /*자사상품*/
		                   GROUP BY G.GOODS_CD, G.OPT_CD1) AS SIZES
		    FROM TB_GOODS_SEARCH A
		    WHERE A.SELF_GOODS_YN = 'Y' /*자사상품*/
		) Z
		WHERE SIZES_AS <![CDATA[<>]]> SIZES
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = (CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END)
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 자사 옵션정보 변경건 -->
	<insert id="saveGoodsSearchSectionTempBySelfOpt_ORG"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempBySelfOpt_ORG */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END       AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.SIZES             AS SIZES_AS
		         , B.SIZES             AS SIZES
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN (SELECT G.GOODS_CD
		                          , G.OPT_CD1
		                          , GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                      ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N' ELSE 'Y'  END END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		                     FROM  TB_GOODS_SEARCH G
		                     INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                            AND G.OPT_CD1 = VS.OPT_CD1
		                                            AND VS.DISP_YN = 'Y'

		                     WHERE G.SELF_GOODS_YN = 'Y' /*자사상품*/
		                     GROUP BY G.GOODS_CD, G.OPT_CD1) B
		                     ON A.GOODS_CD = B.GOODS_CD
		                     AND A.OPT_CD1 = B.OPT_CD1
		    WHERE A.SELF_GOODS_YN = 'Y' /*자사상품*/
		) Z
		WHERE SIZES_AS <![CDATA[<>]]> SIZES
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = (CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END)
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 입점 옵션정보 변경건 -->
	<insert id="saveGoodsSearchSectionTempBySelfNoOpt"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempBySelfNoOpt */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END       AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.SIZES             AS SIZES_AS
		         , (SELECT GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                                      ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N'
		                                                                            ELSE 'Y'  END
		                                                                      END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		            FROM  TB_GOODS_SEARCH G
		            INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                   AND VS.DISP_YN = 'Y'

		            WHERE G.GOODS_CD = A.GOODS_CD
		            AND G.OPT_CD1 = A.OPT_CD1
		            AND G.SELF_GOODS_YN = 'N' /*입점상품*/
		            GROUP BY G.GOODS_CD, G.OPT_CD1
		            )   AS SIZES
		    FROM TB_GOODS_SEARCH A
		    WHERE A.SELF_GOODS_YN = 'N' /*입점상품*/
		) Z
		WHERE SIZES_AS <![CDATA[<>]]> SIZES
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = (CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END)
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 재고수량 변경건 -->
	<insert id="updateGoodsSearchSectionTempByStock"  timeout="300">
		/* TsbGoods.updateGoodsSearchSectionTempByStock */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		     SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.STOCK_QTY
		         , (CASE WHEN A.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'Y' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = A.GOODS_CD AND OPT_CD1 = A.OPT_CD1)
		     		 WHEN A.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = A.GOODS_CD)
		            ELSE (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = A.GOODS_CD)
		            END) AS CHANGE_STOCK_QTY
		    FROM TB_GOODS_SEARCH A
		) Z
		WHERE STOCK_QTY <![CDATA[<>]]> CHANGE_STOCK_QTY

		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 입점 옵션정보 변경건 -->
	<insert id="saveGoodsSearchSectionTempBySelfNoOpt_org"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempBySelfNoOpt_org */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END       AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.SIZES             AS SIZES_AS
		         , B.SIZES             AS SIZES
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN (SELECT G.GOODS_CD
		                          , G.OPT_CD1
		                          , GROUP_CONCAT(DISTINCT CONCAT(VS.OPT_CD2 ,':' ,CASE WHEN VS.SOLDOUT_YN = 'Y' THEN 'Y'
		                                                      ELSE CASE WHEN VS.CURR_STOCK_QTY - VS.BASE_STOCK_QTY > 0 THEN 'N' ELSE 'Y'  END END) ORDER BY VS.DISP_ORD SEPARATOR ',')  AS SIZES
		                     FROM  TB_GOODS_SEARCH G
		                     INNER JOIN VW_STOCK VS ON G.GOODS_CD = VS.GOODS_CD
		                                            AND VS.DISP_YN = 'Y'

		                     WHERE G.SELF_GOODS_YN = 'N' /*입점상품*/
		                     GROUP BY G.GOODS_CD, G.OPT_CD1) B
		                     ON A.GOODS_CD = B.GOODS_CD
		    WHERE A.SELF_GOODS_YN = 'N' /*입점상품*/
		) Z
		WHERE SIZES_AS <![CDATA[<>]]> SIZES
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = (CASE WHEN NULLIF(SIZES,'') IS NULL THEN 'D' ELSE 'C' END)
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 부분색인용 상품정보 생성 - 상품상태 변경건 -->
	<insert id="saveGoodsSearchSectionTempByStat"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByStat */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      <![CDATA[
		      ,CASE WHEN GOODS_STAT <> 'G008_90' THEN 'D'
		            WHEN SELF_MALL_YN <> 'Y' THEN 'D'
		            WHEN SELL_STDT > NOW() THEN 'D'
		            WHEN SELL_EDDT <  NOW() THEN 'D'
		            ELSE 'C' END AS PROC_GB
		      ]]>
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.GOODS_STAT             AS GOODS_STAT_AS
		         , B.GOODS_STAT             AS GOODS_STAT
		         , A.SELF_MALL_YN           AS SELF_MALL_YN_AS
		         , B.SELF_MALL_YN           AS SELF_MALL_YN
		         , A.SELL_STDT              AS SELL_STDT_AS
		         , B.SELL_STDT              AS SELL_STDT
		         , A.SELL_EDDT              AS SELL_EDDT_AS
		         , B.SELL_EDDT              AS SELL_EDDT
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN (SELECT G.GOODS_CD
		                          , G.OPT_CD1
		                          , TG.GOODS_STAT
		                          , TG.SELF_MALL_YN
		                          , TG.SELL_STDT
		                          , TG.SELL_EDDT
		                     FROM  TB_GOODS_SEARCH G
		                     INNER JOIN TB_GOODS TG ON G.GOODS_CD = TG.GOODS_CD
		                     ) B
		                     ON A.GOODS_CD = B.GOODS_CD
		) Z
		WHERE (GOODS_STAT_AS <![CDATA[<>]]> GOODS_STAT )
		  OR (SELF_MALL_YN_AS <![CDATA[<>]]> SELF_MALL_YN )
		  OR (SELL_STDT_AS <![CDATA[<>]]> SELL_STDT )
		  OR (SELL_EDDT_AS <![CDATA[<>]]> SELL_EDDT )
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		ON DUPLICATE KEY UPDATE
		      <![CDATA[
		      PROC_GB = (CASE WHEN GOODS_STAT <> 'G008_90' THEN 'D'
		                      WHEN SELF_MALL_YN <> 'Y' THEN 'D'
		                      WHEN SELL_STDT > NOW() THEN 'D'
		                      WHEN SELL_EDDT <  NOW() THEN 'D'
		                      ELSE 'C' END)
		        ]]>
		    , DATA_UPD_DT = NOW()
	</insert>


	<!-- 상품검색 부분색인용 상품정보 생성 - 전시카텍 변경건 -->
	<insert id="saveGoodsSearchSectionTempByCate"  timeout="300">
		/* TsbGoods.saveGoodsSearchSectionTempByCate */
		INSERT INTO TB_GOODS_SEARCH_SECTION_TEMP (
		     GOODS_CD
		   , OPT_CD1
		   , PROC_GB
		)
		SELECT GOODS_CD
		      ,OPT_CD1
		      ,'C' AS PROC_GB
		FROM (
		    SELECT A.GOODS_CD
		         , A.OPT_CD1
		         , A.DEFAULT_CATE_YN            AS DEFAULT_CATE_YN_AS
		         , A.BRAND_CATE_NO              AS BRAND_CATE_NO_AS
		         , A.BYITEM_CATE_NO             AS BYITEM_CATE_NO_AS
		         , A.BYBRAND_CATE_NO            AS BYBRAND_CATE_NO_AS
		         , B.DEFAULT_CATE_YN            AS DEFAULT_CATE_YN
		         , B.BRAND_CATE_NO              AS BRAND_CATE_NO
		         , B.BYITEM_CATE_NO             AS BYITEM_CATE_NO
		         , B.BYBRAND_CATE_NO            AS BYBRAND_CATE_NO
		    FROM TB_GOODS_SEARCH A
		    LEFT OUTER JOIN (SELECT GOODS_CD
		                          , MAX(DEFAULT_CATE_YN) AS DEFAULT_CATE_YN
		                          , MAX(BRAND_CATE_NO) AS BRAND_CATE_NO
		                          , GROUP_CONCAT(DISTINCT BYITEM_CATE_NO) AS BYITEM_CATE_NO
		                          , GROUP_CONCAT(DISTINCT  BYBRAND_CATE_NO) AS BYBRAND_CATE_NO
		                     FROM (
		                              SELECT G.GOODS_CD
		                                   , MAX(BG.DEFAULT_CATE_YN) AS DEFAULT_CATE_YN
		                                   , MAX(BG.BRAND_CATE_NO) AS BRAND_CATE_NO
		                                   , CG.CATE_NO AS BYITEM_CATE_NO
		                                   , 0          AS BYBRAND_CATE_NO
		                              FROM TB_GOODS G
		                              INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                                                    AND B.USE_YN = 'Y'
		                              INNER JOIN TB_BRAND_GROUP BG ON B.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                                                           AND BG.USE_YN = 'Y'
		                              INNER JOIN TB_CATE_GOODS CG ON G.GOODS_CD = CG.GOODS_CD
		                              INNER JOIN TB_CATE_4SRCH TCS ON CG.CATE_NO = TCS.LEAF_CATE_NO
		                              AND TCS.SITE_CD = 'G000_10'  -- 사이트
		                              AND TCS.CATE_GB = 'G032_101' -- BY ITEM
		                              AND TCS.CATE_TYPE = 'G031_10' -- 카테고리유형
		                              GROUP BY G.GOODS_CD, CG.CATE_NO
		                              UNION ALL
		                              SELECT G.GOODS_CD
		                                   , MAX(BG.DEFAULT_CATE_YN) AS DEFAULT_CATE_YN
		                                   , MAX(BG.BRAND_CATE_NO) AS BRAND_CATE_NO
		                                   , 0          AS BYITEM_CATE_NO
		                                   , CG.CATE_NO AS BYBRAND_CATE_NO
		                              FROM TB_GOODS G
		                              INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                                                    AND B.USE_YN = 'Y'
		                              INNER JOIN TB_BRAND_GROUP BG ON B.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		                                                           AND BG.USE_YN = 'Y'
		                              INNER JOIN TB_CATE_GOODS CG ON G.GOODS_CD = CG.GOODS_CD
		                              INNER JOIN TB_CATE_4SRCH TCS ON CG.CATE_NO = TCS.LEAF_CATE_NO
		                              AND TCS.SITE_CD = 'G000_10'  -- 사이트
		                              AND TCS.CATE_GB = 'G032_102' -- BY BRAND
		                              AND TCS.CATE_TYPE = 'G031_10' -- 카테고리유형
		                              GROUP BY G.GOODS_CD, CG.CATE_NO
		                          ) Z
		                     GROUP BY GOODS_CD) B ON A.GOODS_CD = B.GOODS_CD
		) Z
		WHERE (DEFAULT_CATE_YN_AS <![CDATA[<>]]> DEFAULT_CATE_YN
		    OR BRAND_CATE_NO_AS <![CDATA[<>]]> BRAND_CATE_NO
		    OR BYITEM_CATE_NO_AS <![CDATA[<>]]> BYITEM_CATE_NO
		    OR BYBRAND_CATE_NO_AS <![CDATA[<>]]> BYBRAND_CATE_NO
		      )
		AND NOT EXISTS( SELECT 1
		                FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		                WHERE ST.GOODS_CD = Z.GOODS_CD
		                AND ST.OPT_CD1 = Z.OPT_CD1
		                AND ST.PROC_GB = 'D'
		              )
		AND EXISTS( SELECT 1
		            FROM TB_GOODS_SEARCH ST
		            WHERE ST.GOODS_CD = Z.GOODS_CD
		            AND ST.OPT_CD1 = Z.OPT_CD1
		            AND ST.PROC_GB = 'C'
		          )
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'C'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 temp 상품기본정보 생성 -->
	<insert id="updateGoodsSearchSectionByDel"  timeout="600">
		/* TsbGoods.updateGoodsSearchSectionByDel */
		INSERT INTO TB_GOODS_SEARCH_TEMP (
		      GOODS_CD
		    , OPT_CD1
		    , GOODS_NM
		    , GOODS_TNM
		    , GOODS_GB
		    , FOREIGN_BUY_YN
		    , PARALLEL_IMPORT_YN
		    , ORDER_MADE_YN
		    , BRAND_CD
		    , ITEMKIND_CD
		    , FORMAL_GB
		    , LIST_PRICE
		    , CURR_PRICE
		    , SELF_GOODS_YN
		    , GOODS_STAT
		    , SEX_GB
		    , MAIN_COLOR_CD
		    , SUPPLY_COMP_CD
		    , SELF_MALL_YN
		    , AGE_GRP_CD
		    , SEASON_CD
		    , GOODS_TYPE
		    , REG_DT
		    , BRAND_GROUP_KNM
		    , BRAND_GROUP_ENM
		    , BRAND_GROUP_NM
		    , BRAND_GROUP_NO
		    , DEFAULT_CATE_YN
		    , BRAND_CATE_NO
		    , ORDER_CNT
		    , REVIEW_CNT
		    , SIZE_GB
		    , SIZE_GB_NM
		    , SELL_STDT
		    , SELL_EDDT
		    , STOCK_QTY
		    , PROC_GB
		)
		SELECT ST.GOODS_CD
		     , ST.OPT_CD1
		     , G.GOODS_NM
		     , G.GOODS_TNM
		     , G.GOODS_GB
		     , G.FOREIGN_BUY_YN
		     , G.PARALLEL_IMPORT_YN
		     , G.ORDER_MADE_YN
		     , G.BRAND_CD
		     , G.ITEMKIND_CD
		     , G.FORMAL_GB
		     , G.LIST_PRICE
		     , G.CURR_PRICE
		     , G.SELF_GOODS_YN
		     , G.GOODS_STAT
		     , G.SEX_GB
		     , G.MAIN_COLOR_CD
		     , G.SUPPLY_COMP_CD
		     , G.SELF_MALL_YN
		     , G.AGE_GRP_CD
		     , G.SEASON_CD
		     , G.GOODS_TYPE
		     , G.REG_DT AS REG_DT
		     , BG.BRAND_GROUP_KNM
		     , BG.BRAND_GROUP_ENM
		     , (CASE WHEN BG.DISP_NM_LANG = 'EN' THEN BG.BRAND_GROUP_ENM ELSE BG.BRAND_GROUP_KNM END) AS BRAND_GROUP_NM
		     , BG.BRAND_GROUP_NO
		     , BG.DEFAULT_CATE_YN
		     , BG.BRAND_CATE_NO
		     , GS.SELL_WEEK_QTY AS ORDER_CNT
		     , GS.REVIEW_REG_CNT AS REVIEW_CNT
		     , I.SIZE_GB
		     , CASE WHEN I.SIZE_GB = 'T' THEN '상의'
		            WHEN I.SIZE_GB = 'B' THEN '하의'
		            WHEN I.SIZE_GB = 'S' THEN '신발'
		            ELSE NULL END AS SIZE_GB_NM
		     , DATE_FORMAT(G.SELL_STDT, '%Y-%m-%d %H:%i:%S') AS SELL_STDT
		     , DATE_FORMAT(G.SELL_EDDT, '%Y-%m-%d %H:%i:%S') AS SELL_EDDT
		     , (CASE WHEN G.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'Y' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD AND OPT_CD1 = ST.OPT_CD1)
		     		 WHEN G.GOODS_TYPE = 'G056_N' AND SELF_GOODS_YN = 'N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = G.GOODS_CD)
		            ELSE (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = G.GOODS_CD)
		            END) AS STOCK_QTY
		     , ST.PROC_GB
		FROM TB_GOODS_SEARCH_SECTION_TEMP ST
		INNER JOIN TB_GOODS G ON ST.GOODS_CD = G.GOODS_CD
		INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		INNER JOIN TB_BRAND_GROUP BG ON B.BRAND_GROUP_NO = BG.BRAND_GROUP_NO
		INNER JOIN TB_GOODS_SUMMARY GS ON ST.GOODS_CD = GS.GOODS_CD
		INNER JOIN TB_ITEMKIND I ON  G.ITEMKIND_CD = I.ITEMKIND_CD

		WHERE ST.PROC_GB = 'D'
		ON DUPLICATE KEY UPDATE
		      PROC_GB = 'D'
		    , DATA_UPD_DT = NOW()
	</insert>

	<!-- 상품검색 TB_GOODS_SEARCHS_SECTION 테이블 삭제 -->
	<delete id="deleteGoodsSearchSection" timeout="600">
		/* TsbGoodsSearch.deleteGoodsSearchSection */
		TRUNCATE TABLE TB_GOODS_SEARCH_SECTION
	</delete>

	<!-- 상품검색 TB_GOODS_SEARCHS_SECTION 테이블 생성 -->
	<insert id="createGoodsSearchSection" timeout="600">
		/* TsbGoodsSearch.createGoodsSearchSection */
		INSERT INTO TB_GOODS_SEARCH_SECTION (
		    GOODS_CD
		  , OPT_CD1
		  , GOODS_NM
		  , GOODS_TNM
		  , GOODS_GB
		  , FOREIGN_BUY_YN
		  , PARALLEL_IMPORT_YN
		  , ORDER_MADE_YN
		  , BRAND_CD
		  , ITEMKIND_CD
		  , FORMAL_GB
		  , LIST_PRICE
		  , CURR_PRICE
		  , SELF_GOODS_YN
		  , GOODS_STAT
		  , SEX_GB
		  , MAIN_COLOR_CD
		  , SUPPLY_COMP_CD
		  , SELF_MALL_YN
		  , AGE_GRP_CD
		  , SEASON_CD
		  , GOODS_TYPE
		  , REG_DT
		  , BRAND_GROUP_KNM
		  , BRAND_GROUP_ENM
		  , BRAND_GROUP_NM
		  , BRAND_GROUP_NO
		  , DEFAULT_CATE_YN
		  , BRAND_CATE_NO
		  , ORDER_CNT
		  , REVIEW_CNT
		  , SIZE_GB
		  , SIZE_GB_NM
		  , SIZES
		  , COLOR_CD
		  , COLOR_CHIP
		  , BENEFIT_GB
		  , PC_CURR_PRICE
		  , MO_CURR_PRICE
		  , APP_CURR_PRICE
		  , AD_KEYWORD
		  , VIDEO_GB_M
		  , VIDEO_VAL_M
		  , VIDEO_GB_S
		  , VIDEO_VAL_S
		  , BYITEM_CATE_NO
		  , BYBRAND_CATE_NO
		  , SYS_IMG_NM
		  , SYS_IMG_NM2
		  , SELL_STDT
		  , SELL_EDDT
		  , STOCK_QTY
		  , OPT_CD1_CHIP
		  , COLOR_CHIPS
		  , PROC_GB
		)
		SELECT GOODS_CD
		     , OPT_CD1
		     , GOODS_NM
		     , GOODS_TNM
		     , GOODS_GB
		     , FOREIGN_BUY_YN
		     , PARALLEL_IMPORT_YN
		     , ORDER_MADE_YN
		     , BRAND_CD
		     , ITEMKIND_CD
		     , FORMAL_GB
		     , LIST_PRICE
		     , CURR_PRICE
		     , SELF_GOODS_YN
		     , GOODS_STAT
		     , SEX_GB
		     , MAIN_COLOR_CD
		     , SUPPLY_COMP_CD
		     , SELF_MALL_YN
		     , AGE_GRP_CD
		     , SEASON_CD
		     , GOODS_TYPE
		     , REG_DT
		     , BRAND_GROUP_KNM
		     , BRAND_GROUP_ENM
		     , BRAND_GROUP_NM
		     , BRAND_GROUP_NO
		     , DEFAULT_CATE_YN
		     , BRAND_CATE_NO
		     , ORDER_CNT
		     , REVIEW_CNT
		     , SIZE_GB
		     , SIZE_GB_NM
		     , SIZES
		     , COLOR_CD
		     , COLOR_CHIP
		     , BENEFIT_GB
		     , PC_CURR_PRICE
		     , MO_CURR_PRICE
		     , APP_CURR_PRICE
		     , AD_KEYWORD
		     , VIDEO_GB_M
		     , VIDEO_VAL_M
		     , VIDEO_GB_S
		     , VIDEO_VAL_S
		     , BYITEM_CATE_NO
		     , BYBRAND_CATE_NO
		     , SYS_IMG_NM
		     , SYS_IMG_NM2
		     , DATE_FORMAT(SELL_STDT, '%Y-%m-%d %H:%i:%S') AS SELL_STDT
		     , DATE_FORMAT(SELL_EDDT, '%Y-%m-%d %H:%i:%S') AS SELL_EDDT
		     , STOCK_QTY
		     , OPT_CD1_CHIP
		     , COLOR_CHIPS
		     , PROC_GB
		FROM TB_GOODS_SEARCH_TEMP
	</insert>

	<!-- 부분색인 상품정보 전체색인 에 적용 - PROC_GB: C -->
	<update id="updateGoodsSearchAll" timeout="600">
		/* TsbGoodsSearch.updateGoodsSearchAll */
		UPDATE TB_GOODS_SEARCH A,
		   (SELECT  GOODS_CD
		          , OPT_CD1
		          , GOODS_NM
		          , GOODS_TNM
		          , GOODS_GB
		          , FOREIGN_BUY_YN
		          , PARALLEL_IMPORT_YN
		          , ORDER_MADE_YN
		          , BRAND_CD
		          , ITEMKIND_CD
		          , FORMAL_GB
		          , LIST_PRICE
		          , CURR_PRICE
		          , SELF_GOODS_YN
		          , GOODS_STAT
		          , SEX_GB
		          , MAIN_COLOR_CD
		          , SUPPLY_COMP_CD
		          , SELF_MALL_YN
		          , AGE_GRP_CD
		          , SEASON_CD
		          , GOODS_TYPE
		          , REG_DT
		          , BRAND_GROUP_KNM
		          , BRAND_GROUP_ENM
		          , BRAND_GROUP_NM
		          , BRAND_GROUP_NO
		          , DEFAULT_CATE_YN
		          , BRAND_CATE_NO
		          , ORDER_CNT
		          , REVIEW_CNT
		          , SIZE_GB
		          , SIZE_GB_NM
		          , SIZES
		          , COLOR_CD
		          , COLOR_CHIP
		          , BENEFIT_GB
		          , PC_CURR_PRICE
		          , MO_CURR_PRICE
		          , APP_CURR_PRICE
		          , AD_KEYWORD
		          , VIDEO_GB_M
		          , VIDEO_VAL_M
		          , VIDEO_GB_S
		          , VIDEO_VAL_S
		          , BYITEM_CATE_NO
		          , BYBRAND_CATE_NO
		          , SYS_IMG_NM
		          , SYS_IMG_NM2
		          , SELL_STDT
		          , SELL_EDDT
		          , STOCK_QTY
		          , OPT_CD1_CHIP
		          , COLOR_CHIPS
		          , PROC_GB
		    FROM TB_GOODS_SEARCH_SECTION
		    WHERE PROC_GB = 'C'
		    ) B
		SET A.GOODS_NM = B.GOODS_NM
		  , A.GOODS_TNM = B.GOODS_TNM
		  , A.GOODS_GB = B.GOODS_GB
		  , A.FOREIGN_BUY_YN = B.FOREIGN_BUY_YN
		  , A.PARALLEL_IMPORT_YN = B.PARALLEL_IMPORT_YN
		  , A.ORDER_MADE_YN = B.ORDER_MADE_YN
		  , A.BRAND_CD = B.BRAND_CD
		  , A.ITEMKIND_CD = B.ITEMKIND_CD
		  , A.FORMAL_GB = B.FORMAL_GB
		  , A.LIST_PRICE = B.LIST_PRICE
		  , A.CURR_PRICE = B.CURR_PRICE
		  , A.SELF_GOODS_YN = B.SELF_GOODS_YN
		  , A.GOODS_STAT = B.GOODS_STAT
		  , A.SEX_GB = B.SEX_GB
		  , A.MAIN_COLOR_CD = B.MAIN_COLOR_CD
		  , A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD
		  , A.SELF_MALL_YN = B.SELF_MALL_YN
		  , A.AGE_GRP_CD = B.AGE_GRP_CD
		  , A.SEASON_CD = B.SEASON_CD
		  , A.GOODS_TYPE = B.GOODS_TYPE
		  , A.REG_DT = B.REG_DT
		  , A.BRAND_GROUP_KNM = B.BRAND_GROUP_KNM
		  , A.BRAND_GROUP_ENM = B.BRAND_GROUP_ENM
		  , A.BRAND_GROUP_NM = B.BRAND_GROUP_NM
		  , A.BRAND_GROUP_NO = B.BRAND_GROUP_NO
		  , A.DEFAULT_CATE_YN = B.DEFAULT_CATE_YN
		  , A.BRAND_CATE_NO = B.BRAND_CATE_NO
		  , A.ORDER_CNT = B.ORDER_CNT
		  , A.REVIEW_CNT = B.REVIEW_CNT
		  , A.SIZE_GB = B.SIZE_GB
		  , A.SIZE_GB_NM = B.SIZE_GB_NM
		  , A.SIZES = B.SIZES
		  , A.COLOR_CD = B.COLOR_CD
		  , A.COLOR_CHIP = B.COLOR_CHIP
		  , A.BENEFIT_GB = B.BENEFIT_GB
		  , A.PC_CURR_PRICE = B.PC_CURR_PRICE
		  , A.MO_CURR_PRICE = B.MO_CURR_PRICE
		  , A.APP_CURR_PRICE = B.APP_CURR_PRICE
		  , A.AD_KEYWORD = B.AD_KEYWORD
		  , A.VIDEO_GB_M = B.VIDEO_GB_M
		  , A.VIDEO_VAL_M = B.VIDEO_VAL_M
		  , A.VIDEO_GB_S = B.VIDEO_GB_S
		  , A.VIDEO_VAL_S = B.VIDEO_VAL_S
		  , A.BYITEM_CATE_NO = B.BYITEM_CATE_NO
		  , A.BYBRAND_CATE_NO = B.BYBRAND_CATE_NO
		  , A.SYS_IMG_NM = B.SYS_IMG_NM
		  , A.SYS_IMG_NM2 = B.SYS_IMG_NM2
		  , A.SELL_STDT = DATE_FORMAT(B.SELL_STDT, '%Y-%m-%d %H:%i:%S')
		  , A.SELL_EDDT = DATE_FORMAT(B.SELL_EDDT, '%Y-%m-%d %H:%i:%S')
		  , A.STOCK_QTY = B.STOCK_QTY
		  , A.OPT_CD1_CHIP = B.OPT_CD1_CHIP
		  , A.COLOR_CHIPS = B.COLOR_CHIPS
		  , A.PROC_GB = B.PROC_GB
		  , A.DATA_UPD_DT = NOW()
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD1 = B.OPT_CD1
	</update>

	<!-- 부분색인 상품정보 전체색인 에 적용 - PROC_GB: D -->
	<update id="updateGoodsSearchAllDel" timeout="600">
		/* TsbGoodsSearch.updateGoodsSearchAllDel */
		UPDATE TB_GOODS_SEARCH A, (SELECT GOODS_CD
		                                , OPT_CD1
		                                , PROC_GB
		                           FROM TB_GOODS_SEARCH_SECTION
		                           WHERE PROC_GB = 'D'
		                          ) B
		SET  A.PROC_GB = B.PROC_GB
		WHERE A.GOODS_CD = B.GOODS_CD
		AND A.OPT_CD1 = B.OPT_CD1
	</update>

</mapper>