<?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.fuzamei.mapperInterface.OrdersIssueMapper">
	
	<select id="queryOrdersByPlanner" parameterType="Params" resultType="IssueOrder">
		select 
			o.order_id,
			u.person_name as supplier_name,
			o.part_no,
			o.box_no,
			o.inbox_qty,
			o.require_time,
			o.update_time,
			o.status_id,
			s.status_name
		from 
			orders o
		left join 
			status s
		on 
			o.status_id=s.status_id
		left join 
			users u
		on 
			o.supplier_id=u.user_id
		<where>
			o.planner_id=#{userId}
			<if test="supplierId!=null">
				and o.supplier_id=#{supplierId}
			</if>
			<if test="boxNo!=null and boxNo!=''">
				and o.box_no=#{boxNo}
			</if>
			<if test="statusId!=null">
				and o.status_id=#{statusId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and o.require_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by o.status_id asc,o.require_time desc
		limit #{startPage},#{rowNum}
	</select>
	
	<select id="findQueryCountByPlanner" parameterType="Params" resultType="int">
		select 
			count(*)
		from 
			orders
		<where>
			planner_id=#{userId}
			<if test="supplierId!=null">
				and supplier_id=#{supplierId}
			</if>
			<if test="boxNo!=null and boxNo!=''">
				and box_no=#{boxNo}
			</if>
			<if test="statusId!=null">
				and status_id=#{statusId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and require_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<select id="queryOrdersBySupplier" parameterType="Params" resultType="IssueOrder">
		select 
			o.order_id,
			o.part_no,
			o.box_no,
			o.inbox_qty,
			o.require_time,
			o.update_time,
			s.status_name
		from 
			orders o
		left join 
			status s
		on 
			o.status_id=s.status_id
		<where>
			o.supplier_id=#{userId}
			<if test="orderId!=null">
				and o.order_id=#{orderId}
			</if>
			<if test="boxNo!=null and boxNo!=''">
				and o.box_no=#{boxNo}
			</if>
			<if test="statusId!=null">
				and o.status_id=#{statusId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and o.require_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by o.status_id asc,o.require_time desc
		limit #{startPage},#{rowNum}
	</select>
	
	<select id="findQueryCountBySupplier" parameterType="Params" resultType="int">
		select 
			count(*)
		from 
			orders o
		<where>
			o.supplier_id=#{userId}
			<if test="orderId!=null">
				and o.order_id=#{orderId}
			</if>
			<if test="boxNo!=null and boxNo!=''">
				and o.box_no=#{boxNo}
			</if>
			<if test="statusId!=null">
				and status_id=#{statusId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and o.require_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	<update id="consignment">
		update 
			orders
		set 
			send_time=#{sendTime},
			carrier_id=#{carrierId},
			status_id=#{statusId},
			update_time=#{updateTime}
		where 
			order_id=#{orderId}
	</update>
	
	<!-- 插入新订单 -->
	<insert id="addOrder">
		insert into 
			orders(order_id,
				   part_no,
				   box_no,
				   box_qty,
				   inbox_qty,
				   require_time,
				   create_time,
				   update_time,
				   status_id,
				   planner_id,
				   supplier_id)
			values(#{orderId},
				   #{partNo},
				   #{boxNo},
				   #{boxQty},
				   #{inboxQty},
				   #{requireTime},
				   #{createTime},
				   #{updateTime},
				   #{statusId},
				   #{plannerId},
				   #{supplierId})
	</insert>
	
	
	
</mapper>