<?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.OrderCostMapper">
	<resultMap id="BaseResultMap"
			   type="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderCostDO">
		<result property="orderCostId" column="order_cost_id"/>
		<result property="orderId" column="order_id"/>
		<result property="costId" column="cost_id"/>
		<result property="costItemIds" column="cost_item_ids"
				typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
		<result property="name" column="name"/>
		<result property="price" column="price"/>
		<result property="unit" column="unit"/>
		<result property="count" column="count"/>
		<result property="belong" column="belong"/>
		<result property="principal" column="principal"/>
		<result property="type" column="type"/>
		<result property="isPaid" column="is_paid"/>
		<result property="poState" column="po_state"/>
		<result property="createdAt" column="created_at"/>
		<result property="updatedAt" column="updated_at"/>
		<result property="isDelete" column="is_delete"/>
		<result property="version" column="version"/>
	</resultMap>
	<resultMap id="SelectByOrderIdResultMap"
			   type="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderCostDO">
		<result property="orderCostId" column="order_cost_id"/>
		<result property="orderId" column="order_id"/>
		<result property="costId" column="cost_id"/>
		<result property="name" column="name"/>
		<result property="price" column="price"/>
		<result property="unit" column="unit"/>
		<result property="count" column="count"/>
		<result property="belong" column="belong"/>
		<result property="principal" column="principal"/>
		<result property="type" column="type"/>
		<result property="isPaid" column="is_paid"/>
		<result property="poState" column="po_state"/>
		<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"/>
		</association>
	</resultMap>
	<select id="selectPage"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderCostDO"
			resultMap="BaseResultMap">
		SELECT
		oc.*,
		po.state as po_state
		FROM order_cost oc
		LEFT JOIN order_vehicle ov ON oc.order_id = ov.order_id AND ov.is_delete
		= 0
		LEFT JOIN `order` po ON oc.order_id = po.order_id AND
		po.is_delete = 0
		<where>
			oc.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>
	<select id="selectByOrderId"
			resultType="com.xunhong.erp.turbo.biz.infrastructure.entity.OrderCostDO"
			resultMap="SelectByOrderIdResultMap">
		SELECT
			oc.*,
			po.state as po_state,
			ov.vehicle_id,
			ov.vehicle_no,
			ov.dealer_id,
			ov.dealer_name
		FROM order_cost oc
		LEFT JOIN order_vehicle ov ON oc.order_id = ov.order_id AND ov.is_delete = 0
		LEFT JOIN `order` po ON oc.order_id = po.order_id AND
		po.is_delete = 0
		WHERE oc.order_id = #{orderId}
		AND oc.name = #{name}
		AND oc.is_delete = 0
	</select>
</mapper>

