<?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.front.biz.dao.TsfRecentlyGoodsDao">

	<!-- 최근본상품 총건수 -->
	<select id="getRecentlyGoodsTotalCount"  parameterType="RecentlyGoods" resultType="int">
		/* TsfRecentlyGoods.getRecentlyGoodsTotalCount */
		SELECT COUNT(*)
		FROM   TB_RECENTLY_GOODS
		WHERE  1 = 1
		AND    (
		        JSESSION_ID = #{jsessionId}
		        OR
		        CUST_NO = #{custNo}
		       )
	</select>
	
	<!-- 맨 처음 등록된 최근본상품 삭제 -->
	<delete id="deleteRecentlyFirstGoods"  parameterType="RecentlyGoods">
		/* TsfRecentlyGoods.deleteRecentlyFirstGoods */
		DELETE
		FROM   TB_RECENTLY_GOODS
		WHERE  RGOODS_SQ IN (SELECT * 
		                     FROM (SELECT RGOODS_SQ
		                           FROM   TB_RECENTLY_GOODS
		                           WHERE  1 = 1
		                           AND    (
		                                   JSESSION_ID = #{jsessionId}
		                                   OR
		                                   CUST_NO = #{custNo}
		                                  )
		                           ORDER  BY REG_DT LIMIT 1
		                           ) Z
		                     )
	</delete>
	
	<!-- 최근본상품 저장 -->
	<insert id="createRecentlyGoods" parameterType="RecentlyGoods">
		/* TsfRecentlyGoods.createRecentlyGoods */
		INSERT INTO TB_RECENTLY_GOODS (
		       RGOODS_SQ
		     , GOODS_CD
		     , JSESSION_ID
		     , CUST_NO
		     , REG_DT
		     , UPD_DT
		)
		SELECT NULL
		     , #{goodsCd}
		     , #{jsessionId}
		     , #{custNo}
		     , NOW()
		     , NOW()
		FROM   DUAL
		WHERE  (SELECT COUNT(*)
		        FROM   TB_RECENTLY_GOODS
		        WHERE  1 = 1
		        AND    (
		                (GOODS_CD = #{goodsCd} AND JSESSION_ID = #{jsessionId})
		                OR
		                (GOODS_CD = #{goodsCd} AND CUST_NO = #{custNo})
		               )
		       ) = 0
	</insert>
	
	<!-- 로그인 후 최근본상품 Update -->
	<insert id="updateRecentlyGoodsAfterLogin" parameterType="RecentlyGoods">
		/* TsfRecentlyGoods.updateRecentlyGoodsAfterLogin */
		UPDATE TB_RECENTLY_GOODS
		SET    CUST_NO = #{custNo}
		     , UPD_DT = NOW()
		WHERE  JSESSION_ID = #{jsessionId}
	</insert>
	
	<!-- 최근본상품 목록 -->
	<select id="getRecentlyGoodsList"  parameterType="RecentlyGoods" resultType="RecentlyGoods">
		/* TsfRecentlyGoods.getRecentlyGoodsList */
		SELECT G.GOODS_CD
		     , G.MAIN_COLOR_CD
		     , GI.SYS_IMG_NM
		FROM   (
		        SELECT DISTINCT GOODS_CD
		             , RANK() OVER(ORDER BY UPD_DT DESC) AS DISP_ORD
		        FROM   TB_RECENTLY_GOODS
		        WHERE  1 = 1
		        <choose>
		            <when test="custNo != null and custNo > 0"> <!-- 로그인했으면 -->
		        AND    CUST_NO = #{custNo}
		            </when>
		            <otherwise>
		        AND    JSESSION_ID = #{jsessionId}
		            </otherwise>
		        </choose>
		        LIMIT 30
		       ) A
		     , TB_GOODS G
		     , TB_GOODS_IMG GI
		WHERE  A.GOODS_CD = G.GOODS_CD
		AND    G.GOODS_CD = GI.GOODS_CD
		AND    G.MAIN_COLOR_CD = GI.COLOR_CD
		AND    G.GOODS_STAT = 'G008_90' /*승인완료된상품만*/
		AND    GI.DEFAULT_IMG_YN = 'Y' /*기본이미지만*/
		GROUP  BY G.GOODS_CD, G.MAIN_COLOR_CD, GI.SYS_IMG_NM, A.DISP_ORD
		ORDER  BY A.DISP_ORD
	</select>
	
	<!-- 최근 본 상품 목록 등록 여부 -->
	<select id="getRecentlyGoodsOwnCount" parameterType="RecentlyGoods" resultType="int">
		/* TsfRecentlyGoods.getRecentlyGoodsOwnCount */
		SELECT COUNT(*)
		    FROM   TB_RECENTLY_GOODS
		    WHERE  1 = 1
		    AND    (
		            (GOODS_CD =  #{goodsCd} AND JSESSION_ID =   #{jsessionId})
		            OR
		            (GOODS_CD =  #{goodsCd} AND CUST_NO =  #{custNo})
		           )
	</select>
	
	<update id="updateRecentlyGoods"  parameterType="RecentlyGoods">
		/* TsfRecentlyGoods.updateRecentlyGoods */
		UPDATE TB_RECENTLY_GOODS
		SET    UPD_DT = NOW()
		WHERE  1=1
		AND    GOODS_CD    = #{goodsCd}
		AND    CUST_NO     = #{custNo}
	</update>
	
</mapper>
