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

	<!--포인트 적립 생성-->
	<insert id="createCustomerPoint" parameterType="Point" keyProperty="custPntSq">
		/*TscPoint.createCustomerPoint*/
		INSERT INTO TB_CUST_POINT (
		       CUST_NO
		     , GV_PNT_AMT
		     , US_PNT_AMT
		     , RM_PNT_AMT
		     , EXP_BE_DT
		     , EXP_CMP_DT
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		) VALUES (
		       #{custNo}
		     , #{gvPntAmt}
		     , #{usPntAmt}
		     , #{rmPntAmt}
		     <if test="expBeDt != null and expBeDt != ''">
		         <choose>
		            <when test='signGb == "-"'>
		     , NOW()
		            </when>
		            <otherwise>
		     , DATE_FORMAT(#{expBeDt}, '%Y%m%d%H%i%S')
		            </otherwise>
		     </choose>
		     </if>
		     , #{expCmpDt}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
	</insert>

	<!--포인트 이력 생성-->
	<insert id="createCustomerPointHst" parameterType="Point" keyProperty="pntHstSq">
		/*TscPoint.createCustomerPointHst*/
		INSERT INTO TB_CUST_POINT_HST (
		       CUST_NO
		     , OCCUR_GB
		     , OCCUR_DTL_DESC
		     , PNT_AMT
		     , CUST_PNT_SQ
		     , ORD_NO
		     , ORD_DTL_NO
		     , REVIEW_SQ
		     , SWITCH_DUE_DT
		     , PNT_UPLOAD_STAT
		     , PNT_UPLOAD_DT
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		) VALUES (
		       #{custNo}
		     , #{occurGb}
		     , #{occurDtlDesc}
		     , #{pntAmt}
		     , #{custPntSq}
		     , #{ordNo}
		     , #{ordDtlNo}
		     , #{reviewSq}
		     <if test='pntUploadStat == "G070_30"'>
		     , NOW()                                -- SWITCH_DUE_DT
		     </if>
		     <if test='pntUploadStat != "G070_30"'>
		     , DATE_FORMAT(#{switchDueDt}, '%Y%m%d%H%i%S') -- SWITCH_DUE_DT
		     </if>
		     , #{pntUploadStat}
		     <if test='pntUploadStat == "G070_30"'>
		     , NOW()                                -- PNT_UPLOAD_DT
		     </if>
		     <if test='pntUploadStat != "G070_30"'>
		     , NULL                                 -- PNT_UPLOAD_DT
		     </if>
		     , #{regNo}
		     , NOW()                                -- REG_DT
		     , #{updNo}
		     , NOW()                                -- UPD_DT
		)
	</insert>

	<!-- 구매확정 포인트 생성 -->
	<insert id="createOrderDecisionPoint" parameterType="Point" keyProperty="custPntSq">
		/* TscPoint.createOrderDecisionPoint */
		INSERT INTO TB_CUST_POINT (
		       CUST_NO
		     , GV_PNT_AMT
		     , US_PNT_AMT
		     , RM_PNT_AMT
		     , EXP_BE_DT
		     , EXP_CMP_DT
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT CUST_NO
			 , SUM(PNT_AMT) AS GV_POINT_AMT
			 , 0            AS US_PNT_AMT
			 , SUM(PNT_AMT) AS RM_POINT_AMT
			 , CONCAT(CURRENT_DATE + INTERVAL #{buyPointExpireDays} DAY, ' 23:59:59') AS EXP_BE_DT
			 , NULL         AS EXP_CMP_DT
			 , #{regNo}     AS REG_NO
			 , NOW()        AS REG_DT
			 , #{updNo}     AS UPD_NO
			 , NOW()        AS UPD_DT
		FROM  TB_CUST_POINT_HST
		WHERE CUST_NO = #{custNo}
		AND   ORD_NO = #{ordNo}
		AND   ORD_DTL_NO = #{ordDtlNo}
		AND   OCCUR_GB IN ('G069_12', 'G069_13')  -- 예정포인트적립
		AND   PNT_UPLOAD_STAT = 'G070_10'         -- 반영예정
	</insert>
	
	<!-- 구매확정 포인트 이력 수정 -->
	<update id="updateOrderDecisionPointHst" parameterType="Point">
		/* TscPoint.updateOrderDecisionPoint */
		UPDATE TB_CUST_POINT_HST
		SET    CUST_PNT_SQ = #{custPntSq}
		<if test="reviewSq != null and reviewSq != ''">
		     , REVIEW_SQ = #{reviewSq}
		</if>
		     , SWITCH_DUE_DT = NOW()
		     , PNT_UPLOAD_STAT = 'G070_30'
		     , PNT_UPLOAD_DT = NOW()
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  CUST_NO = #{custNo}
		AND    ORD_NO = #{ordNo}
		AND    ORD_DTL_NO = #{ordDtlNo}
		AND    OCCUR_GB IN ('G069_12', 'G069_13')
		AND    PNT_UPLOAD_STAT = 'G070_10'
	</update>
	
	<!-- 구매확정 포인트 조회 -->
	<select id="getOrderDecisionPoint" parameterType="Point" resultType="int">
		/* TscPoint.getOrderDecisionPoint */
		SELECT GV_PNT_AMT
		  FROM TB_CUST_POINT
		 WHERE CUST_PNT_SQ = #{custPntSq}
	</select>

	<!-- 구매확정 포인트 전환 가능 여부 조회 -->
	<select id="getOrderDecisionPointHstCount" parameterType="Point" resultType="int">
		/* TscPoint.getOrderDecisionPointHstCount */
		SELECT COUNT(*)
		  FROM TB_CUST_POINT_HST
		 WHERE CUST_NO = #{custNo}
		   AND ORD_NO = #{ordNo}
		   AND ORD_DTL_NO = #{ordDtlNo}
		   AND OCCUR_GB IN ('G069_12', 'G069_13')
		   AND PNT_UPLOAD_STAT = 'G070_10'
	</select>

	<!-- 가용포인트 리스트 -->
	<select id="getCustomerRmPntAmtList" parameterType="Integer" resultType="Point">
		/* TscPoint.getCustomerRmPntAmtList */
		SELECT CP.CUST_PNT_SQ
		     , CP.RM_PNT_AMT
		     , CP.EXP_BE_DT
		FROM   TB_CUST_POINT CP
		WHERE  CP.CUST_NO = #{custNo}
		AND    CP.EXP_BE_DT > NOW()
		AND    CP.EXP_CMP_DT IS NULL
		AND    CP.RM_PNT_AMT > 0
		ORDER BY CP.EXP_BE_DT, CP.CUST_PNT_SQ
	</select>

	<!--포인트 차감 -->
	<update id="updatePointDeduction" parameterType="Point">
		/* TscPoint.updatePointDeduction */
		UPDATE TB_CUST_POINT
		SET    RM_PNT_AMT = RM_PNT_AMT + #{pntAmt}
		     , US_PNT_AMT = US_PNT_AMT - #{pntAmt}
		     , UPD_DT = NOW()
		     , UPD_NO = #{updNo}
		WHERE  CUST_PNT_SQ = #{custPntSq}
	</update>

	<!-- 가용포인트 합산 -->
	<select id="getCustomerSumRmPntAmt" parameterType="Integer" resultType="int">
		/* TscPoint.getCustomerSumRmPntAmt */
		SELECT IFNULL(SUM(CP.RM_PNT_AMT), 0) AS RM_PNT_AMT
		FROM   TB_CUST_POINT CP
		WHERE  CP.CUST_NO = #{custNo}
		AND    CP.EXP_BE_DT > NOW()
		AND    CP.EXP_CMP_DT IS NULL
	</select>

</mapper>
