<?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.admin.biz.dao.TsaSocialDao">
	<!-- 소셜 저장 -->
	<insert id="saveSocial" parameterType="Social">
		/* TsaSocial.saveSocial */
		INSERT INTO TB_SOCIAL
				(
				  SOCIAL_SQ
				,  SOCIAL_NM
				, SOCIAL_TYPE
				, SITE_CD
				, FRONT_GB
				, SOCIAL_STDT
				, SOCIAL_EDDT
				, SOCIAL_TNM
				, USE_YN
				, APPLY_GB
				, PLAN_SQ
				, REG_NO
				, REG_DT
				, UPD_NO
				, UPD_DT
				)
		VALUES(
		        #{socialSq}
			  ,	#{socialNm}
		      , #{socialType}
		      , #{siteCd}
		      , #{frontGb}
		      , #{socialStdt}
		      , #{socialEddt}
		      , #{socialTnm}
		      , #{useYn}
		      , #{applyGb}
		      , #{planSq}
		      , #{regNo}
		      , now()
		      , #{updNo}
		      , now()
		     )
		   ON
		     DUPLICATE KEY 
		UPDATE 
		   SOCIAL_NM=#{socialNm}
		 , SOCIAL_TYPE=#{socialType}
		 , SITE_CD=#{siteCd}
		 , FRONT_GB=#{frontGb}
		 , SOCIAL_STDT=  #{socialStdt}
		 , SOCIAL_EDDT= #{socialEddt}
		 , SOCIAL_TNM= #{socialTnm}
		 , USE_YN= #{useYn}
		 , APPLY_GB=#{applyGb}
		 , PLAN_SQ=#{planSq}
		 , UPD_NO=#{updNo}
		 , UPD_DT=now()
	</insert>
	
		<!-- 쇼셜 목록 -->
	<select id="getSocialList" parameterType="Social" resultType="Social">
		/* TsaSocial.getSocialList */
		SELECT Z.*
		FROM (
				 SELECT @rownum := @rownum + 1 AS RNUM
				 	         ,A.*
				 	   FROM (
								SELECT SOCIAL_SQ                                                    
								     , SOCIAL_NM                                                    
								     , SOCIAL_TNM                                                   
								     , SOCIAL_TYPE                                                  
								     , SITE_CD                                                      
								     , FRONT_GB                                                     
								     , SOCIAL_STDT													
								     , SOCIAL_EDDT 
								     , PLAN_SQ                                                      
								     , USE_YN  
								     , APPLY_GB 
								     , REG_DT
								     , REG_NO
								     , FN_GET_USER_NM(REG_NO)                AS REG_NM
								FROM   TB_SOCIAL JOIN ( SELECT @rownum := 0) R
								WHERE  1 = 1
								<include refid="getPlanListCondition_sql"/>
								ORDER  BY SOCIAL_SQ DESC
			<include refid="getListPagingCondition_sql"/>
	</select>
	
	<select id="getSocialCount" parameterType="Social" resultType="int">
		/* TsaSocial.getSocialCount */
		SELECT count(*) AS CNT
		FROM TB_SOCIAL	
	</select>
	
	<!-- 소셜 상품 저장 -->
	<insert id="saveSocialGoods" parameterType="Social">
		/* TsaSocial.saveSocialGoods */
		INSERT INTO TB_SOCIAL_GOODS
				(
					SOCIAL_SQ
				  , GOODS_CD
				  , CURR_BPRICE
				  , CURR_APRICE
				  , DC_BRATE
				  , DC_ARATE
				  , PNT_BPRATE
				  , PNT_APRATE
				  , PNT_BMRATE
				  , PNT_AMRATE
				  , ERP_PRICE_LINK_YN
				  , DISP_ORD
				  , DEL_YN
				  , REG_NO
				  , REG_DT
				  , UPD_NO
				  , UPD_DT
				)
			VALUES
				(
				   #{socialSq}
				 , #{goodsCd}
				 , #{currBprice}
				 , #{currAprice}
				 , #{dcBrate}
				 , #{dcArate}
				 , #{pntBprate}
				 , #{pntAprate}
				 , #{pntBmrate}
				 , #{pntAmrate}
				 , 'N'
				 , #{dispOrd}
				 , #{delYn}
				 , #{regNo}
				 , NOW()
				 , #{updNo}
				 , NOW()
				 )
			ON
			    DUPLICATE KEY 
			UPDATE 
			     CURR_BPRICE=#{currBprice}
			   , CURR_APRICE=#{currAprice}
			   , DC_BRATE=#{dcBrate}
			   , DC_ARATE=#{dcArate}
			   , PNT_BPRATE=#{pntBprate} 
			   , PNT_APRATE=#{pntAprate} 
			   , PNT_BMRATE=#{pntBmrate} 
			   , PNT_AMRATE=#{pntAmrate} 
			   , DISP_ORD=#{dispOrd}
			   , DEL_YN=#{delYn}
			   , UPD_NO=#{updNo}
			   , UPD_DT= NOW()
					
	</insert>
	
	<!-- 쇼셜 상품 목록 -->
	<select id="getSocialGoodsList" parameterType="Social" resultType="Social">
		/* TsaSocial.getSocialGoodsList */
		SELECT P.*
		FROM
		(
			SELECT SG.SCGOODS_SQ    
			     , SG.SOCIAL_SQ     
			     , SG.GOODS_CD      
			     , SG.CURR_BPRICE   
			     , SG.CURR_APRICE   
			     , SG.DC_BRATE      
			     , SG.DC_ARATE      
			     , SG.PNT_BPRATE    
			     , SG.PNT_APRATE    
			     , SG.PNT_BMRATE    
			     , SG.PNT_AMRATE    
			     , SG.DISP_ORD      
			     , SG.DEL_YN
			     , G.GOODS_NM 
			     , G.LIST_PRICE
			     , (SELECT MAX(SYS_IMG_NM) FROM TB_GOODS_IMG  WHERE GOODS_CD = G.GOODS_CD AND DEFAULT_IMG_YN = 'Y' ) AS GOODS_IMG
			FROM   TB_SOCIAL_GOODS SG LEFT JOIN TB_GOODS G ON SG.GOODS_CD = G.GOODS_CD 
			WHERE  SG.SOCIAL_SQ = #{socialSq}
			<if test="goodsCd != null and goodsCd != ''">
			AND    SG.GOODS_CD = #{goodsCd}
			</if>
			AND    SG.DEL_YN = 'N'
			ORDER  BY SG.DISP_ORD
		)P
	</select>
	
	<!-- 소셜 상품 삭제 -->
	<update id="deleteSocialGoods" parameterType="Social">
		/* TsaSocial.deleteSocialGoods */
		UPDATE TB_SOCIAL_GOODS
		SET    DEL_YN = #{delYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = now()
		WHERE  SOCIAL_SQ = #{socialSq}
		AND    SCGOODS_SQ = #{scgoodsSq}
		AND    GOODS_CD = #{goodsCd}
	</update>
	
	<!-- 진행중인 소셜 확인 -->
	<select id="getSocialGoOffCount" parameterType="Social" resultType="int">
		SELECT COUNT(*)
		FROM tb_social A
		WHERE 1=1
			AND A.USE_YN = 'Y'
			AND A.APPLY_GB = 'A'
			AND A.SOCIAL_SQ != #{socialSq}
			AND  (A.SOCIAL_STDT <![CDATA[ <= ]]>  DATE_FORMAT(DATE_ADD(#{socialStdt}, INTERVAL 0 DAY), '%Y%m%d%H%i%S')  
			OR A.SOCIAL_EDDT <![CDATA[ >= ]]>  DATE_FORMAT(DATE_ADD(#{socialStdt}, INTERVAL 0 DAY), '%Y%m%d%H%i%S'))  
	</select>
	
	
	
	<sql id="getPlanListCondition_sql">
		<if test ="siteCd != null and siteCd !=''">
		AND    SITE_CD = #{siteCd}
		</if>
		<if test="useYn != null and useYn != ''">
		AND    USE_YN = #{useYn}
		</if>
		<if test="socialSq != null and socialSq != ''">
		AND    SOCIAL_SQ = #{socialSq}
		</if>
		<if test="socialType != null and socialType != ''">
		AND    SOCIAL_TYPE = #{socialType}
		</if>
		<if test='frontGb != null and frontGb != "" and frontGb != "A"'>
			AND (FRONT_GB = 'A' OR FRONT_GB = #{frontGb} )
		</if>
		<if test="stDate != null and stDate != '' and edDate != null and edDate != ''">
        AND    SOCIAL_STDT <![CDATA[ >= ]]> DATE_FORMAT(#{stDate},'%Y-%m-%d %H:%i:%S')
        AND    SOCIAL_STDT <![CDATA[ <= ]]> DATE_FORMAT(#{edDate},'%Y-%m-%d %H:%i:%S')
		</if>
	</sql>
	
	<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>

</mapper>