<?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.KwtWaybillOrderV1Mapper">

    <select id="statistics" resultType="com.sckw.core.model.vo.TableTop"
            parameterType="com.sckw.transport.model.dto.WaybillOrderQueryDto">
        SELECT
        wo.status 'value',
        count(1) total
        from kwt_waybill_order wo
        where wo.del_flag = 0
        <if test="entId != null and entId != ''">
            and wo.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="userIds != null and userIds.size() > 0">
            and wo.create_by IN
            <foreach collection="userIds" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="(loadCityCode != null and loadCityCode != '') or (unloadCityCode != null and unloadCityCode != '')">
            and wo.id in (
            SELECT DISTINCT w_order_id from kwt_waybill_order_address where del_flag = 0
            <if test="loadCityCode != null and loadCityCode != ''">
                and address_type = 1
                and left(city_code, #{loadCityCodeLevel, jdbcType=INTEGER}) = left( #{loadCityCode, jdbcType=INTEGER}, #{loadCityCodeLevel, jdbcType=INTEGER} )
            </if>
            <if test="unloadCityCode != null and unloadCityCode != ''">
                and address_type = 2
                and left(city_code, #{loadCityCodeLevel, jdbcType=INTEGER}) = left( #{unloadCityCode, jdbcType=INTEGER}, #{loadCityCodeLevel, jdbcType=INTEGER} )
            </if>
            )
        </if>
        <if test="(startLoadTime != null and startLoadTime != '') or (endLoadTime != null and endLoadTime != '')
               or (startUnloadTime != null and startUnloadTime != '') or (endUnloadTime != null and endUnloadTime != '')">
            and wo.id in (
                SELECT DISTINCT w_order_id from kwt_waybill_order_subtask where del_flag = 0
            <if test="startLoadTime != null and startLoadTime != ''">
                    and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endLoadTime != null and endLoadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="startUnloadTime != null and startUnloadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endUnloadTime != null and endUnloadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            )
        </if>
        <if test="startSendCarTime != null and startSendCarTime != '' " >
            and DATE( wo.create_time) <![CDATA[ >= ]]> #{startSendCarTime,jdbcType=TIMESTAMP}
        </if>
        <if test="startSendCarTime != null and startSendCarTime != '' " >
            and DATE( wo.create_time ) <![CDATA[ <= ]]> #{endSendCarTime,jdbcType=TIMESTAMP}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
                wo.w_order_no like concat('%',#{keywords},'%')
                or wo.truck_no like concat('%',#{keywords},'%')
                or wo.driver_name like concat('%',#{keywords},'%')
            )
        </if>
        GROUP BY wo.status
    </select>

    <select id="findListPage" resultType="com.sckw.transport.model.vo.WaybillOrderSelectVo"
            parameterType="com.sckw.transport.model.dto.WaybillOrderQueryDto">
        SELECT
        wo.id wOrderId, wo.ent_id entId, wo.w_order_no wOrderNo, wo.type, wo.truck_no truckNo, wo.driver_name driverName,
        wo.driver_phone driverPhone, wo.type, wo.create_time sendCarTime, wo.create_by createBy, wo.create_time createTime,
        wo.update_time updateTime, wo.status
        from kwt_waybill_order wo
        where wo.del_flag = 0
        <if test="lOrderId != null and lOrderId != ''">
            and exists(select 1 from kwt_waybill_order_subtask wos where wo.id = wos.w_order_id and wos.l_order_id = #{lOrderId})
        </if>
        <if test="lOrderId == null or lOrderId == ''">
            <if test="entId != null and entId != ''">
                and wo.ent_id = #{entId, jdbcType=VARCHAR}
            </if>
            <if test="userIds != null and userIds.size() > 0">
                and wo.create_by IN
                <foreach collection="userIds" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="entIds != null and entIds.size() > 0">
                and wo.ent_id IN
                <foreach collection="entIds" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
        </if>
        <if test="queryWstatus != null and queryWstatus != ''">
            AND wo.status in
            <foreach collection="queryWstatus.split(',')" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="(loadCityCode != null and loadCityCode != '') or (unloadCityCode != null and unloadCityCode != '')">
            and wo.id in (
            SELECT DISTINCT w_order_id from kwt_waybill_order_address where del_flag = 0
            <if test="loadCityCode != null and loadCityCode != ''">
                and address_type = 1
                and left(city_code, #{loadCityCodeLevel, jdbcType=INTEGER}) = left( #{loadCityCode, jdbcType=INTEGER}, #{loadCityCodeLevel, jdbcType=INTEGER} )
            </if>
            <if test="unloadCityCode != null and unloadCityCode != ''">
                and address_type = 2
                and left(city_code, #{unloadCityCodeLevel, jdbcType=INTEGER}) = left( #{unloadCityCode, jdbcType=INTEGER}, #{unloadCityCodeLevel, jdbcType=INTEGER} )
            </if>
            )
        </if>
        <if test="(startLoadTime != null and startLoadTime != '') or (endLoadTime != null and endLoadTime != '')
               or (startUnloadTime != null and startUnloadTime != '') or (endUnloadTime != null and endUnloadTime != '')">
            and wo.id in (
            SELECT DISTINCT w_order_id from kwt_waybill_order_subtask where del_flag = 0
            <if test="startLoadTime != null and startLoadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endLoadTime != null and endLoadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="startUnloadTime != null and startUnloadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endUnloadTime != null and endUnloadTime != ''">
                and DATE(load_time) <![CDATA[ >= ]]> #{startLoadTime,jdbcType=TIMESTAMP}
            </if>
            )
        </if>
        <if test="startSendCarTime != null and startSendCarTime != '' " >
            and DATE( wo.create_time) <![CDATA[ >= ]]> #{startSendCarTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endSendCarTime != null and endSendCarTime != '' " >
            and DATE( wo.create_time ) <![CDATA[ <= ]]> #{endSendCarTime,jdbcType=TIMESTAMP}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
            wo.w_order_no like concat('%',#{keywords},'%')
            or wo.truck_no like concat('%',#{keywords},'%')
            or wo.driver_name like concat('%',#{keywords},'%')
            )
        </if>
        order by wo.update_time desc
    </select>

    <select id="findCapacityListPage" resultType="com.sckw.transport.model.vo.WaybillOrderSelectVo"
            parameterType="com.sckw.transport.model.dto.WaybillSubtaskOrderQueryDto">
        SELECT
        wo.driver_id driverId, wo.driver_name driverName, wo.driver_phone driverPhone, wo.driver_idcard driverIdcard,
        wo.truck_id truckId, wo.truck_no truckNo, count(1) count
        from kwt_waybill_order wo
        left join kwt_waybill_order_subtask wos on wos.w_order_id = wo.id
        WHERE wo.del_flag = 0 and wos.del_flag = 0
        <if test="ids != null and ids != ''">
            AND wos.l_order_id IN
            <foreach collection="ids.split(',')" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="findStatisticsCountByDriver" resultType="java.util.Map">
        SELECT
        count(case when wo.status = 101 then 1 end) waitTotal,
        count(case when wo.status = 201 then 1 end) departureTotal,
        count(case when wo.status in (203, 301, 302, 401, 402) then 1 end) executeTotal,
        count(case when wo.status in (501, 502, 503, 504, 103, 202) then 1 end) endTotal
        from kwt_waybill_order wo
        where wo.del_flag = 0
        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>
    </select>

    <select id="findWaybillOrderSubtask" resultType="com.sckw.transport.model.vo.WaybillOrderSubtaskVo" parameterType="java.util.Map">
        SELECT
        wo.id, wo.id wOrderId, wo.ent_id entId, wo.w_order_no wOrderNo, wo.truck_no truckNo, wo.driver_name driverName, wo.driver_phone driverPhone,
        wo.driver_idcard driverIdcard, wo.type, wo.create_time sendCarTime, wo.create_time createTime, wo.update_time updateTime, wo.status, wos.unit,
        wos.load_amount loadAmount, wos.unload_amount unloadAmount, wos.load_time loadTime, wos.unload_time unloadTime, wos.entrust_amount entrustAmount,
        wos.deficit_amount deficitAmount, wos.deficit_price deficitPrice, wos.id wSubtaskId
        from kwt_waybill_order wo
        left join kwt_waybill_order_subtask wos on wos.w_order_id = wo.id
        where wo.del_flag = 0 and wos.del_flag = 0
        <if test="entId != null and entId != ''">
            and wo.ent_id = #{entId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and wos.l_order_id = #{lOrderId, jdbcType=BIGINT}
        </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>
        <if test="driverId != null and driverId != ''">
            and wo.driver_id = #{driverId, jdbcType=BIGINT}
        </if>
        <if test="busAppStatus != null and busAppStatus != ''">
            and wos.status = #{busAppStatus}
        </if>
        <if test="busStatus != null and busStatus == 1">
            and wo.status = 101
        </if>
        <if test="busStatus != null and busStatus == 2">
            and wo.status = 201
        </if>
        <if test="busStatus != null and busStatus == 3">
            and wo.status in (203, 301, 302, 401, 402)
        </if>
        <if test="busStatus != null and busStatus == 4">
            and wo.status in (103, 202, 501, 502, 503, 504)
        </if>
        <if test="month != null and month != ''">
            and LEFT(wo.create_time, 7) = #{month, jdbcType=TIMESTAMP}
        </if>
        <if test="startTime != null and startTime != ''">
            and LEFT(wo.create_time, 10) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != ''">
            and LEFT(wo.create_time, 10) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="busStatus != null and busStatus == 2">
            ORDER BY wos.load_time
        </if>
        <if test="busStatus != null and busStatus == 4">
            ORDER BY CASE WHEN wo.status = 503 THEN  wo.status * -1  ELSE wo.status END ASC, wo.update_time desc
        </if>
    </select>

    <select id="findCountByDriverOrTruck" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT
        count(case when wo.status = 502 then 1 end) totalComplete,
        count(case when wo.status not in (101, 102, 103, 202) then 1 end) totalTake,
        count(case when wo.status in (203, 301, 302, 401, 402) then 1 end) totalExecute
        from kwt_waybill_order wo
        left join kwt_waybill_order_subtask wos on wos.w_order_id = wo.id
        where wo.del_flag = 0 and wos.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>
    </select>

    <select id="findCountByLOrderId" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT
        count(case when wo.status = 502 then 1 end) totalComplete
        from kwt_waybill_order wo
        left join kwt_waybill_order_subtask wos on wos.w_order_id = wo.id
        where wo.del_flag = 0 and wos.del_flag = 0
        <if test="lOrderId != null and lOrderId != ''">
            and wos.l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="findAmountByDriverOrTruck" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT
        IFNULL(SUM(wos.load_amount), 0.0) loadAmount
        from kwt_waybill_order_subtask wos
        left join kwt_waybill_order wo on wos.w_order_id = wo.id
        where wo.del_flag = 0 and wos.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>
    </select>

    <select id="findUnfinishedByTruck" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT count(1) totalExecute from kwt_waybill_order wo
        where wo.del_flag = 0
        and wo.status in (101, 201, 203, 301, 302, 401, 402)
        <if test="type != null and type != ''">
            and wo.type = #{type, jdbcType=INTEGER}
        </if>
        <if test="truckId != null and truckId != ''">
            and wo.truck_id = #{truckId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="findTicketCountByAddress" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT
        IFNULL(sum(wot.amount), 0.0) amount,
        MIN(wot.operate_time) loadTime,
        MAX(wot.operate_time) unloadTime
        from kwt_waybill_order_ticket wot
        left join kwt_waybill_order_address woa on woa.id = wot.w_address_id
        left join kwt_logistics_order_address loa on loa.id = woa.l_address_id
        left join kwt_logistics_order lo on lo.id = loa.l_order_id
        left join kwt_waybill_order wo on wo.id = wot.w_order_id
        where wot.del_flag = 0
        <if test="passStatus != null and passStatus != ''">
            and wo.status = 502
        </if>
        <if test="wOrderId != null and wOrderId != ''">
            and wot.w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
        <if test="type != null and type != ''">
            and wot.type = #{type, jdbcType=VARCHAR}
        </if>
        <if test="unit != null and unit != ''">
            and wot.unit = #{unit, jdbcType=VARCHAR}
        </if>
        <if test="lAddressId != null and lAddressId != ''">
            and loa.id = #{lAddressId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and lo.id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, lo.pids)
        </if>
    </select>

    <select id="findTicketAmountByAddress" resultType="com.sckw.transport.model.vo.WaybillCountVo" >
        SELECT
        IFNULL(sum(wot.amount), 0.0) amount
        from kwt_waybill_order_ticket wot
        left join kwt_waybill_order_address woa on woa.id = wot.w_address_id
        left join kwt_waybill_order wo on wo.id = wot.w_order_id
        where wot.del_flag = 0 and wo.status = 502
        <if test="type != null and type != ''">
            and wot.type = #{type, jdbcType=VARCHAR}
        </if>
        <if test="unit != null and unit != ''">
            and wot.unit = #{unit, jdbcType=VARCHAR}
        </if>
        <choose>
            <when test="lAddressIds != null and lAddressIds != '' and lAddressIds.size() > 0">
                and woa.l_address_id in
                <foreach collection="lAddressIds" item="lAddressId" open="(" close=")" separator=",">
                    #{lAddressId,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
    </select>

    <select id="selectWaybillOrderCarPage" parameterType="java.util.Map" resultType="com.sckw.transport.model.dto.OrderCarDTO">
        SELECT
        kwo.id as wOrderId,
        kwo.w_order_no    as wOrderNo,
        kwo.driver_id     as driverId,
        kwo.driver_name   as driverName,
        kwo.driver_phone  as driverPhone,
        kwo.driver_idcard as driverCard,
        kwo.truck_no      as truckNo,
        kwo.status        as status,
        kwos.l_order_id    as lOrderId,
        klo.l_order_no    as lOrderNo,
        klo.t_order_id    as tOrderId,
        klo.t_order_no    as tOrderNo
        FROM kwt_waybill_order kwo
        left join kwt_waybill_order_subtask kwos on kwos.w_order_id = kwo.id
        LEFT JOIN kwt_logistics_order klo ON kwos.l_order_id = klo.id
        WHERE kwo.del_flag = 0 and kwos.del_flag = 0 and klo.del_flag = 0
        <if test="tOrderIds != null and tOrderIds.size() > 0">
            and klo.t_order_id in
            <foreach collection="tOrderIds" item="item" close=")" open="(" separator=",">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
    </select>

    <select id="findWaybillOrder" resultType="com.sckw.transport.model.KwtWaybillOrderV1" parameterType="java.util.Map">
        SELECT
        kwo.id, kwo.id wOrderId, kwo.w_order_no worderNo, kwo.type, kwo.ent_id carrierEntId, kwo.truck_id truckId, kwo.truck_no truckNo, kwo.status,
        kwo.create_time createTime, kwo.driver_id driverId, kwo.driver_name driverName, kwo.driver_phone driverPhone, kwo.ent_id entId
        FROM kwt_waybill_order kwo
        where kwo.del_flag = 0
        <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 = 201
        </if>
        <if test="busStatus != null and busStatus == 3">
            and kwo.status in (203, 301, 302, 401, 402)
        </if>
        <if test="busStatus != null and busStatus == 4">
            and kwo.status in (102, 103, 202, 501, 502, 503, 504)
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and kwo.id in (SELECT w_order_id from kwt_waybill_order_subtask where del_flag = 0 and l_order_id = #{lOrderId, jdbcType=BIGINT} ))
        </if>
        <choose>
            <when test="lOrderIds != null and lOrderIds != '' and lOrderIds.size() > 0">
                and kwo.id in (
                SELECT w_order_id from kwt_waybill_order_subtask where del_flag = 0 and l_order_id in
                <foreach collection="lOrderIds" item="lOrderId" open="(" close=")" separator=",">
                    #{lOrderId,jdbcType=BIGINT}
                </foreach>
                )
            </when>
        </choose>
    </select>

    <select id="findWaybillOrderCount" resultType="com.sckw.transport.model.vo.WaybillCountVo">
        SELECT
        count(case when c.status = 8 then 1 end) totalComplete,
        count(case when c.status not in (1, 9, 10, 11) then 1 end) totalTake,
        count(case when c.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(c.entrust_amount), 0.0) entrustAmount, IFNULL(SUM(c.load_amount), 0.0) loadAmount,
        IFNULL(SUM(c.unload_amount), 0.0) unloadAmount, IFNULL(SUM(c.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_subtask c on wo.id = c.w_order_id
        left join kwt_waybill_order_ticket wot1 on wot1.w_order_id = wo.id and wot1.type = 1 and c.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 c.unit = wot2.unit and wot1.type = 2  and wot2.del_flag = 0

        left join kwt_logistics_order lo on lo.id = c.l_order_id
        where c.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 c.status = 8
        </if>
        <if test="loadCountStatus != null and loadCountStatus != ''">
            and c.status in (5, 6, 7, 8, 12, 13)
        </if>
        <if test="unloadCountStatus != null and unloadCountStatus != ''">
            and c.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="selectDataByLorderId" resultType="java.lang.Integer">
        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="selectDataByLorderIds" resultType="java.lang.Integer">
        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="list != null and list.size() > 0">
                and b.id in <foreach collection="list" item="item" open="(" close=")" separator=",">  #{item}</foreach>
            </if>
            <if test="status != null">
                AND a.`status`= #{status}
            </if>
        </where>
    </select>

    <select id="selectOrderByOrderId" resultType="java.util.Map">
        SELECT COUNT(a.id) total,
        IFNULL(SUM(b.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="findWaybillOrderCountByLOrderId" resultType="java.lang.Integer">
        SELECT count(1) totalTake from kwt_waybill_order_subtask kwos
        left join kwt_waybill_order kwo on kwo.id = kwos.w_order_id
        left join kwt_logistics_order klo on klo.id = kwos.l_order_id
        where kwos.del_flag = 0 and kwo.status = 502
        and klo.id = #{lOrderId, jdbcType=BIGINT}
    </select>

    <select id="saleSum" resultType="com.sckw.transport.model.vo.WaybillOrderSaleDto">
        SELECT ROUND(SUM(b.unload_amount), 0)                   AS chl,
               ROUND(SUM(b.unload_amount * klo.goods_price), 0) AS xsl
        FROM kwt_waybill_order a
                 INNER JOIN kwt_waybill_order_subtask b
                            ON a.id = b.w_order_id AND b.del_flag = 0
                 INNER JOIN kwt_logistics_order klo
                            ON klo.id = a.l_order_id AND klo.del_flag = 0
                 INNER JOIN kwt_logistics_order_goods klog
                            ON klog.l_order_id = a.l_order_id AND klog.del_flag = 0
        inner JOIN kwt_logistics_order_unit d ON a.l_order_id = d.l_order_id AND d.unit_type = 1 AND d.del_flag = 0
        <where>
            a.del_flag = 0
              AND b.status = 25              -- 已完成
              AND b.update_time >= #{date}
              AND b.update_time &lt; #{end}
                and d.ent_id in (538038314096136193,538039617157337089,538040297439891457)
            <if test="list != null and list.size() != 0">
                AND klog.goods_type IN
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
