<?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.rent.service.order.mapper.ServiceCouponMapper">
	
	<!-- list -->
	<select id="list" parameterType="ServiceCouponInfo" resultType="ServiceCouponInfo">
		SELECT
			*
		FROM
			TB_IAMBERRY_RENT_SERVICE_COUPON
		<where>
			<if test="serviceId != null">
				SERVICE_ID = #{serviceId}
			</if>
			<if test="serviceStatus != null">
				AND SERVICE_STATUS = #{serviceStatus}
			</if>
			<if test="serviceOpenId != null">
				AND SERVICE_OPENID = #{serviceOpenId}
			</if>
		</where>
		<if test="serviceId != null">
			LIMIT 0, 1
		</if>
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     LIMIT ${page.recordBegin},${page.pageSize}
	    </if>
	</select>
	
	<!-- 修改服务费抵扣券信息 -->
	<update id="update" parameterType="ServiceCouponInfo">
		UPDATE
			TB_IAMBERRY_RENT_SERVICE_COUPON 
		<set>
			<if test="serviceName != null">
				SERVICE_NAME = #{serviceName}, 
			</if>
			<if test="serviceOpenId != null">
				SERVICE_OPENID = #{serviceOpenId}, 
			</if>
			<if test="serviceMoney != null">
				SERVICE_MONEY = #{serviceMoney}, 
			</if>
			<if test="serviceStatus != null">
				SERVICE_STATUS = #{serviceStatus}
			</if>
		</set>
		<where>
			SERVICE_ID = #{serviceId}
			<if test="serviceOpenId != null">
				AND SERVICE_OPENID = #{serviceOpenId}
			</if>
			<if test="serviceOldStatus != null and serviceOldStatus != ''">
				AND SERVICE_STATUS = #{serviceOldStatus} 
			</if>
		</where>
	</update>
	
	<!-- 添加服务费抵扣券信息 -->
	<insert id="saveServiceCoupon" parameterType="ServiceCouponInfo" useGeneratedKeys="true" keyProperty="serviceId">
		INSERT INTO TB_IAMBERRY_RENT_SERVICE_COUPON
		(
			SERVICE_NAME,SERVICE_OPENID,SERVICE_MONEY,SERVICE_START_TIME,
			SERVICE_END_TIME,SERVICE_STATUS,SERVICE_CREATE_TIME
		) 
		VALUES 
		(
			#{serviceName},#{serviceOpenId},#{serviceMoney},#{serviceStartTime},
			#{serviceEndTime},#{serviceStatus},NOW()
		)
	</insert>
	
	<!-- 添加押金抵扣券 -->
	<insert id="savePledgeCoupon" parameterType="PledgeCouponInfo" useGeneratedKeys="true" keyProperty="pledgeId">
		INSERT INTO TB_IAMBERRY_RENT_PLEDGE_COUPON
		(
			PLEDGE_NAME,PLEDGE_OPENID,PLEDGE_MONEY,PLEDGE_START_TIME,
			PLEDGE_END_TIME,PLEDGE_STATUS,PLEDGE_CREATE_TIME
		) 
		VALUES 
		(
			#{pledgeName},#{pledgeOpenId},#{pledgeMoney},#{pledgeStartTime},
			#{pledgeEndTime},#{pledgeStatus},NOW()
		)
	</insert>
	
	<!-- 根据条件查询押金提取券信息 -->
	<select id="listPledgeCoupon" parameterType="PledgeCouponInfo" resultType="PledgeCouponInfo">
		SELECT
		 	* 
		FROM 
			TB_IAMBERRY_RENT_PLEDGE_COUPON 
		<where>
			<if test="pledgeOpenId != null and pledgeOpenId != ''">
				PLEDGE_OPENID = #{pledgeOpenId} 
			</if>
			<if test="pledgeStatus != null and pledgeStatus != ''">
				AND PLEDGE_STATUS = #{pledgeStatus} 
			</if>
			<if test="pledgeId != null and pledgeId != ''">
				AND pledge_id = #{pledgeId}
			</if>
		</where>
	</select>
	
	<!-- 批量修改服务费抵扣券状态 -->
 	<update id="updateServiceCouponList" parameterType="java.util.List">
 		UPDATE 
			TB_IAMBERRY_RENT_SERVICE_COUPON 
		SET 
			SERVICE_STATUS = 1 
		WHERE 
			SERVICE_ID  IN (
				<foreach collection="list" item="item" separator=",">
					#{item} 
		        </foreach>) 
		 AND 
		 	SERVICE_STATUS = 2 
 	</update>

	<!-- 修改押金提取券状态 -->
	<update id="updatePledgeCouponStatus" parameterType="PledgeCouponInfo">
		UPDATE
		TB_IAMBERRY_RENT_PLEDGE_COUPON
		<set>
			<if test="pledgeStatus != null">
				pledge_status = #{pledgeStatus}
			</if>
		</set>
		<where>
			pledge_id = #{pledgeId}
		</where>
	</update>
</mapper>