<?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">
<mapper
	namespace="com.xunhong.erp.turbo.biz.infrastructure.mapper.OrderRebateMapper">
	<resultMap id="BaseResultMap"
			   type="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderRebateDO">
		<result property="orderRebateId" column="order_rebate_id"/>
		<result property="orderId" column="order_id"/>
		<result property="dealerId" column="dealer_id"/>
		<result property="name" column="name"/>
		<result property="calcMethod" column="calc_method"/>
		<result property="netWeight" column="net_weight"/>
		<result property="unitPrice" column="unit_price"/>
		<result property="amount" column="amount"/>
		<result property="isPaid" column="is_paid"/>
		<result property="poState" column="po_state"/>
		<result property="createdAt" column="created_at"/>
		<result property="createdAt" column="created_at"/>
		<result property="updatedAt" column="updated_at"/>
		<result property="isDelete" column="is_delete"/>
		<result property="version" column="version"/>
	</resultMap>
	<select id="selectPage"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderRebateDO"
			resultMap="BaseResultMap">
		SELECT
			ore.*,
			po.state as po_state
		FROM order_rebate ore
		LEFT JOIN order_vehicle ov ON ore.order_id = ov.order_id AND ov.is_delete = 0
		LEFT JOIN `order` po ON ore.order_id = po.order_id AND po.is_delete = 0
		<where>
			ore.is_delete = 0
			<if test="query.createdBy != null">
				AND po.created_by = #{query.createdBy}
			</if>
			<if test="query.orderId != null">
				AND po.order_id = #{query.orderId}
			</if>
			<if test="query.poType != null">
				AND po.type = #{query.poType}
			</if>
			<if test="query.poStates != null and query.poStates.size() > 0">
				AND po.state IN
				<foreach collection="query.poStates" item="state" open="(" separator="," close=")">
					#{state.type}
				</foreach>
			</if>
			<if test="ew != null">
				<if test="ew.nonEmptyOfWhere">
					AND
				</if>
				${ew.sqlSegment}
			</if>
		</where>
	</select>
</mapper>

