<?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_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>

    <select id="findWaybillOrderCount" resultType="com.sckw.transport.model.vo.WaybillCountVo"
            parameterType="java.util.Map">
        SELECT
        count(case when wo.status not in (1, 9, 11) then 1 end) totalComplete,
        count(case when wo.status not in (1, 9, 10, 11) then 1 end) totalTake,
        count(case when wo.status not in (3, 4, 5, 6) then 1 end) totalExecute,
        count(DISTINCT case when lo.status not 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 wot1.del_flag = 0
        left join kwt_waybill_order_ticket wot2 on wot2.w_order_id = wo.id and wot2.type = 2 and wot2.del_flag = 0
        left join kwt_logistics_order lo on lo.id = wo.l_order_id
        where wo.del_flag = 0 and lo.del_flag = 0
        <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="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)
        </if>
        <if test="unloadCountStatus != null and unloadCountStatus != ''">
            and wo.status in (7, 8, 12)
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, pids)
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(id, #{belowlOrderId, jdbcType=VARCHAR})
        </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_logistics_order b ON a.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 a.l_order_id in
                <foreach collection="ids" 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">
        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}) tabA
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
                       LEFT JOIN kwt_logistics_order lo on lo.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}) tabB on tabB.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
                       LEFT JOIN kwt_logistics_order lo on lo.id = wo.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status = 2
                and wo.driver_id = #{driverId, jdbcType=BIGINT}) tabC on tabC.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
                       LEFT JOIN kwt_logistics_order lo on lo.id = wo.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}) tabD on tabD.indexNum = tabA.indexNum
                 left join
             (SELECT count(1) total,
                     0        indexNum
              FROM kwt_waybill_order wo
                       LEFT JOIN kwt_logistics_order lo on lo.id = wo.l_order_id
              where wo.del_flag = 0
                and lo.del_flag = 0
                and wo.status in (7, 8, 9, 10, 11, 12)
                and wo.driver_id = #{driverId, jdbcType=BIGINT}) 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_logistics_order lo on lo.id = wo.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, 11, 12)
        </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>
    </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,
                     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,
                     wo.load_amount      loadAmount,
                     wo.unload_amount    unloadAmount,
                     wo.deficit_amount   deficitAmount,
                     wo.deficit_price    deficitPrice,
                     wo.truck_no         truckNo,
                     wo.status,
                     wo.create_time      createTime
              FROM kwt_waybill_order wo
                       LEFT JOIN kwt_logistics_order lo on lo.id = wo.l_order_id
                       LEFT JOIN kwt_waybill_order_address woa1 on woa1.w_order_id = wo.id and woa1.type = 1
                       LEFT JOIN kwt_waybill_order_address woa2 on woa2.w_order_id = wo.id and woa2.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}
              UNION
              SELECT loc.id              wOrderId,
                     null                wOrderNo,
                     loc.l_order_id      lOrderId,
                     lo.l_order_no       lOrderNo,
                     loc.type,
                     loc.ent_id          carrierEntId,
                     'xxxxx'             carrierFirmName,
                     log.goods_name      goodsName,
                     loc.start_time      startTime,
                     loc.end_time        endTime,
                     loc.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,
                     null                loadAmount,
                     null                unloadAmount,
                     null                deficitAmount,
                     null                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_waybill_order_address woa1 on woa1.w_order_id = lo.id and woa1.type = 1
                       LEFT JOIN kwt_waybill_order_address woa2 on woa2.w_order_id = lo.id and woa2.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}) tab
        order by createTime desc
    </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, 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, wo.load_amount loadAmount,
        wo.unload_amount unloadAmount,
        wo.deficit_amount deficitAmount, wo.deficit_price deficitPrice, wo.truck_no truckNo, wo.status, wo.create_time
        createTime
        FROM kwt_waybill_order wo
        LEFT JOIN kwt_logistics_order lo on lo.id = wo.l_order_id
        LEFT JOIN kwt_waybill_order_address woa1 on woa1.w_order_id = wo.id and woa1.type = 1
        LEFT JOIN kwt_waybill_order_address woa2 on woa2.w_order_id = wo.id and woa2.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="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, 11, 12)
        </if>
        <if test="month != null and month != ''">
            and LEFT(wo.create_time, 7) = #{month, jdbcType=TIMESTAMP}
        </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.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}
    </select>
</mapper>