<?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.sckw.transport.dao.KwtLogisticsOrderCirculateMapper">
    <resultMap id="BaseResultMap" type="com.sckw.transport.model.KwtLogisticsOrderCirculate">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="ent_id" jdbcType="BIGINT" property="entId" />
        <result column="l_order_id" jdbcType="BIGINT" property="lOrderId" />
        <result column="w_order_id" jdbcType="BIGINT" property="wOrderId" />
        <result column="type" jdbcType="VARCHAR" property="type" />
        <result column="truck_id" jdbcType="BIGINT" property="truckId" />
        <result column="truck_no" jdbcType="VARCHAR" property="truckNo" />
        <result column="driver_id" jdbcType="BIGINT" property="driverId" />
        <result column="driver_name" jdbcType="VARCHAR" property="driverName" />
        <result column="driver_phone" jdbcType="VARCHAR" property="driverPhone" />
        <result column="driver_idcard" jdbcType="VARCHAR" property="driverIdcard" />
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
        <result column="entrust_amount" jdbcType="DECIMAL" property="entrustAmount" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="BIGINT" property="updateBy" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    </resultMap>

    <sql id="Base_Column_List">
        `id`, `ent_id`, `l_order_id`, `w_order_id`, `type`, `truck_id`, `truck_no`, `driver_id`,
        `driver_name`, `driver_phone`, `driver_idcard`, `start_time`, `end_time`, `entrust_amount`,
        `remark`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`
    </sql>

    <update id="updateIdByKey">
        UPDATE kwt_logistics_order_circulate
        set
        id = #{newId}, w_order_id = #{wOrderId},
        update_by = #{updateBy}, update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <update id="updateCirculate" parameterType="com.sckw.transport.model.KwtLogisticsOrderCirculate">
        UPDATE kwt_logistics_order_circulate
        <set>
            <if test="null != entId and '' != entId">ent_id = #{entId},</if>
            <if test="null != lOrderId and '' != lOrderId">l_order_id = #{lOrderId},</if>
            <if test="null != wOrderId and '' != wOrderId">w_order_id = #{wOrderId},</if>
            <if test="null != type and '' != type">type = #{type},</if>
            <if test="null != truckId and '' != truckId">truck_id = #{truckId},</if>
            <if test="null != truckNo and '' != truckNo">truck_no = #{truckNo},</if>
            <if test="null != driverId and '' != driverId">driver_id = #{driverId},</if>
            <if test="null != driverName and '' != driverName">driver_name = #{driverName},</if>
            <if test="null != driverPhone and '' != driverPhone">driver_phone = #{driverPhone},</if>
            <if test="null != driverIdcard and '' != driverIdcard">driver_idcard = #{driverIdcard},</if>
            <if test="null != startTime and '' != startTime">start_time = #{startTime},</if>
            <if test="null != endTime and '' != endTime">end_time = #{endTime},</if>
            <if test="null != entrustAmount and '' != entrustAmount">entrust_amount = #{entrustAmount},</if>
            <if test="null != totalTake and '' != totalTake">total_take = #{totalTake},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            <if test="null != status and '' != status">status = #{status},</if>
            <if test="null != createBy and '' != createBy">create_by = #{createBy},</if>
            <if test="null != createTime and '' != createTime">create_time = #{createTime},</if>
            <if test="null != updateBy and '' != updateBy">update_by = #{updateBy},</if>
            <if test="null != updateTime and '' != updateTime">update_time = #{updateTime},</if>
            <if test="null != delFlag and '' != delFlag">del_flag = #{delFlag}</if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectDriverOrderByStatus" resultType="java.lang.Long">
        SELECT b.w_order_id as wOrderId
        FROM kwt_waybill_order a
        LEFT JOIN `kwt_logistics_order_circulate` b ON a.id = b.w_order_id
        WHERE a.del_flag = '0'
        AND b.`status` = '0'
        AND a.type = '1'
        <if test="id != null and id !=''">
           and  b.driver_id = #{id,jdbcType=BIGINT}
        </if>
        <if test="status != null and status.size() > 0">
            AND a.`status` IN
            <foreach collection="status" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="findCirculate" resultType="com.sckw.transport.model.KwtLogisticsOrderCirculate" >
        select
        id, ent_id entId, l_order_id lOrderId, w_order_id wOrderId, type, truck_id truckId,
        truck_no truckNo, driver_id driverId, driver_name driverName, driver_phone driverPhone,
        driver_idcard driverIdcard, start_time startTime, end_time endTime, entrust_amount entrustAmount,
        remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_logistics_order_circulate
        where del_flag = 0
        and l_order_id = #{lOrderId, jdbcType=BIGINT}
        and truck_id = #{truckId, jdbcType=BIGINT}
        and driver_id = #{driverId, jdbcType=BIGINT}
    </select>

    <select id="findCirculateList" resultType="com.sckw.transport.model.KwtLogisticsOrderCirculate" >
        select
        id, ent_id entId, l_order_id lOrderId, w_order_id wOrderId, type, truck_id truckId,
        truck_no truckNo, driver_id driverId, driver_name driverName, driver_phone driverPhone,
        driver_idcard driverIdcard, start_time startTime, end_time endTime, entrust_amount entrustAmount,
        remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_logistics_order_circulate
        where del_flag = 0
        and l_order_id = #{lOrderId, jdbcType=BIGINT}
        <if test="truckId != null and truckId !=''">
            and truck_id = #{truckId, jdbcType=BIGINT}
        </if>
        <if test="driverId != null and driverId !=''">
            and driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="status != null and status !=''">
            and status = #{status, jdbcType=INTEGER}
        </if>
    </select>

    <select id="selectEntityBylOrderIdAndOrderIds" resultType="java.lang.Long">
        SELECT
        c.id
        FROM
        kwt_logistics_order_circulate c
        LEFT JOIN kwt_logistics_order b ON c.l_order_id = b.id
        WHERE
        b.del_flag = 0
        AND c.`status` = '0'
        <if test="lOrderId != null and lOrderId != ''">
          and   b.id =#{lOrderId}
        </if>
        <if test="ids != null and ids.size()>0">
            AND c.id IN
            <foreach collection="ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
</mapper>