<?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.AdminCouponItemMapper">
	<sql id="couponItemFiled">
		I.COUPON_ITEM_ID couponItemId,
		I.COUPON_ID couponId,
		I.COUPON_ITEM_USER_OPENID couponItemUserOpenId,
		I.COUPON_USE_END_DATE couponUseEndDate,
		I.COUPON_RECEIVE_DATE couponReceiveDate,
		I.COUPON_USE_STATUS couponUseStatus,
		I.COUPON_USE_DATE couponUseDate,
		I.COUPON_ITEM_REMARK couponItemRemark 
	</sql>

	<select id="getAllCouponItem" parameterType="AdminCouponItemDto" resultType="AdminCouponItemDto">
		SELECT 
			<include refid="couponItemFiled" />,
			T.COUPON_NAME couponName,
			U.USER_NICKNAME userName 
		FROM 
			TB_IAMBERRY_COUPON_ITEM I LEFT JOIN TB_IAMBERRY_COUPON_TYPE T 
			ON I.COUPON_ID = T.COUPON_ID 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON I.COUPON_ITEM_USER_OPENID = U.USER_OPENID 
		<where>
			<if test="couponUseStatus != null and couponUseStatus != ''">
				AND I.COUPON_USE_STATUS = #{couponUseStatus} 
			</if>
			<if test="userName != null and userName != ''">
				AND U.USER_NICKNAME LIKE CONCAT('%',#{userName},'%') OR T.COUPON_NAME LIKE CONCAT('%',#{couponName},'%') 
			</if>
			<if test="couponUseEndDate != null">
				<![CDATA[AND DATE_FORMAT(COUPON_USE_END_DATE, '%Y-%m-%d') >= ]]>DATE_FORMAT(#{couponUseEndDate},'%Y-%m-%d') 
			</if>
		</where>
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="getCouponItemCount" parameterType="AdminCouponItemDto" resultType="Integer">
		SELECT 
			COUNT(I.COUPON_ID) 
		FROM 
			TB_IAMBERRY_COUPON_ITEM I LEFT JOIN TB_IAMBERRY_COUPON_TYPE T 
			ON I.COUPON_ID = T.COUPON_ID 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON I.COUPON_ITEM_USER_OPENID = U.USER_OPENID 
		<where>
			<if test="couponUseStatus != null and couponUseStatus != ''">
				AND I.COUPON_USE_STATUS = #{couponUseStatus} 
			</if>
			<if test="userName != null and userName != ''">
				AND U.USER_NICKNAME LIKE CONCAT('%',#{userName},'%') OR T.COUPON_NAME LIKE CONCAT('%',#{couponName},'%') 
			</if>
			<if test="couponUseEndDate != null">
				<![CDATA[AND DATE_FORMAT(COUPON_USE_END_DATE, '%Y-%m-%d') >= ]]>DATE_FORMAT(#{couponUseEndDate},'%Y-%m-%d') 
			</if>
		</where>
	</select>
	
	<select id="getCouponItemByCouponItemId" parameterType="java.lang.String" resultType="AdminCouponItemDto">
		SELECT 
			<include refid="couponItemFiled" /> 
		FROM 
			TB_IAMBERRY_COUPON_ITEM I 
		WHERE 
			I.COUPON_ITEM_ID = #{0}
	</select>
	
	<update id="updateCouponItem" parameterType="AdminCouponItemDto">
		UPDATE 
			TB_IAMBERRY_COUPON_ITEM 
		SET 
			COUPON_USE_END_DATE = #{couponUseEndDate},
			COUPON_USE_STATUS = #{couponUseStatus} 
		WHERE 
			COUPON_ITEM_ID = #{couponItemId}
	</update>
</mapper>