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

	<!-- 상품 목록 조건 정보 -->
	<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>
	</sql>

	<!-- 목록 페이징 정보 -->
	<sql id="getListPagingCondition_sql">
		<choose>
		<when test="pageable != null">
		    ) A
		    JOIN ( SELECT @rownum := 0) R
		)Z WHERE RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
		</when>
		<otherwise>
		    ) A
		    JOIN ( SELECT @rownum := 0) R
		)Z
		</otherwise>
		</choose>
	</sql>

	<!-- 목록 페이징 정보 LIMIT 용 -->
	<sql id="getListLimitPagingCondition_sql">
		<choose>
		<when test="pageable != null">
		    ) A
		)Z
		LIMIT #{pageable.limitStartRow} ,  #{pageable.pageSize}
		</when>
		<otherwise>
		    ) A
		)Z
		</otherwise>
		</choose>
	</sql>

	<!-- 상품 사이즈별 재고 현황 목록 건수 -->
	<select id="getGoodsSizeStockCount" parameterType="GoodsSearch" resultType="int">
		/* TsaStock.getGoodsSizeStockCount */
		SELECT COUNT(*) AS TOTCNT
		FROM (
		      SELECT Y.*
		      FROM (
		            SELECT G.GOODS_CD
		                 , ST.OPT_CD
		                 , GREATEST(GREATEST(ST.CURR_STOCK_QTY - ST.BASE_STOCK_QTY, 0) - IFNULL(SQ.STOCK_QTY, 0), 0) AS STOCK_QTY
		            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_OPTION ST ON G.GOODS_CD = ST.GOODS_CD
		            <if test="searchGb == null or searchGb =='BASIC'">
		                                    <if test="optCd1 == null or optCd1 != ''">
		                                    AND ST.OPT_CD1 = #{optCd1}
		                                    </if>
		                                    <if test="optCd2 == null or optCd2 != ''">
		                                    AND ST.OPT_CD2 = #{optCd2}
		                                    </if>
		            </if>
		            LEFT OUTER JOIN  (SELECT GOODS_CD,
		                                     OPT_CD,
		                                     SUM((CASE SELL_GB WHEN '10' THEN 1
		                                                       WHEN '20' THEN -1
		                                           END) * SELL_QTY) AS STOCK_QTY
		                              FROM TB_SELL_QTY
		                              GROUP BY GOODS_CD, OPT_CD
		                              ) SQ
		                           ON (    ST.GOODS_CD = SQ.GOODS_CD
		                               AND ST.OPT_CD = SQ.OPT_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>
		            <if test="searchGb != null and searchGb =='EXCELRESULT'">
		            INNER JOIN (
		                        SELECT SEARCH_CD
		                             , TMP_DISP_ORD
		                             , OPT_CD
		                        FROM (
		                              SELECT SEARCH_CD
		                                   , DUMMY1 AS OPT_CD
		                                   , MIN(DISP_ORD) AS TMP_DISP_ORD
		                              FROM TB_SEARCH_DATA
		                              WHERE REG_NO = #{regNo}
		                              GROUP BY SEARCH_CD, DUMMY1) 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>
		                        AND ST.GOODS_CD = SD.SEARCH_CD
		                        AND ST.OPT_CD = SD.OPT_CD
		                   )
		            </if>
		            WHERE 1=1
		            AND G.GOODS_TYPE = 'G056_N' /*일반상품만 조회*/
		            <if test="searchGb == null or searchGb =='BASIC'">
		            <include refid="getGoodsListCondition_sql"/>
		            </if>

		           ) Y
		      ) X
		WHERE 1 = 1
		<if test="searchGb == null or searchGb =='BASIC'">
		    <if test="stockQtySt != null and stockQtySt != ''">
		AND X.STOCK_QTY >= #{stockQtySt}
		    </if>
		    <if test="stockQtyEd != null and stockQtyEd != ''">
		    <![CDATA[
		AND X.STOCK_QTY <= #{stockQtyEd}
		    ]]>
		    </if>
		</if>
	</select>

	<!-- 상품 사이즈별 재고 현황 목록  -->
	<select id="getGoodsSizeStockList" parameterType="GoodsSearch" resultType="Option">
		/* TsaStock.getGoodsSizeStockList */
		<include refid="getGoodsSizeStockList_sql"/>
	</select>

	<!-- 사이즈별재고현황 - 엑셀다운로드 -->
	<select id="getGoodsSizeStockExcelList" parameterType="GoodsSearch" resultType="paramMap">
		/* TsaStock.getGoodsSizeStockExcelList */
		<include refid="getGoodsSizeStockList_sql"/>
	</select>

	<!--  사이즈별 재고현황 정보 -->
	<sql id="getGoodsSizeStockList_sql">
		SELECT Z.*
		    , (SELECT ITEMKIND_NM FROM TB_ITEMKIND WHERE ITEMKIND_CD = Z.ITEMKIND_CD) AS ITEMKIND_NM
		    , FN_GET_CODE_NM('G008', Z.GOODS_STAT) AS GOODS_STAT_NM
		FROM (
		    SELECT A.* -- , @rownum := @rownum + 1  RNUM
		    FROM (
		        SELECT  X.GOODS_CD
		              , X.BRAND_ENM
		              , X.BRAND_GRP_NM
		              , X.BRAND_CD
		              , X.ITEMKIND_CD
		              , X.SUPPLY_COMP_CD
		              , X.SUPPLY_GOODS_CD
		              , X.GOODS_NM
		              , X.GOODS_STAT
		              , X.OPT_CD
		              , X.OPT_CD1
		              , X.OPT_CD2
		              , X.SOLDOUT_YN
		              , X.CURR_STOCK_QTY
		              , X.BASE_STOCK_QTY
		              , X.SALE_STOCK_QTY /*출고대기*/
		              , X.ABLE_STOCK_QTY /*판매가능재고*/
		              , X.REG_DT
		        FROM (

		                    SELECT
		                            G.GOODS_CD
		                          , B.BRAND_ENM
		                          , B.BRAND_GRP_NM
		                          , G.BRAND_CD
		                          , G.ITEMKIND_CD
		                          , G.SUPPLY_COMP_CD
		                          , G.SUPPLY_GOODS_CD
		                          , G.GOODS_NM
		                          , G.GOODS_STAT
		                          , ST.OPT_CD
		                          , ST.OPT_CD1
		                          , ST.OPT_CD2
		                          , ST.SOLDOUT_YN
		                          , ST.CURR_STOCK_QTY
		                          , ST.BASE_STOCK_QTY
		                          , IFNULL(SQ.STOCK_QTY,0) AS SALE_STOCK_QTY
		                          , GREATEST(GREATEST(ST.CURR_STOCK_QTY - ST.BASE_STOCK_QTY, 0) - IFNULL(SQ.STOCK_QTY, 0), 0) AS ABLE_STOCK_QTY
		                          , G.REG_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_OPTION ST ON G.GOODS_CD = ST.GOODS_CD
		                    <if test="searchGb == null or searchGb =='BASIC'">
		                                            <if test="optCd1 != null and optCd1 != ''">
		                                            AND ST.OPT_CD1 = #{optCd1}
		                                            </if>
		                                            <if test="optCd2 != null and optCd2 != ''">
		                                            AND ST.OPT_CD2 = #{optCd2}
		                                            </if>
		                    </if>
		                    LEFT OUTER JOIN  (SELECT GOODS_CD,
		                                             OPT_CD,
		                                             SUM((CASE SELL_GB WHEN '10' THEN 1
		                                                               WHEN '20' THEN -1
		                                                   END) * SELL_QTY) AS STOCK_QTY
		                                      FROM TB_SELL_QTY
		                                      GROUP BY GOODS_CD, OPT_CD
		                                      ) SQ
		                                   ON (    ST.GOODS_CD = SQ.GOODS_CD
		                                       AND ST.OPT_CD = SQ.OPT_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>
		                    <if test="searchGb != null and searchGb =='EXCELRESULT'">
		                    INNER JOIN (
		                                SELECT SEARCH_CD
		                                     , TMP_DISP_ORD
		                                     , OPT_CD
		                                FROM (
		                                      SELECT SEARCH_CD
		                                           , DUMMY1 AS OPT_CD
		                                           , MIN(DISP_ORD) AS TMP_DISP_ORD
		                                      FROM TB_SEARCH_DATA
		                                      WHERE REG_NO = #{regNo}
		                                      GROUP BY SEARCH_CD, DUMMY1) 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>
		                                AND ST.GOODS_CD = SD.SEARCH_CD
		                                AND ST.OPT_CD = SD.OPT_CD
		                           )
		                    </if>
		                    WHERE 1=1
		                    AND G.GOODS_TYPE = 'G056_N' /*일반상품만 조회*/
		                    <if test="searchGb == null or searchGb =='BASIC'">
		                    <include refid="getGoodsListCondition_sql"/>
		                    </if>
		              ) X
		        WHERE 1 = 1
		        <if test="searchGb == null or searchGb =='BASIC'">
		            <if test="stockQtySt != null and stockQtySt != ''">
		        AND X.CURR_STOCK_QTY >= #{stockQtySt}
		            </if>
		            <if test="stockQtyEd != null and stockQtyEd != ''">
		            <![CDATA[
		        AND X.CURR_STOCK_QTY <= #{stockQtyEd}
		            ]]>
		            </if>
		        ORDER BY X.GOODS_CD, X.REG_DT DESC
		        </if>
		<include refid="getListLimitPagingCondition_sql"/>
	</sql>

	<!-- 상품 사이즈별 품절여부 변경 -->
	<update id="updateStockSoldOut" parameterType="Option">
		/* TsaStock.updateStockSoldOut */
		UPDATE TB_OPTION
		SET BASE_STOCK_QTY = #{baseStockQty}
		  , UPD_NO = #{updNo}
		  , UPD_DT = NOW()
		  , SOLDOUT_YN = #{soldoutYn}
		WHERE GOODS_CD = #{goodsCd}
		AND OPT_CD = #{optCd}
	</update>
	
	<!-- 상품 사이즈별 품절여부 변경2 -->
	<update id="updateStockSoldOut2" parameterType="Option">
		/* TsaStock.updateStockSoldOut2 */
		UPDATE TB_OPTION
		SET UPD_NO = #{updNo}
		  , UPD_DT = NOW()
		  , SOLDOUT_YN = 'Y'
		WHERE GOODS_CD = #{goodsCd}
		AND OPT_CD = #{optCd}
	</update>

</mapper>