<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.CouponItemMapper">
 
	<sql id="allField">
		CI.COUPON_ITEM_ID couponItemId,
		CI.COUPON_ID couponId,
		CI.COUPON_ITEM_USER_OPENID useropenid,
		CI.COUPON_USE_END_DATE couponUseEndDate,
		CI.COUPON_RECEIVE_DATE couponReceiveDate,
		CI.COUPON_USE_STATUS couponUseStatus,
		CI.COUPON_USE_DATE couponUseDate,
		CI.COUPON_ITEM_REMARK couponItemRemark,
		
		CT.COUPON_NAME couponName,
 		CT.COUPON_REDUCE couponReduce,
 		CT.COUPON_CONSUME_ENOUGH couponConsumeEnough
	</sql>
 	<!-- 查询优惠券详情列表 分页 -->
 	<select id="getCouponItemDtoList" resultType="CouponItemDto" parameterType="CouponItemDto">
 		SELECT 
 			<include refid="allField"></include>,
 		CT.COUPON_TYPE couponType	
 		FROM TB_IAMBERRY_COUPON_ITEM CI
 		JOIN TB_IAMBERRY_COUPON_TYPE CT ON CI.COUPON_ID=CT.COUPON_ID
		<where>
			<if test="couponId != null and couponId != ''">
				AND CI.COUPON_ID=#{couponId}
			</if>
			<if test="useropenid != null and useropenid != ''">
				AND CI.COUPON_ITEM_USER_OPENID=#{useropenid}
			</if>
			<if test="couponType != null and couponType != '' and couponType=='10' ">
				AND  (CT.coupon_type=1 OR CT.coupon_type=2)
			</if>
			<if test="couponType != null and couponType != '' and couponType=='11' ">
				AND  CT.coupon_type=3
			</if>
			<!-- couponReduce 是将状态是7的优惠券  属于已使用的优惠券-->
			<if test="couponUseStatus != null and couponUseStatus != ''">
				AND CI.COUPON_USE_STATUS IN (#{couponUseStatus},#{couponReduce})
			</if>
			<if test="beginDate != null and beginDate != ''">
				AND CI.COUPON_USE_END_DATE<![CDATA[>]]>#{beginDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND CI.COUPON_USE_END_DATE<![CDATA[<]]>#{endDate}
			</if>
			<!-- 查已过期的优惠券时需去掉已经使用的优惠券 -->
			<if test="couponItemRemark != null and couponItemRemark != ''">
				AND CI.COUPON_USE_STATUS != 2
			</if>
		</where>
		ORDER BY CI.coupon_id,CI.COUPON_RECEIVE_DATE DESC
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     LIMIT ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     LIMIT ${page.pageSize}
	    </if>
 	</select>
 	
 	<!-- 查询优惠券详情总数 -->
 	<select id="getCouponItemCount" resultType="Integer" parameterType="CouponItemDto">
 		SELECT COUNT(COUPON_ITEM_ID) FROM TB_IAMBERRY_COUPON_ITEM
 		<where>
			<if test="couponId != null and couponId != ''">
				COUPON_ID=#{couponId}
			</if>
			<if test="useropenid != null and useropenid != ''">
				AND COUPON_ITEM_USER_OPENID=#{useropenid}
			</if>
			<if test="couponUseStatus != null and couponUseStatus != ''">
				AND COUPON_USE_STATUS=#{couponUseStatus}
			</if>
			<if test="beginDate != null and beginDate != ''">
				AND COUPON_USE_END_DATE<![CDATA[>]]>#{beginDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND COUPON_USE_END_DATE<![CDATA[<]]>#{endDate}
			</if>
		</where>
 	</select>
 	
 	
 	<!-- 添加一张优惠券 -->
 	<insert id="insertCouponItem" parameterType="CouponItem"
 		useGeneratedKeys="true" keyProperty="couponItemId">
 		INSERT INTO TB_IAMBERRY_COUPON_ITEM
		(
			COUPON_ITEM_ID ,
			COUPON_ID ,
			COUPON_ITEM_USER_OPENID ,
			COUPON_USE_END_DATE ,
			COUPON_RECEIVE_DATE ,
			COUPON_USE_STATUS ,
			COUPON_USE_DATE ,
			COUPON_ITEM_REMARK 
		)
		VALUES 
		(
			#{couponItemId},#{couponId},#{couponItemUseropenid},#{couponUseEndDate},
			NOW(),#{couponUseStatus},#{couponUseDate},#{couponItemRemark}
		)
 	</insert>
 	
 	<select id="getCouponItemById" resultType="CouponItemDto" parameterType="String">
 		SELECT 
 			<include refid="allField"></include>
 		FROM TB_IAMBERRY_COUPON_ITEM CI
 		JOIN TB_IAMBERRY_COUPON_TYPE CT ON CI.COUPON_ID=CT.COUPON_ID
		WHERE CI.COUPON_ITEM_ID=#{couponItemId}
 	</select>
 	
 	<!-- 根据id修改 -->
 	<update id="updateCouponItemById" parameterType="CouponItem">
 		UPDATE TB_IAMBERRY_COUPON_ITEM 
		<set>
	        <if test="couponUseEndDate!=null and couponUseEndDate!=''">
				COUPON_USE_END_DATE=#{couponUseEndDate},
	        </if>
	        <if test="couponUseStatus!=null and couponUseStatus!=''">
				COUPON_USE_STATUS=#{couponUseStatus},
	        </if>
	        <if test="couponUseDate!=null and couponUseDate!=''">
				COUPON_USE_DATE=#{couponUseDate},
	        </if>
	        <if test="couponItemRemark!=null and couponItemRemark!=''">
				COUPON_ITEM_REMARK=#{couponItemRemark},
	        </if>
		</set> 
		WHERE COUPON_ITEM_ID=#{couponItemId} 
		<if test="couponItemUseropenid!=null and couponItemUseropenid!=''">
				AND COUPON_ITEM_USER_OPENID=#{couponItemUseropenid}
	    </if>
	    <if test="oldCouponUseStatus!=null and oldCouponUseStatus!=''">
				AND COUPON_USE_STATUS=#{oldCouponUseStatus}
	    </if>
 	</update>
 	
 	<update id="updateCouponItemList" parameterType="java.util.List">
 		<foreach collection="list" item="item" index="index" separator=";" open="" close="">
 			UPDATE TB_IAMBERRY_COUPON_ITEM 
			<set>
	        <if test="item.couponUseEndDate!=null and item.couponUseEndDate!=''">
				COUPON_USE_END_DATE=#{item.couponUseEndDate},
	        </if>
	        <if test="item.couponUseStatus!=null and item.couponUseStatus!=''">
				COUPON_USE_STATUS=#{item.couponUseStatus},
	        </if>
	        <if test="item.couponUseDate!=null and item.couponUseDate!=''">
				COUPON_USE_DATE=#{item.couponUseDate},
	        </if>
	        <if test="item.couponItemRemark!=null and item.couponItemRemark!=''">
				COUPON_ITEM_REMARK=#{item.couponItemRemark},
	        </if>
			</set> 
			WHERE COUPON_ITEM_ID=#{item.couponItemId} 
			<if test="item.couponItemUseropenid!=null and item.couponItemUseropenid!=''">
				AND COUPON_ITEM_USER_OPENID=#{item.couponItemUseropenid}
	    	</if>
	    	<if test="item.oldCouponUseStatus!=null and item.oldCouponUseStatus!=''">
				AND COUPON_USE_STATUS=#{item.oldCouponUseStatus}
	    	</if>
 		</foreach>
 	</update>

	<!-- 查询优惠券详情列表 分页 -->
	<select id="expiredAndNoTake" resultType="CouponItemDto" parameterType="CouponItemDto">
		SELECT
			<include refid="allField"></include>,
		CT.COUPON_TYPE couponType
		FROM TB_IAMBERRY_COUPON_ITEM CI
		JOIN TB_IAMBERRY_COUPON_TYPE CT ON CI.COUPON_ID=CT.COUPON_ID
		<where>
			<if test="couponId != null and couponId != ''">
				AND CI.COUPON_ID=#{couponId}
			</if>
			<if test="couponUseStatus != null">
				AND CI.COUPON_USE_STATUS = #{couponUseStatus}
			</if>
			<if test="beginDate != null and beginDate != ''">
				AND CI.COUPON_USE_END_DATE<![CDATA[>]]>#{beginDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND CI.COUPON_USE_END_DATE<![CDATA[<]]>#{endDate}
			</if>
		</where>
		ORDER BY CI.COUPON_RECEIVE_DATE DESC
	</select>
</mapper>