<?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.rebate.mapper.OrderRebateMapper">
	
	<sql id="rentType">
		TYPE_ID typeId,
		TYPE_NAME typeName,
		TYPE_DURATION typeDuration,
		TYPE_MONEY typeMoney,
		TYPE_PLEDGE_MONEY typePledgeMoney,
		TYPE_SERVICE_MONEY typeServiceMoney,
		TYPE_WATER_NUM typeWaterNum,
		TYPE_STATUS typeStatus,
		TYPE_REMARK typeRemark,
		TYPE_CREATE_TIME typeCreateTime,
		TYPE_PAY_DURATION typePayDuration
	</sql>
	
	<sql id="rentOrderField">
		RENT_ORDERID rentOrderId,
		RENT_OPENID rentOpenid,
		RENT_AMOUNT rentAmount,
		RENT_PAY_MONEY rentPayMoney,
		RENT_SERVICE_ID rentServiceId,
		RENT_SERVICE_MONEY rentServiceMoney,
		RENT_TYPE_ID rentTypeId,
		RENT_STATUS rentStatus,
		RENT_SEND_DATE rentSendDate,
		RENT_POST_NUM rentPostNum,
		RENT_POST_FIRM rentPostFirm,
		RENT_ADDRESS_INFO rentAddressInfo,
		RENT_ADDRESS_TEL rentAddressTel,
		RENT_ADDRESS_NAME rentAddressName,
		RENT_TRANSACTION_ID rentTransactionId,
		RENT_TRANSACTION_DATE rentTransactionDate,
		RENT_CREATE_DATE rentCreateDate,
		RENT_ADMIN_REMARK rentAdminRemark,
		RENT_USER_REMARK rentUserRemark
	</sql>
	
	<sql id="rebateRecordField">
		RECORD_ID recordId,
		RECORD_ORDER_ID recordOrderId,
		RECORD_OPENID recordOpenid,
		RECORD_RENT_OPENID recordRentOpenid,
		RECORD_TYPE_ID recordTypeId,
		RECORD_MONEY recordMoney,
		RECORD_REPLACE_ID rebateReplaceId,
		RECORD_DEDUCTION_ID rebateDeductionId,
		RECORD_IDENTIFY recordIdentify,
		RECORD_STATUS recordStatus,
		RECORD_CREATE_TIME recordCreateTime,
		RECORD_UPDATE_TIME recordUpdateTime 
	</sql>
	
	<sql id="rebateTypeField">
		REBATE_ID rebateId,
		REBATE_NAME rebateName,
		REBATE_TYPE_ID rebateTypeId,
		REBATE_LEVEL rebateLevel,
		REBATE_MONEY rebateMoney,
		REBATE_REPLACE_MONEY rebateReplaceMoney,
		REBATE_DEDUCTION_MONEY rebateDeductionMoney,
		REBATE_IDENTIFY rebateIdentify,
		REBATE_STATUS rebateStatus,
		REBATE_CREATE_TIME rebateCreateTime 
	</sql>
	
	<!-- 根据id和状态查询租赁类型数据 -->
	<select id="getRebateType" parameterType="RentTypeInfo" resultType="RentTypeInfo">
		SELECT 
			<include refid="rentType" />
		FROM 
			TB_IAMBERRY_RENT_TYPE 
		<where>
			<if test="typeId != null and typeId != ''">
				TYPE_ID = #{typeId} 
			</if>
			<if test="typeStatus != null and typeStatus != ''">
				AND TYPE_STATUS = #{typeStatus} 
			</if>
		</where>
	</select>
	
	<!-- 查询租赁方式列表 -->
	<select id="listRebateType" parameterType="RentTypeInfo" resultType="RentTypeInfo">
		SELECT 
				* 
			FROM 
				TB_IAMBERRY_RENT_TYPE 
			<where>
				<if test="typeStatus != null and typeStatus != ''">
					TYPE_STATUS = #{typeStatus} 
				</if>
			</where>
	</select>
	
		<!-- 根据条件查询订单列表 -->
	<select id="listRentOrderByStatus" parameterType="RentOrder" resultType="RentOrder">
		SELECT 
			<include refid="rentOrderField" />
		FROM 
			TB_IAMBERRY_RENT_ORDER 
		<where>
			<if test="rentOpenid != null and rentOpenid != ''">
				RENT_OPENID = #{rentOpenid} 
			</if>
			<if test="rentStatus != null and rentStatus != ''">
				AND RENT_STATUS = #{rentStatus}
			</if>
		</where>
	</select>
	
	<!-- 根据条件查询返利记录信息 -->
	<select id="listRebateRecord" parameterType="RebateRecord" resultType="RebateRecord">
		SELECT
			<include refid="rebateRecordField" />
		FROM 
			TB_IAMBERRY_RENT_REBATE_RECORD 
		<where>
			<if test="recordOpenid != null and recordOpenid != ''">
				RECORD_OPENID = #{recordOpenid} 
			</if>
			<if test="recordRentOpenid != null and recordRentOpenid != ''">
				AND RECORD_RENT_OPENID = #{recordRentOpenid} 
			</if>
			<if test="recordStatus != null and recordStatus == 5">
				AND RECORD_STATUS IN (2,3)
			</if>
		</where>
	</select>
	
	<!-- 根据三个条件查询一条返利类型 -->
	<select id="getOneRebateType" parameterType="RebateType" resultType="RebateType">
		SELECT
			<include refid="rebateTypeField" />
		FROM 
			TB_IAMBERRY_RENT_REBATE_TYPE 
		<where>
			<if test="rebateLevel != null and rebateLevel != ''">
				REBATE_LEVEL = #{rebateLevel} 
			</if>
			<if test="rebateTypeId != null and rebateTypeId != ''">
				AND REBATE_TYPE_ID = #{rebateTypeId} 
			</if>
			<if test="rebateStatus != null and rebateStatus != ''">
				AND REBATE_STATUS = #{rebateStatus} 
			</if>
		</where>
	</select>
	
	<!-- 保存待返利记录 -->
	<insert id="saveRebateRecord" parameterType="RebateRecord" useGeneratedKeys="true" keyProperty="recordId">
		INSERT INTO TB_IAMBERRY_RENT_REBATE_RECORD
		(
			RECORD_ORDER_ID,RECORD_OPENID,RECORD_RENT_OPENID,RECORD_TYPE_ID,RECORD_MONEY,
			RECORD_REPLACE_ID,RECORD_DEDUCTION_ID,RECORD_IDENTIFY,RECORD_STATUS,RECORD_CREATE_TIME
		) 
		VALUES 
		(
			#{recordOrderId},#{recordOpenid},#{recordRentOpenid},#{recordTypeId},#{recordMoney},
			#{recordReplaceId},#{recordDeductionId},#{recordIdentify},#{recordStatus},NOW()
		)
	</insert>
	
	<!-- 查询待转账的返利记录 -->
	<select id="listPayRebate" parameterType="java.util.Map" resultType="RebateRecord">
		SELECT
			r.*, u.user_nickname reUserNickName, t.rebate_name rentType, ru.user_nickname userNickName
		FROM
			TB_IAMBERRY_RENT_REBATE_RECORD r
		LEFT JOIN
			tb_iamberry_user_userinfo u
		ON
			r.record_openid = u.user_openid
		LEFT JOIN
			tb_iamberry_user_userinfo ru
		ON
			ru.user_openid = r.record_rent_openid
		LEFT JOIN
			tb_iamberry_rent_rebate_type t
		ON
			t.rebate_id = r.record_type_id
		WHERE
			record_status = 2 AND record_identify = 1
		LIMIT
			#{min}, #{max}
	</select>

	<!-- 根据id，查询返利金额 -->
	<select id="get" parameterType="Integer" resultType="RebateRecord">
		SELECT * FROM TB_IAMBERRY_RENT_REBATE_RECORD WHERE record_id = #{id}
	</select>

	<!-- 统计待转账的返利记录 -->
	<select id="countPayRebate" resultType="Integer">
		SELECT COUNT(record_id) FROM TB_IAMBERRY_RENT_REBATE_RECORD WHERE record_status = 2 AND record_identify = 1
	</select>

	<!-- 修改返利记录 -->
	<update id="updateRebate" parameterType="Integer">
		UPDATE TB_IAMBERRY_RENT_REBATE_RECORD SET record_status = 3 WHERE record_id = #{id} AND record_status = 2
	</update>
</mapper>