<?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.scm.biz.dao.TssGoodsDao">

  <!-- 품목 목록 -->
  <select id="getItemkindList" parameterType="Itemkind" resultType="Itemkind">
    /* TssGoods.getItemkindList */
    SELECT ITEMKIND_CD
         , ITEMKIND_NM
         , NI_CLSF_CD
         , SIZE_GB
         , USE_YN
         , REPLACE(SUBSTRING_INDEX(ITEMKIND_NM, '>', -2) ,'>','') AS ITEMKIND_LEAF_NM
    FROM TB_ITEMKIND
    WHERE 1 = 1
    <if test="itemkindCd != null and itemkindCd != ''">
    AND ITEMKIND_CD = #{itemkindCd}
    </if>
    <if test="useYn != null and useYn != ''">
    AND USE_YN = #{useYn}
    </if>
    <if test="multiItemkindCd != null and multiItemkindCd.length > 0">
    AND ITEMKIND_CD IN
        <foreach collection="multiItemkindCd" item="item" index="index"  open="(" close=")" separator=",">
         UPPER(#{item})
        </foreach>
    </if>
    <if test="multiItemkindNm != null and multiItemkindNm.length > 0">
    AND (
        <foreach collection="multiItemkindNm" item="item" index="index" separator="or">
        ITEMKIND_NM LIKE CONCAT('%',#{item},'%')
      </foreach>
     )
    </if>
    ORDER BY ITEMKIND_CD
  </select>

  <!-- 상품 목록 건수 -->
  <select id="getGoodsListCount" parameterType="GoodsSearch" resultType="int">
    /* TssGoods.getGoodsListCount */
            SELECT COUNT(*) AS TOTCNT
            FROM TB_GOODS G
            INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
            INNER JOIN TB_SUPPLY_COMPANY S ON G.SUPPLY_COMP_CD = S.SUPPLY_COMP_CD
            INNER JOIN TB_GOODS_SUMMARY GS ON G.GOODS_CD = GS.GOODS_CD
            <if test="searchGb != null and searchGb =='EXCEL'">
            INNER JOIN (
                         SELECT SEARCH_CD
                              , TMP_DISP_ORD
                         FROM (
                               SELECT SEARCH_CD
                                    , MIN(DISP_ORD) AS TMP_DISP_ORD
                               FROM TB_SEARCH_DATA
                               WHERE REG_NO = #{regNo}
                               GROUP BY SEARCH_CD) T
                       ) SD
                       ON ( (G.GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%')
                            OR G.SUPPLY_GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%'))
                            <if test="mdNo != null and mdNo != ''">
                            AND G.BRAND_CD IN (
                                               SELECT DISTINCT BRAND_CD
                                               FROM TB_BRAND_MD
                                               WHERE MD_NO = CAST(#{mdNo} AS UNSIGNED)
                                               )
                           </if>
                       )
            </if>
            WHERE 1=1
            <if test="searchGb == null or searchGb =='BASIC' or searchGb =='EXTEND' or searchGb =='MASTER'" >
            <include refid="getGoodsListCondition_sql"/>
            </if>
  </select>

  <!-- 상품 목록 -->
  <select id="getGoodsList" parameterType="GoodsSearch" resultType="Goods">
    /* TssGoods.getGoodsList */
    SELECT Q.*
      --  , (CASE WHEN Q.GOODS_TYPE = 'G056_N' THEN (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK WHERE GOODS_CD = Q.GOODS_CD)
      --          ELSE (SELECT IFNULL(SUM(CURR_STOCK_QTY - BASE_STOCK_QTY),0) FROM VW_STOCK_COMPOSE WHERE GOODS_CD = Q.GOODS_CD)
      --          END) AS STOCK_QTY_SUM
        , IFNULL((SELECT STOCK_QTY FROM TB_GOODS_STOCK WHERE GOODS_CD = Q.GOODS_CD),0) AS STOCK_QTY_SUM
        , (SELECT ITEMKIND_NM FROM TB_ITEMKIND WHERE ITEMKIND_CD = Q.ITEMKIND_CD ) AS ITEMKIND_NM
        , FN_GET_USER_NM(REG_NO) AS REG_NM
        , FN_GET_USER_NM(UPD_NO) AS UPD_NM
        , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = Q.GOODS_CD AND COLOR_CD = Q.MAIN_COLOR_CD AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM
        , CASE IFNULL((SELECT COUNT(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = Q.GOODS_CD AND COLOR_CD = Q.MAIN_COLOR_CD AND DEFAULT_IMG_YN = 'Y' ),0)
                WHEN 0 THEN 'N'
                ELSE 'Y' END AS GOODS_IMAGE_YN
    FROM (
    SELECT Z.*
    FROM (
        SELECT A.* -- , @rownum := @rownum + 1 AS RNUM
        FROM (
            SELECT
                    G.GOODS_CD
                  , G.GOODS_TYPE
                  , B.BRAND_ENM
                  , B.BRAND_GRP_NM
                  , G.BRAND_CD
                  , G.ITEMKIND_CD
                  , G.SUPPLY_COMP_CD
                  , G.SUPPLY_GOODS_CD
                  , G.GOODS_NUM
                  , G.GOODS_NM
                  , G.GOODS_STAT
                  , G.LIST_PRICE
                  , G.CURR_PRICE
                  , G.DC_RATE
                  , G.SELF_GOODS_YN
                  , G.SELF_MALL_YN
                  , G.FORMAL_GB
                  , G.STYLE_YEAR
                  , G.SEASON_CD
                  , G.ERP_STOCK_LINK_YN
                  , G.ERP_PRICE_LINK_YN
                  , G.CHANGEABLE_YN
                  , G.RETURNABLE_YN
                  , G.NEW_CUST_ORD_YN
                  , G.GIFT_PACK_YN
                  , G.DELV_LOC_CD
                  , G.DELV_FEE_CD
                  , G.MAIN_COLOR_CD
                  , DATE_FORMAT(G.FRST_CFRM_DT, '%Y%m%d%H%i%S') AS FRST_CFRM_DT
                  , G.SELL_FEE_RATE
                  , DATE_FORMAT(G.PRICE_UPD_DT,'%Y%m%d%H%i%S') AS PRICE_UPD_DT
                  , G.REG_NO
                  , DATE_FORMAT(G.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
                  , G.UPD_NO
                  , DATE_FORMAT(G.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
                  , GS.SELL_DAY15_QTY
                  <choose>
                      <when test="searchGb != null and searchGb =='EXCEL'">
                  , SD.TMP_DISP_ORD
                      </when>
                      <otherwise>
                  , 0 AS TMP_DISP_ORD
                      </otherwise>
                  </choose>
            FROM TB_GOODS G
           -- JOIN ( SELECT @rownum := 0) R
            INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
            INNER JOIN TB_SUPPLY_COMPANY S ON G.SUPPLY_COMP_CD = S.SUPPLY_COMP_CD
            INNER JOIN TB_GOODS_SUMMARY GS ON G.GOODS_CD = GS.GOODS_CD
            <if test="searchGb != null and searchGb =='EXCEL'">
            INNER JOIN (
                         SELECT SEARCH_CD
                              , TMP_DISP_ORD
                         FROM (
                               SELECT SEARCH_CD
                                    , MIN(DISP_ORD) AS TMP_DISP_ORD
                               FROM TB_SEARCH_DATA
                               WHERE REG_NO = #{regNo}
                               GROUP BY SEARCH_CD) T
                       ) SD
                       ON ( (G.GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%')
                            OR G.SUPPLY_GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%'))
                            <if test="mdNo != null and mdNo != ''">
                            AND G.BRAND_CD IN (
                                               SELECT DISTINCT BRAND_CD
                                               FROM TB_BRAND_MD
                                               WHERE MD_NO = CAST(#{mdNo} AS UNSIGNED)
                                               )
                           </if>
                       )
            </if>
            WHERE 1=1
            <if test="searchGb == null or searchGb =='BASIC' or searchGb =='EXTEND' or searchGb =='MASTER'" >
            <include refid="getGoodsListCondition_sql"/>
            ORDER BY G.REG_DT DESC, G.GOODS_CD
            </if>
            <if test="searchGb != null and searchGb =='EXCEL'">
            ORDER BY SD.TMP_DISP_ORD
            </if>
    <include refid="getListLimitPagingCondition_sql"/>
  </select>

  <!-- 상품 목록 조건 정보 -->
  <sql id="getGoodsListCondition_sql">
            <if test='conditionList != null and conditionList.length>0'>
                <choose>
                  <when test='search != null and search == "searchGoodsCd"'>
            AND (
                  <foreach collection="conditionList" item="item" index="index" separator="or">
                   G.GOODS_CD LIKE CONCAT(UPPER(#{item}),'%')
                  </foreach>
                 )
                  </when>
                  <when test='search != null and search == "searchGoodsNm"'>
            AND (
                  <foreach collection="conditionList" item="item" index="index" separator="or">
                   UPPER(G.GOODS_NM) LIKE CONCAT(UPPER(#{item}),'%')
                  </foreach>
                 )
                  </when>
                  <when test='search != null and search == "searchGoodsNum"'>
            AND (
                  <foreach collection="conditionList" item="item" index="index" separator="or">
                   UPPER(G.GOODS_NUM) LIKE CONCAT(UPPER(#{item}),'%')
                  </foreach>
                 )
                  </when>
                  <when test='search != null and search == "searchSupplyGoodsCd"'>
            AND (
                  <foreach collection="conditionList" item="item" index="index" separator="or">
                   UPPER(G.SUPPLY_GOODS_CD) LIKE CONCAT(UPPER(#{item}),'%')
                  </foreach>
                 )
                  </when>
                  <when test='search != null and search == "searchExtendGoodsCd"'>
            AND 1 = 1
                  </when>
                  <when test='search != null and search == "searchMasterGoodsCd"'>
            AND 1 = 1
                  </when>
                  <otherwise>
            AND 1 = 1
                  </otherwise>
                </choose>
            </if>
            <if test="goodsCd != null and goodsCd != ''">
            AND G.GOODS_CD LIKE CONCAT(UPPER(#{goodsCd}),'%')
            </if>
             <if test="goodsNm != null and goodsNm != ''">
            AND UPPER(G.GOODS_NM) LIKE CONCAT(UPPER(#{goodsNm}),'%')
            </if>
            <if test="goodsType != null and goodsType != ''">
            AND G.GOODS_TYPE = #{goodsType}
            </if>
            <if test="supplyCompCd != null and supplyCompCd != ''">
            AND G.SUPPLY_COMP_CD = #{supplyCompCd}
            </if>
            <if test="brandCd != null and brandCd != ''">
            AND G.BRAND_CD = #{brandCd}
            </if>
            <if test="multiBrand != null and multiBrand != ''">
            AND G.BRAND_CD IN
                <foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
            #{item}
                </foreach>
            </if>
            <if test="selfMallYn != null and selfMallYn != ''">
            AND G.SELF_MALL_YN = #{selfMallYn}
            </if>
            <if test="itemkindCd != null and itemkindCd != ''">
            AND G.ITEMKIND_CD = #{itemkindCd}
            </if>
            <if test="multiItemkindCd != null and multiItemkindCd != ''">
            AND G.ITEMKIND_CD IN
                <foreach collection="multiItemkindCd" item="item" index="index"  open="(" close=")" separator=",">
            #{item}
                </foreach>
            </if>
            <if test="goodsStat != null and goodsStat != ''">
            AND G.GOODS_STAT = #{goodsStat}
            </if>
            <if test="multiGoodsStat != null and multiGoodsStat != ''">
            AND G.GOODS_STAT IN
                <foreach collection="multiGoodsStat" item="item" index="index"  open="(" close=")" separator=",">
            #{item}
                </foreach>
            </if>
            <if test="styleYear != null and styleYear != ''">
            AND G.STYLE_YEAR = #{styleYear}
            </if>
            <if test="multiStyleYear != null and multiStyleYear != ''">
            AND G.STYLE_YEAR IN
                <foreach collection="multiStyleYear" item="item" index="index"  open="(" close=")" separator=",">
                #{item}
                </foreach>
            </if>
            <if test="seasonCd != null and seasonCd != ''">
            AND G.SEASON_CD = #{seasonCd}
            </if>
            <if test="multiSeasonCd != null and multiSeasonCd != ''">
            AND G.SEASON_CD IN
                <foreach collection="multiSeasonCd" item="item" index="index"  open="(" close=")" separator=",">
            #{item}
                </foreach>
            </if>
            <if test="sexGb != null and sexGb != ''">
            AND G.SEX_GB = #{sexGb}
            </if>
            <if test="changeableYn != null and changeableYn != ''">
            AND G.CHANGEABLE_YN = #{changeableYn}
            </if>
            <if test="returnableYn != null and returnableYn != ''">
            AND G.RETURNABLE_YN = #{returnableYn}
            </if>
            <if test="returnFeeFreeYn != null and returnFeeFreeYn != ''">
            AND G.RETURN_FEE_FREE_YN = #{returnFeeFreeYn}
            </if>
            <if test="changeFeeFreeYn != null and changeFeeFreeYn != ''">
            AND G.CHANGE_FEE_FREE_YN = #{changeFeeFreeYn}
            </if>
            <if test="formalGb != null and formalGb != ''">
            AND G.FORMAL_GB = #{formalGb}
            </if>
            <if test='soldOutYn != null and soldOutYn == "Y"'>
            AND EXISTS (
                        SELECT GOODS_CD
                        FROM VW_STOCK
                        WHERE SOLDOUT_YN = 'Y'
                        AND GOODS_CD = G.GOODS_CD
                       )
            </if>
            <if test='soldOutYn != null and soldOutYn == "N"'>
            AND NOT EXISTS (
                        SELECT GOODS_CD
                        FROM VW_STOCK
                        WHERE SOLDOUT_YN = 'Y'
                        AND GOODS_CD = G.GOODS_CD
                       )
            </if>
            <if test="erpStockLinkYn != null and erpStockLinkYn != ''">
            AND G.ERP_STOCK_LINK_YN = #{erpStockLinkYn}
            </if>
            <if test="erpPriceLinkYn != null and erpPriceLinkYn != ''">
            AND G.ERP_PRICE_LINK_YN = #{erpPriceLinkYn}
            </if>
            <if test="mdNo != null and mdNo != ''">
            AND G.BRAND_CD IN (
                SELECT DISTINCT BRAND_CD
                FROM   TB_BRAND_MD
                WHERE  MD_NO = CAST(#{mdNo} AS UNSIGNED)
            )
            </if>
            <if test="currPriceSt != null and currPriceSt != ''">
            AND G.CURR_PRICE >= #{currPriceSt}
            </if>
            <if test="currPriceEd != null and currPriceEd != ''">
            <![CDATA[
            AND G.CURR_PRICE <= #{currPriceEd}
             ]]>
            </if>
            <if test="dcRateSt != null and dcRateSt != ''">
            AND G.DC_RATE >= #{dcRateSt}
            </if>
            <if test="dcRateEd != null and dcRateEd != ''">
            <![CDATA[
            AND G.DC_RATE <= #{dcRateEd}
            ]]>
            </if>
            <if test='dateGbn != null and dateGbn == "P"'>
                <if test="stDate != null and stDate != ''">
            AND G.PRICE_UPD_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
                </if>
                <if test="edDate != null and edDate != ''">
                <![CDATA[
            AND G.PRICE_UPD_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
                ]]>
                </if>
            </if>
            <if test='dateGbn != null and dateGbn == "R"'>
                <if test="stDate != null and stDate != ''">
            AND G.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
                </if>
                <if test="edDate != null and edDate != ''">
                <![CDATA[
            AND G.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
                ]]>
                </if>
            </if>
            <if test='dateGbn != null and dateGbn == "S"'>
                <if test="stDate != null and stDate != ''">
            AND G.FRST_CFRM_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
                </if>
                <if test="edDate != null and edDate != ''">
                <![CDATA[
            AND G.FRST_CFRM_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
                ]]>
                </if>
            </if>
            <if test="stockQtySt != null and stockQtySt != ''">
            AND ST.CURR_STOCK_QTY >= #{stockQtySt}
            </if>
            <if test="stockQtyEd != null and stockQtyEd != ''">
            <![CDATA[
            AND ST.CURR_STOCK_QTY <= #{stockQtyEd}
            ]]>
            </if>
            <if test="ageGrpCd != null and ageGrpCd != ''">
            AND G.AGE_GRP_CD = #{ageGrpCd}
            </if>
            <if test="cateCd != null and cateCd != ''">
            AND G.GOODS_CD IN (
                SELECT GOODS_CD
                FROM   TB_CATE_GOODS
                WHERE  CATE_CD = #{cateCd}
            )
            </if>
            <if test='roleCd != null and roleCd == "G001_B000" '>
            AND G.SELF_GOODS_YN = 'N'    -- 입점상품
            </if>
            <if test='roleCd != null and roleCd == "G001_E000" '>
            AND G.SELF_GOODS_YN = 'Y'     -- 자사상품
            </if>
  </sql>

  <!-- 목록 페이징 정보 -->
  <sql id="getListPagingCondition_sql">
    <choose>
    <when test="pageable != null">
        ) A
    )Z
    WHERE RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
    ) Q
    <if test='goodsPriceYn != null and goodsPriceYn == "Y"'>
    LEFT OUTER JOIN TB_GOODS_BENEFIT_PRICE GB ON Q.GOODS_CD = GB.GOODS_CD AND GB.CUST_GB = 'G100_00'
    </if>
    </when>
    <otherwise>
        ) A
    )Z
    )Q
    <if test='goodsPriceYn != null and goodsPriceYn == "Y"'>
    LEFT OUTER JOIN TB_GOODS_BENEFIT_PRICE GB ON Q.GOODS_CD = GB.GOODS_CD AND GB.CUST_GB = 'G100_00'
    </if>
    </otherwise>
    </choose>
  </sql>

  <!-- 목록 페이징 정보 LIMIT 용 -->
  <sql id="getListLimitPagingCondition_sql">
    <choose>
    <when test="pageable != null">
        ) A
    )Z
    LIMIT #{pageable.limitStartRow} ,  #{pageable.pageSize}
    ) Q
    <if test='goodsPriceYn != null and goodsPriceYn == "Y"'>
    LEFT OUTER JOIN TB_GOODS_BENEFIT_PRICE GB ON Q.GOODS_CD = GB.GOODS_CD AND GB.CUST_GB = 'G100_00'
    </if>
    </when>
    <otherwise>
        ) A
    )Z
    )Q
    <if test='goodsPriceYn != null and goodsPriceYn == "Y"'>
    LEFT OUTER JOIN TB_GOODS_BENEFIT_PRICE GB ON Q.GOODS_CD = GB.GOODS_CD AND GB.CUST_GB = 'G100_00'
    </if>
    </otherwise>
    </choose>
  </sql>

  <!-- 상품 목록 기본정보 엑셀  -->
  <select id="getGoodsInfoExcelList" parameterType="GoodsSearch" resultType="paramMap">
    /* TssGoods.getGoodsInfoExcelList */
    SELECT G.GOODS_CD
         , B.BRAND_ENM
         , B.BRAND_GRP_NM
         , G.BRAND_CD
         , G.ITEMKIND_CD
         , (SELECT SUPPLY_COMP_NM FROM TB_SUPPLY_COMPANY WHERE SUPPLY_COMP_CD = G.SUPPLY_COMP_CD) AS SUPPLY_COMP_CD
         , G.SUPPLY_GOODS_CD
         , G.GOODS_NUM
         , G.GOODS_NM
         , G.GOODS_TYPE
         , FN_GET_CODE_NM('G008', G.GOODS_STAT) AS GOODS_STAT
         , G.LIST_PRICE
         , G.CURR_PRICE
         , G.DC_RATE
         , G.SELF_GOODS_YN
         , FN_GET_CODE_NM('G009', G.FORMAL_GB) AS FORMAL_GB
         , G.STYLE_YEAR
         , FN_GET_CODE_NM('G006', G.SEASON_CD) AS SEASON_CD
         , G.ERP_STOCK_LINK_YN
         , G.CHANGEABLE_YN
         , G.RETURNABLE_YN
         , G.SELL_FEE_RATE
         , DATE_FORMAT(G.PRICE_UPD_DT, '%Y-%m-%d %H:%i:%S') AS PRICE_UPD_DT
         , DATE_FORMAT(G.FRST_CFRM_DT, '%Y-%m-%d %H:%i:%S') AS FRST_CFRM_DT
         , CASE IFNULL((SELECT COUNT(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' ),0)
                WHEN 0 THEN 'N'
                ELSE 'Y' END AS GOODS_IMAGE_YN
         , IFNULL((SELECT STOCK_QTY FROM TB_GOODS_STOCK WHERE GOODS_CD = G.GOODS_CD),0) AS STOCK_QTY_SUM
         , (SELECT ITEMKIND_NM FROM TB_ITEMKIND WHERE ITEMKIND_CD = G.ITEMKIND_CD) AS ITEMKIND_NM
         , (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
         , G.REG_NO
         , DATE_FORMAT(G.REG_DT, '%Y-%m-%d %H:%i:%S') AS REG_DT
         , G.UPD_NO
         , DATE_FORMAT(G.UPD_DT, '%Y-%m-%d %H:%i:%S') AS UPD_DT
    FROM TB_GOODS G
    INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
    INNER JOIN TB_SUPPLY_COMPANY S ON G.SUPPLY_COMP_CD = S.SUPPLY_COMP_CD
    INNER JOIN TB_GOODS_SUMMARY GS ON G.GOODS_CD = GS.GOODS_CD
    <if test="searchGb != null and searchGb =='EXCEL'">
    INNER JOIN (
                 SELECT SEARCH_CD
                      , TMP_DISP_ORD
                 FROM (
                       SELECT SEARCH_CD
                            , MIN(DISP_ORD) AS TMP_DISP_ORD
                       FROM TB_SEARCH_DATA
                       WHERE REG_NO = #{regNo}
                       GROUP BY SEARCH_CD) T
              ) SD
               ON ( (G.GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%')
                     OR G.SUPPLY_GOODS_CD LIKE CONCAT(SD.SEARCH_CD,'%'))
                    <if test="mdNo != null and mdNo != ''">
                    AND G.BRAND_CD IN (
                                       SELECT DISTINCT BRAND_CD
                                       FROM TB_BRAND_MD
                                       WHERE MD_NO = CAST(#{mdNo} AS UNSIGNED)
                                      )
                   </if>
                  )
    </if>
    WHERE 1=1
    <if test="searchGb == null or searchGb =='BASIC' or searchGb =='EXTEND' or searchGb =='MASTER'" >
    <include refid="getGoodsListCondition_sql"/>
    ORDER BY G.REG_DT DESC, G.GOODS_CD
    </if>
    <if test="searchGb != null and searchGb =='EXCEL'">
    ORDER BY SD.TMP_DISP_ORD, G.GOODS_CD
    </if>
  </select>

  <!-- 상품 정보 -->
  <select id="getGoods" parameterType="Goods" resultType="Goods">
    /* TssGoods.getGoods */
    SELECT G.GOODS_CD
         , G.GOODS_NM
         , G.GOODS_TNM
         , G.GOODS_SNM
         , G.GOODS_SNM1
         , G.BRAND_CD
         , B.BRAND_GRP_NM
         , B.BRAND_NO
         , G.ITEMKIND_CD
         , G.STYLE_YEAR
         , G.SEASON_CD
         , G.SEX_GB
         , G.GOODS_NUM
         , G.MAIN_COLOR_CD
         , G.GOODS_TYPE
         , G.LIST_PRICE
         , G.CURR_PRICE
         , G.CURR_BPRICE
         , DATE_FORMAT(G.PRICE_UPD_DT, '%Y%m%d%H%i%S') AS PRICE_UPD_DT
         , G.COST_PRICE
         , G.DC_RATE
         , G.GOODS_STAT
         , G.SELF_GOODS_YN
         , G.SELF_MALL_YN
         , G.GOODS_GB
         , G.FOREIGN_BUY_YN
         , G.PARALLEL_IMPORT_YN
         , G.ORDER_MADE_YN
         , G.DISTRIBUTION_GB
         , G.SUPPLY_COMP_CD
         , G.SUPPLY_GOODS_CD
         , G.AGE_GRP_CD
         , G.DELV_LOC_CD
         , G.DELV_FEE_CD
         , D.DELV_FEE
         , D.MIN_ORD_AMT
         , G.PNT_PRATE
         , G.PNT_MRATE
         , G.SELL_FEE_RATE
         , G.FORMAL_GB
         , G.RETURNABLE_YN
         , G.CHANGEABLE_YN
         , G.PRE_PPNT_USABLE_YN
         , G.PRE_MPNT_USABLE_YN
         , G.NEW_CUST_ORD_YN
         , DATE_FORMAT(G.NEW_CUST_ORD_STDT, '%Y%m%d%') AS NEW_CUST_ORD_STDT
         , DATE_FORMAT(G.NEW_CUST_ORD_EDDT, '%Y%m%d%') AS NEW_CUST_ORD_EDDT
         , G.MIN_ORD_QTY
         , G.MAX_ORD_QTY
         , G.DAY_MAX_ORD_QTY
         , G.GIFT_PACK_YN
         , G.ERP_STOCK_LINK_YN
         , G.ERP_PRICE_LINK_YN
         , G.NEW_CUST_ORD_YN
         , G.ADULT_YN
         , FN_GET_CODE_NM('G076', G.ORIGIN_CD) AS ORIGIN_NM
         , G.ORIGIN_CD
         , G.MAKE_YMD
         , G.TAX_GB
         , G.GOODS_TYPE
         , FN_GET_CODE_NM('G056', G.GOODS_TYPE) AS GOODS_TYPE_NM
         , DATE_FORMAT(G.FRST_CFRM_DT, '%Y%m%d%H%i%S') AS FRST_CFRM_DT
         , (SELECT I.NI_CLSF_CD FROM TB_ITEMKIND I WHERE I.ITEMKIND_CD = G.ITEMKIND_CD) AS NI_CLSF_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
         , CASE IFNULL((SELECT COUNT(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' ),0)
                WHEN 0 THEN 'N'
                ELSE 'Y' END AS GOODS_IMAGE_YN
         , (SELECT B.CD_NM
           FROM TB_ITEMKIND A
              , TB_COMMON_CODE B
           WHERE A.ITEMKIND_CD = G.ITEMKIND_CD
           AND A.NI_CLSF_CD = B.CD
           AND B.CD_GB = 'G004') AS  NI_CLSF_NM
         , FN_GET_USER_NM(G.UPD_NO) AS UPD_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
    FROM TB_GOODS G
    INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
    LEFT OUTER JOIN TB_DELV_FEE_POLICY D ON G.SUPPLY_COMP_CD = D.SUPPLY_COMP_CD
                                         AND G.DELV_FEE_CD =  D.DELV_FEE_CD
    WHERE 1 = 1
    <choose>
    <when test="goodsCd != null and goodsCd != ''">
    AND G.GOODS_CD = #{goodsCd}
    </when>
    <otherwise>
    AND G.SUPPLY_COMP_CD = #{supplyCompCd}
    AND G.SUPPLY_GOODS_CD = #{supplyGoodsCd}
    </otherwise>
    </choose>
  </select>

  <!-- 상품 상세 정보 조회 -->
  <select id="getGoodsDescList" parameterType="GoodsDesc" resultType="GoodsDesc">
    /* TssGoods.getGoodsDescList */
    SELECT GOODS_CD
         , DESC_GB
         , SEQ
         , GOODS_DESC
    FROM TB_GOODS_DESC
    WHERE GOODS_CD = #{goodsCd}
    AND DESC_GB = #{descGb}
    ORDER BY SEQ
  </select>

  <!-- 상품 고시정보 조회 -->
  <select id="getGoodsNotiInfoList" parameterType="GoodsNotiInfo" resultType="GoodsNotiInfo">
    /* TssGoods.getGoodsNotiInfoList */
    SELECT C.GOODS_CD
         , C.NI_CLSF_CD
         , FN_GET_CODE_NM('G005', C.NI_ITEM_CD) AS NI_ITEM_NM
         , C.NI_ITEM_CD
         , C.NI_CONTENT
         , C.DISP_ORD
    FROM TB_GOODS_NOTI_INFO C
    INNER JOIN TB_GOODS B ON C.GOODS_CD = B.GOODS_CD
    INNER JOIN TB_NOTI_INFO A ON C.NI_CLSF_CD = A.NI_CLSF_CD
                              AND C.NI_ITEM_CD = A.NI_ITEM_CD
                              AND A.SUPPLY_COMP_CD = CASE B.SUPPLY_COMP_CD
                                                          WHEN '1' THEN '1'
                                                          WHEN '2' THEN '2'
                                                          WHEN '3' THEN '3'
                                                          WHEN '4' THEN '4'
                                                          WHEN '5' THEN '5'
                                                          ELSE 'E'
                                                     END
    WHERE C.GOODS_CD = #{goodsCd}
    ORDER BY C.DISP_ORD

  </select>

  <!-- 상품의 정보고시 항목 목록 -->
  <select id="getNotiGoodsInfoList" parameterType="NotiInfo" resultType="NotiInfo">
    /* TssGoods.getNotiGoodsInfoList */
    SELECT  N.SUPPLY_COMP_CD
          , N.NI_CLSF_CD
          , N.NI_ITEM_CD
          , FN_GET_CODE_NM('G005', N.NI_ITEM_CD) AS NI_ITEM_NM
          , CASE WHEN NI_ITEM_CD = 'G005_003' THEN IFNULL(G.COLOR_NM,N.NI_CONTENT)
                 WHEN NI_ITEM_CD = 'G005_004' THEN IFNULL(G.SIZE_NAME,N.NI_CONTENT)
                 WHEN NI_ITEM_CD = 'G005_006' THEN IFNULL(FN_GET_CODE_NM('G076', G.ORIGIN_CD),N.NI_CONTENT)
                 WHEN NI_ITEM_CD = 'G005_097' AND FN_GET_CODE_NM('G076', G.ORIGIN_CD) IN ('대한민국', '한국', 'KOREA') THEN 'N'
                 WHEN NI_ITEM_CD = 'G005_097' AND FN_GET_CODE_NM('G076', G.ORIGIN_CD) NOT IN ('대한민국', '한국', 'KOREA') THEN 'Y'
                 WHEN NI_ITEM_CD = 'G005_009' THEN IFNULL(G.MAKE_YMD, N.NI_CONTENT)
            ELSE N.NI_CONTENT
            END
            AS  NI_CONTENT
          , N.DISP_ORD
     FROM (
         SELECT G.GOODS_CD
             , G.ITEMKIND_CD
             , G.SUPPLY_COMP_CD
             , G.ORIGIN_CD
             , G.MAKE_YMD
             , (SELECT GROUP_CONCAT(DISTINCT T.OPT_CD1) FROM TB_OPTION T WHERE T.GOODS_CD = G.GOODS_CD)  AS COLOR_NM
             , (SELECT GROUP_CONCAT(DISTINCT T.OPT_CD2) FROM TB_OPTION T WHERE T.GOODS_CD = G.GOODS_CD)  AS SIZE_NAME
             , G.BRAND_CD
         FROM TB_GOODS G
         WHERE 1=1
         AND G.GOODS_CD = #{goodsCd}
       ) G
      , TB_NOTI_INFO N
     WHERE 1 = 1
     AND N.NI_CLSF_CD = #{niClsfCd}
     AND N.SUPPLY_COMP_CD =  CASE G.SUPPLY_COMP_CD
                                    WHEN '1' THEN '1'
                                    WHEN '2' THEN '2'
                                    WHEN '3' THEN '3'
                                    WHEN '4' THEN '4'
                                    WHEN '5' THEN '5'
                                    ELSE 'E'
                               END
     ORDER BY  G.GOODS_CD , N.NI_CLSF_CD, N.DISP_ORD
  </select>

  <!-- 상품 기본정보 이력 생성 -->
  <insert id="createGoodsHst" parameterType="Goods">
    /* TssGoods.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 GOODS_CD = #{goodsCd}
  </insert>

  <!-- 상품  수정 항목 일괄변경 -->
  <update id="updateGoodsState" parameterType="Goods">
    /* TssGoods.updateGoodsState */
    UPDATE TB_GOODS SET
          UPD_NO = #{updNo}
        , UPD_DT = NOW()
    <if test="goodsStat != null and goodsStat != ''" >
        , GOODS_STAT = #{goodsStat}
        , FRST_CFRM_DT = IF(#{chGoodsStatYn} = 'Y', COALESCE(FRST_CFRM_DT, NOW()) , FRST_CFRM_DT)
    </if>
    <if test="erpStockLinkYn != null and erpStockLinkYn != ''" >
        , ERP_STOCK_LINK_YN = #{erpStockLinkYn}
    </if>
    <if test="erpPriceLinkYn != null and erpPriceLinkYn != ''" >
        , ERP_PRICE_LINK_YN = #{erpPriceLinkYn}
    </if>
    <if test="formalGb != null and formalGb != ''" >
        , FORMAL_GB = #{formalGb}
        , PNT_PRATE = IFNULL(#{pntPrate}, PNT_PRATE)
        , PNT_MRATE = IFNULL(#{pntMrate}, PNT_MRATE)
    </if>
    <if test='procJob == "pntPrate" and pntPrate != null' >
        , PNT_PRATE = #{pntPrate}
    </if>
    <if test='procJob == "pntMrate"  and pntMrate != null' >
        , PNT_MRATE = #{pntMrate}
    </if>
    <if test="returnableYn != null and returnableYn != ''" >
        , RETURNABLE_YN = #{returnableYn}
    </if>
    <if test="changeableYn != null and changeableYn != ''" >
        , CHANGEABLE_YN = #{changeableYn}
    </if>
    <if test="prePpntUsableYn != null and prePpntUsableYn != ''" >
        , PRE_PPNT_USABLE_YN = #{prePpntUsableYn}
    </if>
    <if test="preMpntUsableYn != null and preMpntUsableYn != ''" >
        , PRE_MPNT_USABLE_YN = #{preMpntUsableYn}
    </if>
    <if test="newCustOrdYn != null and newCustOrdYn != ''" >
        , NEW_CUST_ORD_YN = #{newCustOrdYn}
    </if>
    <if test='procJob == "minOrdAmt"  and minOrdAmt != null and minOrdAmt != ""' >
        , MIN_ORD_AMT = #{minOrdAmt}
    </if>
    <if test='blankFlag != null and blankFlag == "Y"'>
        , GOODS_TNM = ''
    </if>
    <if test="goodsTnm != null and goodsTnm != ''" >
        , GOODS_TNM = #{goodsTnm}
    </if>
    <if test="minOrdQty != null and minOrdQty > 0" >
        , MIN_ORD_QTY = #{minOrdQty}
    </if>
    <if test="maxOrdQty != null and maxOrdQty > 0" >
        , MAX_ORD_QTY = #{maxOrdQty}
    </if>
    <if test="dayMaxOrdQty != null and dayMaxOrdQty > 0" >
        , DAY_MAX_ORD_QTY = #{dayMaxOrdQty}
    </if>
    <if test="giftPackYn != null and giftPackYn != ''" >
        , GIFT_PACK_YN = #{giftPackYn}
    </if>
    <if test="delvFeeCd != null and delvFeeCd != ''" >
        , DELV_FEE_CD = #{delvFeeCd}
    </if>
    <if test="sellStdt != null and sellStdt != ''" >
        , SELL_STDT = STR_TO_DATE(#{sellStdt},'%Y%m%d%H%i%S')
    </if>
    <if test="sellEddt != null and sellEddt != ''" >
        , SELL_EDDT = STR_TO_DATE(#{sellEddt},'%Y%m%d%H%i%S')
    </if>
    WHERE GOODS_CD = #{goodsCd}
  </update>

  <!-- 상품 이미지 필수  항목 입력 여부  조회 -->
  <select id="getGoodsImgsYn" parameterType="Goods" resultType="Goods">
    /* TssGoods.getGoodsImgsYn */
    SELECT A.GOODS_CD
         , A.SUPPLY_COMP_CD
         , (SELECT I.NI_CLSF_CD FROM TB_ITEMKIND I WHERE I.ITEMKIND_CD = A.ITEMKIND_CD) AS NI_CLSF_CD
         , CASE IFNULL((SELECT COUNT(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = A.GOODS_CD AND COLOR_CD = A.MAIN_COLOR_CD AND DEFAULT_IMG_YN = 'Y' ),0)
                WHEN 0 THEN 'N'
                ELSE 'Y' END AS GOODS_IMAGE_YN
    FROM TB_GOODS A
    WHERE 1 = 1
    <choose>
    <when test="goodsCd != null and goodsCd != ''">
    AND A.GOODS_CD  = #{goodsCd}
    </when>
    <otherwise>
    AND A.GOODS_CD IN
        <foreach collection="arrGoodsCd" item="item" index="index"  open="(" close=")" separator=",">
    #{item}
        </foreach>
    </otherwise>
    </choose>
  </select>

  <!-- 상품 정보 이력 -->
  <select id="getGoodsHstList" parameterType="Goods" resultType="GoodsHst">
    /*TssGoods.getGoodsHstList*/
    SELECT CURR_PRICE
         , CURR_BPRICE
         , GOODS_NM
         , GOODS_TNM
         , GOODS_SNM
         , FORMAL_GB
         , GOODS_STAT
         , SELL_FEE_RATE
         , ERP_STOCK_LINK_YN
         , CHANGEABLE_YN
         , PRE_PPNT_USABLE_YN
         , PRE_MPNT_USABLE_YN
         , GOODS_SNM1
         , ITEMKIND_CD
         , SELF_MALL_YN
         , GIFT_PACK_YN
         , DATE_FORMAT(FRST_CFRM_DT, '%Y%m%d%H%i%S') AS FRST_CFRM_DT
         , UPD_NO
         , FN_GET_USER_NM(UPD_NO) AS UPD_NM
         , DATE_FORMAT(UPD_DT, '%Y%m%d%H%i%S') AS UPD_DT
         , REG_NO
         , FN_GET_USER_NM(REG_NO) AS REG_NM
         , DATE_FORMAT(REG_DT, '%Y%m%d%H%i%S') AS REG_DT
    FROM TB_GOODS_HST
    WHERE GOODS_CD = #{goodsCd}
    ORDER BY REG_DT DESC
  </select>

  <!-- 상품 품목 변경 -->
  <update id="updateGoodItemKindCd" parameterType="Goods">
    /* TssGoods.updateGoodItemKindCd */
    UPDATE TB_GOODS
    SET ITEMKIND_CD = #{itemkindCd}
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE GOODS_CD = #{goodsCd}
  </update >

  <!-- 상품 자동 검색어 조회-->
  <select id="getGoodsSnm" parameterType="String" resultType="String">
    /* TssGoods.getGoodsSnm */
    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_CD = #{goodsCd}
            ) Z
            GROUP BY GOODS_CD
    )
    SELECT 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
    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_CD = #{goodsCd}
  </select>

  <!-- 상품검색어 수정 -->
  <update id="updateGoodsSnm" parameterType="Goods">
    /* TssGoods.updateGoodsSnm */
    UPDATE TB_GOODS A
    SET UPD_NO = #{updNo}
      , UPD_DT = NOW()
      , GOODS_SNM = #{goodsSnm}
    WHERE GOODS_CD = #{goodsCd}
  </update>

  <!-- 상품 품목변경 고시정보 조회  -->
  <select id="getNewNotiInfo" parameterType="Goods" resultType="GoodsNotiInfo">
    /* TssGoods.getNewNotiInfo */
    SELECT D.NI_CLSF_CD
         , IFNULL(C.NI_ITEM_CD, D.NI_ITEM_CD) AS NI_ITEM_CD
         , IFNULL(C.NI_CONTENT, D.NI_CONTENT) AS NI_CONTENT
         , D.DISP_ORD
    FROM (
           SELECT A.NI_CLSF_CD
              , A.NI_ITEM_CD
              , A.NI_CONTENT
              , A.DISP_ORD
           FROM TB_NOTI_INFO A
              , TB_ITEMKIND B
           WHERE A.NI_CLSF_CD = B.NI_CLSF_CD
           AND A.SUPPLY_COMP_CD = #{supplyCompCd}
           AND B.ITEMKIND_CD = #{itemkindCd}
         ) D
        LEFT OUTER JOIN (
           SELECT NI_ITEM_CD
                , NI_CONTENT
           FROM TB_GOODS_NOTI_INFO
           WHERE GOODS_CD = #{goodsCd}
         ) C
         ON (
           C.NI_ITEM_CD = D.NI_ITEM_CD
         )
    ORDER BY D.DISP_ORD
  </select>

  <!-- 상품 품목변경 고시정보 삭제  -->
  <delete id="deleteGoodsNotiInfo" parameterType="Goods">
    /* TssGoods.deleteGoodsNotiInfo */
    DELETE FROM TB_GOODS_NOTI_INFO
    WHERE GOODS_CD = #{goodsCd}
  </delete>

  <!-- 상품 고시 정보  저장 -->
  <update id="saveGoodsNotiInfo" parameterType="GoodsNotiInfo">
    /* TssGoods.saveGoodsNotiInfo */
    INSERT INTO TB_GOODS_NOTI_INFO(
           GOODS_CD
         , NI_CLSF_CD
         , NI_ITEM_CD
         , NI_CONTENT
         , DISP_ORD
         , REG_NO
         , REG_DT
         , UPD_NO
         , UPD_DT
    )
    VALUES (
           #{goodsCd}
         , #{niClsfCd}
         , #{niItemCd}
         , #{niContent}
         , #{dispOrd}
         , #{regNo}
         , NOW()
         , #{updNo}
         , NOW()
    )
    ON DUPLICATE KEY UPDATE
           NI_CONTENT = #{niContent}
         , UPD_NO = #{updNo}
         , UPD_DT = NOW()
  </update>

  <!-- 상품 사이즈 조회 -->
  <select id="getGoodsSizeList" parameterType="Goods" resultType="Option">
    /* TssGoods.getGoodsSizeList */
    WITH TMP_GOODS AS (
        SELECT A.GOODS_CD
             , A.GOODS_TYPE
             , IFNULL(B.COMPS_GOODS_CD, A.GOODS_CD) AS COMPS_GOODS_CD
             , IFNULL(B.DISP_ORD,1) AS DISP_ORD
             , A.SELF_GOODS_YN
        FROM TB_GOODS A
        LEFT OUTER JOIN TB_GOODS_COMPOSE B ON A.GOODS_CD = B.GOODS_CD
        WHERE A.GOODS_CD = #{goodsCd}
        )
    SELECT *
    FROM (
    SELECT #{goodsCd} AS GOODS_CD
         , A.GOODS_CD AS COMPS_GOODS_CD
         , A.OPT_CD
         , A.OPT_CD1
         , A.OPT_CD2
         , A.ADD_PRICE
         , A.BASE_STOCK_QTY
         , A.CURR_STOCK_QTY
         , A.STORE_STOCK_QTY
         , IFNULL(B.STOCK_QTY,0) AS SALE_STOCK_QTY  /*출고대기*/
         , GREATEST(GREATEST((GREATEST(A.CURR_STOCK_QTY,0) + GREATEST(A.STORE_STOCK_QTY,0)) - A.BASE_STOCK_QTY, 0) - IFNULL(B.STOCK_QTY, 0), 0) AS ABLE_STOCK_QTY  /*판매가능재고*/
         , A.SOLDOUT_YN
         , A.DISP_ORD
         , A.DISP_YN
         , C.DISP_ORD AS GOODS_ORD
         , COUNT(*) OVER(PARTITION BY A.GOODS_CD ) AS RCOUNT
         , ROW_NUMBER() OVER(PARTITION BY A.GOODS_CD ORDER BY C.DISP_ORD, A.DISP_ORD, A.OPT_CD1, A.OPT_CD2) AS RNUM
    FROM  TMP_GOODS C
    INNER JOIN TB_OPTION A ON C.COMPS_GOODS_CD = A.GOODS_CD
    LEFT OUTER JOIN (
                     SELECT Z.GOODS_CD
                          , Z.OPT_CD
                          , SUM((CASE Z.SELL_GB WHEN '10' THEN 1
                                                WHEN '20' THEN 1
                                                ELSE -1 END) * Z.SELL_QTY) AS STOCK_QTY
                     FROM TB_SELL_QTY Z
                     INNER JOIN TMP_GOODS Y ON Z.GOODS_CD = Y.COMPS_GOODS_CD
                     GROUP BY Z.GOODS_CD, Z.OPT_CD
                    ) B
               ON (  A.GOODS_CD = B.GOODS_CD
                 AND A.OPT_CD = B.OPT_CD)
    WHERE 1 = 1
    AND C.GOODS_CD = #{goodsCd}
    ) Z
    ORDER BY GOODS_ORD, RNUM
  </select>

  <!-- 상품 색상목록 조회 -->
  <select id="getGoodsColorList" parameterType="Goods" resultType="GoodsImg">
    /* TssGoods.getGoodsColorList */
    SELECT A.GOODS_CD
         , IFNULL(A.OPT_CD1, A.MAIN_COLOR_CD) AS OPT_CD1
         , A.MAIN_COLOR_CD
         , IF(A.MAIN_COLOR_CD = IFNULL(A.OPT_CD1, A.MAIN_COLOR_CD), 'Y','N') AS  MAIN_COLOR_YN
         , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = A.GOODS_CD AND COLOR_CD = A.OPT_CD1 AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM
         , C.COLOR_ENM
    FROM (
          SELECT A.GOODS_CD
               , A.MAIN_COLOR_CD
               , IFNULL(B.OPT_CD1,'XX') AS OPT_CD1
          FROM TB_GOODS A
          LEFT OUTER JOIN TB_OPTION B ON A.GOODS_CD = B.GOODS_CD
                                      AND 1 = (CASE WHEN A.SELF_GOODS_YN = 'Y' THEN 1
                                                    ELSE 2 END
                                              )
          WHERE A.GOODS_CD  = #{goodsCd}
          GROUP BY A.GOODS_CD , B.OPT_CD1
         ) A
    LEFT OUTER JOIN TB_COLOR C ON A.OPT_CD1 = C.COLOR_CD
  </select>

  <!-- 상품 기본 정보 등록 -->
  <insert id="createGoods" parameterType="Goods">
    /* TssGoods.createGoods */
    INSERT INTO TB_GOODS (
        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
      , COST_PRICE
      , DC_RATE
      , GOODS_STAT
      , SELL_STDT
      , SELL_EDDT
      , SELF_MALL_YN
      , GOODS_GB
      , FOREIGN_BUY_YN
      , ORDER_MADE_YN
      , PARALLEL_IMPORT_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
      , MIN_ORD_QTY
      , MAX_ORD_QTY
      , DAY_MAX_ORD_QTY
      , GIFT_PACK_YN
      , ORIGIN_CD
      , MAKE_YMD
      , TAX_GB
      , ERP_STOCK_LINK_YN
      , ADULT_YN
      , TOBE_FORM_YN
      , REG_NO
      , REG_DT
      , UPD_NO
      , UPD_DT
    )
    VALUES(
        #{goodsCd}
      , #{brandCd}
      , #{itemkindCd}
      , IFNULL(#{goodsNm},'상품명없음')
      , #{goodsTnm}
      , #{goodsSnm}
      , #{goodsSnm1}
      , IFNULL(#{mainColorCd},'XX')
      , #{styleYear}
      , #{seasonCd}
      , #{sexGb}
      , #{goodsNum}
      , IFNULL(#{goodsType},'G056_N')
      , #{listPrice}
      , #{currPrice}
      , #{costPrice}
      , #{dcRate}
      , #{goodsStat}
      , STR_TO_DATE(#{sellStdt},'%Y%m%d%H%i%S')
      , STR_TO_DATE(#{sellEddt},'%Y%m%d%H%i%S')
      , IFNULL(#{selfMallYn},'Y')
      , #{goodsGb}
      , IFNULL(#{foreignBuyYn},'N')
      , IFNULL(#{orderMadeYn},'N')
      , IFNULL(#{parallelImportYn},'N')
      , #{distributionGb}
      , #{selfGoodsYn}
      , #{supplyCompCd}
      , #{supplyGoodsCd}
      , #{ageGrpCd}
      , #{delvLocCd}
      , #{delvFeeCd}
      , #{pntPrate}
      , #{pntMrate}
      , #{sellFeeRate}
      , #{formalGb}
      , IFNULL(#{changeableYn},'Y')
      , IFNULL(#{returnableYn},'Y')
      , CASE WHEN NULLIF(#{minOrdQty},'') IS NULL THEN 1
             WHEN #{minOrdQty} = 0 THEN 1
             ELSE #{minOrdQty} END
      , CASE WHEN NULLIF(#{maxOrdQty},'') IS NULL THEN 20
             WHEN #{maxOrdQty} = 0 THEN 20
             ELSE #{maxOrdQty} END
      , CASE WHEN NULLIF(#{dayMaxOrdQty},'') IS NULL THEN 20
             WHEN #{dayMaxOrdQty} = 0 THEN 20
             ELSE #{dayMaxOrdQty} END
      , IFNULL(#{giftPackYn},'N')
      , #{originCd}
      , #{makeYmd}
      , #{taxGb}
      , IFNULL(#{erpStockLinkYn},'N')
      , IFNULL(#{adultYn},'N')
      , IFNULL(#{tobeFormYn},'N')
      , #{regNo}
      , NOW()
      , #{updNo}
      , NOW()
    )
  </insert>

  <!-- 상품 기본 정보 등록 -->
  <insert id="createGoodsSmmary" parameterType="Goods">
    /* TssGoods.createGoodsSmmary */
    INSERT INTO TB_GOODS_SUMMARY (
        GOODS_CD
      , REG_NO
      , REG_DT
      , UPD_NO
      , UPD_DT
    )
    VALUES(
        #{goodsCd}
      , #{regNo}
      , NOW()
      , #{updNo}
      , NOW()
    )
  </insert>

  <!-- 상품 기본 정보 수정 -->
  <update id="updateGoods" parameterType="Goods">
    /* TssGoods.updateGoods */
    UPDATE TB_GOODS
    SET GOODS_TNM = #{goodsTnm}
      , GOODS_NM = #{goodsNm}
      , SEASON_CD = #{seasonCd}
      , SEX_GB = #{sexGb}
      , LIST_PRICE = #{listPrice}
      , CURR_PRICE = #{currPrice}
      <if test="currBprice != null and currBprice != ''">
      , CURR_BPRICE = #{currBprice}
      , PRICE_UPD_DT = NOW()
      , DC_RATE = #{dcRate}
      </if>
      , COST_PRICE = #{costPrice}
      <if test="goodsStat != null and goodsStat != 'G008_90'">
      , GOODS_STAT = #{goodsStat}
      </if>
      <if test="goodsStat != null and goodsStat == 'G008_90' and frstCfrmDt != null and frstCfrmDt != ''">
      , GOODS_STAT = #{goodsStat}
      </if>
      , GOODS_GB = #{goodsGb}
      , FOREIGN_BUY_YN = #{foreignBuyYn}
      , PARALLEL_IMPORT_YN = #{parallelImportYn}
      , ORDER_MADE_YN = #{orderMadeYn}
      , DELV_LOC_CD = #{delvLocCd}
      , DELV_FEE_CD = #{delvFeeCd}
      , PNT_PRATE = #{pntPrate}
      , PNT_MRATE = #{pntMrate}
      , FORMAL_GB = #{formalGb}
      , MIN_ORD_QTY = #{minOrdQty}
      , MAX_ORD_QTY = #{maxOrdQty}
      , DAY_MAX_ORD_QTY = #{dayMaxOrdQty}
      , ERP_STOCK_LINK_YN = #{erpStockLinkYn}
      , GOODS_SNM1 = #{goodsSnm1}
      , SELF_MALL_YN = #{selfMallYn}
      , GOODS_SNM = #{goodsSnm}
      , FRST_CFRM_DT = (CASE #{chGoodsStatYn} WHEN 'Y' THEN COALESCE(FRST_CFRM_DT, NOW(), FRST_CFRM_DT)
                                              ELSE FRST_CFRM_DT END)
      , PRE_PPNT_USABLE_YN = #{prePpntUsableYn}
      , PRE_MPNT_USABLE_YN = #{preMpntUsableYn}
      , DISTRIBUTION_GB = #{distributionGb}
      , RETURNABLE_YN = #{returnableYn}
      , CHANGEABLE_YN = #{changeableYn}
      , NEW_CUST_ORD_YN = #{newCustOrdYn}
      <choose>
      <when test="newCustOrdStdt != null and newCustOrdStdt != ''">
      , NEW_CUST_ORD_STDT = STR_TO_DATE(#{newCustOrdStdt},'%Y%m%d%H%i%S')
      , NEW_CUST_ORD_EDDT = STR_TO_DATE(#{newCustOrdEddt},'%Y%m%d%H%i%S')
      </when>
      <otherwise>
      , NEW_CUST_ORD_STDT = null
      , NEW_CUST_ORD_EDDT = null
      </otherwise>
      </choose>
      , AGE_GRP_CD = #{ageGrpCd}
      , SELL_FEE_RATE = #{sellFeeRate}
      , GIFT_PACK_YN = #{giftPackYn}
      , MAIN_COLOR_CD = #{mainColorCd}
      , NEW_CUST_ORD_YN = #{newCustOrdYn}
      , ADULT_YN = #{adultYn}
      , SELL_STDT = STR_TO_DATE(#{sellStdt},'%Y%m%d%H%i%S')
      , SELL_EDDT = STR_TO_DATE(#{sellEddt},'%Y%m%d%H%i%S')
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE GOODS_CD = #{goodsCd}
  </update>

  <!-- 상품 상세 정보 이력 생성 -->
  <insert id="createGoodsDetailDescHst" parameterType="GoodsDesc">
    /* TssGoods.createGoodsDetailDescHst */
    INSERT INTO TB_GOODS_DESC_HST (
        GOODS_CD
      , DESC_GB
      , SEQ
      , GOODS_DESC
      , REG_NO
      , REG_DT
    )
    SELECT GOODS_CD
         , DESC_GB
         , SEQ
         , GOODS_DESC
         , #{regNo}
         , NOW()
    FROM TB_GOODS_DESC
    WHERE GOODS_CD = #{goodsCd}
    ORDER BY DESC_GB, DESC_GB
  </insert>

  <!-- 상품 상세정보 삭제 -->
  <delete id="deleteGoodsDesc" parameterType="GoodsDesc">
    /* TssGoods.deleteGoodsDesc */
    DELETE FROM TB_GOODS_DESC
    WHERE GOODS_CD = #{goodsCd}
    AND DESC_GB = #{descGb}
  </delete>

  <!-- 상품 상세정보 등록 -->
  <insert id="createGoodsDesc" parameterType="GoodsDesc">
    /* TssGoods.createGoodsDesc */
    INSERT INTO TB_GOODS_DESC (
        GOODS_CD
      , DESC_GB
      , SEQ
      , GOODS_DESC
      , REG_NO
      , REG_DT
    )
    VALUES(
        #{goodsCd}
      , #{descGb}
      , #{seq}
      , REPLACE(#{goodsDesc},char(13), CONCAT(char(13),char(10)))
      , #{regNo}
      , NOW()
    )
  </insert>

  <!-- 상품 옵션/재고 정보 이력생성 -->
  <insert id="createStockHst" parameterType="Option">
  /* TssGoods.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}
    <if test='optCd != null and optCd != ""'>
    AND OPT_CD = #{optCd}
    </if>
  </insert>

  <!-- 상품 옵션/재고 정보 저장 -->
  <update id="saveStock" parameterType="Option">
    /* TssGoods.saveStock */
    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
    )
    VALUES (
           #{goodsCd}
         , #{optCd}
         , #{optCd1}
         , #{optCd2}
         , #{skuModelNo}
         , #{productNo}
         , #{productCode}
         , #{baseStockQty}
         , #{currStockQty}
         , #{storeStockQty}
         , GREATEST(#{currStockQty},#{storeStockQty})
         , #{addPrice}
         , #{soldoutYn}
         , #{dispOrd}
         , #{dispYn}
         , #{regNo}
         , NOW()
         , #{updNo}
         , NOW()
    )
    ON DUPLICATE KEY UPDATE
            BASE_STOCK_QTY = #{baseStockQty}
          , ADD_PRICE = #{addPrice}
          , SOLDOUT_YN = #{soldoutYn}
          , DISP_ORD = #{dispOrd}
          , DISP_YN = #{dispYn}
          , UPD_NO = #{updNo}
          , UPD_DT = NOW()
          <if test='selfGoodsYn != null and selfGoodsYn == "N"'>
          , CURR_STOCK_QTY = #{currStockQty}
          </if>
  </update>

  <!-- 상품코드 생성 -->
  <insert id="createGoodsSequence" parameterType="Goods" keyProperty="goodsSq">
    /* TssGoods.createGoodsSequence */
    INSERT INTO TB_GOODS_SEQUENCE (GOODS_SQ) VALUES (NULL)
  </insert>

  <!-- 상품 옵션 존재여부 확인 -->
  <select id="getGoodsOptionCount" parameterType="Option" resultType="int">
    /* TssGoods.getGoodsOptionCount */
    SELECT COUNT(*)
    FROM TB_OPTION
    WHERE GOODS_CD = #{goodsCd}
    <if test='optCd != null and optCd != ""'>
    AND OPT_CD = #{optCd}
    </if>
  </select>

  <!-- 상품 가격 승인 목록 건수 -->
  <select id="getGoodsSupplyPriceCount"  parameterType="GoodsSupplyPrice" resultType="int">
    /* TssGoods.getGoodsSupplyPriceCount */
    SELECT COUNT(A.GOODS_SUPPLY_PRICE_SQ)
    FROM TB_GOODS_SUPPLY_PRICE A
    INNER JOIN TB_GOODS G ON A.GOODS_CD = G.GOODS_CD
                          AND G.SELF_GOODS_YN = 'N'
    INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
    INNER JOIN TB_SUPPLY_COMPANY S ON G.SUPPLY_COMP_CD = S.SUPPLY_COMP_CD
    WHERE 1 = 1
    <if test="goodsCd != null and goodsCd != ''">
    AND G.GOODS_CD = CONCAT(UPPER(#{goodsCd}),'%')
    </if>
    <if test="supplyCompCd != null and supplyCompCd != ''">
    AND G.SUPPLY_COMP_CD = #{supplyCompCd}
    </if>
    <if test="brandCd != null and brandCd != ''">
    AND G.BRAND_CD = #{brandCd}
    </if>
    <if test="cfrmYn != null and cfrmYn != ''">
    AND A.CFRM_YN = #{cfrmYn}
    </if>
    <if test='confirmY != null and confirmY == "N"'>
        <if test='dateGbn != null and dateGbn == "R"'>
            <if test="stDate != null and stDate != ''">
    AND A.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="edDate != null and edDate != ''">
            <![CDATA[
    AND A.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
            ]]>
            </if>
        </if>
        <if test='dateGbn != null and dateGbn == "P"'>
            <if test="stDate != null and stDate != ''">
    AND A.CFRM_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="edDate != null and edDate != ''">
            <![CDATA[
    AND A.CFRM_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
            ]]>
            </if>
        </if>
    </if>
  </select>

  <!-- 상품 가격 승인 목록 -->
  <select id="getGoodsSupplyPriceList"  parameterType="GoodsSupplyPrice" resultType="GoodsSupplyPrice">
    /* TssGoods.getGoodsSupplyPriceList */
    SELECT Q.*
    FROM (
    SELECT Z.*
    FROM (
        SELECT A.*
        FROM (
            SELECT X.GOODS_SUPPLY_PRICE_SQ
                 , X.GOODS_CD
                 , X.SUPPLY_GOODS_PRICE
                 , X.SUPPLY_SELL_FEE_RATE
                 , X.CFRM_YN
                 , X.CFRM_NM
                 , X.CFRM_NO
                 , X.CFRM_DT
                 , X.REG_NM
                 , X.REG_DT
                 , X.UPD_NM
                 , X.UPD_DT
                 , X.LIST_PRICE
                 , X.CURR_PRICE
                 , X.GOODS_STAT
                 , X.GOODS_SELL_FEE_RATE
                 , X.SUPPLY_COMP_CD
                 , X.SUPPLY_COMP_NM
                 , X.BRAND_CD
                , X.BRAND_ENM
            FROM (
                SELECT A.GOODS_SUPPLY_PRICE_SQ
                     , A.GOODS_CD
                     , A.SUPPLY_GOODS_PRICE
                     , A.SUPPLY_SELL_FEE_RATE
                     , A.CFRM_YN
                     , FN_GET_USER_NM(A.CFRM_NO) AS CFRM_NM
                     , A.CFRM_NO
                     , DATE_FORMAT(A.CFRM_DT,'%Y%m%d%H%i%S') AS CFRM_DT
                     , FN_GET_USER_NM(A.REG_NO) AS REG_NM
                     , DATE_FORMAT(A.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
                     , FN_GET_USER_NM(A.UPD_NO) AS UPD_NM
                     , DATE_FORMAT(A.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
                     , G.LIST_PRICE
                     , G.CURR_PRICE
                     , G.SELL_FEE_RATE AS GOODS_SELL_FEE_RATE
                     , ROW_NUMBER() OVER(PARTITION BY A.GOODS_CD ORDER BY A.REG_DT DESC) AS RNUM
                     , G.GOODS_STAT
                     , G.BRAND_CD
                     , B.BRAND_ENM
                     , G.SUPPLY_COMP_CD
                     , S.SUPPLY_COMP_NM
                FROM TB_GOODS_SUPPLY_PRICE A
                INNER JOIN TB_GOODS G ON A.GOODS_CD = G.GOODS_CD
                                      AND G.SELF_GOODS_YN = 'N'
                INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
                INNER JOIN TB_SUPPLY_COMPANY S ON G.SUPPLY_COMP_CD = S.SUPPLY_COMP_CD
                WHERE 1 = 1
                <if test="goodsCd != null and goodsCd != ''">
                AND G.GOODS_CD = UPPER(#{goodsCd})
                </if>
                <if test="supplyCompCd != null and supplyCompCd != ''">
                AND G.SUPPLY_COMP_CD = #{supplyCompCd}
                </if>
                <if test="brandCd != null and brandCd != ''">
                AND G.BRAND_CD = #{brandCd}
                </if>
                <if test="cfrmYn != null and cfrmYn != ''">
                AND A.CFRM_YN = #{cfrmYn}
                </if>
                <if test='confirmY != null and confirmY == "N"'>
                    <if test='dateGbn != null and dateGbn == "R"'>
                        <if test="stDate != null and stDate != ''">
                AND A.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
                        </if>
                        <if test="edDate != null and edDate != ''">
                        <![CDATA[
                AND A.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
                        ]]>
                        </if>
                    </if>
                    <if test='dateGbn != null and dateGbn == "P"'>
                        <if test="stDate != null and stDate != ''">
                AND A.CFRM_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
                        </if>
                        <if test="edDate != null and edDate != ''">
                        <![CDATA[
                AND A.CFRM_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
                        ]]>
                        </if>
                    </if>
                </if>
    ) X
            WHERE 1 = 1
            <if test='confirmY != null and confirmY == "Y"'>
            AND RNUM = 1
            AND CFRM_DT IS NULL
            </if>
            ORDER BY X.REG_DT DESC, X.GOODS_CD
    <include refid="getListLimitPagingCondition_sql"/>
  </select>

  <!-- 상품상세 공지사항 목록 -->
  <select id="getNoticeList" parameterType="GoodsSearch" resultType="Notice">
    /* TssGoods.getNoticeList */
    SELECT A.NOTICE_SQ
         , A.NOTICE_TYPE
         , A.NOTICE_TITLE
         , A.NOTICE_CONTENT
         , DATE_FORMAT(A.NOTICE_STDT,'%Y-%m-%d')  AS NOTICE_STDT
         , DATE_FORMAT(A.NOTICE_EDDT,'%Y-%m-%d')  AS NOTICE_EDDT
         , A.USE_YN
         , FN_GET_USER_NM(A.REG_NO)             AS REG_NM
         , DATE_FORMAT(A.REG_DT,'%Y-%m-%d %H:%i:%S') AS REG_DT
         , FN_GET_USER_NM(A.UPD_NO)             AS UPD_NM
         , DATE_FORMAT(A.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
    FROM   TB_NOTICE A
    WHERE  A.NOTICE_TYPE = 'G047_30'
    <if test="stDate != null and stDate !=''">
    AND A.NOTICE_EDDT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d')
    </if>
    <if test="edDate != null and edDate !=''">
    <![CDATA[
    AND A.NOTICE_STDT <= DATE_FORMAT(#{edDate}, '%Y-%m-%d')
    ]]>
    </if>
    <if test='useYn != null and useYn !=""'>
    AND A.USE_YN = #{useYn}
    </if>
    <if test="noticeTitle != null and noticeTitle !=''">
    AND LOWER(A.NOTICE_TITLE) LIKE CONCAT(LOWER(#{noticeTitle}),'%')
    </if>
    <if test='conditionList != null and conditionList.length>0'>
    AND A.NOTICE_SQ IN (
                        SELECT NOTICE_SQ
                        FROM TB_NOTICE_GOODS
                        WHERE GOODS_CD IN (
                                           SELECT GOODS_CD
                                           FROM TB_GOODS G
                                           WHERE 1 = 1
                                           AND (
        <choose>
          <when test='search != null and search == "searchGoodsCd"'>
              <foreach collection="conditionList" item="item" index="index" separator="or">
           G.GOODS_CD LIKE CONCAT(UPPER(#{item}),'%')
              </foreach>
          </when>
          <when test='search != null and search == "searchGoodsNm"'>
              <foreach collection="conditionList" item="item" index="index" separator="or">
           UPPER(G.GOODS_NM) LIKE CONCAT(UPPER(#{item}),'%')
              </foreach>
          </when>
          <when test='search != null and search == "searchGoodsNum"'>
              <foreach collection="conditionList" item="item" index="index" separator="or">
           UPPER(G.GOODS_NUM) LIKE CONCAT(UPPER(#{item}),'%')
              </foreach>
          </when>
          <when test='search != null and search == "searchSupplyGoodsCd"'>
              <foreach collection="conditionList" item="item" index="index" separator="or">
           UPPER(G.SUPPLY_GOODS_CD) LIKE CONCAT(UPPER(#{item}),'%')
              </foreach>
          </when>
        </choose>
                                                )
                                          )
                       )
    </if>
    <if test="supplyCompCd != null and supplyCompCd  != ''">
    AND A.NOTICE_SQ IN (
                        SELECT NOTICE_SQ
                        FROM TB_NOTICE_GOODS
                        WHERE GOODS_CD IN (
                                           SELECT GOODS_CD
                                           FROM TB_GOODS
                                           WHERE SUPPLY_COMP_CD = #{supplyCompCd}
                                           <if test="brandCd != null and brandCd  != ''">
                                           AND BRAND_CD = #{brandCd}
                                           </if>
                                          )
                       )
    </if>
    ORDER  BY A.NOTICE_SQ DESC
  </select>

  <!-- 상품상세 공지사항 상품 목록 -->
  <select id="getNoticeGoodsList" parameterType="Notice" resultType="NoticeGoods">
    /* TssGoods.getNoticeGoodsList */
    SELECT A.NOTICE_SQ
         , A.GOODS_CD
         , B.GOODS_NM
    FROM TB_NOTICE_GOODS A
    INNER JOIN TB_GOODS B ON A.GOODS_CD = B.GOODS_CD
    WHERE NOTICE_SQ = #{noticeSq}
    ORDER BY A.GOODS_CD
  </select>

  <!-- 상품상세 공지사항 상품저장 -->
  <insert id="saveNoticeGoods" parameterType="NoticeGoods">
    /* TssGoods.saveNoticeGoods */
    INSERT INTO TB_NOTICE_GOODS (
           NOTICE_SQ
         , GOODS_CD
         , REG_NO
         , REG_DT
         , UPD_NO
         , UPD_DT
    )
    VALUES (
           #{noticeSq}
         , #{goodsCd}
         , #{regNo}
         , NOW()
         , #{updNo}
         , NOW()
    )
    ON DUPLICATE KEY UPDATE
           GOODS_CD = #{goodsCd}
  </insert>

  <!-- 상품상세 공지사항 상품 삭제 -->
  <delete id="deleteNoticeGoods" parameterType="NoticeGoods">
    /* TssGoods.deleteNoticeGoods */
    DELETE FROM TB_NOTICE_GOODS
    WHERE NOTICE_SQ =  #{noticeSq}
    AND GOODS_CD = #{goodsCd}
  </delete>

  <!-- 입점 상품 가격  -->
  <insert id="createGoodSupplyPrice" parameterType="GoodsSupplyPrice">
    /* TssGoods.createGoodSupplyPrice */
    INSERT INTO TB_GOODS_SUPPLY_PRICE (
      GOODS_SUPPLY_PRICE_SQ
    , GOODS_CD
    , SUPPLY_GOODS_PRICE
    , SUPPLY_SELL_FEE_RATE
    , CFRM_YN
    , CFRM_NO
    , CFRM_DT
    , REG_NO
    , REG_DT
    , UPD_NO
    , UPD_DT
    )
    VALUES(
      NULL
    , #{goodsCd}
    , #{supplyGoodsPrice}
    , #{supplySellFeeRate}
    , IFNULL(#{cfrmYn},'N')
    , #{cfrmNo}
    , #{cfrmDt}
    , #{regNo}
    , NOW()
    , #{updNo}
    , NOW()
    )
  </insert>

  <!-- 상품 구매등급 목록 -->
  <select id="getGoodsDetailOrderGradeList" parameterType="Goods" resultType="Goods">
    /* TssGoods.getGoodsDetailOrderGradeList */
    SELECT  G.GOODS_CD
          , A.CUST_GRADE
          , FN_GET_CODE_NM('G101', A.CUST_GRADE) AS CUST_GRADE_NAME
     FROM TB_GOODS_ORDER_GRADE A
     INNER JOIN TB_GOODS G ON A.GOODS_CD = G.GOODS_CD
     WHERE A.GOODS_CD = #{goodsCd}
     ORDER BY G.GOODS_CD, A.CUST_GRADE
  </select>

  <!-- 상품 구매등급 삭제 -->
  <delete id="deleteGoodsCustGrade" parameterType="Goods" >
    /* TssGoods.saveGoodsNaverLowestPrice */
    DELETE FROM TB_GOODS_ORDER_GRADE
    WHERE GOODS_CD = #{goodsCd}
  </delete>

  <!-- 상품 구매등급 저장 -->
  <insert id="createGoodsCustGrade"  parameterType="Goods" >
    /* TssGoods.createGoodsCustGrade */
    INSERT INTO TB_GOODS_ORDER_GRADE (
        GOODS_CD
      , CUST_GRADE
      , REG_NO
      , REG_DT
    )
    VALUES(
        #{goodsCd}
      , #{custGrade}
      , #{regNo}
      , NOW()
    )
  </insert>

  <!-- 상품이미지 목록 -->
  <select id="getGoodsImageList" parameterType="GoodsImg" resultType="GoodsImg">
    /* TssGoods.getGoodsImageList */
    SELECT GOODS_CD
         , COLOR_CD
         , DISP_ORD
         , SUBSTRING_INDEX(SYS_IMG_NM , '/', -1) AS SYS_IMG_NM  /* S0001/20201221/14505664_L.jpg */
         , SUBSTRING_INDEX(SYS_IMG_NM , '/', 2) AS SYS_IMG_URL
         , DEFAULT_IMG_YN
         , MOUSEOVER_IMG_YN
         , EXTMALL_IMG_YN
    FROM   TB_GOODS_IMG
    WHERE  GOODS_CD = #{goodsCd}
    AND    COLOR_CD = #{colorCd}
    ORDER  BY DISP_ORD
  </select>

  <!-- 상품이미지 전체 삭제 -->
  <delete id="deleteGoodsImageTotal" parameterType="GoodsImg">
    /* TssGoods.deleteGoodsImageTotal */
    DELETE
    FROM TB_GOODS_IMG
    WHERE GOODS_CD = #{goodsCd}
    AND COLOR_CD = #{colorCd}
  </delete>

  <!-- 상품이미지 삭제 -->
  <delete id="deleteGoodsImage" parameterType="GoodsImg">
    /* TssGoods.deleteGoodsImage */
    DELETE
    FROM TB_GOODS_IMG
    WHERE GOODS_CD = #{goodsCd}
    AND COLOR_CD = #{colorCd}
    AND DISP_ORD = #{dispOrd}
  </delete>

  <!-- 상품이미지 등록 -->
  <insert id="createGoodsImage" parameterType="GoodsImg">
    /* TssGoods.createGoodsImage */
    INSERT INTO TB_GOODS_IMG (
           GOODS_CD
         , COLOR_CD
         , DISP_ORD
         , SYS_IMG_NM
         , DEFAULT_IMG_YN
         , MOUSEOVER_IMG_YN
         , EXTMALL_IMG_YN
         , REG_NO
         , REG_DT
    )
    VALUES (
           #{goodsCd}
         , #{colorCd}
         , #{dispOrd}
         , #{sysImgNm}
         , #{defaultImgYn}
         , #{mouseoverImgYn}
         , #{extmallImgYn}
         , #{regNo}
         , NOW()
    )
  </insert>

  <!-- 상품이미지 네이밍순 조회  -->
  <select id="getGoodsImageNewDispOrdList" parameterType="GoodsImg" resultType="GoodsImg">
    /* AdmGoods.getGoodsImageNewDispOrdList */
    WITH TAB_GOODS_IMG AS (
    SELECT GOODS_IMG_SQ
          ,GOODS_CD
          ,COLOR_CD
          ,SYS_IMG_NM
          ,RNUM AS DISP_ORD
          ,IMG_NM
          ,IMG_DEFAULT_IMG_YN
          ,IMG_MOUSEOVER_IMG_YN
          ,CASE WHEN RNUM = 1 THEN 'Y'
                ELSE 'N' END AS DEFAULT_IMG_YN
    FROM (
        SELECT GI.GOODS_IMG_SQ
              ,GI.GOODS_CD
              ,GI.COLOR_CD
              ,GI.SYS_IMG_NM
              ,GI.DEFAULT_IMG_YN AS IMG_DEFAULT_IMG_YN
              ,GI.MOUSEOVER_IMG_YN AS IMG_MOUSEOVER_IMG_YN
              , SUBSTRING_INDEX(SYS_IMG_NM,'_',-1) AS IMG_NM
              ,GI.DISP_ORD  AS IMG_DISP_ORD
              ,IFNULL(C.DISP_ORD,99999) AS  DISP_ORD
              , ROW_NUMBER() OVER(PARTITION BY GI.GOODS_CD, GI.COLOR_CD ORDER BY IFNULL(C.DISP_ORD,99999) , GI.DISP_ORD) AS RNUM
          FROM TB_GOODS_IMG GI
          LEFT OUTER JOIN TB_COMMON_CODE C ON  SUBSTRING_INDEX(GI.SYS_IMG_NM,'_',-1) = C.CD
                                           AND C.CD_GB  = 'G998'
          WHERE GI.GOODS_CD  = #{goodsCd}
          AND GI.COLOR_CD = #{colorCd}
          ORDER BY IFNULL(C.DISP_ORD,99999) , GI.DISP_ORD
          ) Z
    )
    , TAB_GOODS_IMG_DEFUALT AS (
    SELECT  A.*
          ,CASE WHEN DEFAULT_IMG_YN ='N' AND INSTR(lower(IMG_NM),'01.jpg') > 0 THEN 'Y'
              WHEN DEFAULT_IMG_YN ='N' AND INSTR(IMG_NM,'01') > 0
                   AND 0 = (SELECT COUNT(1) FROM TAB_GOODS_IMG S WHERE S.DEFAULT_IMG_YN ='N' AND INSTR(LOWER(S.IMG_NM),'01.jpg'))
                   THEN 'Y'
                ELSE 'N' END AS MOUSEOVER_IMG_YN
    FROM TAB_GOODS_IMG  A
    )
    , TAB_GOODS_IMG_MOUSEOVER_CNT AS (
    SELECT COUNT(*) AS CNT
    FROM TAB_GOODS_IMG_DEFUALT
    WHERE MOUSEOVER_IMG_YN = 'Y'
    )
    SELECT B.GOODS_IMG_SQ
         , B.GOODS_CD
         , B.COLOR_CD
         , B.SYS_IMG_NM
         , B.DISP_ORD
         , B.IMG_NM
         , B.IMG_DEFAULT_IMG_YN
         , B.IMG_MOUSEOVER_IMG_YN
         , B.DEFAULT_IMG_YN
         , CASE WHEN C.CNT = 0 AND B.DISP_ORD = 2 THEN 'Y'
                ELSE B.MOUSEOVER_IMG_YN END MOUSEOVER_IMG_YN
    FROM TAB_GOODS_IMG_DEFUALT B
    INNER JOIN TAB_GOODS_IMG_MOUSEOVER_CNT C
  </select>

  <!-- 상품이미지 순서변경 -->
  <update id="updateGoodsImageDispOrd" parameterType="GoodsImg">
    /* TssGoods.updateGoodsImageDispOrd */
    UPDATE TB_GOODS_IMG
    SET DEFAULT_IMG_YN = #{defaultImgYn}
      , MOUSEOVER_IMG_YN = #{mouseoverImgYn}
      , DISP_ORD = #{dispOrd}
    WHERE GOODS_IMG_SQ = #{goodsImgSq}
  </update>

  <!-- 상품기본이미지명 조회 -->
  <select id="getGoodsDefaultImageName" parameterType="GoodsImg" resultType="String">
    /* AdmGoods.getGoodsDefaultImageName */
    SELECT SYS_IMG_NM
    FROM   TB_GOODS_IMG
    WHERE  GOODS_CD = #{goodsCd}
    AND    COLOR_CD = #{colorCd}
    AND    DEFAULT_IMG_YN = 'Y'
    AND    EXTMALL_IMG_YN = 'N'
    LIMIT 1
  </select>


  <!-- wms 상품정보 조회 -->
  <select  id="getWmsGoodsInfo" parameterType="Goods" resultType="WmsGoods" >
    /* TssGoods.getWmsGoodsInfo */
    SELECT A.PRODUCT_NO
         , A.PRODUCT_CODE
         , A.PRODUCT_NAME
         , A.MODEL_NO
         , A.PROVIDER_NO
         , A.PROVIDER_NAME
         , A.BRAND_NO
         , A.BRAND_NAME
         , A.GOODS_REG_DT
         , A.GOODS_REG_GB
    FROM TB_WMS_GOODS A
    INNER JOIN TB_BRAND B ON A.BRAND_NO = B.BRAND_NO
                          AND B.SELF_YN  = 'Y'
                          AND B.USE_YN  = 'Y'
                          AND B.BRAND_CD = #{brandCd}
    INNER JOIN TB_BRAND_SUPPLY S ON B.BRAND_CD = S.BRAND_CD
    WHERE MODEL_NO = #{goodsCd}
    LIMIT 1
  </select>

  <!-- WMS 입고상품 사은품 상품 구분 저장 -->
  <update id="saveGoodsWmsIncomelot" parameterType="WmsGoods">
    /* TssGoods.saveGoodsWmsIncomelot */
    UPDATE TB_WMS_GOODS
    SET GOODS_REG_GB = #{goodsRegGb} /* G: 상품, F: 사은품*/
      , GOODS_REG_DT = NOW()
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE MODEL_NO = #{modelNo}
  </update>


  <!-- 품목 별 고시기본정보  조회 -->
  <select id="getItemkindNotiInfoList" parameterType="Goods" resultType="GoodsNotiInfo">
    /* TssGoods.getItemkindNotiInfoList */
    SELECT B.NI_CLSF_CD
         , B.NI_ITEM_CD
         , B.NI_CONTENT
         , B.DISP_ORD
         , FN_GET_CODE_NM('G005', B.NI_ITEM_CD) AS NI_ITEM_NM
         , #{goodsCd} AS GOODS_CD
    FROM TB_NOTI_INFO B
    WHERE B.SUPPLY_COMP_CD = (CASE #{supplyCompCd}
                              WHEN '1' THEN '1'
                              WHEN '2' THEN '2'
                              WHEN '3' THEN '3'
                              WHEN '4' THEN '4'
                              WHEN '5' THEN '5'
                              ELSE 'E'
                              END)
    AND B.NI_CLSF_CD = #{niClsfCd}
    ORDER BY  B.NI_CLSF_CD, B.DISP_ORD
  </select>

  <!-- 상품  등록 로그 생성 -->
  <insert id="createGoodsRegLog" parameterType="GoodsMass">
    /* TssGoods.createGoodsRegLog */
    INSERT INTO TB_GOODS_REG_LOG (
        PROC_GB
      , GOODS_CD
      , SUPPLY_COMP_CD
      , GOODS_NM
      , GOODS_TNM
      , GOODS_SNM1
      , ORIGIN_CD
      , MAKE_YMD
      , BRAND_CD
      , ITEMKIND_CD
      , STYLE_YEAR
      , SEASON_CD
      , SEX_GB
      , GOODS_NUM
      , GOODS_STAT_CD
      , LIST_PRICE
      , CURR_PRICE
      , COST_PRICE
      , SUPPLY_GOODS_CD
      , FORMAL_GB
      , SELL_FEE_RATE
      , GOODS_GB
      , TAX_GB
      , ADULT_YN
      , MIN_ORD_QTY
      , MAX_ORD_QTY
      , DAY_MAX_ORD_QTY
      , PNT_PRATE
      , PNT_MRATE
      , PRE_PPNT_USABLE_YN
      , PRE_MPNT_USABLE_YN
      , CHANGEABLE_YN
      , RETURNABLE_YN
      , GIFT_PACK_YN
      , NEW_CUST_ORD_YN
      , CERT_TARGET_GB
      , CERT_FORM_GB
      , CERT_TYPE
      , CERT_NUM
      , OPT_STR
      , GOODS_CONTENT
      , NI_CLSF_CD
      , NI_CONTENT1
      , NI_CONTENT2
      , NI_CONTENT3
      , NI_CONTENT4
      , NI_CONTENT5
      , NI_CONTENT6
      , NI_CONTENT7
      , NI_CONTENT8
      , NI_CONTENT9
      , NI_CONTENT10
      , NI_CONTENT11
      , NI_CONTENT12
      , NI_CONTENT13
      , NI_CONTENT14
      , NI_CONTENT15
      , NI_CONTENT16
      , NI_CONTENT17
      , NI_CONTENT18
      , NI_CONTENT19
      , NI_CONTENT20
      , NI_CONTENT21
      , NI_CONTENT22
      , NI_CONTENT23
      , NI_CONTENT24
      , NI_CONTENT25
      , NI_CONTENT26
      , NI_CONTENT27
      , NI_CONTENT28
      , REG_SUCC_YN
      , REG_FAIL_RSN
      , REG_NO
      , REG_DT
    )
    VALUES(
        #{procGb}
      , #{goodsCd}
      , #{supplyCompCd}
      , #{goodsNm}
      , #{goodsTnm}
      , #{goodsSnm1}
      , #{originCd}
      , #{makeYmd}
      , #{brandCd}
      , #{itemkindCd}
      , #{styleYear}
      , #{seasonCd}
      , #{sexGb}
      , #{goodsNum}
      , #{goodsStatCd}
      , #{listPrice}
      , #{currPrice}
      , #{costPrice}
      , #{supplyGoodsCd}
      , #{formalGb}
      , #{sellFeeRate}
      , #{goodsGb}
      , #{taxGb}
      , #{adultYn}
      , #{minOrdQty}
      , #{maxOrdQty}
      , #{dayMaxOrdQty}
      , #{pntPrate}
      , #{pntMrate}
      , #{prePpntUsableYn}
      , #{preMpntUsableYn}
      , #{changeableYn}
      , #{returnableYn}
      , #{giftPackYn}
      , #{newCustOrdYn}
      , #{certTargetGb}
      , #{certFormGb}
      , #{certType}
      , #{certNum}
      , #{optStr}
      , #{goodsContent}
      , #{niClsfCd}
      , #{niContent1}
      , #{niContent2}
      , #{niContent3}
      , #{niContent4}
      , #{niContent5}
      , #{niContent6}
      , #{niContent7}
      , #{niContent8}
      , #{niContent9}
      , #{niContent10}
      , #{niContent11}
      , #{niContent12}
      , #{niContent13}
      , #{niContent14}
      , #{niContent15}
      , #{niContent16}
      , #{niContent17}
      , #{niContent18}
      , #{niContent19}
      , #{niContent20}
      , #{niContent21}
      , #{niContent22}
      , #{niContent23}
      , #{niContent24}
      , #{niContent25}
      , #{niContent26}
      , #{niContent27}
      , #{niContent28}
      , #{regSuccYn}
      , #{regFailRsn}
      , #{regNo}
      , NOW()
    )
  </insert>

		<!-- 상품별 전시카테고리 삭제-->
	<delete id="deleteGoodsCategory" parameterType="GoodsCategory">
		/* TsaGoods.deleteGoodsCategory */
		DELETE
		FROM TB_CATE_GOODS
		WHERE GOODS_CD = #{goodsCd}
		<choose>
		<when test="cateType == 'G031_20' and cateCd != null and cateCd != ''">
		AND CATE_NO = #{cateCd}
		</when>
		<otherwise>
		AND CATE_NO IN (
			SELECT CATE_CD FROM
			(
				SELECT CATE1_NO AS CATE_CD
				, CATE1_NO
				, CATE2_NO
				, CATE3_NO
				, CATE4_NO
				, CATE_TYPE
				FROM TB_CATE_4SRCH
				<if test="cateCd != null and cateCd1 != cateCd">
				WHERE EXISTS (
					SELECT 1 FROM (
						SELECT COUNT(*) AS CATE_CNT
						 FROM (
							SELECT CATE_NO
							FROM TB_CATE_GOODS
							WHERE GOODS_CD = #{goodsCd}
							AND CATE_NO IN (
							    	SELECT LEAF_CATE_NO
									FROM TB_CATE_4SRCH
									WHERE CATE1_NO  = #{cateCd1}
								)
							) ALIAS_FOR_SUBQUERY
						) ALIAS_FOR_SUBQUERY
					<![CDATA[WHERE CATE_CNT < 2 ]]>
				)
				</if>
				UNION ALL
				SELECT CATE2_NO AS CATE_CD
					, CATE1_NO
					, CATE2_NO
					, CATE3_NO
					, CATE4_NO
					, CATE_TYPE
				FROM TB_CATE_4SRCH
				<if test="cateCd != null and cateCd1 != cateCd and cateCd2 != cateCd">
				WHERE EXISTS (
					SELECT 1 FROM (
						SELECT COUNT(*) AS CATE_CNT
						FROM (
							SELECT CATE_NO
							FROM TB_CATE_GOODS
							WHERE GOODS_CD = #{goodsCd}
							AND CATE_NO IN (
							    SELECT LEAF_CATE_NO
								FROM TB_CATE_4SRCH
								WHERE CATE2_NO = #{cateCd2}
							)
						) ALIAS_FOR_SUBQUERY
					) ALIAS_FOR_SUBQUERY
					<![CDATA[WHERE CATE_CNT < 2 ]]>
				)
				</if>
				UNION ALL
				SELECT CATE3_NO AS CATE_CD
					, CATE1_NO
					, CATE2_NO
					, CATE3_NO
					, CATE4_NO
					, CATE_TYPE
				FROM TB_CATE_4SRCH
				<if test="cateCd != null and cateCd == cateCd4">
				WHERE EXISTS (
					SELECT 1 FROM (
						SELECT COUNT(*) AS CATE_CNT
						FROM (
							SELECT CATE_NO
							FROM TB_CATE_GOODS
							WHERE GOODS_CD = #{goodsCd}
							AND CATE_NO IN (
							    	SELECT LEAF_CATE_NO
									FROM TB_CATE_4SRCH
									WHERE CATE3_NO = #{cateCd3}
							)
						) ALIAS_FOR_SUBQUERY
					) ALIAS_FOR_SUBQUERY
					<![CDATA[WHERE CATE_CNT < 2 ]]>
				)
				</if>
				UNION ALL
				SELECT CATE4_NO AS CATE_CD
				, CATE1_NO
				, CATE2_NO
				, CATE3_NO
				, CATE4_NO
				, CATE_TYPE
				FROM TB_CATE_4SRCH
			) ALIAS_FOR_SUBQUERY
			<if test="cateCd != null and cateCd1 == cateCd">
				WHERE CATE1_NO = #{cateCd}
			</if>
			<if test="cateCd != null and cateCd2 == cateCd">
				WHERE CATE2_NO = #{cateCd}
			</if>
			<if test="cateCd != null and cateCd3 == cateCd">
				WHERE CATE3_NO = #{cateCd}
			</if>
			<if test="cateCd != null and cateCd4 == cateCd">
				WHERE CATE4_NO = #{cateCd}
			</if>
			<if test="cateCd == null or cateCd == ''">
				WHERE CATE_TYPE = 'G031_10'
			</if>
		)
		</otherwise>
		</choose>
	</delete>


  <!-- 상품 품목별 매핑 카테고리 저장 (LEAF 상위카테고리까지)-->
  <insert id="createCategoryGoods_ORG" parameterType="Goods">
    /* TssGoods.createCategoryGoods */
    INSERT INTO TB_CATE_GOODS
        (CATE_NO
       , GOODS_CD
       , DISP_ORD
       , REG_NO
       , REG_DT
        )
    WITH TAB_GOODS AS (
        SELECT A.GOODS_CD
             , A.ITEMKIND_CD
             , B.CATE_NO
        FROM TB_GOODS A
        INNER JOIN TB_ITEMKIND_CATE  B ON A.ITEMKIND_CD = B.ITEMKIND_CD
        WHERE A.GOODS_CD = #{goodsCd}
    )
    , TAB_4SCRH AS (
        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' -- 상품타입
    )
    , TAB_CATE AS (
        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 * FROM TAB_4SCRH 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
             ) A
        ORDER BY LEAF_CATE_NO, CATE_LVL
    ) C
    WHERE CATE_NO IS NOT NULL
    )
    SELECT DISTINCT B.CATE_NO
         , A.GOODS_CD
         , 9999
         , #{regNo}
         , NOW()
    FROM TAB_GOODS A, TAB_CATE B
    WHERE 1 = 1
    AND A.CATE_ NO = B.LEAF_CATE_NO
    AND NOT EXISTS (SELECT 1
                    FROM TB_CATE_GOODS
                    WHERE CATE_NO = B.CATE_NO
                    AND GOODS_CD = A.GOODS_CD
                    )
    ON DUPLICATE KEY UPDATE
           REG_NO = #{regNo}
          ,REG_DT = NOW()
  </insert>

  <!-- 상품 품목별 매핑 카테고리 저장 (LEAF CATE에만 상품 적용 )-->
  <insert id="createCategoryGoods" parameterType="Goods">
    /* TssGoods.createCategoryGoods */
    INSERT INTO TB_CATE_GOODS
        (BRAND_GROUP_NO
       , CATE_NO
       , GOODS_CD
       , DISP_ORD
       , REG_NO
       , REG_DT
        )
    WITH TMP_GOODS AS (
        SELECT DISTINCT Z.GOODS_CD
              , Y.ITEMKIND_CD
        FROM (
             SELECT A.GOODS_CD
                  , A.GOODS_TYPE
                  , IFNULL(B.COMPS_GOODS_CD, A.GOODS_CD) AS COMPS_GOODS_CD
                  , IFNULL(B.DISP_ORD, 1)                AS DISP_ORD
                  , A.SELF_GOODS_YN
             FROM TB_GOODS A
             LEFT OUTER JOIN TB_GOODS_COMPOSE B ON A.GOODS_CD = B.GOODS_CD
                                                AND A.GOODS_TYPE = 'G056_D'
             WHERE A.GOODS_CD = #{goodsCd}
             ) Z
        INNER  JOIN TB_GOODS Y ON Z.COMPS_GOODS_CD = Y.GOODS_CD
        )
     SELECT 0    /*BRAND_GROUP_NO*/
          , B.CATE_NO
          , A.GOODS_CD
          , 9999
          , #{regNo}
          , NOW()
    FROM TMP_GOODS A
    INNER JOIN TB_ITEMKIND_CATE  B ON A.ITEMKIND_CD = B.ITEMKIND_CD
    INNER JOIN TB_CATE_4SRCH C ON B.CATE_NO = C.LEAF_CATE_NO
                       AND C.SITE_CD = 'G000_10'
                       AND C.CATE_TYPE = 'G031_10' -- 상품타입
    WHERE A.GOODS_CD = #{goodsCd}
    AND NOT EXISTS (SELECT 1
                    FROM TB_CATE_GOODS
                    WHERE CATE_NO = B.CATE_NO
                    AND   BRAND_GROUP_NO = 0
                    AND GOODS_CD = A.GOODS_CD
                    )
    ON DUPLICATE KEY UPDATE
           REG_NO = #{regNo}
          ,REG_DT = NOW()
  </insert>

  <!-- wms 상품 색상 , 사이즈별 재고 정보 조회 -->
  <select id="getGoodsWmsSizeList_org" parameterType="Goods"  resultType = "Option">
    /* TssGoods.getGoodsWmsSizeList_org */
    SELECT Z.GOODS_CD
         , Z.OPT_CD
         , F.COLOR_CD AS OPT_CD1
         , Z.OPT_CD2
         , Z.SKU_MODEL_NO
         , Z.PRODUCT_NO
         , Z.PRODUCT_CODE
         , SUM(Z.CURR_STOCK_QTY) AS CURR_STOCK_QTY
         , IFNULL((SELECT (MAX(DISP_ORD) + 1) AS DISP_ORD
                FROM TB_OPTION WHERE GOODS_CD = Z.GOODS_CD) , ROW_NUMBER() OVER(PARTITION BY GOODS_CD ORDER BY OPT_CD)) AS DISP_ORD
    FROM (
    SELECT B.GOODS_CD
        , A.SKUCODE AS OPT_CD
        -- , MAX(A.OPTION1) AS OPT_CD1
        , MAX(A.OPTION2) AS OPT_CD2
        , MAX(A.SKUMODELNO) AS SKU_MODEL_NO
        , MAX(A.PRODUCTNO) AS PRODUCT_NO
        , MAX(A.PRODUCTCODE) AS PRODUCT_CODE
        , ROUND(GREATEST(A.SELLINGSTOCKAMOUNT, 0) * (IFNULL(D.STOCK_APPL_RATE, 0)/100)) AS CURR_STOCK_QTY
        , D.DELV_LOC_CD
        , D.STOCK_APPL_RATE
        , MAX(B.SUPPLY_COMP_CD) AS SUPPLY_COMP_CD
        , MAX(B.BRAND_CD) AS BRAND_CD
        , MAX(CASE WHEN INSTR(REPLACE(A.SKUMODELNO, ' ' , ''), 'F20') = 1 THEN MID(REPLACE(A.SKUMODELNO, ' ' , ''),13,2)
                   WHEN INSTR(REPLACE(A.SKUMODELNO, ' ' , ''), '-') > 0 AND INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') <![CDATA[<]]> 10 THEN MID(REPLACE(A.SKUMODELNO, ' ' , ''),12,2)
                   ELSE MID(REPLACE(A.SKUMODELNO, ' ' , ''),11,2) END) AS WMS_COLOR_CD
    FROM TB_IF_PRODUCTSKU A
    INNER JOIN TB_GOODS B ON B.SELF_GOODS_YN = 'Y'
                          AND B.GOODS_TYPE = 'G056_N'
                          AND B.GOODS_CD = #{goodsCd}
    INNER JOIN TB_STOCK_SYNC_BASE C ON B.BRAND_CD = C.BRAND_CD
                                    AND C.STOCK_SYNC_YN = 'Y'
    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'
    WHERE 1 = 1
    AND (CASE WHEN INSTR(REPLACE(A.SKUMODELNO, ' ' , ''), 'F20') = 1 THEN LEFT(REPLACE(A.SKUMODELNO, ' ' , ''),12)
              WHEN INSTR(REPLACE(A.SKUMODELNO, ' ' , ''), '-') > 0 AND INSTR(REPLACE(B.SKUMODELNO, ' ' , ''), '-') <![CDATA[<]]> 10 THEN LEFT(REPLACE(A.SKUMODELNO, ' ' , ''),11)
              ELSE LEFT(REPLACE(A.SKUMODELNO, ' ' , ''),10) END) = #{goodsCd}
    GROUP BY B.GOODS_CD, A.SKUCODE, D.DELV_LOC_CD, D.STOCK_APPL_RATE
    ) Z
    INNER JOIN TB_WMS_COLOR_MAPPING F ON Z.SUPPLY_COMP_CD = F.SUPPLY_COMP_CD
                                      AND Z.WMS_COLOR_CD = F.WMS_COLOR_CD
                                      AND F.USE_YN ='Y'
    GROUP BY Z.GOODS_CD, Z.OPT_CD, F.COLOR_CD, Z.OPT_CD2, Z.SKU_MODEL_NO, Z.PRODUCT_NO, Z.PRODUCT_CODE
  </select>


  <!-- wms 상품 색상, 재고판매율 적용 조회 -->
  <select id="getWmsColorStock" parameterType="IfProductSku"  resultType = "Option">
    /* TssGoods.getGoodsWmsSizeList */
    SELECT B.GOODS_CD
         , F.COLOR_CD AS OPT_CD1
         , ROUND(GREATEST(#{WmsStockQty}, 0) * (IFNULL(D.STOCK_APPL_RATE, 0)/100)) AS CURR_STOCK_QTY
    FROM TB_GOODS B
    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 매장코드
    INNER JOIN TB_WMS_COLOR_MAPPING F ON B.SUPPLY_COMP_CD = F.SUPPLY_COMP_CD
                                      AND #{wmsColorCd} = F.WMS_COLOR_CD
                                      AND F.USE_YN ='Y'
    WHERE B.SELF_GOODS_YN = 'Y'
    AND B.GOODS_TYPE = 'G056_N'
    AND B.GOODS_CD = #{goodsCd}
  </select>

  <!-- 상품등록로그 목록 조회 -->
  <select id="getGoodsRegLogList" parameterType="GoodsSearch" resultType="GoodsMass">
    /* TssGoods.getGoodsRegLogList */
    SELECT A.GOODS_REG_SQ
         , A.PROC_GB
         , A.GOODS_CD
         , A.SUPPLY_COMP_CD
         , A.GOODS_NM
         , A.GOODS_TNM
         , A.GOODS_SNM1
         , A.ORIGIN_CD
         , A.MAKE_YMD
         , A.BRAND_CD
         , A.ITEMKIND_CD
         , A.STYLE_YEAR
         , A.SEASON_CD
         , A.SEX_GB
         , A.GOODS_NUM
         , A.GOODS_STAT_CD
         , A.LIST_PRICE
         , A.CURR_PRICE
         , A.COST_PRICE
         , A.SUPPLY_GOODS_CD
         , A.FORMAL_GB
         , A.GOODS_GB
         , A.TAX_GB
         , A.ADULT_YN
         , A.SELL_FEE_RATE
         , A.MIN_ORD_QTY
         , A.MAX_ORD_QTY
         , A.DAY_MAX_ORD_QTY
         , A.PNT_PRATE
         , A.PNT_MRATE
         , A.PRE_PPNT_USABLE_YN
         , A.PRE_MPNT_USABLE_YN
         , A.CHANGEABLE_YN
         , A.RETURNABLE_YN
         , A.GIFT_PACK_YN
         , A.NEW_CUST_ORD_YN
         , A.CERT_TARGET_GB
         , A.CERT_FORM_GB
         , A.CERT_TYPE
         , A.CERT_NUM
         , A.OPT_STR
         , A.GOODS_CONTENT
         , A.NI_CLSF_CD
         , A.NI_CONTENT1
         , A.NI_CONTENT2
         , A.NI_CONTENT3
         , A.NI_CONTENT4
         , A.NI_CONTENT5
         , A.NI_CONTENT6
         , A.NI_CONTENT7
         , A.NI_CONTENT8
         , A.NI_CONTENT9
         , A.NI_CONTENT10
         , A.NI_CONTENT11
         , A.NI_CONTENT12
         , A.NI_CONTENT13
         , A.NI_CONTENT14
         , A.NI_CONTENT15
         , A.NI_CONTENT16
         , A.NI_CONTENT17
         , A.NI_CONTENT18
         , A.NI_CONTENT19
         , A.NI_CONTENT20
         , A.NI_CONTENT21
         , A.NI_CONTENT22
         , A.NI_CONTENT23
         , A.NI_CONTENT24
         , A.NI_CONTENT25
         , A.NI_CONTENT26
         , A.NI_CONTENT27
         , A.NI_CONTENT28
         , A.REG_SUCC_YN
         , A.REG_FAIL_RSN
         , A.REG_NO
         , FN_GET_USER_NM(A.REG_NO) AS REG_NM
         , DATE_FORMAT(A.REG_DT, '%Y%m%d%H%i%S') AS REG_DT
    FROM TB_GOODS_REG_LOG A
    -- LEFT OUTER JOIN TB_GOODS B ON A.GOODS_CD = B.GOODS_CD
    WHERE 1 = 1
    <if test='condition != null and condition != "" '>
    AND (UPPER(A.GOODS_NUM) LIKE CONCAT(UPPER(#{condition}),'%')
         OR
         A.GOODS_CD LIKE CONCAT(UPPER(#{condition}),'%')
         OR
         UPPER(A.SUPPLY_GOODS_CD) LIKE CONCAT(UPPER(#{condition}),'%')
        )
    </if>
    <if test='regSuccYn != null and regSuccYn != "" '>
    AND A.REG_SUCC_YN  = #{regSuccYn}
    </if>
    <if test='regNo != null and regNo != "" '>
    AND A.REG_No = #{regNo}
    </if>
    <if test="stDate != null and stDate != ''">
    AND A.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
    </if>
    <if test="edDate != null and edDate != ''">
    <![CDATA[
    AND A.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
    ]]>
    </if>
    ORDER BY A.REG_DT DESC, A.GOODS_CD
  </select>

  <!-- 상품 색상, 사이즈 정보 조회 -->
  <select id="getGoodsColorSize"  parameterType="Goods" resultType="GoodsMass">
    /* TssGoods.getGoodsColorSize */
    SELECT GOODS_CD
         , GROUP_CONCAT(DISTINCT OPT_CD1) AS OPT1_STR
         , GROUP_CONCAT(DISTINCT OPT_CD2) AS OPT2_STR
         , MIN(OPT_CD1) AS MAIN_COLOR_CD
    FROM TB_OPTION
    WHERE GOODS_CD = #{goodsCd}
    GROUP BY GOODS_CD
  </select>

  <!-- 상품 대표색상 설정 -->
  <update id="updateGoodsMainColorCd" parameterType="GoodsMass">
    /* TssGoods.updateGoodsMainColorCd */
    UPDATE TB_GOODS
    SET MAIN_COLOR_CD = #{mainColorCd}
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE GOODS_CD = #{goodsCd}
  </update>

  <!-- 상품 상태만 변경 -->
  <update id="updateGoodsStatOnly" parameterType="Goods">
    /* TssGoods.updateGoodsStatOnly */
    UPDATE TB_GOODS
    SET GOODS_STAT= #{goodsStat}
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE GOODS_CD = #{goodsCd}

    <if test='chGoodsStatYn != null and chGoodsStatYn == "Y"'>
      AND GOODS_STAT = 'G008_20'
    </if>
  </update>

  <!-- 상품 기본 정보 수정  - 대량엑셀 -->
  <update id="updateGoodsMass" parameterType="Goods">
    /* TssGoods.updateGoodsMass */
    UPDATE TB_GOODS
    SET UPD_NO = #{updNo}
      , UPD_DT = NOW()
    <if test='goodsNm != null and goodsNm != "X"'>
      , GOODS_NM = #{goodsNm}
    </if>
    <if test='goodsTnm != null and goodsTnm != "X"'>
      , GOODS_TNM = #{goodsTnm}
    </if>
    <if test='goodsSnm1 != null and goodsSnm1 != "X"'>
      , GOODS_SNM1 = #{goodsSnm1}
    </if>
    <if test='itemkindCd != null and itemkindCd != "X"'>
      , ITEMKIND_CD = #{itemkindCd}
    </if>
    <if test='seasonCd != null and seasonCd != "X"'>
      , SEASON_CD = #{seasonCd}
    </if>
    <if test='sexGb != null and sexGb != "X"'>
      , SEX_GB = #{sexGb}
    </if>
    <if test='goodsStatCd != null and goodsStatCd != "X"'>
	  , GOODS_STAT = #{goodsStatCd}
	</if>
    <if test="currPrice != null and currPrice > 0">
      , CURR_PRICE = #{currPrice}
      , CURR_BPRICE = #{currBprice}
      , PRICE_UPD_DT = NOW()
      , DC_RATE = #{dcRate}
    </if>
    <if test="sellFeeRate != null and sellFeeRate > 0">
      , SELL_FEE_RATE = #{sellFeeRate}
    </if>
    <if test='goodsGb != null and goodsGb != "X"'>
      , GOODS_GB = #{goodsGb}
    </if>
    <if test='formalGb != null and formalGb != "X"'>
      , FORMAL_GB = #{formalGb}
      , PNT_PRATE = IFNULL(#{pntPrate}, PNT_PRATE)
      , PNT_MRATE = IFNULL(#{pntMrate}, PNT_MRATE)
    </if>
    <if test="minOrdQty != null and minOrdQty > 0">
      , MIN_ORD_QTY = #{minOrdQty}
    </if>
    <if test="maxOrdQty != null and maxOrdQty > 0">
      , MAX_ORD_QTY = #{maxOrdQty}
    </if>
    <if test="dayMaxOrdQty != null and dayMaxOrdQty > 0">
      , DAY_MAX_ORD_QTY = #{dayMaxOrdQty}
    </if>
    <if test='giftPackYn != null and giftPackYn != "X"'>
      , GIFT_PACK_YN = #{giftPackYn}
    </if>
    <if test='newCustOrdYn != null and newCustOrdYn != "X"'>
      , NEW_CUST_ORD_YN = #{newCustOrdYn}
    </if>
    <if test='returnableYn != null and returnableYn != "X"'>
      , RETURNABLE_YN = #{returnableYn}
    </if>
    <if test='changeableYn != null and changeableYn != "X"'>
      , CHANGEABLE_YN = #{changeableYn}
    </if>
    <if test='taxGb != null and taxGb != "X"'>
      , TAX_GB = #{taxGb}
    </if>
    <if test='ageGrpCd != null and ageGrpCd != "X"'>
      , AGE_GRP_CD = #{ageGrpCd}
    </if>
    <if test='adultYn != null and adultYn != "X"'>
      , ADULT_YN = #{adultYn}
    </if>
    <if test='pntPrate != null and pntPrate >= 0'>
      , PNT_PRATE = #{pntPrate}
    </if>
    <if test='prePpntUsableYn != null and prePpntUsableYn != "X"'>
      , PRE_PPNT_USABLE_YN = DECODE(#{prePpntUsableYn}, 'Y', 'Y', 'N', 'N', PRE_PPNT_USABLE_YN)
    </if>
    <if test='pntMrate != null and pntMrate >= 0'>
      , PNT_MRATE = #{pntMrate}
    </if>
    <if test='makeYmd != preMpntUsableYn and preMpntUsableYn != "X"'>
      , PRE_MPNT_USABLE_YN = DECODE(#{preMpntUsableYn}, 'Y', 'Y', 'N', 'N', PRE_MPNT_USABLE_YN)
    </if>
    WHERE GOODS_CD = #{goodsCd}
  </update>

  <!-- 정보고시 상세 목록 -->
  <select id="getNotiInfoItemList" parameterType="NotiInfo" resultType="NotiInfo">
    /* TssGoods.getNotiInfoItemList */
    SELECT B.SUPPLY_COMP_CD
         , B.NI_CLSF_CD
         , B.NI_ITEM_CD
         , FN_GET_CODE_NM('G005', B.NI_ITEM_CD) AS NI_ITEM_NM
         , B.NI_CONTENT
         , B.DISP_ORD
    FROM TB_NOTI_INFO B
    WHERE B.SUPPLY_COMP_CD = CASE #{supplyCompCd}
                             WHEN '1' THEN '1'
                             WHEN '2' THEN '2'
                             WHEN '3' THEN '3'
                             WHEN '4' THEN '4'
                             WHEN '5' THEN '5'
                             ELSE 'E' END
    <if test="niClsfCd != null and niClsfCd != ''">
    AND B.NI_CLSF_CD = #{niClsfCd}
    </if>
    ORDER BY B.NI_CLSF_CD, B.DISP_ORD
  </select>

  <!-- 상품옵션 패턴조회 -->
  <select id="getGoodsOption" parameterType="Option" resultType="Option">
    /* TssGoods.getGoodsOption */
    SELECT GOODS_CD
         , OPT_CD
         , OPT_CD1
         , OPT_CD2
    FROM TB_OPTION
    WHERE  GOODS_CD = #{goodsCd}
    ORDER BY DISP_ORD
    LIMIT 1
  </select>

  <!-- 상품옵션 중복건수여부 확인 -->
  <select id="getGoodsOptionDupCountCheck" parameterType="Option" resultType="int">
    /* TssGoods.getGoodsOptionDupCountCheck */
    SELECT COUNT(*)
    FROM TB_OPTION
    WHERE  GOODS_CD = #{goodsCd}
    AND OPT_CD1 = #{optCd1}
    AND OPT_CD2 = #{optCd2}
  </select>

  <!-- 상품옵션1 존재 여부 확인 -->
  <select id="getGoodsOption1Count" parameterType="Option" resultType="int">
    /* TssGoods.getGoodsOption1Count */
    SELECT COUNT(*)
    FROM TB_OPTION
    WHERE  GOODS_CD = #{goodsCd}
    AND OPT_CD1 = #{optCd1}
  </select>

  <!-- 사은품 목록 -->
  <select id="getFreeGoodsList" parameterType="GoodsSearch" resultType="FreeGoods">
    /* TssGoods.getFreeGoodsList */
    SELECT G.PRODUCT_NO
         , G.PRODUCT_CODE
         , G.GOODS_NM
         , G.USE_YN
         , G.SYS_IMG_NM
         , G.BRAND_CD
         , B.BRAND_ENM
         , FN_GET_USER_NM(G.REG_NO) AS REG_NM
         , DATE_FORMAT(G.REG_DT,'%Y%m%d%H%i%S') AS REG_DT
         , FN_GET_USER_NM(G.UPD_NO) AS UPD_NM
         , DATE_FORMAT(G.UPD_DT,'%Y%m%d%H%i%S') AS UPD_DT
    FROM TB_FREE_GOODS G
    INNER JOIN TB_BRAND B ON G.BRAND_CD = B.BRAND_CD
    WHERE 1 = 1
    <if test='conditionList != null and conditionList.length>0'>
        <choose>
          <when test='search != null and search == "searchProductNo"'>
    AND (
          <foreach collection="conditionList" item="item" index="index" separator="or">
           G.PRODUCT_NO = #{item}
          </foreach>
         )
          </when>
          <when test='search != null and search == "searchGoodsNm"'>
    AND (
          <foreach collection="conditionList" item="item" index="index" separator="or">
           UPPER(G.GOODS_NM) LIKE CONCAT(UPPER(#{item}),'%')
          </foreach>
         )
          </when>
          <otherwise>
    AND 1 = 1
          </otherwise>
        </choose>
    </if>
    <if test="brandCd != null and brandCd != ''">
    AND B.BRAND_CD = #{brandCd}
    </if>
    <if test="multiBrand != null and multiBrand != ''">
    AND B.BRAND_CD IN
        <foreach collection="multiBrand" item="item" index="index"  open="(" close=")" separator=",">
    #{item}
        </foreach>
    </if>
    <if test='dateGbn != null and dateGbn == "R"'>
        <if test="stDate != null and stDate != ''">
    AND G.REG_DT >= DATE_FORMAT(#{stDate}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="edDate != null and edDate != ''">
        <![CDATA[
    AND G.REG_DT < DATE_FORMAT(DATE_ADD(#{edDate}, INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S')
        ]]>
        </if>
    </if>
    ORDER BY G.PRODUCT_NO DESC
  </select>

  <!-- 상품이미지 변경 -->
  <update id="updateFreeGoods" parameterType="FreeGoods">
    /* TssGoods.updateFreeGoods */
    UPDATE TB_FREE_GOODS
    SET GOODS_NM = #{goodsNm}
      , SYS_IMG_NM = #{sysImgNm}
      , USE_YN = #{useYn}
      , UPD_NO = #{updNo}
      , UPD_DT = NOW()
    WHERE PRODUCT_NO = #{productNo}
  </update>

  <!-- 상품 안전인증 목록 -->
  <select id="getGoodsDetailSafe" parameterType="Goods" resultType="GoodsSafeNo">
    /* TssGoods.getGoodsDetailSafe */
    SELECT  GOODS_CD
          , CERT_TARGET_GB
          , CERT_FORM_GB
          , CERT_TYPE
          , CERT_NUM
          , CERT_STATE
          , CERT_DIV
          , CERT_ORGAN_NAME
          , CERT_DT
    FROM TB_GOODS_SAFE_NO
    WHERE GOODS_CD = #{goodsCd}
    ORDER BY GOODS_CD
  </select>

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

  <!-- 상품 모델 정보 -->
  <select id="getGoodsModelInfo" parameterType="GoodsImg" resultType="GoodsImg">
    /* TssGoods.getGoodsModelInfo */
    SELECT GOODS_CD
          , MODEL_INFO
    FROM TB_GOODS_MODEL_INFO
    WHERE GOODS_CD = #{goodsCd}
  </select>

  <!-- 상품 모델 정보 저장 -->
  <insert id="saveGoodsModelInfo" parameterType="GoodsImg">
    /* TssGoods.saveGoodsModelInfo */
    INSERT INTO TB_GOODS_MODEL_INFO
        (GOODS_CD
       , MODEL_INFO
       , REG_NO
       , REG_DT
       , UPD_NO
       , UPD_DT
        )
    VALUES
        (
         #{goodsCd}
       , #{modelInfo}
       , #{regNo}
       , NOW()
       , #{updNo}
       , NOW()
        )
    ON DUPLICATE KEY UPDATE
           MODEL_INFO = #{modelInfo}
          ,UPD_NO = #{updNo}
          ,UPD_DT = NOW()
  </insert>

  <!-- 구상 상품등록/저장 -->
  <insert id="saveGoodsCompose" parameterType="GoodsCompose">
    /* TsaGoods.saveGoodsCompose */
    INSERT INTO TB_GOODS_COMPOSE (
               GOODS_CD
             , COMPS_GOODS_CD
             , GOODS_TYPE
             , DISP_ORD
             , QTY
             , COMPS_CURR_PRICE
             , COMPS_GOODS_OPT_NM
             , BASE_YN
             , REP_YN
             , USE_YN
             , REG_NO
             , REG_DT
             , UPD_NO
             , UPD_DT
        )
        VALUES (
               #{goodsCd}
             , #{compsGoodsCd}
             , #{goodsType}
             , #{dispOrd}
             , #{qty}
             , (CASE WHEN #{goodsType} = 'G056_S' THEN #{compsCurrPrice}
                    ELSE 0 END)
             , #{compsGoodsOptNm}
             , #{baseYn}
             , IFNULL(#{repYn},'N')
             , #{useYn}
             , #{regNo}
             , NOW()
             , #{updNo}
             , NOW()
        )
    ON DUPLICATE KEY UPDATE
               DISP_ORD = #{dispOrd}
             , COMPS_GOODS_OPT_NM = #{compsGoodsOptNm}
             , COMPS_CURR_PRICE = (CASE WHEN #{goodsType} = 'G056_S' THEN #{compsCurrPrice}
                                        ELSE 0 END)
             , REP_YN = #{repYn}
             , USE_YN = #{useYn}
             , UPD_NO = #{updNo}
             , UPD_DT = NOW()
  </insert>

  <!-- 구성상품 목록 -->
  <select id="getGoodsDetailComposeList" parameterType="Goods" resultType="GoodsCompose">
    /* TsaGoods.getGoodsDetailComposeList */
    SELECT A.GOODS_CD
         , A.GOODS_TYPE
         , FN_GET_CODE_NM('G056', A.GOODS_TYPE) AS GOODS_TYPE_NM
         , A.COMPS_GOODS_CD
         , B.GOODS_NM AS COMPS_GOODS_NM
         , A.DISP_ORD
         , A.QTY
         , A.COMPS_CURR_PRICE
         , A.COMPS_CURR_PRICE AS COMPS_CURR_PRICE_ORG
         , A.COMPS_GOODS_OPT_NM
         , A.BASE_YN
         , A.REP_YN
         , A.USE_YN
         , B.CURR_PRICE
         , B.GOODS_STAT
         , B.SELF_GOODS_YN
         , B.SUPPLY_COMP_CD
         , (SELECT MAX(COMPS_GOODS_CD) FROM TB_GOODS_COMPOSE WHERE GOODS_CD  = #{goodsCd} AND REP_YN='Y') AS REP_GOODS_CD
         , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = B.GOODS_CD AND COLOR_CD = B.MAIN_COLOR_CD AND DEFAULT_IMG_YN = 'Y' ) AS SYS_IMG_NM
         , A.REG_NO
         , FN_GET_USER_NM(A.REG_NO) AS REG_NM
         , DATE_FORMAT(A.REG_DT, '%Y%m%d%H%i%S') AS REG_DT
         , A.UPD_NO
         , FN_GET_USER_NM(A.UPD_NO) AS UPD_NM
         , DATE_FORMAT(A.UPD_DT, '%Y%m%d%H%i%S') AS UPD_DT
    FROM TB_GOODS_COMPOSE A
    INNER JOIN  TB_GOODS B ON A.COMPS_GOODS_CD = B.GOODS_CD
    WHERE A.GOODS_CD = #{goodsCd}
    ORDER BY A.DISP_ORD
  </select>

  <!-- 자사브랜드, 업체 연관정보 확인  -->
  <select id="getBrandSupplyCount" parameterType="Brand" resultType="int">
    /* TsaGoods.getBrandSupplyCount */
    SELECT  COUNT(*)
    FROM TB_BRAND B
    INNER JOIN TB_BRAND_SUPPLY BS ON B.BRAND_CD  = BS.BRAND_CD
    INNER JOIN TB_SUPPLY_COMPANY TSC ON BS.SUPPLY_COMP_CD  = TSC.SUPPLY_COMP_CD
                                     AND TSC.SUPPLY_COMP_CD = #{supplyCompCd}
    WHERE B.BRAND_CD = #{brandCd}
    AND B.SELF_YN  = 'Y'
  </select>
</mapper>
