<?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.OrderMapper">
	<resultMap id="BaseResultMap"
			   type="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO">
		<result property="orderId" column="order_id"/>
		<result property="active" column="active"/>
		<result property="originPrincipal" column="origin_principal"/>
		<result property="pricingMethod" column="pricing_method"/>
		<result property="orderSn" column="order_sn"/>
		<result property="state" column="state"/>
		<result property="auditState" column="audit_state"/>
		<result property="foreman" column="foreman"/>
		<result property="type" column="type"/>
		<result property="remark" column="remark"/>
		<result property="createdBy" column="created_by"/>
		<result property="createdByName" column="created_by_name"/>
		<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"/>
		<association property="orderVehicleDO"
					 javaType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderVehicleDO">
			<result property="vehicleId" column="vehicle_id"/>
			<result property="orderId" column="order_id"/>
			<result property="vehicleNo" column="vehicle_no"/>
			<result property="dealerId" column="dealer_id"/>
			<result property="dealerName" column="dealer_name"/>
			<result property="origin" column="origin"/>
			<result property="destination" column="destination"/>
			<result property="deliveryTime" column="deliveryTime"/>
		</association>
	</resultMap>

	<resultMap id="SelectByOrderIdListResultMap"
			   type="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO">
		<result property="orderId" column="order_id"/>
		<result property="orderSn" column="order_sn"/>
		<result property="state" column="state"/>
		<result property="auditState" column="audit_state"/>
		<result property="type" column="type"/>
		<association property="orderVehicleDO"
					 javaType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderVehicleDO">
			<result property="vehicleId" column="vehicle_id"/>
			<result property="vehicleNo" column="vehicle_no"/>
			<result property="dealerId" column="dealer_id"/>
			<result property="dealerName" column="dealer_name"/>
			<result property="origin" column="origin"/>
			<result property="destination" column="destination"/>
		</association>
	</resultMap>

	<select id="selectByOrderIdList"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO"
			resultMap="SelectByOrderIdListResultMap">
		SELECT
		po.order_id, po.order_sn, po.state, po.audit_state, po.type,
		ov.vehicle_id, ov.vehicle_no, ov.dealer_id, ov.dealer_name, ov.origin,
		ov.destination
		FROM `order` po
		LEFT JOIN order_vehicle ov ON po.order_id = ov.order_id AND ov.is_delete
		= 0
		<where>
			po.is_delete = 0
			<if test="orderIdList != null and orderIdList.size() > 0">
				AND po.order_id IN
				<foreach item="item" collection="orderIdList" separator=","
						 open="(" close=")" index="">
					#{item}
				</foreach>
			</if>
		</where>

	</select>
	<select id="selectPage"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO"
			resultMap="BaseResultMap">
		SELECT
		po.*,
		ov.vehicle_id, ov.vehicle_no, ov.dealer_id, ov.dealer_name, ov.origin,
		ov.destination
		FROM `order` po
		LEFT JOIN order_vehicle ov ON po.order_id = ov.order_id AND ov.is_delete
		= 0
		<where>
			po.is_delete = 0
			<if test="query.vehicleNo != null">
				AND ov.vehicle_no LIKE CONCAT('%',#{query.vehicleNo},'%')
			</if>
			<if test="query.dealerId != null">
				AND ov.dealer_id = #{query.dealerId}
			</if>
			<if test="query.supplierId != null">
				AND ov.order_id IN (
				SELECT order_id
				FROM order_supplier
				WHERE supplier_id = #{query.supplierId}
				)
			</if>
			<if test="query.supplierName != null">
				AND ov.order_id IN (
				SELECT order_id
				FROM order_supplier
				WHERE name LIKE CONCAT('%',#{query.supplierName},'%')
				)
			</if>
			<if test="ew!= null">
				<if test="ew.nonEmptyOfWhere">
					AND
				</if>
				${ew.sqlSegment}
			</if>
		</where>
	</select>

	<select id="selectByOrderId"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO"
			resultMap="SelectByOrderIdListResultMap">
		SELECT
		po.order_id, po.order_sn, po.state, po.audit_state, po.type,
		ov.vehicle_id, ov.vehicle_no, ov.dealer_id, ov.dealer_name, ov.origin,
		ov.destination
		FROM `order` po
		LEFT JOIN order_vehicle ov ON po.order_id = ov.order_id AND ov.is_delete
		= 0
		where po.is_delete = 0
		AND po.order_id = #{orderId}
		limit 1
	</select>
	<select id="selectByQueryList"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderDO"
			resultMap="BaseResultMap">
		SELECT
		po.*,
		ov.vehicle_id, ov.vehicle_no, ov.dealer_id, ov.dealer_name, ov.origin,
		ov.destination, ov.delivery_time
		FROM `order` po
		LEFT JOIN order_vehicle ov ON po.order_id = ov.order_id AND ov.is_delete
		= 0
		<where>
			po.is_delete = 0
			<if test="query.dealerId != null">
				AND ov.dealer_id = #{query.dealerId}
			</if>
			<if test="query.month != null">
				AND ov.delivery_time between #{query.month} and #{query.month}
				+ INTERVAL 1 MONTH
			</if>
			<if test="ew!= null">
				<if test="ew.nonEmptyOfWhere">
					AND
				</if>
				${ew.sqlSegment}
			</if>
		</where>
	</select>
</mapper>

