<?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.KwtWaybillOrderMapper">
    <resultMap id="BaseResultMap" type="com.sckw.transport.model.KwtWaybillOrder">
        <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_no" jdbcType="VARCHAR" property="wOrderNo"/>
        <result column="truck_no" jdbcType="VARCHAR" property="truckNo"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <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="entrust_amount" jdbcType="DECIMAL" property="entrustAmount"/>
        <result column="unload_amount" jdbcType="DECIMAL" property="unloadAmount"/>
        <result column="load_amount" jdbcType="DECIMAL" property="loadAmount"/>
        <result column="deficit_amount" jdbcType="DECIMAL" property="deficitAmount"/>
        <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="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <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_no, truck_no, driver_id, entrust_amount, unload_amount,
    load_amount, deficit_amount, remark, `status`, create_by, create_time, update_by,
    update_time, del_flag,start_time,end_time
    </sql>
    <select id="selectWaybillOrderCarList" resultType="com.sckw.transport.model.dto.OrderCarDTO">
        SELECT a.id            as wOrderId,
               a.w_order_no    as wOrderNo,
               a.driver_id     as driverId,
               a.driver_name   as driverName,
               a.driver_phone  as driverPhone,
               a.driver_idcard as driverIdcard,
               a.l_order_id    as lOrderId,
               b.l_order_no    as lOrderNo,
               b.t_order_id    as tOrderId,
               b.t_order_no    as tOrderNo
        FROM kwt_waybill_order a
                 LEFT JOIN kwt_logistics_order b ON a.l_order_id = b.id
        WHERE a.del_flag = '0'
          and b.del_flag = '0'
          and b.t_order_id = #{id,jdbcType=VARCHAR} limit #{page}
            , #{pageSize}
    </select>
    <select id="selectWaybillOrderCarCount" resultType="java.lang.Long">
        SELECT count(a.id)
        FROM kwt_waybill_order a
                 LEFT JOIN kwt_logistics_order b ON a.l_order_id = b.id
        WHERE a.del_flag = '0'
          and b.del_flag = '0'
          and b.t_order_id = #{id,jdbcType=VARCHAR}
    </select>

    <select id="selectDriverOrder" resultType="java.lang.Long">
        SELECT a.l_order_id as lOrderId
        FROM kwt_waybill_order a
        LEFT JOIN kwt_waybill_order_track b ON a.id = b.w_order_id
        AND a.del_flag = '0'
        AND b.del_flag = '0'
        AND a.type='0'
        <where>
            <if test="id != null and id !=''">
                a.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>
        </where>
        group by lOrderId
    </select>
    <select id="getWaybillData" resultType="com.sckw.transport.model.vo.WaybillDetailVO">
    </select>

    <select id="selectWaybillRelate" resultType="com.sckw.transport.model.dto.WayBillDetailDTO">
        SELECT `order`.`id`             AS `wOrderId`,
               `order`.`w_order_no`     AS `wOrderNo`,
               `order`.`truck_no`       AS `truckNo`,
               `order`.`entrust_amount` AS `entrustAmount`,
               `order`.`truck_id`       AS `truckId`,
               `order`.`driver_id`      AS `driverId`,
               `order`.`driver_name`    AS `driverName`,
               `order`.`driver_phone`   AS `driverPhone`,
               `order`.`driver_idcard`  AS `driverIdcard`,
               `order`.`type`,
               `order`.`start_time`     AS `startTime`,
               `order`.`end_time`       AS `endTime`,
               `order`.`create_by`      AS `createBy`,
               `logistics`.`id`         AS `lOrderId`,
               `logistics`.`l_order_no` AS `lOrderNo`,
               `logistics`.`price_type` AS `priceType`,
               `goods`.`goods_name`     AS goodsName
        FROM `kwt_waybill_order` `order`
                 LEFT JOIN `kwt_logistics_order` `logistics` ON `logistics`.`id` = `order`.`l_order_id`
                 LEFT JOIN `kwt_logistics_order_goods` `goods` ON `goods`.`l_order_id` = `order`.`l_order_id`
        WHERE `order`.`id` = #{id,jdbcType=BIGINT}
    </select>

    <select id="statistics" resultType="com.sckw.mongo.model.TableTops">
        SELECT count(a.id) AS total
        FROM kwt_waybill_order a
        LEFT JOIN kwt_waybill_order_track b ON a.id = b.w_order_id
        AND a.del_flag = '0'
        AND b.del_flag = '0'
        <where>
            <if test="id != null and id !=''">
                a.driver_id = #{id,jdbcType=BIGINT}
            </if>
            <if test="status != null and status.size() > 0">
                AND b.`status` IN
                <foreach collection="status" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectDataByLorderId" resultType="int">
        SELECT
        COUNT(b.id)
        FROM
        kwt_waybill_order a
        LEFT JOIN kwt_logistics_order b ON a.l_order_id = b.id
        AND a.del_flag = 0
        AND b.del_flag = 0
        <where>
            <if test="id != null and id !=''">
                and b.id = #{id}
            </if>
            <if test="statusList != null and statusList.size() >0">
                AND a.`status` IN
                <foreach collection="statusList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>