<?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.usual.mapper.KwWaybillOrderMapper">

    <select id="selectDataByLorderId" resultType="int">
        SELECT
        COUNT(b.id)
        FROM
        kw_waybill_order a
        LEFT JOIN kw_order b ON a.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.usual.entity.KwWaybillCountVo"
            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 kw_waybill_order_ticket wot1 on wot1.w_order_id = wo.id and wot1.type = 1 and wot1.del_flag = 0
        left join kw_waybill_order_ticket wot2 on wot2.w_order_id = wo.id and wot2.type = 2 and wot2.del_flag = 0
        left join kw_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="logisticsStatus != null and logisticsStatus != ''">
            and lo.status  = #{logisticsStatus, jdbcType=INTEGER}
        </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="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>
        <if test="ownOrderId != null and ownOrderId != ''">
            and lo.id != #{ownOrderId, jdbcType=BIGINT}
        </if>
    </select>


    <select id="findWaybillOrder" resultType="com.sckw.usual.entity.KwWaybillOrder" parameterType="java.util.Map">
        SELECT
        kwo.id id, kwo.type, kwo.ent_id carrierEntId, kwo.start_time startTime,
        kwo.end_time endTime, kwo.entrust_amount entrustAmount, kwo.load_amount loadAmount, kwo.unload_amount unloadAmount,
        kwo.deficit_amount deficitAmount, 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 kw_waybill_order kwo
        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 kwo.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 kwo.l_order_id in
                <foreach collection="lOrderIds" item="lOrderId" open="(" close=")" separator=",">
                    #{lOrderId,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
    </select>
    <select id="selectPageList" parameterType="com.sckw.usual.entity.req.waybill.WaybillPageListReq" resultType="com.sckw.usual.entity.res.waybill.WaybillPageListRes">
        select
            w.id as waybill_id,
            o.id as order_id,
            o.cy_company,
            o.ty_company,
            o.order_source,
            o.ow_system,
            o.order_no,
            w.truck_no,
            w.truck_id,
            w.driver_id,
            w.driver_name,
            w.t_order_no,
            w.status,
            w.t_type,
            w.unit,
            w.entrust_amount as transportNum,
            w.create_time
            from kw_waybill_order as w
        join kw_order as o on w.order_id = o.id
        <where>
            w.del_flag = 0 and o.del_flag = 0
            <if test="params.owSystem != null">
                and o.ow_system = #{params.owSystem}
            </if>
            <if test="params.waybillNo != '' and params.waybillNo != null">
                and w.t_order_no like concat('%',#{params.waybillNo},'%')
            </if>
            <if test="params.cyCompany != '' and params.cyCompany != null">
                and o.cy_company like concat('%',#{params.cyCompany},'%')
            </if>
            <if test="params.tyCompany != '' and params.tyCompany != null">
                and o.ty_company like concat('%',#{params.tyCompany},'%')
            </if>
            <if test="params.goodsName != '' and params.goodsName != null">
                and w.goods_name like concat('%',#{params.goodsName},'%')
            </if>
            <if test="params.truckNo != '' and params.truckNo != null">
                and w.truck_no like concat('%',#{params.truckNo},'%')
            </if>
            <if test="params.status != null">
                and w.status = #{params.status}
            </if>
            <if test="params.startTime != null and params.startTime != '' " >
                and DATE( w.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
            </if>
            <if test="params.endTime != null and params.endTime != '' " >
                and DATE( w.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
            </if>
            <if test="params.tenantId != null and params.tenantId != '' " >
                and o.tenant_id = #{params.tenantId}
            </if>
        </where>
        order by create_time desc

    </select>
    <select id="countByStatus" resultType="java.util.Map">
        SELECT `status`, COUNT(*) AS count FROM kw_waybill_order where del_flag = 0  GROUP BY `status`
    </select>
    <select id="findPathGoodsByWaybillIds" resultType="java.util.Map">
        select waybill_id, goods_name
    </select>
    <select id="findByWaybillIds" resultType="com.sckw.usual.entity.WaybillGoodsDto">
        SELECT * FROM kw_order_car_waybill_transport_path_goods WHERE del_flag = 0 and waybill_id IN
        <foreach item='id' collection='waybillIds' open='(' separator=',' close=')'>
            #{id}
        </foreach>
    </select>
</mapper>