<?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 driverCard,
               a.truck_no      as truckNo,
               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_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.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_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.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 c.l_order_id as lOrderId
        FROM kwt_waybill_order a
        left join kwt_waybill_order_subtask c on a.id = c.w_order_id
        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 c.l_order_id
    </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_waybill_order_subtask c on order.id = c.w_order_id
        LEFT JOIN `kwt_logistics_order` `logistics` ON `logistics`.`id` = c.`l_order_id`
        LEFT JOIN `kwt_logistics_order_goods` `goods` ON `goods`.`l_order_id` = c.`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_subtask k
          left join kwt_waybill_order a on k.w_order_id = a.id and a.del_flag = 0 and k.del_flag = 0
          LEFT JOIN kwt_logistics_order b ON k.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>

    <select id="findWaybillOrderCount" resultType="com.sckw.transport.model.vo.WaybillCountVo"
            parameterType="java.util.Map">
        SELECT
        count(case when wo.status = 8 then 1 end) totalComplete,
        count(case when wo.status not in (1, 9, 10, 11) then 1 end) totalTake,
        count(case when wo.status in (3, 4, 5, 6) then 1 end) totalExecute,
        count(DISTINCT case when lo.status in (0, 1, 2, 3) then lo.id end) totalUnfinished,
        IFNULL(SUM(wo.entrust_amount), 0.0) entrustAmount, IFNULL(SUM(wo.load_amount), 0.0) loadAmount,
        IFNULL(SUM(wo.unload_amount), 0.0) unloadAmount, IFNULL(SUM(wo.deficit_amount), 0.0) deficitAmount,
        MIN(wot1.operate_time) loadTime, MAX(wot2.operate_time) unloadTime
        from kwt_waybill_order wo
        left join kwt_waybill_order_ticket wot1 on wot1.w_order_id = wo.id and wot1.type = 1 and wo.unit = wot1.unit and wot1.type = 1 and wot1.del_flag = 0
        left join kwt_waybill_order_ticket wot2 on wot2.w_order_id = wo.id and wot2.type = 2 and wo.unit = wot2.unit and wot1.type = 2  and wot2.del_flag = 0
        left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
        left join kwt_logistics_order lo on lo.id = c.l_order_id
        where wo.del_flag = 0 and lo.del_flag = 0
        <if test="driverId != null and driverId != ''">
            and wo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="truckId != null and truckId != ''">
            and wo.truck_id = #{truckId, jdbcType=BIGINT}
        </if>
        <if test="passStatus != null and passStatus != ''">
            and wo.status = 8
        </if>
        <if test="loadCountStatus != null and loadCountStatus != ''">
            and wo.status in (5, 6, 7, 8, 12, 13)
        </if>
        <if test="unloadCountStatus != null and unloadCountStatus != ''">
            and wo.status in (7, 8, 12, 13)
        </if>
        <if test="tOrderId != null and tOrderId != ''">
            and lo.t_order_id = #{tOrderId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and lo.id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="logisticsStatus != null and logisticsStatus != ''">
            and lo.status  = #{logisticsStatus, jdbcType=INTEGER}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, lo.pids)
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(lo.id, #{belowlOrderId, jdbcType=VARCHAR})
        </if>
        <if test="ownOrderId != null and ownOrderId != ''">
            and lo.id != #{ownOrderId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="findWaybillOrderTicketCount" parameterType="java.util.Map">
        SELECT IFNULL(SUM(wot.amount), 0.0) amount from
        kwt_waybill_order_ticket wot
        left join kwt_waybill_order wo on wot.w_order_id = wo.id
        left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
        left join kwt_logistics_order lo on lo.id = c.l_order_id
        where wot.del_flag = 0 and wo.del_flag = 0 and lo.del_flag = 0
        <if test="driverId != null and driverId != ''">
            and wo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="truckId != null and truckId != ''">
            and wo.truck_id = #{truckId, jdbcType=BIGINT}
        </if>
        <if test="passStatus != null and passStatus != ''">
            and wo.status = 8
        </if>
        <if test="loadCountStatus != null and loadCountStatus != ''">
            and wo.status in (5, 6, 7, 8, 12, 13)
        </if>
        <if test="unloadCountStatus != null and unloadCountStatus != ''">
            and wo.status in (7, 8, 12, 13)
        </if>
        <if test="unit != null and unit != ''">
            and wot.unit = #{unit, jdbcType=VARCHAR}
        </if>
        <if test="type != null and type != ''">
            and wot.type = #{type, jdbcType=VARCHAR}
        </if>
        <if test="tOrderId != null and tOrderId != ''">
            and lo.t_order_id = #{tOrderId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and lo.id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="logisticsStatus != null and logisticsStatus != ''">
            and lo.status  = #{logisticsStatus, jdbcType=INTEGER}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, lo.pids)
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(lo.id, #{belowlOrderId, jdbcType=VARCHAR})
        </if>
        <if test="ownOrderId != null and ownOrderId != ''">
            and lo.id != #{ownOrderId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="selectWaybillOrderCarListGroupByTruckNo" resultType="com.sckw.transport.model.dto.OrderCarDTO">
        SELECT
        a.truck_no AS truckNo,
        COUNT( a.truck_no ) AS count,
        a.driver_id AS driverId,
        a.driver_name AS driverName,
        a.driver_phone AS driverPhone,
        a.driver_idcard AS driverCard
        FROM
        `kwt_waybill_order` a
        left join kwt_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.l_order_id = b.id
        AND B.del_flag = 0
        AND b.del_flag = 0
        <where>
            <if test="ids != null and ids.size() > 0">
                and c.l_order_id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </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>
        GROUP BY
        a.truck_no
        limit #{page},#{pageSize}
    </select>

    <select id="findStatisticsCountByDriver" resultType="java.util.Map" parameterType="java.util.Map">
        select tabA.total + tabB.total as waitTotal,
               tabC.total                 departureTotal,
               tabD.total                 executeTotal,
               tabE.total                 endTotal
        from (SELECT count(1) total,
                     0        indexNum
              FROM kwt_logistics_order_circulate loc
              LEFT JOIN kwt_logistics_order lo on lo.id = loc.l_order_id
              where loc.del_flag = 0
                and lo.del_flag = 0
                and loc.status = 0
                and loc.w_order_id is null
                and loc.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and loc.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
                ) tabA
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
              left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
              LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status = 1
                and wo.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
                ) tabB on tabB.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
              left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
              LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status = 2
                and wo.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
                ) tabC on tabC.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
              left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
               LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status in (3, 4, 5, 6)
                and wo.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
                ) tabD on tabD.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
               left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
               LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status in (7, 8, 9, 10, 12, 13)
                and wo.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
            ) tabE on tabE.indexNum = tabA.indexNum
    </select>

    <select id="findStatisticsSumByDriver" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT
        count(1) total, IFNULL(sum(wo.entrust_amount), 0.0) entrustAmount, IFNULL(sum(wo.load_amount), 0.0) loadAmount,
        IFNULL(sum(wo.unload_amount), 0.0) unloadAmount, IFNULL(sum(wo.deficit_amount), 0.0) deficitAmount,
        IFNULL(sum(wo.deficit_price), 0.0) deficitPrice
        FROM kwt_waybill_order wo
        left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
        LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
        where wo.del_flag = 0 and lo.del_flag = 0
        <if test="busStatus != null and busStatus == 4">
            and wo.status in (7, 8, 9, 10, 12, 13)
        </if>
        <if test="driverId != null and driverId != ''">
            and wo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="month != null and month != ''">
            and LEFT(wo.create_time, 7) = #{month, jdbcType=TIMESTAMP}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE(wo.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE(wo.create_time) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="specialEntIds != null and specialEntIds.size() > 0">
            and wo.ent_id in
            <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
    </select>

    <select id="deficitLossAmountByDriver" parameterType="java.util.Map">
        SELECT
<!--            FORMAT(IFNULL(sum(deficitLossAmount), 0.0), 2) deficitLossAmount-->
        IFNULL(sum(deficitLossAmount), 0.0) deficitLossAmount
        from (
            SELECT
<!--            ( deficitAmount - FORMAT((loadAmount * loss / lossUnit),2) ) deficitLossAmount-->
            ( deficitAmount - (loadAmount * loss / lossUnit) ) deficitLossAmount
            from (
                SELECT
                IFNULL(wo.deficit_amount, 0.0) deficitAmount,
                IFNULL(lo.loss, 0.0) loss,
                IFNULL(wo.load_amount, 0.0) loadAmount,
                case when lo.loss_unit = 0 then 1000 when lo.loss_unit = 0 then 100 ELSE 0 end as lossUnit
                from kwt_waybill_order wo
                left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
                left join kwt_logistics_order lo on lo.id = c.l_order_id
                where wo.del_flag = 0 and lo.del_flag = 0
                <if test="busStatus != null and busStatus == 4">
                    and wo.status in (7, 8, 9, 10, 12, 13)
                    and wo.deficit_amount > 0
                </if>
                <if test="driverId != null and driverId != ''">
                    and wo.driver_id = #{driverId, jdbcType=BIGINT}
                </if>
                <if test="month != null and month != ''">
                    and LEFT(wo.create_time, 7) = #{month, jdbcType=TIMESTAMP}
                </if>
                <if test="startTime != null and startTime != ''">
                    and DATE(wo.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
                </if>
                <if test="endTime != null and endTime != ''">
                    and DATE(wo.create_time) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
                </if>
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
            ) tab
        ) tab1
    </select>

    <select id="findWaitWaybillOrderByDriver" resultType="com.sckw.transport.model.vo.WaybillOrderDriverVo"
            parameterType="java.util.Map">
        SELECT *
        from (SELECT wo.id               wOrderId,
                     wo.w_order_no       wOrderNo,
                     wo.l_order_id       lOrderId,
                     lo.l_order_no       lOrderNo,
                     wo.type,
                     lo.amount - lo.entrust_amount - lo.subcontract_amount residueAmount,
                     lo.unit,
                     lo.loss_unit lossUnit,
                     wo.ent_id           carrierEntId,
                     'xxxxx'             carrierFirmName,
                     log.goods_name      goodsName,
                     wo.start_time       startTime,
                     wo.end_time         endTime,
                     wo.entrust_amount   entrustAmount,
                     woa1.name           loadName,
                     woa1.city_code      loadCityCode,
                     woa1.city_name      loadCityName,
                     woa1.detail_address loadDetailAddress,
                     woa2.name           unloadName,
                     woa2.city_code      unloadCityCode,
                     woa2.city_name      unloadCityName,
                     woa2.detail_address unloadDetailAddress,
                     IFNULL(wo.load_amount, 0.0)      loadAmount,
                     IFNULL(wo.unload_amount, 0.0)    unloadAmount,
                     IFNULL(wo.deficit_amount, 0.0)   deficitAmount,
                     IFNULL(wo.deficit_price, 0.0)    deficitPrice,
                     wo.truck_no         truckNo,
                     wo.status,
                     wo.create_time      createTime
              FROM kwt_waybill_order wo
                  left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
                       LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
                       LEFT JOIN kwt_waybill_order_address woa1 on woa1.w_order_id = wo.id and woa1.address_type = 1
                       LEFT JOIN kwt_waybill_order_address woa2 on woa2.w_order_id = wo.id and woa2.address_type = 2
                       LEFT JOIN kwt_logistics_order_goods log on log.l_order_id = wo.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status = 1
                and wo.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and wo.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
              UNION
              SELECT loc.id              wOrderId,
                     null                wOrderNo,
                     loc.l_order_id      lOrderId,
                     lo.l_order_no       lOrderNo,
                     loc.type,
                     lo.amount - lo.entrust_amount - lo.subcontract_amount residueAmount,
                     lo.unit,
                     lo.loss_unit lossUnit,
                     loc.ent_id          carrierEntId,
                     'xxxxx'             carrierFirmName,
                     log.goods_name      goodsName,
                     loc.start_time      startTime,
                     loc.end_time        endTime,
                     loc.entrust_amount  entrustAmount,
                     loa1.name           loadName,
                     loa1.city_code      loadCityCode,
                     loa1.city_name      loadCityName,
                     loa1.detail_address loadDetailAddress,
                     loa2.name           unloadName,
                     loa2.city_code      unloadCityCode,
                     loa2.city_name      unloadCityName,
                     loa2.detail_address unloadDetailAddress,
                     0.0                loadAmount,
                     0.0                unloadAmount,
                     0.0                deficitAmount,
                     0.0                deficitPrice,
                     loc.truck_no        truckNo,
                     1                   status,
                     loc.update_time     createTime
              FROM kwt_logistics_order_circulate loc
                LEFT JOIN kwt_logistics_order lo on lo.id = loc.l_order_id
                LEFT JOIN kwt_logistics_order_address loa1 on loa1.l_order_id = lo.id and loa1.address_type = 1
                LEFT JOIN kwt_logistics_order_address loa2 on loa2.l_order_id = lo.id and loa2.address_type = 2
                LEFT JOIN kwt_logistics_order_goods log on log.l_order_id = loc.l_order_id
              where loc.del_flag = 0
                and lo.del_flag = 0
                and loc.status = 0
                and loc.w_order_id is null
                and loc.driver_id = #{driverId, jdbcType=BIGINT}
                <if test="specialEntIds != null and specialEntIds.size() > 0">
                    and loc.ent_id in
                    <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </if>
        ) tab
        order by startTime
    </select>

    <select id="findOtherWaybillOrderByDriver" resultType="com.sckw.transport.model.vo.WaybillOrderDriverVo"
            parameterType="java.util.Map">
        SELECT
        wo.id wOrderId, wo.w_order_no wOrderNo, wo.l_order_id lOrderId, lo.l_order_no lOrderNo,
        lo.unit, lo.loss, lo.loss_unit lossUnit, wo.type,
        wo.ent_id carrierEntId, 'xxxxx' carrierFirmName, log.goods_name goodsName, wo.start_time startTime,
        wo.end_time endTime, wo.entrust_amount entrustAmount, woa1.name loadName, woa1.city_code loadCityCode,
        woa1.city_name loadCityName, woa1.detail_address loadDetailAddress, woa2.name unloadName, woa2.city_code
        unloadCityCode, woa2.city_name unloadCityName, woa2.detail_address unloadDetailAddress, IFNULL(wo.load_amount, 0.0) loadAmount,
        IFNULL(wo.unload_amount, 0.0) unloadAmount, IFNULL(wo.deficit_amount, 0.0) deficitAmount, IFNULL(wo.deficit_price, 0.0) deficitPrice,
        wo.truck_no truckNo, wo.status, wo.create_time createTime
        FROM kwt_waybill_order wo
        left join kwt_waybill_order_subtask c on wo.id = c.w_order_id
        LEFT JOIN kwt_logistics_order lo on lo.id = c.l_order_id
        LEFT JOIN kwt_waybill_order_address woa1 on woa1.w_order_id = wo.id and woa1.address_type = 1
        LEFT JOIN kwt_waybill_order_address woa2 on woa2.w_order_id = wo.id and woa2.address_type = 2
        LEFT JOIN kwt_logistics_order_goods log on log.l_order_id = wo.l_order_id
        where wo.del_flag = 0 and lo.del_flag = 0
        <if test="specialEntIds != null and specialEntIds.size() > 0">
            and wo.ent_id in
            <foreach collection="specialEntIds" item="item" open="(" close=")" separator=",">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
        <if test="driverId != null and driverId != ''">
            and wo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="busStatus != null and busStatus == 2">
            and wo.status = 2
        </if>
        <if test="busStatus != null and busStatus == 3">
            and wo.status in (3, 4, 5, 6)
        </if>
        <if test="busStatus != null and busStatus == 4">
            and wo.status in (7, 8, 9, 10, 12, 13)
        </if>
        <if test="month != null and month != ''">
            and LEFT(wo.create_time, 7) = #{month, jdbcType=TIMESTAMP}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE(wo.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE(wo.create_time) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="busStatus != null and busStatus == 2">
            ORDER BY wo.start_time
        </if>
        <if test="busStatus != null and busStatus == 4">
            ORDER BY CASE WHEN wo.status = 12 THEN  wo.status * -1  ELSE wo.status END ASC,
            wo.update_time desc
        </if>
    </select>

    <select id="selectWaybillOrderCarListNotPage" 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 driverCard,
               a.truck_no      as truckNo,
               a.status        as status,
               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_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.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="selectWaybillOrderCarListByTradeOrderId" 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 driverCard,
        a.truck_no      as truckNo,
        a.status        as status,
        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_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.l_order_id = b.id
        WHERE a.del_flag = 0
        and b.del_flag = 0
        and c.del_flag = 0
        <if test="ids != null and ids.size() > 0">
            and b.t_order_id in
            <foreach collection="ids" item="item" close=")" open="(" separator=",">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
    </select>

    <select id="selectWaybillOrderCountByStatus" resultType="java.lang.Long">
        SELECT COUNT(id)
        FROM kwt_waybill_order
        WHERE ent_id = #{entId}
        AND del_flag = 0
        <if test="createBys != null and createBys.size() > 0">
            AND create_by IN
            <foreach collection="createBys" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="statuses != null and statuses.size() > 0">
            AND status IN
            <foreach collection="statuses" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
            w_order_no LIKE concat('%',#{keyword},'%')
            OR truck_no LIKE concat('%',#{keyword},'%')
            OR driver_name LIKE concat('%',#{keyword},'%')
            OR driver_phone LIKE concat('%',#{keyword},'%')
            )
        </if>
    </select>

    <select id="selectOrderByOrderId" resultType="java.util.Map">
        SELECT COUNT(a.id) total,
               IFNULL(SUM(a.entrust_amount),0) AS amount
          FROM kwt_waybill_order a
          left join kwt_waybill_order_subtask b on a.id = b.w_order_id
         WHERE b.l_order_id = #{orderId}
           AND a.type = #{type}
           and a.del_flag = 0
        <if test="statusList != null and statusList.size() > 0">
            and a.status not in
            <foreach collection="statusList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectWaybillOrderAmountByStatus" resultType="java.util.Map">
        SELECT
        COUNT(id) total,
        SUM(load_amount) loadAmount,
        SUM(unload_amount) unloadAmount,
        SUM(load_amount - unload_amount) lossAmount,
        SUM( IF(load_amount - unload_amount - deficit_amount > 0, load_amount - unload_amount - deficit_amount , 0) ) deficitRealAmount
        FROM
        kwt_waybill_order
        WHERE ent_id = #{entId}
        AND del_flag = 0
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
            w_order_no LIKE concat('%',#{keyword},'%')
            OR truck_no LIKE concat('%',#{keyword},'%')
            OR driver_name LIKE concat('%',#{keyword},'%')
            OR driver_phone LIKE concat('%',#{keyword},'%')
            )
        </if>
        <if test="statuses != null and statuses.size() > 0">
            AND status IN
            <foreach collection="statuses" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectWaybillOrdersByStatus" resultType="com.sckw.transport.model.dto.WaybillAmountDTO">
        SELECT
        wo.load_amount as loadAmount,
        wo.unload_amount as unloadAmount,
        wo.deficit_amount as DeficitAmount,
        lo.loss as loss,
        lo.loss_unit as lossUnit
        FROM
        kwt_waybill_order AS wo
        left join kwt_waybill_order_subtask b on wo.id = b.w_order_id
        LEFT JOIN kwt_logistics_order AS lo ON lo.id = b.l_order_id
        WHERE wo.ent_id = #{entId}
        AND wo.del_flag = 0
        <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
            AND wo.create_time BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="createBys != null and createBys.size() > 0">
            AND wo.create_by IN
            <foreach collection="createBys" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="statuses != null and statuses.size() > 0">
            AND wo.status IN
            <foreach collection="statuses" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
            wo.w_order_no LIKE concat('%',#{keyword},'%')
            OR wo.truck_no LIKE concat('%',#{keyword},'%')
            OR wo.driver_name LIKE concat('%',#{keyword},'%')
            OR wo.driver_phone LIKE concat('%',#{keyword},'%')
            )
        </if>
    </select>

    <select id="getMaxOrderNo" resultType="java.lang.String">
        SELECT w_order_no FROM `kwt_waybill_order`  WHERE w_order_no LIKE concat(#{lOrderNo},'%') ORDER BY w_order_no DESC LIMIT 1
    </select>

    <select id="findWaybillOrder" resultType="com.sckw.transport.model.KwtWaybillOrder" parameterType="java.util.Map">
        SELECT
        kwo.*
        FROM kwt_waybill_order kwo
        left join kwt_waybill_order_subtask b on kwo.id = b.w_order_id and b.del_flag = 0
        where kwo.del_flag = 0
        <if test="tOrderId != null and tOrderId != ''">
            and kwo.t_order_id = #{tOrderId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and b.l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="driverId != null and driverId != ''">
            and kwo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and kwo.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="status != null and status != ''">
            and kwo.status = #{status, jdbcType=INTEGER}
        </if>
        <if test="busStatus != null and busStatus == 2">
            and kwo.status = 2
        </if>
        <if test="busStatus != null and busStatus == 3">
            and kwo.status in (3, 4, 5, 6)
        </if>
        <if test="busStatus != null and busStatus == 4">
            and kwo.status in (7, 8, 9, 10, 12, 13)
        </if>
        <choose>
            <when test="lOrderIds != null and lOrderIds != '' and lOrderIds.size() > 0">
                and b.l_order_id in
                <foreach collection="lOrderIds" item="lOrderId" open="(" close=")" separator=",">
                    #{lOrderId,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
    </select>

    <select id="selectInvalidWaybillOrderCarListGroupByTruckNo" resultType="com.sckw.transport.model.dto.OrderCarDTO">
        SELECT
        a.truck_no AS truckNo,
        COUNT( a.truck_no ) AS count,
        a.driver_id AS driverId,
        a.driver_name AS driverName,
        a.driver_phone AS driverPhone,
        a.driver_idcard AS driverCard
        FROM
        `kwt_waybill_order` a
        left join kwt_waybill_order_subtask c on a.id = c.w_order_id
        LEFT JOIN kwt_logistics_order b ON c.l_order_id = b.id
        AND B.del_flag = 0
        AND b.del_flag = 0
        <where>
            <if test="ids != null and ids.size() > 0">
                and b.l_order_id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="statusList != null and statusList.size() > 0">
                and a.status not in
                <foreach collection="statusList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
        a.truck_no
        limit #{page},#{pageSize}
    </select>

    <select id="queryList" resultType="com.sckw.transport.api.model.vo.LogisticsVo">
        SELECT d.firm_name,
               aa.create_time,
               aa.w_order_no,
               aa.status,
               aa.unload_time,
               g.goods_name,
               aa.entrust_amount taskAmount,
               aa.unload_amount

        FROM kwt_waybill_order_subtask aa
                 LEFT JOIN kwt_waybill_order a on a.id = aa.w_order_id and a.del_flag = 0
                 LEFT JOIN kwt_logistics_order b ON a.l_order_id = b.id and b.del_flag = 0
                 LEFT JOIN kwt_logistics_order_unit c ON b.id = c.l_order_id AND c.unit_type = 1 AND c.del_flag = 0
                 LEFT JOIN kwt_logistics_order_unit d ON b.id = d.l_order_id AND d.unit_type = 2 AND d.del_flag = 0
                 LEFT JOIN kwt_logistics_order_goods g ON b.id = g.l_order_id AND g.del_flag = 0
        <where>
            aa.del_flag = 0
              and aa.status = 20
            <if test="ids != null and ids.size() != 0">
                and c.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            order by aa.unload_time desc
            limit 10
        </where>
    </select>

    <select id="countLogistics" resultType="com.sckw.transport.api.model.vo.WaybillVo">
        SELECT d.firm_name,
               (case b.billing_mode
                    when 1 then aa.load_amount * b.price
                    when 2 then aa.unload_amount * b.price end) as money

        FROM kwt_waybill_order_subtask aa
                 inner JOIN kwt_waybill_order a on a.id = aa.w_order_id and a.del_flag = 0
                 inner JOIN kwt_logistics_order b ON a.l_order_id = b.id and b.del_flag = 0
                 inner JOIN kwt_logistics_order_unit c ON b.id = c.l_order_id AND c.unit_type = 1 AND c.del_flag = 0
                 inner JOIN kwt_logistics_order_unit d ON b.id = d.l_order_id AND d.unit_type = 2 AND d.del_flag = 0
        <where>
            aa.del_flag = 0
              and aa.status = 20
            <if test="ids != null and ids.size() != 0">
                and c.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and aa.create_time between #{para.startTime} and #{para.endTime}
        </where>
    </select>

    <select id="logisticsRank" resultType="com.sckw.transport.api.model.vo.WaybillRankVo">
        SELECT d.firm_name,
               (case b.billing_mode
                    when 1 then aa.load_amount
                    when 2 then aa.unload_amount end) as amount

        FROM kwt_waybill_order_subtask aa
                 inner JOIN kwt_waybill_order a on a.id = aa.w_order_id and a.del_flag = 0
                 inner JOIN kwt_logistics_order b ON a.l_order_id = b.id and b.del_flag = 0
                 inner JOIN kwt_logistics_order_unit c ON b.id = c.l_order_id AND c.unit_type = 1 AND c.del_flag = 0
                 inner JOIN kwt_logistics_order_unit d ON b.id = d.l_order_id AND d.unit_type = 2 AND d.del_flag = 0
        <where>
            aa.del_flag = 0
              and aa.status = 20
            <if test="ids != null and ids.size() != 0">
                and c.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and aa.create_time between #{para.startTime} and #{para.endTime}
            order by amount desc
            limit 5
        </where>
    </select>
</mapper>
