<?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.core.biz.dao.TscLookbookDao">

	<!-- 룩북 저장/수정 -->
	<insert id="saveLookbook" parameterType="Lookbook" keyProperty="lookbookSq">
		/* TscLookbook.saveLookbook */
		INSERT INTO  TB_LOOKBOOK(
		    LOOKBOOK_SQ
		    ,BRAND_CD
		    ,TITLE
		    ,ORG_TNFILE_NM
		    ,SYS_TNFILE_NM
		    ,DISP_STDT
		    ,DISP_EDDT
		    ,DISP_YN
		    ,DISP_ORD
		    ,MAIN_DISP_YN
		    ,REG_NO
		    ,REG_DT
		    ,UPD_NO
		    ,UPD_DT
		) VALUES (
		    #{lookbookSq}
		    ,#{brandCd}
		    ,#{title}
		    ,#{orgTnfileNm}
		    ,#{sysTnfileNm}
		    ,#{dispStdt}
		    ,#{dispEddt}
		    ,#{dispYn}
		    ,#{dispOrd}
		    ,#{mainDispYn}
		    ,#{regNo}
		    ,NOW()
		    ,#{updNo}
		    ,NOW()
		) ON DUPLICATE KEY UPDATE
		    BRAND_CD = #{brandCd}
		    ,TITLE = #{title}
		    ,ORG_TNFILE_NM = #{orgTnfileNm}
		    ,SYS_TNFILE_NM = #{sysTnfileNm}
		    ,DISP_STDT = #{dispStdt}
		    ,DISP_EDDT = #{dispEddt}
		    ,DISP_YN = #{dispYn}
		    ,DISP_ORD = #{dispOrd}
		    ,MAIN_DISP_YN = #{mainDispYn}
		    ,UPD_NO = #{updNo}
		    ,UPD_DT = NOW()
	</insert>
	
	<!-- 룩북 리스트 카운트 -->
	<select id="getLookbookListCount" parameterType="Lookbook" resultType="int">
		/* TscLookbook.getLookbookListCount */
		SELECT	COUNT(*) AS TOTCNT
		FROM	TB_LOOKBOOK
		WHERE	1=1
		<if test='frontYn != null and frontYn != ""'>
		AND DISP_YN = 'Y'
		AND NOW() BETWEEN DISP_STDT AND DISP_EDDT
		</if>
		<if test="dispStdt != null and dispStdt !=''">
		<![CDATA[
		AND DISP_STDT >= DATE_FORMAT(CONCAT(#{dispStdt},#{dispStTime}),'%Y-%m-%d %H:%i:%S')
		]]>
		</if>
		<if test="dispEddt != null and dispEddt !=''">
		<![CDATA[
		AND DISP_EDDT <= DATE_FORMAT(CONCAT(#{dispEddt},#{dispEdTime}),'%Y-%m-%d %H:%i:%S')
		]]>
		</if>
		<if test='brandCd != null and brandCd != ""'>
		AND	BRAND_CD = #{brandCd}
		</if>
		<if test='frontGb != null and frontGb != ""'>
		AND NOW() BETWEEN DISP_STDT AND DISP_EDDT
		</if>
		<if test="multiBrandCd!= null and multiBrandCd.length>0">
		AND BRAND_CD IN
		<foreach collection="multiBrandCd" item="item" open="(" separator="," close=")">
		    #{item}
		</foreach>
		</if>
	</select>

	<!-- 룩북 리스트 -->
	<select id="getLookbookList" parameterType="Lookbook" resultType="Lookbook">
		/* TscLookbook.getLookbookList */
		SELECT Z.*
		FROM(
		    SELECT  A.* -- @rownum := @rownum + 1 AS RNUM
		    FROM (
		         SELECT	LOOKBOOK_SQ
		            ,BRAND_CD
		            ,FN_GET_BRAND_GROUP_NM(IFNULL(NULLIF(BRAND_CD, ''), 0)) AS BRAND_NM
		            ,TITLE
		            ,ORG_TNFILE_NM
		            ,SYS_TNFILE_NM
		            ,DISP_STDT
		            ,DISP_EDDT
		            ,DISP_YN
		            ,DISP_ORD
		            ,MAIN_DISP_YN
		            ,REG_NO
		            , FN_GET_USER_NM(REG_NO) AS REG_NM
		            ,REG_DT
		            ,UPD_NO
		            , FN_GET_USER_NM(UPD_NO) AS UPD_NM
		            ,UPD_DT
		       FROM	TB_LOOKBOOK
		       WHERE	1=1
		       <if test='frontYn != null and frontYn != ""'>
		       	AND DISP_YN = 'Y'
				   AND NOW() BETWEEN DISP_STDT AND DISP_EDDT
		       </if>
		       <if test="dispStdt != null and dispStdt !=''">
		       <![CDATA[
		       AND DISP_STDT >= DATE_FORMAT(CONCAT(#{dispStdt},#{dispStTime}),'%Y-%m-%d %H:%i:%S')
		       ]]>
		       </if>
		       <if test="dispEddt != null and dispEddt !=''">
		       <![CDATA[
		       AND DISP_EDDT <= DATE_FORMAT(CONCAT(#{dispEddt},#{dispEdTime}),'%Y-%m-%d %H:%i:%S')
		        ]]>
		       </if>
		       <if test='brandCd != null and brandCd != ""'>
		       AND	BRAND_CD = #{brandCd}
		       </if>
		       <if test='frontGb != null and frontGb != ""'>
		       AND NOW() BETWEEN DISP_STDT AND DISP_EDDT
		       </if>
		       <if test="multiBrandCd!= null and multiBrandCd.length>0">
		       AND BRAND_CD IN
		       <foreach collection="multiBrandCd" item="item" open="(" separator="," close=")">
		       	#{item}
		       </foreach>
		       </if>
		       ORDER BY REG_DT DESC
			<include refid="getListLimitPagingCondition_sql"/>
	</select>

	<select id="lookbookBrandList" parameterType="Lookbook" resultType="Lookbook">
		/* TscLookbook.getLookbookList */
		SELECT   BRAND_CD
		       , FN_GET_BRAND_GROUP_NM(IFNULL(NULLIF(BRAND_CD, ''), 0)) AS BRAND_NM
		FROM  TB_LOOKBOOK
		WHERE 1=1
		<if test='frontYn != null and frontYn != ""'>
		  AND DISP_YN = 'Y'
		  AND NOW() BETWEEN DISP_STDT AND DISP_EDDT
		</if>
		GROUP BY BRAND_CD
	</select>

	<sql id="getListPagingCondition_sql">
		<choose>
			<when test="pageable != null">
				) A
				)Z
				WHERE RNUM BETWEEN  #{pageable.startRow} AND #{pageable.endRow}
			</when>
			<otherwise>
				) A
				)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>

	<!-- 룩북 노출안함 처리 -->
	<update id="deleteLookbook" parameterType="Lookbook">
		/* TscLookbook.deleteLookbook */
		UPDATE	TB_LOOKBOOK
		SET 	DISP_YN = 'N'
		WHERE	LOOKBOOK_SQ = #{lookbookSq}
	</update>

	<!-- 룩북 배너 저장 -->
	<insert id="saveLookbookBanner" parameterType="LookbookBanner" keyProperty="lookbookbSq">
		/* TscLookbook.saveLookbookBanner */
		INSERT INTO TB_LOOKBOOK_BANNER (
		    LOOKBOOKB_SQ
		    ,LOOKBOOK_SQ
		    ,ORG_FILE_NM
		    ,SYS_FILE_NM
		    ,IMG_DESC
		    ,DISP_YN
		    ,DISP_ORD
		    ,REG_NO
		    ,REG_DT
		    ,UPD_NO
		    ,UPD_DT
		) VALUES (
		    #{lookbookbSq}
		    ,#{lookbookSq}
		    ,#{orgFileNm}
		    ,#{sysFileNm}
		    ,#{imgDesc}
		    ,'Y'
		    ,#{dispOrd}
		    ,#{regNo}
		    ,NOW()
		    ,#{updNo}
		    ,NOW()
		) ON DUPLICATE KEY UPDATE
		     ORG_FILE_NM = #{orgFileNm}
		    ,SYS_FILE_NM = #{sysFileNm}
		    ,IMG_DESC = #{imgDesc}
		    ,DISP_YN = #{dispYn}
		    ,DISP_ORD = #{dispOrd}
		    ,UPD_NO = #{updNo}
		    ,UPD_DT = NOW()
	</insert>

	<!-- 룩북 마스터 정보 조회 -->
	<select id="getLookbookMstInfo" parameterType="Integer" resultType="Lookbook">
		/* TscLookbook.getLookbookMstInfo */
		SELECT	  LOOKBOOK_SQ
		         ,BRAND_CD
		         ,TITLE
		         ,ORG_TNFILE_NM
		         ,SYS_TNFILE_NM
		         ,DISP_STDT
		         ,DISP_EDDT
		         ,DISP_YN
		         ,DISP_ORD
		         ,MAIN_DISP_YN
		         ,REG_NO
		         , FN_GET_USER_NM(REG_NO) AS REG_NM
		         ,REG_DT
		         ,UPD_NO
		         , FN_GET_USER_NM(UPD_NO) AS UPD_NM
		         ,UPD_DT
		FROM	TB_LOOKBOOK
		WHERE	1=1
		AND 	LOOKBOOK_SQ = #{lookbookSq}
	</select>

	<!-- 룩북 배너 리스트 -->
	<select id="getLookbookBannerDetailList" parameterType="Integer" resultType="LookbookBanner">
		/* TscLookbook.getLookbookBannerDetailList */
		SELECT	LOOKBOOKB_SQ
		       ,LOOKBOOK_SQ
		       ,ORG_FILE_NM
		       ,SYS_FILE_NM
		       ,IMG_DESC
		       ,DISP_YN
		       ,DISP_ORD
		FROM	TB_LOOKBOOK_BANNER
		WHERE	1=1
		AND 	LOOKBOOK_SQ = #{lookbookSq}
		AND DISP_YN = 'Y'
	</select>

	<!-- 룩북 배너 삭제 -->
	<update id="deleteLookbookBanner" parameterType="Integer">
		/* TscLookbook.deleteLookbookBanner */
		UPDATE	TB_LOOKBOOK_BANNER
		   SET  DISP_YN = 'N'
		WHERE	LOOKBOOK_SQ = #{lookbookSq}
	</update>

	<!-- 룩북 배너 피커 저장 -->
	<insert id="saveLookbookPicker" parameterType="LookbookGoods">
		/* TscLookbook.saveLookbookPicker */
		INSERT INTO TB_LOOKBOOK_GOODS (
		    LOOKBOOKB_SQ
		    ,GOODS_CD
		    ,XLIM
		    ,YLIM
		    ,DISP_ORD
		    ,DEL_YN
		    ,REG_NO
		    ,REG_DT
		    ,UPD_NO
		    ,UPD_DT
		) VALUES (
		    #{lookbookbSq}
		    ,#{goodsCd}
		    ,#{xlim}
		    ,#{ylim}
		    ,( SELECT	IFNULL((MAX(DISP_ORD) + 1), 1) AS DISP_ORD
		        FROM	TB_LOOKBOOK_GOODS ALIAS_FOR_SUBQUERY
		       WHERE	LOOKBOOKB_SQ = #{lookbookbSq}
		    )
		    ,#{delYn}
		    ,#{regNo}
		    ,NOW()
		    ,#{updNo}
		    ,NOW()
		)
	</insert>

	<!-- 룩북 배너 피커 리스트 -->
	<select id="getLookbookBannerPickerList" parameterType="LookbookGoods" resultType="LookbookGoods">
		/* TscLookbook.getLookbookBannerPickerList */
		SELECT	LOOKBOOKB_SQ
		       ,GOODS_CD
		       ,XLIM
		       ,YLIM
		       ,DISP_ORD
		       ,DEL_YN
		       ,REG_NO
		       ,REG_DT
		       ,UPD_NO
		       ,UPD_DT
		FROM	TB_LOOKBOOK_GOODS
		WHERE	1=1
		AND 	LOOKBOOKB_SQ = #{lookbookbSq}
		AND 	DEL_YN = 'N'
		ORDER BY LOOKBOOKB_SQ , DISP_ORD
	</select>

	<!-- 룩북 배너 피커 삭제 -->
	<delete id="deleteLookbookPicker" parameterType="Integer">
		/* TscLookbook.deleteLookbookPicker */
		DELETE	FROM	TB_LOOKBOOK_GOODS
		WHERE	LOOKBOOKB_SQ = #{lookbookbSq}
	</delete>

	<!-- 룩북 메인 전시 여부 업데이트 -->
	<update id="deleteLookbookMainDispYn" parameterType="Lookbook">
		/* TscLookbook.deleteLookbookMainDispYn */
		UPDATE	TB_LOOKBOOK
		   SET MAIN_DISP_YN = 'N'
		WHERE	BRAND_CD = #{brandCd}
	</update>

	<!-- 룩북 메인 전시 여부 업데이트 -->
	<update id="updateLookbookMainDispYn" parameterType="Lookbook">
		/* TscLookbook.updateLookbookMainDispYn */
		UPDATE	TB_LOOKBOOK
		SET MAIN_DISP_YN = 'Y'
		WHERE	LOOKBOOK_SQ = #{lookbookSq}
		  AND	BRAND_CD = #{brandCd}
	</update>

	<!-- front 영역-->
	
	<!-- 룩북 베너 목록 조회 -->
	<select id="getLookbookBannerList" parameterType="Lookbook" resultType="Lookbook">
		/* TscLookbook.getLookbookBannerList */
		SELECT A.LOOKBOOK_SQ
		     , A.BRAND_CD
		     ,FN_GET_BRAND_GROUP_NM(IFNULL(NULLIF(BRAND_CD, ''), 0)) AS BRAND_NM
		     , A.TITLE
		     , B.LOOKBOOKB_SQ
		     , B.SYS_FILE_NM
		     , B.IMG_DESC
		FROM TB_LOOKBOOK A
		INNER JOIN TB_LOOKBOOK_BANNER B ON A.LOOKBOOK_SQ = B.LOOKBOOK_SQ 
		                              AND B.DISP_YN = 'Y'
		                              <if test='goodsCd != null and goodsCd != ""'>
		                              AND B.LOOKBOOKB_SQ IN (SELECT LOOKBOOKB_SQ FROM TB_LOOKBOOK_GOODS WHERE  GOODS_CD = #{goodsCd} AND DEL_YN  ='N')
		                              </if>
		                              <if test='brandCd != null and brandCd != ""'>
		                              AND A.BRAND_CD = #{brandCd}
		                              </if>
		                              <if test='mainDispYn != null and mainDispYn != ""'>
		                              AND A.MAIN_DISP_YN = #{mainDispYn}
		                              </if>
		                              <if test='lookbookSq != null and lookbookSq != ""'>
		                              AND A.LOOKBOOK_SQ = #{lookbookSq}
		                              </if>
		WHERE NOW() BETWEEN A.DISP_STDT AND A.DISP_EDDT
		AND A.DISP_YN  = 'Y'
		<choose>
		    <when test='brandCd != null and brandCd != ""'>
		ORDER BY B.LOOKBOOKB_SQ, A.DISP_ORD, B.DISP_ORD
		    </when>
		    <otherwise>
		ORDER BY A.DISP_ORD, B.DISP_ORD
		    </otherwise>
		</choose>
	</select>
	
	<!-- 룩북 베너 상품 목록 조회 -->
	<select id="getLookbookBannerGoodsList" parameterType="Lookbook" resultType="LookbookGoods">
		/* TscLookbook.getLookbookBannerGoodsList */
		WITH TAB_GOODS AS (
		     SELECT A.LOOKBOOK_SQ
		           , B.LOOKBOOKB_SQ
		           , P.GOODS_CD
		           , RANK() OVER(PARTITION BY A.LOOKBOOK_SQ, B.LOOKBOOKB_SQ  ORDER BY P.DISP_ORD ) RNUM
		           , P.XLIM
		           , P.YLIM
		           , G.GOODS_NM
		           , G.GOODS_GB
		           , G.FOREIGN_BUY_YN
		           , G.PARALLEL_IMPORT_YN
		           , G.ORDER_MADE_YN
		           , G.GOODS_STAT
		           , G.GOODS_TYPE
		           , G.SELF_GOODS_YN
		           , G.MIN_ORD_QTY
		           , (CASE WHEN E.DISP_NM_LANG = 'EN' THEN E.BRAND_GROUP_ENM ELSE E.BRAND_GROUP_KNM END) AS BRAND_GROUP_NM
		           , G.LIST_PRICE
		           , FN_GET_BENEFIT_PRICE(#{frontGb},G.GOODS_CD, G.CURR_PRICE,#{custGb}) AS CURR_PRICE
		           , G.MAIN_COLOR_CD
		           , (CASE WHEN W.GOODS_CD IS NULL THEN 'N' ELSE 'Y' END) AS WISH_YN
		      FROM TB_LOOKBOOK A
		      INNER JOIN TB_LOOKBOOK_BANNER B ON A.LOOKBOOK_SQ = B.LOOKBOOK_SQ
		                                      AND B.DISP_YN = 'Y'
		                                      AND B.LOOKBOOKB_SQ = #{lookbookbSq}
		      INNER JOIN TB_LOOKBOOK_GOODS P ON B.LOOKBOOKB_SQ  = P.LOOKBOOKB_SQ
		                                      AND P.DEL_YN = 'N'
		      INNER JOIN TB_GOODS G ON P.GOODS_CD = G.GOODS_CD
		                            AND G.GOODS_STAT = 'G008_90'
		                            AND G.SELF_MALL_YN = 'Y'
		                            AND G.GOODS_TYPE IN ('G056_N','G056_S')  -- 일반,셋트만
		      INNER JOIN TB_BRAND D ON G.BRAND_CD = D.BRAND_CD
		                            AND D.USE_YN = 'Y'
		      INNER JOIN TB_BRAND_GROUP E ON D.BRAND_GROUP_NO = E.BRAND_GROUP_NO
		                                  AND E.USE_YN = 'Y'
		      LEFT OUTER JOIN TB_WISHLIST W ON G.GOODS_CD = W.GOODS_CD
		                                    AND IFNULL(#{custNo}, 0) = W.CUST_NO
		      WHERE NOW() BETWEEN A.DISP_STDT AND A.DISP_EDDT
		      AND A.DISP_YN  = 'Y'
		      AND A.LOOKBOOK_SQ = #{lookbookSq}
		)
		, TAB_GOODS_COMPOSE AS (
		    SELECT A.GOODS_CD
		         , IFNULL(B.COMPS_GOODS_CD, A.GOODS_CD) AS COMPS_GOODS_CD
		    FROM TAB_GOODS A
		    LEFT OUTER JOIN TB_GOODS_COMPOSE B ON A.GOODS_CD = B.GOODS_CD
		)
		, TAB_GOODS_COMPOSE_OPTION AS (
		    SELECT G.GOODS_CD
		         , G.COMPS_GOODS_CD
		         , V.OPT_CD
		         , V.SOLDOUT_YN
		         , GREATEST(V.CURR_STOCK_QTY - V.BASE_STOCK_QTY,0) AS STOCK_QTY
		         , RANK() OVER (PARTITION BY G.COMPS_GOODS_CD ORDER BY V.SOLDOUT_YN,  V.CURR_STOCK_QTY - V.BASE_STOCK_QTY DESC  , V.OPT_CD) AS ROWNUM
		    FROM TAB_GOODS_COMPOSE G
		    INNER JOIN VW_STOCK V ON G.COMPS_GOODS_CD = V.GOODS_CD
		    AND V.DISP_YN = 'Y'
		)
		, TAG_OPTION AS (
		    SELECT GOODS_CD
		         , COMPS_GOODS_CD
		         , SOLDOUT_YN
		         , STOCK_QTY
		         , OPT_CD
		    FROM TAB_GOODS_COMPOSE_OPTION O
		    WHERE O.ROWNUM = 1
		)
		, TAB_OPTION_DISTINCT AS (
		    SELECT O.GOODS_CD
		         , SUM(STOCK_QTY) AS STOCK_QTY
		         , MAX(O.SOLDOUT_YN) AS SOLDOUT_YN
		         , GROUP_CONCAT(CONCAT(O.COMPS_GOODS_CD, ':', O.OPT_CD, ':', O.SOLDOUT_YN) ORDER BY O.COMPS_GOODS_CD SEPARATOR ',') AS OPT_CD_INFO
		    FROM TAG_OPTION O
		    GROUP BY O.GOODS_CD
		)
		SELECT G.LOOKBOOK_SQ
		     , G.LOOKBOOKB_SQ
		     , G.GOODS_CD
		     , G.XLIM
		     , G.YLIM
		     , G.GOODS_STAT
		     , G.GOODS_TYPE
		     , G.SELF_GOODS_YN
		     , G.MIN_ORD_QTY
		     , G.BRAND_GROUP_NM
		     , G.LIST_PRICE
		     , G.CURR_PRICE
		     , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG WHERE GOODS_CD = G.GOODS_CD AND COLOR_CD = IFNULL(G.MAIN_COLOR_CD,'XX') AND DEFAULT_IMG_YN = 'Y') AS SYS_IMG_NM
		     , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG WHERE GOODS_CD = G.GOODS_CD AND COLOR_CD = IFNULL(G.MAIN_COLOR_CD,'XX') AND MOUSEOVER_IMG_YN = 'Y') AS SYS_IMG_NM2
		     , G.WISH_YN
		     , 100 - ROUND((G.CURR_PRICE / G.LIST_PRICE) * 100 ,0) AS DC_RATE
		     , FN_GET_GOODS_NM(G.GOODS_NM,G.GOODS_GB,G.FOREIGN_BUY_YN,G.PARALLEL_IMPORT_YN,G.ORDER_MADE_YN) AS GOODS_FULL_NM /*상품FULL명*/
		     , O.STOCK_QTY
		     , O.SOLDOUT_YN
		     , O.OPT_CD_INFO
		FROM TAB_GOODS G
		   , TAB_OPTION_DISTINCT O
		WHERE G.GOODS_CD = O.GOODS_CD
		ORDER BY G.RNUM
	</select>
	
	<!-- 룩북 베너 상품 목록 조회 -->
	<select id="getLookbookBannerGoodsList_ORG" parameterType="Lookbook" resultType="LookbookGoods">
		/* TscLookbook.getLookbookBannerGoodsList_ORG */
		SELECT Z.*
		     , 100 - ROUND((CURR_PRICE / LIST_PRICE) * 100 ,0) AS DC_RATE
		     , FN_GET_GOODS_NM(GOODS_NM,GOODS_GB,FOREIGN_BUY_YN,PARALLEL_IMPORT_YN,ORDER_MADE_YN) AS GOODS_FULL_NM /*상품FULL명*/
		FROM (
		      SELECT A.LOOKBOOK_SQ
		           , B.LOOKBOOKB_SQ
		           , P.GOODS_CD
		           , RANK() OVER(PARTITION BY A.LOOKBOOK_SQ, B.LOOKBOOKB_SQ  ORDER BY P.DISP_ORD ) RNUM
		           , P.DISP_ORD
		           , P.XLIM
		           , P.YLIM
		           , G.GOODS_NM
		           , G.GOODS_GB
		           , G.FOREIGN_BUY_YN
		           , G.PARALLEL_IMPORT_YN
		           , G.ORDER_MADE_YN
		           , G.GOODS_STAT
		           , G.GOODS_TYPE
		           , G.MIN_ORD_QTY
		           , (CASE WHEN E.DISP_NM_LANG = 'EN' THEN E.BRAND_GROUP_ENM ELSE E.BRAND_GROUP_KNM END) AS BRAND_GROUP_NM
		           , G.LIST_PRICE
		           , FN_GET_BENEFIT_PRICE(#{frontGb},G.GOODS_CD, G.CURR_PRICE,#{custGb}) AS CURR_PRICE
		           , G.MAIN_COLOR_CD
		           , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG WHERE GOODS_CD = G.GOODS_CD AND COLOR_CD = IFNULL(G.MAIN_COLOR_CD,'XX') AND DEFAULT_IMG_YN = 'Y') AS SYS_IMG_NM
		           , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG WHERE GOODS_CD = G.GOODS_CD AND COLOR_CD = IFNULL(G.MAIN_COLOR_CD,'XX') AND MOUSEOVER_IMG_YN = 'Y') AS SYS_IMG_NM2
		           , S.STOCK_QTY
		           , S.SOLDOUT_YN
		           , S.OPT_CD
		         , (CASE WHEN W.GOODS_CD IS NULL THEN 'N' ELSE 'Y' END) AS WISH_YN
		      FROM TB_LOOKBOOK A
		      INNER JOIN TB_LOOKBOOK_BANNER B ON A.LOOKBOOK_SQ = B.LOOKBOOK_SQ
		                                     AND B.DISP_YN = 'Y'
		                                     AND B.LOOKBOOKB_SQ = #{lookbookbSq}
		      INNER JOIN TB_LOOKBOOK_GOODS P ON B.LOOKBOOKB_SQ  = P.LOOKBOOKB_SQ
		                                     AND P.DEL_YN = 'N'
		      INNER JOIN TB_GOODS G ON P.GOODS_CD = G.GOODS_CD
		                           AND G.GOODS_STAT = 'G008_90'
		                           AND G.SELF_MALL_YN = 'Y'
		      INNER JOIN TB_BRAND D ON G.BRAND_CD = D.BRAND_CD
		                           AND D.USE_YN = 'Y'
		      INNER JOIN TB_BRAND_GROUP E ON D.BRAND_GROUP_NO = E.BRAND_GROUP_NO
		                           AND E.USE_YN = 'Y'
		      LEFT OUTER JOIN (SELECT GOODS_CD
		                            , SUM(CASE WHEN SOLDOUT_YN = 'Y' THEN 0 ELSE CURR_STOCK_QTY END) AS STOCK_QTY
		                            , MIN(SOLDOUT_YN) AS SOLDOUT_YN
		                            , MIN(CASE WHEN SOLDOUT_YN = 'N' THEN OPT_CD   ELSE 'XXX' END) AS OPT_CD
		                       FROM VW_STOCK
		                       WHERE DISP_YN = 'Y'
		                       GROUP BY GOODS_CD) S ON G.GOODS_CD = S.GOODS_CD
		      LEFT OUTER JOIN TB_WISHLIST W ON G.GOODS_CD = W.GOODS_CD
		                                   AND IFNULL(#{custNo}, 0) = W.CUST_NO
		      WHERE NOW() BETWEEN A.DISP_STDT AND A.DISP_EDDT
		      AND A.DISP_YN  = 'Y'
		      AND A.LOOKBOOK_SQ = #{lookbookSq}
		) Z
	</select>

	<!-- 다른 룩북 조회 -->
	<select id="getOtherLookbookList" parameterType="Lookbook" resultType="Lookbook">
		/* TscLookbook.getOtherLookbookList */
		SELECT LOOKBOOK_SQ
		     , BRAND_CD
		     , TITLE
		     , SYS_TNFILE_NM AS SYS_FILE_NM
		FROM TB_LOOKBOOK
		WHERE NOW() BETWEEN DISP_STDT AND DISP_EDDT
		AND DISP_YN  = 'Y'
		AND LOOKBOOK_SQ <![CDATA[ <> ]]> #{lookbookSq}
		<if test='lookbookGb != null and lookbookGb == "BL"'>
		     <if test='brandCd != null and brandCd != ""'>
		AND BRAND_CD = #{brandCd}
		     </if>
		</if>
		ORDER BY REG_DT DESC
	</select>

</mapper>
