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

	<!-- 상품별 전시카테고리 삭제-->
	<delete id="deleteGoodsCategory" parameterType="GoodsCategory">
		/* TssDisplay.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>

	<!-- 카테고리 목록 -->
	<select id="getCategoryList" parameterType="Category" resultType="Category">
		/* TsaDisplay.getCategoryList */
		<if test='selLvl != null and selLvl != ""'>
			SELECT SITE_CD                 /*사이트코드*/
			, CATE_GB                 /*카테고리구분*/
			<if test='selLvl == 1'>
				, CATE1_NO
				, NULL     AS CATE2_NO
				, NULL     AS CATE3_NO
				, NULL     AS CATE4_NO
				, NULL     AS CATE5_NO
				, CATE1_NO AS CATE_NO     /*카테고리번호*/
				, CATE1_NM AS CATE_NM     /*카테고리명*/
			</if>
			<if test='selLvl == 2'>
				, CATE2_NO
				, NULL     AS CATE3_NO
				, NULL     AS CATE4_NO
				, NULL     AS CATE5_NO
				, CATE2_NO AS CATE_NO     /*카테고리번호*/
				, CATE2_NM AS CATE_NM     /*카테고리명*/
			</if>
			<if test='selLvl == 3'>
				, CATE2_NO
				, CATE3_NO
				, NULL     AS CATE4_NO
				, NULL     AS CATE5_NO
				, CATE3_NO AS CATE_NO     /*카테고리번호*/
				, CATE3_NM AS CATE_NM     /*카테고리명*/
			</if>
			<if test='selLvl == 4'>
				, CATE2_NO
				, CATE3_NO
				, CATE4_NO
				, NULL     AS CATE5_NO
				, CATE4_NO AS CATE_NO     /*카테고리번호*/
				, CATE4_NM AS CATE_NM     /*카테고리명*/
			</if>
			<if test='selLvl == 5'>
				, CATE2_NO
				, CATE3_NO
				, CATE4_NO
				, CATE5_NO
				, CATE5_NO AS CATE_NO     /*카테고리번호*/
				, CATE5_NM AS CATE_NM     /*카테고리명*/
			</if>
			, CATE_TYPE               /*카테고리유형*/
			, LEAF_YN                 /*단말여부*/
			, DISP_ORD                /*노출순서*/
			, FORMAL_GB               /*정상이월구분*/
			, CONTENTS_LOC            /*컨텐츠위치*/
			, DISP_YN                 /*전시여부*/
			, USE_YN                  /*사용여부*/
			<if test='selLvl == 1'>
				FROM   TB_CATE1
				WHERE  SITE_CD = #{siteCd}
				AND    CATE_GB = #{cateGb}
			</if>
			<if test='selLvl == 2'>
				FROM   TB_CATE2
				WHERE  SITE_CD = #{siteCd}
				AND    CATE_GB = #{cateGb}
				AND    CATE1_NO = #{cate1No}
			</if>
			<if test='selLvl == 3'>
				FROM   TB_CATE3
				WHERE  SITE_CD = #{siteCd}
				AND    CATE_GB = #{cateGb}
				AND    CATE1_NO = #{cate1No}
				AND    CATE2_NO = #{cate2No}
			</if>
			<if test='selLvl == 4'>
				FROM   TB_CATE4
				WHERE  SITE_CD = #{siteCd}
				AND    CATE_GB = #{cateGb}
				AND    CATE1_NO = #{cate1No}
				AND    CATE2_NO = #{cate2No}
				AND    CATE3_NO = #{cate3No}
			</if>
			<if test='selLvl == 5'>
				FROM   TB_CATE5
				WHERE  SITE_CD = #{siteCd}
				AND    CATE_GB = #{cateGb}
				AND    CATE1_NO = #{cate1No}
				AND    CATE2_NO = #{cate2No}
				AND    CATE3_NO = #{cate3No}
				AND    CATE4_NO = #{cate4No}
			</if>
			ORDER  BY DISP_ORD
		</if>
	</select>
	
</mapper>