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

    <select id="findByWOrderId" resultType="com.sckw.transport.model.KwtWaybillOrderSubtask" >
        select
        id, ent_id entId, l_order_id lOrderId, w_order_id wOrderId, w_order_no wOrderNo, unit,
        entrust_amount entrustAmount, unload_amount unloadAmount, unload_time unloadTime,
        load_amount loadAmount, load_time loadTime, deficit_amount deficitAmount, deficit_price deficitPrice,
        remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_waybill_order_subtask
        where del_flag = 0
        <if test="wOrderId != null">
            and w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="findByOrderId" resultType="com.sckw.transport.model.KwtWaybillOrderSubtask" >
        select
        id, ent_id entId, l_order_id lOrderId, w_order_id wOrderId, w_order_no wOrderNo, unit,
        entrust_amount entrustAmount, unload_amount unloadAmount, unload_time unloadTime,
        load_amount loadAmount, load_time loadTime, deficit_amount deficitAmount, deficit_price deficitPrice,
        remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_waybill_order_subtask
        where del_flag = 0
        <if test="wOrderId != null and wOrderId != ''">
            and w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
    </select>

    <select id="findListPage" resultType="com.sckw.transport.model.vo.WaybillSubtaskSelectVo"
            parameterType="com.sckw.transport.model.dto.WaybillSubtaskOrderQueryDto">
        SELECT
                wos.w_order_id wOrderId,
                wos.unit,
                wos.entrust_amount entrustAmount,
                wos.unload_amount unloadAmount,
                wos.unload_time unloadDateTime,
                wos.load_time loadDateTime,
                wos.load_amount loadAmount,
                wos.deficit_amount deficitAmount,
                wos.deficit_price deficitPrice,
                wos.id wSubtaskId,
                wo.w_order_no wOrderNo,
                wo.driver_id driverId,
                wo.driver_name driverName,
                wo.driver_phone driverPhone,
                wo.driver_idcard driverIdcard,
                wo.truck_id truckId,
                wo.truck_no truckNo,
                wo.type,
                wo.create_by createBy,
                wo.create_time createTime,
                wo.create_time sendCarDate,
                wo.ent_id entId,
                wo.status,
                lo.l_order_no lOrderNo,
                lo.loss,
                lo.loss_unit lossLabel,
                lo.goods_price
        from kwt_waybill_order_subtask wos
        left join kwt_waybill_order wo on wo.id = wos.w_order_id
        left join kwt_logistics_order lo on lo.id = wos.l_order_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="selectCountByOrderId" resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM kwt_waybill_order_subtask
        WHERE del_flag = 0
        and l_order_id = #{orderId}
        <if test="orderStatusList != null and orderStatusList.size() > 0">
            AND status IN
            <foreach collection="orderStatusList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectLoopOrderAndDriverListByOrderId" resultType="com.sckw.transport.model.vo.DriverListVo">
        SELECT a.id            AS lOrderId,
               a.l_order_no    AS lOrderNo,
               b.driver_id     AS driverId,
               b.driver_name   AS driverName,
               b.driver_phone  AS driverPhone,
               b.driver_idcard AS driverIdCard,
               b.truck_no      AS truckNo,
               b.truck_id      AS truckId,
               c.STATUS        AS STATUS,
               b.type,
               count(IF(b.status in (502, 504, 503, 501), 1, NULL))
        FROM kwt_logistics_order a
                 LEFT JOIN
             kwt_waybill_order_subtask c ON a.id = c.l_order_id
                 LEFT JOIN kwt_waybill_order b on b.id = c.w_order_id
        <where>
            a.id = c.l_order_id
              AND b.type = #{type}
              AND a.del_flag = 0
              AND c.del_flag = 0
            <if test="orderStatus != null and orderStatus.size() != 0">
                and c.status in
                <foreach collection="orderStatus" item="item" separator="," close=")" open="(">
                    #{item}
                </foreach>
            </if>
            <if test="driverParam.lOrderId != null and driverParam.lOrderId != ''">
                AND a.id = #{driverParam.lOrderId}
            </if>
            <if test="driverParam.driverName != null and driverParam.driverName != '' or (driverParam.driverPhone != null and driverParam.driverPhone != '') or (driverParam.truckNo != null and driverParam.truckNo != '')">
                <!--                and (-->
                <if test="driverParam.driverName != null and driverParam.driverName != ''">
                    and b.driver_name like concat('%'
                        , #{driverParam.driverName}
                        , '%')
                </if>
                <if test="driverParam.driverPhone != null and driverParam.driverPhone != ''">
                    and b.driver_phone like concat('%'
                        , #{driverParam.driverPhone}
                        , '%')
                </if>
                <if test="driverParam.truckNo != null and driverParam.truckNo != ''">
                    and b.truck_no like concat('%'
                        , #{driverParam.truckNo}
                        , '%')
                </if>
                <!--                )-->
            </if>
        </where>
        GROUP BY b.truck_no
    </select>

    <select id="findWaybillOrderCount" parameterType="com.sckw.transport.model.dto.WaybillListAppDTO" resultType="java.util.Map">
        SELECT
        count(1) total,
        count(case when wo.status in (101, 201, 203, 301, 302, 401, 402, 501, 503, 504) then 1 end) proceed,
        count(case when wo.status = 502 then 1 end) finish,
        count(case when wo.status in (102, 103, 202) then 1 end) cancel,
        count(case when wo.status in (101, 201, 203, 301, 302, 401, 402, 501, 503, 504) then 1 end) ndexProceed,
        count(case when wo.status in (102, 103, 202, 502) then 1 end) ndexFinish
        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>
    </select>
</mapper>