<?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.LeaseOrderMapper">
	<!-- 保存租用订单信息 -->
	<insert id="saveLeaseOrder" parameterType="LeaseOrder">
			INSERT INTO TB_IAMBERRY_RENT_ORDER
			(
				ORDER_ID,ORDER_OPENID,ORDER_PRODUCT_ID,ORDER_COLOR_ID,ORDER_DEPOSIT,
				ORDER_PRODUCT_PRICE,ORDER_AMOUNT,ORDER_STATUS,ORDER_ADDRESS_INFO,
				ORDER_ADDRESS_POSTNUM,ORDER_ADDRESS_TEL,ORDER_ADDRESS_NAME,
				ORDER_USER_REMARK,ORDER_TRANSACTION_ID,ORDER_TRANSACTION_DATE,
				ORDER_CREATE_DATE,ORDER_RENT_TEL,ORDER_PRODUCT_NAME,
				ORDER_PRODUCT_DISCOUNT,ORDER_PRODUCT_PIC,ORDER_PRODUCT_COLOR
			)
			VALUES 
			(
				#{orderId},#{orderOpenid},#{orderProductId},#{orderColorId},#{orderDeposit},
				#{orderProductPrice},#{orderAmount},#{orderStatus},#{orderAddressInfo},
				#{orderAddressPostnum},#{orderAddressTel},#{orderAddressName},
				#{orderAdminRemark},#{orderTransactionId},#{orderTransactionDate},
				NOW(),#{orderRentTel},#{orderProductName},#{orderProductDiscount},
				#{orderProductPic},#{orderProductColor}
			)
	</insert>
	
	<!-- 支付完成后修改状态和激活码 -->
	<update id="updateLeaseOrderInfo" parameterType="LeaseOrder">
		UPDATE 
			TB_IAMBERRY_RENT_ORDER 
		SET 
			ORDER_STATUS = #{orderStatus},
			ORDER_TRANSACTION_ID = #{orderTransactionId},
			ORDER_TRANSACTION_DATE = #{orderTransactionDate},
			ORDER_INIT_CODE = #{orderInitCode},
			ORDER_CODE_STATE = #{orderCodeState} 
		WHERE 
			ORDER_ID = #{orderId} 
		AND 
			ORDER_STATUS = 1
	</update>
	
	<select id="selectLeaseOrderInfo" parameterType="Integer" resultType="LeaseOrder">
		select * from TB_IAMBERRY_RENT_ORDER
		where order_openid = #{openId} 
	</select>
	<!-- 根据orderId查询租金和电话 -->
	<select id="selectDepositByOrderId" parameterType="String" resultType="LeaseOrder">
		SELECT 
			ORDER_ADDRESS_TEL orderAddressTel,
			ORDER_DEPOSIT orderDeposit 
		FROM 
			TB_IAMBERRY_RENT_ORDER 
		WHERE 
			ORDER_ID = #{orderId}
		AND 
			<![CDATA[ORDER_STATUS > 0]]>
	</select>
	
	<!-- 修改订单状态（退款） -->
	<update id="updateLeaseOrderStatus" parameterType="LeaseOrder">
		UPDATE 
			TB_IAMBERRY_RENT_ORDER 
		SET 
			order_status = #{orderStatus}
			<if test="orderReturnDate != null">
				,order_return_date = #{orderReturnDate}
			</if>
			<if test="orderReturnDes != null">
				,order_return_des = #{orderReturnDes}
			</if>
		WHERE 
			ORDER_ID = #{orderId} 
	</update>
	
	<!-- 添加租赁申请 -->
	<insert id="addApply" parameterType="RentApply" useGeneratedKeys="true" keyProperty="applyId">
			INSERT INTO TB_IAMBERRY_RENT_APPLY
			(
				APPLY_OPENID, APPLY_COLOR, APPLY_TIME, APPLY_ADDR, APPLY_NAME,
				APPLY_TEL, APPLY_RE_TEL, APPLY_PAY
			)
			VALUES 
			(
				#{applyOpenid},#{applyColor},#{applyTime},#{applyAddr},#{applyName},
				#{applyTel},#{applyReTel},1
			)
	</insert>
	
	<!-- 修改订单状态（退款） -->
	<update id="updateApply" parameterType="RentApply">
		UPDATE 
			TB_IAMBERRY_RENT_APPLY 
		SET 
			APPLY_PAY = 2,
			APPLY_PAY_MONEY = #{applyPayMoney},
			APPLY_PAY_TIME = NOW()
		WHERE 
			APPLY_ID = #{applyId} 
	</update>
</mapper>