<?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">
		/* AdmGoods.createGoodsHst */
		INSERT INTO TB_GOODS_HST 
		(       GOODS_CD
		      , PRODUCT_NO
		      , PRODUCT_CODE
		      , 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
		      , SELF_MALL_YN
		      , GOODS_GB
		      , DISTRIBUTION_GB
		      , SELF_GOODS_YN
		      , SUPPLY_COMP_CD
		      , SUPPLY_GOODS_CD
		      , AGE_GRP_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
		      , ADULT_YN
		      , REG_NO
		      , REG_DT
		      , UPD_NO
		      , UPD_DT
		)
		SELECT GOODS_CD            
		     , PRODUCT_NO
		     , PRODUCT_CODE
		     , 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          
		     , SELF_MALL_YN             
		     , GOODS_GB            
		     , DISTRIBUTION_GB     
		     , SELF_GOODS_YN       
		     , SUPPLY_COMP_CD      
		     , SUPPLY_GOODS_CD     
		     , AGE_GRP_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
		     , ADULT_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) RANK
		                             FROM TB_GOODS_TNM_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                            ) Z
		                       WHERE RANK = 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) RANK
		                             FROM TB_GOODS_TNM_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                            ) Z
		                       WHERE RANK = 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) RANK
		                             FROM TB_GOODS_PRICE_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                            ) Z
		                       WHERE RANK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND G.CURR_PRICE = S.RES_GOODS_PRICE
		                 AND G.SELF_GOODS_YN = 'Y'
		                )
		      </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) RANK
		                             FROM TB_GOODS_PRICE_RES GT
		                             WHERE GT.APPLY_YN = 'N'
		                             AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                            ) Z
		                       WHERE RANK = 1
		                      ) S
		                 WHERE G.GOODS_CD = S.GOODS_CD
		                 AND (G.CURR_PRICE <![CDATA[<>]]> S.RES_GOODS_PRICE)
		                 AND G.SELF_GOODS_YN = 'Y'
		                )
		      </when>
		      <otherwise>
		AND 1 = 2
		      </otherwise>
		    </choose>

	</insert>
	
	<!-- 상품 타이틀 예약건 초기화 -->
	<update id="updateGoodTnmInit">
		/* TsbGoods.updateGoodTnmInit */
		UPDATE TB_GOODS A
		SET A.UPD_NO = 0
		  , 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) RANK
		                       FROM TB_GOODS_TNM_RES GT
		                       WHERE GT.APPLY_YN = 'N'
		                       AND NOW() BETWEEN GT.APPLY_STDT AND GT.APPLY_EDDT
		                      ) S
		                   WHERE S.RANK = 1
		                   AND S.GOODS_CD = A.GOODS_CD
		                   )
		WHERE A.GOODS_CD IN (
		                   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) RANK
		                              FROM TB_GOODS_TNM_RES RS
		                              WHERE RS.APPLY_EDDT <![CDATA[<]]> NOW()
		                              AND RS.APPLY_YN = 'N'
		                              ) Z
		                         WHERE RANK = 1
		                        ) TS
		                   WHERE  P.GOODS_CD = TS.GOODS_CD
		                  )
	</update>
	
	<!-- 상품예약 종료처리 -->
	<update id="updateGoodsTnmRes">
		/* TsbGoods.updateGoodsTnmRes */
		UPDATE TB_GOODS_TNM_RES
		   SET APPLY_YN = 'Y'
		     , UPD_NO = 0
		     , UPD_DT = NOW()
		WHERE APPLY_EDDT <![CDATA[<]]> NOW()
		AND APPLY_YN = 'N'
	</update>
	
	<!-- 상품타이틀 변경 -->
	<update id="updateGoodsTnm">
		/* TsbGoods.updateGoodsTnm */
		UPDATE TB_GOODS A
		SET A.UPD_NO = 0
		  , A.UPD_DT = NOW()
		  , A.GOODS_TNM = (
		                 SELECT S.GOODS_TNM
		                 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) RANK
		                             FROM TB_GOODS_TNM_RES RS
		                             WHERE NOW() BETWEEN RS.APPLY_STDT AND RS.APPLY_EDDT
		                             AND RS.APPLY_YN = 'N'
		                             ) Z
		                       WHERE RANK = 1
		                      ) S
		                   WHERE P.GOODS_CD = S.GOODS_CD
		                   AND  P.GOODS_CD = A.GOODS_CD
		                   AND (P.GOODS_TNM IS NULL OR P.GOODS_TNM <![CDATA[<>]]> S.GOODS_TNM)
		                 )
		WHERE A.GOODS_CD IN (
		                   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) RANK
		                              FROM TB_GOODS_TNM_RES RS
		                              WHERE NOW() BETWEEN RS.APPLY_STDT AND RS.APPLY_EDDT
		                              AND RS.APPLY_YN = 'N'
		                              ) Z
		                         WHERE RANK = 1
		                        ) S
		                   WHERE  P.GOODS_CD = S.GOODS_CD
		                   AND (P.GOODS_TNM IS NULL OR P.GOODS_TNM <![CDATA[<>]]> S.GOODS_TNM)
		                  )
	</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) RANK
		                               FROM TB_GOODS_PRICE_RES GT
		                               WHERE GT.APPLY_EDDT <![CDATA[<]]> NOW()
		                               AND GT.APPLY_YN = 'N'
		                              ) S
		                         WHERE S.RANK = 1
		                         ) TS
		                    WHERE P.GOODS_CD = TS.GOODS_CD
		                    ) B
		SET A.UPD_NO = 0
		  , 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
		AND A.SELF_GOODS_YN = 'Y'
	</update>
	
	<!-- 상품 가격 예약 종료처리 -->
	<update id="updateGoodsPriceRes">
		/* TsbGoods.updateGoodsPriceRes */
		UPDATE TB_GOODS_PRICE_RES
		   SET APPLY_YN = 'Y'
		     , UPD_NO = 0
		     , 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) RANK
		                                FROM TB_GOODS_PRICE_RES RS
		                                WHERE NOW() BETWEEN RS.APPLY_STDT AND RS.APPLY_EDDT
		                                AND RS.APPLY_YN = 'N'
		                                ) Z
		                          WHERE RANK = 1
		                         ) S
		                    WHERE P.GOODS_CD = S.GOODS_CD
		                    AND P.CURR_PRICE <![CDATA[<>]]> S.RES_GOODS_PRICE
		                 ) B
		SET A.UPD_NO = 0
		  , 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
		AND A.SELF_GOODS_YN = 'Y'
	</update>
	
	<!--상품 검색어 작업 - 사용자검색어 없는 상품 일괄 -->
	<update id="updateGoodsSnmByGoodsSnm1Null">
		/* TsbGoods.updateGoodsSnmByGoodsSnm1Null */
		UPDATE TB_GOODS A, (
		                    WITH RECURSIVE 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,';',
		                                  B.BRAND_GRP_NM,';',
		                                  G.STYLE_YEAR,';',
		                                  FN_GET_CODE_NM('G006',G.SEASON_CD),';',
		                                  FN_GET_CODE_NM('G007',G.SEX_GB),';',
		                                  NVL2(D.COLOR_INFO,CONCAT(D.COLOR_INFO,';'),''),
		                                  REPLACE(I.ITEMKIND_NM,'>',';')
		                                  )) AS GOODS_SNM_COMP
		                           FROM TB_GOODS G
		                           INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
		                           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 <> NVL(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">
		WITH RECURSIVE 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,';',
		       B.BRAND_GRP_NM,';',
		       G.STYLE_YEAR,';',
		       FN_GET_CODE_NM('G006',G.SEASON_CD),';',
		       FN_GET_CODE_NM('G007',G.SEX_GB),';',
		       NVL2(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
		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">
		SELECT GOODS_CD
		       , 'G100_00' AS CUST_GB
		       , CURR_PRICE
		       , MIN(CURR_PRICE - 
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> DC_PVAL THEN MAX_DC_AMT ELSE DC_PVAL END)
		            ELSE
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> FLOOR((CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit} THEN MAX_DC_AMT ELSE FLOOR((CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit} END)
		            END) )AS PC_CURR_PRICE
		       , MIN(CURR_PRICE - 
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> DC_MVAL THEN MAX_DC_AMT ELSE DC_MVAL END)
		            ELSE
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> FLOOR((CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit} THEN MAX_DC_AMT ELSE FLOOR((CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit} END)
		            END)  )AS MO_CURR_PRICE
		       , STAFF_CURR_PRICE
		       , MIN(STAFF_CURR_PRICE - 
		           ( CASE WHEN DC_WAY = 'V' THEN
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> DC_PVAL THEN MAX_DC_AMT ELSE DC_PVAL END)
		            ELSE
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> FLOOR((STAFF_CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit} THEN MAX_DC_AMT ELSE FLOOR((STAFF_CURR_PRICE * DC_PVAL / 100) / #{pointUnit}) * #{pointUnit} END)
		            END) )AS STAFF_PC_CURR_PRICE
		       , MIN(STAFF_CURR_PRICE - 
		           ( CASE WHEN DC_WAY = 'G240_10' THEN
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> DC_MVAL THEN MAX_DC_AMT ELSE DC_MVAL END)
		            ELSE
		                 (CASE WHEN NVL(MAX_DC_AMT, 0) >0 AND MAX_DC_AMT <![CDATA[<]]> FLOOR((STAFF_CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit} THEN MAX_DC_AMT ELSE FLOOR((STAFF_CURR_PRICE * DC_MVAL / 100) / #{pointUnit}) * #{pointUnit} END)
		            END)  )AS STAFF_MO_CURR_PRICE
		FROM   (
		        SELECT CP.CPN_ID
		             , CP.CPN_NM
		             , CP.DC_WAY                                              /*할인방식*/
		             , CP.DC_PVAL                                             /*PC 할인값*/
		             , CP.DC_MVAL                                             /*MO 할인값*/
		             , CP.MAX_DC_AMT                                          /*최대할인금액*/
		             , CP.CURR_PRICE                                          /*전체 판매가*/
		             , CP.CURR_PRICE_YN
		             , CP.STAFF_CURR_PRICE                                          /*임직원판매가*/
		             , CP.STAFF_CURR_PRICE_YN
		             , CP.AVAIL_EDDT
		             , CP.USABLE_CUST_GB AS CUST_GB
		             , CP.GOODS_CD
		        FROM   (
		                SELECT CP.CPN_ID    /*쿠폰ID*/
		                     , CP.CPN_NM    /*쿠폰명*/
		                     , CP.DC_WAY    /*할인방식*/
		                     , CP.DC_PVAL   /*PC할인값*/
		                     , CP.DC_MVAL   /*모바일할인값*/
		                     , CP.MAX_DC_AMT  /*최대할인금액*/
		                     , G.CURR_PRICE  /*현재판매가*/
		                     , CASE WHEN BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE THEN 'Y'
		                            ELSE 'N'
		                            END AS CURR_PRICE_YN
		                     , G.CURR_PRICE AS STAFF_CURR_PRICE  /*임직원판매가*/
		                     , CASE WHEN BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE THEN 'Y'
		                            ELSE 'N'
		                            END AS STAFF_CURR_PRICE_YN
		                     , IF (CP.PD_GB = 'D', DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S'), CP.AVAIL_EDDT) AS AVAIL_EDDT  
		                     , CP.USABLE_CUST_GB   /*쿠폰사용이 가능한 고객(G100_00:전체, G100_10:일반회원, G100_20:임직원)*/
		                     , G.GOODS_CD
		                FROM   TB_GOODS G
		                     , TB_COUPON CP
		                WHERE  CP.SITE_CD = '10'
		                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.DN_GB = 'G058_10' /* 자동다운로드(결제시)다운로드*/
		                AND    CP.CPN_TYPE = 'G230_10' /*즉시할인쿠폰*/
		                AND    CP.USABLE_CUST_GB  = #{usableCustGb}
		                AND    CP.USABLE_CUST_GRADE  = 'G100_00' /* 사용가능고객등급이 일반만*/
		                AND    CP.BUY_LIMIT_AMT <![CDATA[<=]]> G.CURR_PRICE  /*최소주문금액*/
		                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) /*총발행제한수*/
		                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
		                                                   ) > 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 NVL(REF_FORMAL_GB,G.FORMAL_GB) = G.FORMAL_GB
		                                                   AND NVL(REF_BRAND_CD,G.BRAND_CD) = G.BRAND_CD
		                                                   AND REF_VAL IN (SELECT CATE_CD
		                                                                   FROM TB_CATEGORY_GOODS
		                                                                   WHERE GOODS_CD = 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_12' 
		                                                   AND DEL_YN = 'N'
		                                                   AND REF_VAL = G.BRAND_CD
		                                                   AND NVL(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 NVL(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 = '40' /* 제외상품*/
		                               )
		                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">
		/* TsbGoods.deleteGoodsBenefitPriceTemp */
		TRUNCATE TABLE TB_GOODS_BENEFIT_PRICE_TEMP
	</delete>
	
	
	<!-- 상품혜택가 temp 적용 등록 -->
	<insert id="crteateGoodsBenefitPriceTemp"  parameterType="Coupon"  timeout="600">
		/* TsbGoods.crteateGoodsBenefitPriceTemp */
		INSERT INTO TB_GOODS_BENEFIT_PRICE_TEMP (
		     GOODS_CD
		   , CUST_GB
		   , CURR_PRICE
		   , PC_CURR_PRICE
		   , MO_CURR_PRICE
		   , STAFF_CURR_PRICE
		   , STAFF_PC_CURR_PRICE
		   , STAFF_MO_CURR_PRICE
		   )
		    <include refid="getGoodsBenefitList_sql"/>
	</insert>
	
	<!-- 상품혜택가 temp 적용 변경 -->
	<update id="updateGoodsBenefitPriceTemp"  parameterType="Coupon"  timeout="600">
		/* TsbGoods.updateGoodsBenefitPriceTemp */
		INSERT INTO TB_GOODS_BENEFIT_PRICE_TEMP (
		         GOODS_CD
		       , CUST_GB
		       , CURR_PRICE
		       , PC_CURR_PRICE
		       , MO_CURR_PRICE
		       , STAFF_CURR_PRICE
		       , STAFF_PC_CURR_PRICE
		       , STAFF_MO_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
		    <if test="usableCustGb != null and usableCustGb == 'G100_00'">
		   , PC_CURR_PRICE = B.PC_CURR_PRICE
		   , MO_CURR_PRICE = B.MO_CURR_PRICE
		   , STAFF_PC_CURR_PRICE = B.STAFF_PC_CURR_PRICE
		   , STAFF_MO_CURR_PRICE = B.STAFF_MO_CURR_PRICE
		    </if>
		    <if test="usableCustGb != null and usableCustGb == 'G100_10'">
		   , PC_CURR_PRICE = IF (TB_GOODS_BENEFIT_PRICE_TEMP.PC_CURR_PRICE >= B.PC_CURR_PRICE, B.PC_CURR_PRICE, TB_GOODS_BENEFIT_PRICE_TEMP.PC_CURR_PRICE)
		   , MO_CURR_PRICE = IF (TB_GOODS_BENEFIT_PRICE_TEMP.MO_CURR_PRICE >= B.MO_CURR_PRICE, B.MO_CURR_PRICE, TB_GOODS_BENEFIT_PRICE_TEMP.MO_CURR_PRICE)
		    </if>
		    <if test="usableCustGb != null and usableCustGb == 'G100_20'">
		   , STAFF_PC_CURR_PRICE = IF (TB_GOODS_BENEFIT_PRICE_TEMP.STAFF_PC_CURR_PRICE >= B.STAFF_PC_CURR_PRICE, B.STAFF_PC_CURR_PRICE, TB_GOODS_BENEFIT_PRICE_TEMP.STAFF_PC_CURR_PRICE)
		   , STAFF_MO_CURR_PRICE = IF (TB_GOODS_BENEFIT_PRICE_TEMP.STAFF_MO_CURR_PRICE >= B.STAFF_MO_CURR_PRICE, B.STAFF_MO_CURR_PRICE, TB_GOODS_BENEFIT_PRICE_TEMP.STAFF_MO_CURR_PRICE)
		    </if>
	</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
		    , STAFF_CURR_PRICE
		    , STAFF_PC_CURR_PRICE
		    , STAFF_MO_CURR_PRICE
		    , REG_DT
		    )
		SELECT GOODS_CD
		     , CUST_GB
		     , CURR_PRICE
		     , PC_CURR_PRICE
		     , MO_CURR_PRICE
		     , STAFF_CURR_PRICE
		     , STAFF_PC_CURR_PRICE
		     , STAFF_MO_CURR_PRICE
		     , NOW()
		FROM TB_GOODS_BENEFIT_PRICE_TEMP
	</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
		     , 0
		     , 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_TEMP
	</delete>

	<!--전시상품 통계 - 판매 -->
	<insert id="createGoodsSummaryBySale" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryBySale */
		INSERT INTO TB_GOODS_SUMMARY_TEMP (
		    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
		              , 0
		              , 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_TEMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,COUNT(GOODS_CD) AS COL_DATA
		             ,0
		             ,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_TEMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,COUNT(GOODS_CD) AS COL_DATA
		             ,0
		             ,NOW()
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       <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_DATA
	</insert>

	<!--전시상품 통계 - 상품평 점수 합계 -->
	<insert id="createGoodsSummaryByReviewSum" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewSum */
		INSERT INTO TB_GOODS_SUMMARY_TEMP (
		    GOODS_CD
		  , ${colNm1}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,SUM(${reviewColNm})AS COL_DATA
		             ,0
		             ,NOW()
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       <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_DATA
	</insert>

	<!--전시상품 통계 - 상품평 스코어합계-->
	<insert id="createGoodsSummaryByReviewScore" parameterType="GoodsSummary">
		/* TsbGoods.createGoodsSummaryByReviewScore */
		INSERT INTO TB_GOODS_SUMMARY_TEMP (
		    GOODS_CD
		  , ${colNm1}
		  , ${colNm2}
		  , ${colNm3}
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT GOODS_CD
		             ,NVL(SUM(DECODE(${reviewColNm},'1',1,0)),0) AS COL_DATA1
		             ,NVL(SUM(DECODE(${reviewColNm},'2',1,0)),0) AS COL_DATA2
		             ,NVL(SUM(DECODE(${reviewColNm},'3',1,0)),0) AS COL_DATA3
		             ,0
		             ,NOW()
		       FROM TB_REVIEW
		       WHERE DISP_YN = 'Y'
		       AND DEL_YN = 'N'
		       <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_TEMP (
		    GOODS_CD
		  , GOODS_QNA_CNT
		  , REG_NO
		  , REG_DT
		)
		(
		    SELECT *
		    FROM (
		       SELECT REL_GOODS_CD
		             ,COUNT(REL_GOODS_CD) AS COL_DATA
		             ,0
		             ,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="saveGoodsSummary">
		/* TsbGoods.saveGoodsSummary */
		UPDATE TB_GOODS_SUMMARY A, TB_GOODS_SUMMARY_TEMP GS
		SET  A.SELL_TOT_QTY        = NVL(GS.SELL_TOT_QTY,0)
		   , A.SELL_WEEK_QTY       = NVL(GS.SELL_WEEK_QTY,0)
		   , A.SELL_MONTH_QTY      = NVL(GS.SELL_MONTH_QTY,0)
		   , A.SELL_YDAY_QTY       = NVL(GS.SELL_YDAY_QTY,0)
		   , A.SELL_DAY15_QTY      = NVL(GS.SELL_DAY15_QTY,0)
		   , A.READ_NWEEK_CNT      = NVL(GS.READ_NWEEK_CNT,0)
		   , A.READ_PWEEK_CNT      = NVL(GS.READ_PWEEK_CNT,0)
		   , A.GOODS_QNA_CNT       = NVL(GS.GOODS_QNA_CNT,0)
		   , A.REVIEW_REG_CNT      = NVL(GS.REVIEW_REG_CNT,0)
		   , A.RREVIEW_REG_CNT     = NVL(GS.RREVIEW_REG_CNT,0)
		   , A.PREVIEW_REG_CNT     = NVL(GS.PREVIEW_REG_CNT,0)
		   , A.SCORE               = NVL(GS.SCORE,0)
		   , A.SCORE_SIZE1         = NVL(GS.SCORE_SIZE1,0)
		   , A.SCORE_SIZE2         = NVL(GS.SCORE_SIZE2,0)
		   , A.SCORE_SIZE3         = NVL(GS.SCORE_SIZE3,0)
		   , A.SCORE_COLOR1        = NVL(GS.SCORE_COLOR1,0)
		   , A.SCORE_COLOR2        = NVL(GS.SCORE_COLOR2,0)
		   , A.SCORE_COLOR3        = NVL(GS.SCORE_COLOR3,0)
		   , A.SCORE_FIT1          = NVL(GS.SCORE_FIT1,0)
		   , A.SCORE_FIT2          = NVL(GS.SCORE_FIT2,0)
		   , A.SCORE_FIT3          = NVL(GS.SCORE_FIT3,0)
		   , A.SCORE_THICK1        = NVL(GS.SCORE_THICK1,0)
		   , A.SCORE_THICK2        = NVL(GS.SCORE_THICK2,0)
		   , A.SCORE_THICK3        = NVL(GS.SCORE_THICK3,0)
		   , A.SCORE_WEIGHT1       = NVL(GS.SCORE_WEIGHT1,0)
		   , A.SCORE_WEIGHT2       = NVL(GS.SCORE_WEIGHT2,0)
		   , A.SCORE_WEIGHT3       = NVL(GS.SCORE_WEIGHT2,0)
		   , A.SCORE_BALL1         = NVL(GS.SCORE_BALL1,0)
		   , A.SCORE_BALL2         = NVL(GS.SCORE_BALL2,0)
		   , A.SCORE_BALL3         = NVL(GS.SCORE_BALL3,0)
		   , A.UPD_NO              = 0
		   , A.UPD_DT              = NOW()
		WHERE A.GOODS_CD = GS.GOODS_CD
	</insert>
	
	<!--  안전인증대상 상품 조회 -->
	<select id="getGoodsSafeNoList"   resultType="GoodsSafeNo">
		/* TsbGoods.getGoodsSafeNoList */
		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 TB_GOODS_SAFE_NO (
		    GOODS_CD
		  , CERT_NUM
		  , CERT_STATE
		  , CERT_DIV
		  , CERT_DT
		  , REG_NO
		  , REG_DT
		  , UPD_NO
		  , UPD_DT
		)
		VALUES (
		    #{goodsCd}
		  , #{certNum}
		  , #{certState}
		  , #{certDiv}
		  , #{certDt}
		  , #{regNo}
		  , NOW()
		  , #{updNo}
		  , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       CERT_NUM = #{certNum}
		     , CERT_DIV = #{certDiv}
		     , CERT_STATE = #{certState}
		     , CERT_DT = #{certDt}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>
	
</mapper>