<?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.KwtLogisticsOrderMapper">
    <resultMap id="BaseResultMap" type="com.sckw.transport.model.KwtLogisticsOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="type" jdbcType="BIGINT" property="type"/>
        <result column="t_order_id" jdbcType="BIGINT" property="tOrderId"/>
        <result column="t_order_no" jdbcType="VARCHAR" property="tOrderNo"/>
        <result column="l_order_no" jdbcType="VARCHAR" property="lOrderNo"/>
        <result column="pid" jdbcType="BIGINT" property="pid"/>
        <result column="pids" jdbcType="VARCHAR" property="pids"/>
        <result column="level" jdbcType="VARCHAR" property="level"/>
        <result column="total_load_amount" jdbcType="VARCHAR" property="totalLoadAmount"/>
        <result column="total_unload_amount" jdbcType="VARCHAR" property="totalUnloadAmount"/>
        <result column="settlement_cycle" jdbcType="BIGINT" property="settlementCycle"/>
        <result column="billing_mode" jdbcType="BIGINT" property="billingMode"/>
        <result column="price" jdbcType="DECIMAL" property="price"/>
        <result column="price_type" jdbcType="BIGINT" property="priceType"/>
        <result column="amount" jdbcType="DECIMAL" property="amount"/>
        <result column="unit" jdbcType="VARCHAR" property="unit"/>
        <result column="loss" jdbcType="DECIMAL" property="loss"/>
        <result column="loss_unit" jdbcType="VARCHAR" property="lossUnit"/>
        <result column="goods_price" jdbcType="DECIMAL" property="goodsPrice"/>
        <result column="goods_price_unit" jdbcType="VARCHAR" property="goodsPriceUnit"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <result column="subcontract_amount" jdbcType="DECIMAL" property="subcontractAmount"/>
        <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="ignore_amount" jdbcType="DECIMAL" property="ignoreAmount"/>
        <result column="deficit_amount" jdbcType="DECIMAL" property="deficitAmount"/>
        <result column="deficit_price" jdbcType="DECIMAL" property="deficitPrice"/>
        <result column="payment" jdbcType="BIGINT" property="payment"/>
        <result column="tax_rate" jdbcType="DECIMAL" property="taxRate"/>
        <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="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, t_order_id,type,level,total_load_amount,total_unload_amount, t_order_no, l_order_no, pid,pids, settlement_cycle, price, price_type,billing_mode,
    amount, unit, loss, loss_unit, goods_price, goods_price_unit, start_time, end_time,
    subcontract_amount, entrust_amount, unload_amount, load_amount, ignore_amount, deficit_amount,
    deficit_price, payment, tax_rate, remark, `status`, create_by, create_time, update_by,
    update_time, del_flag
    </sql>
    <select id="selectOrderList" resultType="com.sckw.transport.model.dto.LogisticsOrderDTO">
        SELECT a.id          as lOrderId,
               a.l_order_no  as lOrderNo,
               b.`status`    as `status`,
               a.t_order_id  as tOrderId,
               a.t_order_no  as tOrderNo,
               a.payment     as payment,
               a.amount,
               a.price,
               a.loss,
               a.goods_price as goodsPrice,
               c.firm_name   as comanyName,
               c.contacts,
               c.phone,
               a.create_by   as createBy,
               a.create_time as createTime
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_track b ON a.id = b.l_order_id and a.`status`=b.`status`
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id AND c.unit_type = #{type}
        WHERE a.del_flag = '0'
          AND b.del_flag = '0'
          AND a.t_order_id = #{id,jdbcType=VARCHAR} limit #{page}
            , #{pageSize}
    </select>

    <select id="selectOrderListNotPage" resultType="com.sckw.transport.model.dto.LogisticsOrderDTO">
        SELECT a.id          as lOrderId,
               a.l_order_no  as lOrderNo,
               b.`status`    as `status`,
               a.t_order_id  as tOrderId,
               a.t_order_no  as tOrderNo,
               a.payment     as payment,
               a.amount,
               a.price,
               a.loss,
               a.goods_price as goodsPrice,
               c.firm_name   as comanyName,
               c.contacts,
               c.phone,
               a.create_by   as createBy,
               a.create_time as createTime
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_track b ON a.id = b.l_order_id and a.`status` = b.`status`
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id AND c.unit_type = #{type}
        WHERE a.del_flag = '0'
          AND b.del_flag = '0'
          AND a.t_order_id = #{id,jdbcType=VARCHAR}
        order by createTime desc
    </select>
    <select id="selectOrderCount" resultType="java.lang.Long">
        SELECT count(a.id)
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_track b ON a.id = b.l_order_id and a.`status` = b.`status`
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id AND c.unit_type = #{type}
        WHERE a.del_flag = '0'
          AND b.del_flag = '0'
          AND a.t_order_id = #{id,jdbcType=VARCHAR}
    </select>
    <select id="selectLogisticOrderData" resultType="com.sckw.transport.api.model.dto.LogisticsOrderDTO">
        SELECT
        a.id as lOrderId,
        a.l_order_no as lOrderNO,
        a.t_order_id as tOrderId,
        a.t_order_no as tOrderNO,
        a.payment as payment,
        a.billing_mode as billingMode,
        c.contract_id as contractId,
        b.goods_name as goodsName,
        a.amount ,
        a.price,
        a.goods_price as goodsPrice,
        a.loss ,
        a.load_amount as loadAmount,
        a.unload_amount as unloadAmount,
        a.start_time as startTime,
        a.end_time AS endTime,
        a.create_time AS createTime,
        a.create_by AS createBy,
        a.update_by AS updateBy,
        a.update_time AS updateTime
        FROM
        kwt_logistics_order a
        LEFT JOIN kwt_logistics_order_goods b ON a.id = b.l_order_id
        LEFT JOIN kwt_logistics_order_unit d ON a.id = d.l_order_id
        LEFT JOIN kwt_logistics_order_contract c ON a.id=c.l_order_id
        <where>
            a.del_flag = 0 and d.unit_type = 1
            <if test="dictId != null and dictId != ''">
                AND a.payment = #{dictId}
            </if>
            <if test="logisticsOrderParam.companyId !=null and logisticsOrderParam.companyId != ''">
                AND d.ent_id = #{logisticsOrderParam.companyId}
            </if>
            <if test="logisticsOrderParam.taxRate != null and logisticsOrderParam.taxRate != ''">
                AND a.tax_rate = #{logisticsOrderParam.taxRate}
            </if>
            <if test="logisticsOrderParam.goodsName != null and logisticsOrderParam.goodsName != ''">
                AND b.goods_name LIKE concat('%',#{logisticsOrderParam.goodsName},'%')
            </if>
            <if test="logisticsOrderParam.startTime != null and logisticsOrderParam.startTime !=''">
                AND a.start_time &gt;= #{logisticsOrderParam.startTime}
            </if>
            <if test="logisticsOrderParam.endTime != null and logisticsOrderParam.endTime !=''">
                AND a.ent_time &lt;= #{logisticsOrderParam.endTime}
            </if>
        </where>
        ORDER BY a.create_time DESC
        <if test="logisticsOrderParam.page != null and logisticsOrderParam.pageSize != null ">
            limit #{logisticsOrderParam.page,jdbcType=INTEGER},#{logisticsOrderParam.pageSize,jdbcType=INTEGER}
        </if>
    </select>
    <select id="selectLogisticOrderCount" resultType="java.lang.Long">
        SELECT
        count(a.id)
        FROM
        kwt_logistics_order a
        LEFT JOIN kwt_logistics_order_goods b ON a.id = b.l_order_id
        LEFT JOIN kwt_logistics_order_unit d ON a.id = d.l_order_id
        LEFT JOIN kwt_logistics_order_contract c ON a.id=c.l_order_id
        <where>
            a.del_flag = 0 and d.unit_type = 1
            <if test="dictId != null and dictId != ''">
                AND a.payment = #{dictId}
            </if>
            <if test="logisticsOrderParam.companyId !=null and logisticsOrderParam.companyId != ''">
                AND d.ent_id = #{logisticsOrderParam.companyId}
            </if>
            <if test="logisticsOrderParam.taxRate != null and logisticsOrderParam.taxRate != ''">
                AND a.tax_rate = #{logisticsOrderParam.taxRate}
            </if>
            <if test="logisticsOrderParam.goodsName != null and logisticsOrderParam.goodsName != ''">
                AND b.goods_name LIKE concat('%',#{logisticsOrderParam.goodsName},'%')
            </if>
            <if test="logisticsOrderParam.startTime != null and logisticsOrderParam.startTime !=''">
                AND a.start_time &gt;= #{logisticsOrderParam.startTime}
            </if>
            <if test="logisticsOrderParam.endTime != null and logisticsOrderParam.endTime !=''">
                AND a.ent_time &lt;= #{logisticsOrderParam.endTime}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="getRejectReason" resultType="java.util.Map">
        SELECT a.id AS  lOrderId,
               b.remark remark
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_track b ON a.id = b.l_order_id
        WHERE a.del_flag = '0'
          AND b.del_flag = '0'
          and b.status = #{status}
          and a.id = #{id}
    </select>

    <select id="getSubcontractConsignment" resultType="com.sckw.transport.model.vo.SubcontractConsignmentVO">
        SELECT a.id,
               a.pid,
               a.`status`         as status,
               a.l_order_no       as consignOrderNo,
               c.firm_name        as carrierContacts,
               d.goods_name       as goodsName,
               a.amount           as amount,
               a.price,
               a.price_type       as priceType,
               e.detail_address   as loadAddress,
               f.detail_address   as unloadAddress,
               a.billing_mode     as billingMode,
               a.loss,
               a.loss_unit        as lossUnit,
               a.goods_price      as goodsPrice,
               a.goods_price_unit as goodsPriceUnit,
               a.settlement_cycle as settlementCycle,
               c.contacts         as carrierContacts,
               c.phone            as carrierPhone,
               b.firm_name        as consignCompany,
               b.contacts         as consignContacts,
               b.phone            as consignPhone,
               e.contacts         as loadContacts,
               e.phone            as loadContacts,
               f.contacts         as unloadContacts,
               f.phone            as unloadPhone,
               a.start_time       as shipmentsDate,
               a.end_time         as receiveGoodsDate,
               a.remark,
               a.create_by        as createBy,
               a.create_time      as createTime,
               g.contract_id      as contractId
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_unit b ON a.id = b.l_order_id
            AND b.unit_type = 1
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id
            AND c.unit_type = 2
                 LEFT JOIN kwt_logistics_order_goods d ON a.id = d.l_order_id
                 LEFT JOIN kwt_logistics_order_address e ON a.id = e.l_order_id
            AND e.address_type = 1
                 LEFT JOIN kwt_logistics_order_address f ON a.id = f.l_order_id
            AND f.address_type = 2
                 LEFT JOIN kwt_logistics_order_contract g ON a.id = g.l_order_id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND c.del_flag = 0
          AND e.del_flag = 0
          AND f.del_flag = 0
          and a.pid = #{lOrderId}
        ORDER BY a.create_time
            limit #{page}, #{pageSize}
    </select>

    <select id="getSubcontractConsignmentNotPage" resultType="com.sckw.transport.model.vo.SubcontractConsignmentVO">
        SELECT a.id,
               a.pid,
               a.`status`         as status,
               a.l_order_no       as consignOrderNo,
               c.firm_name        as carrierContacts,
               d.goods_name       as goodsName,
               a.amount           as amount,
               a.price,
               a.price_type       as priceType,
               e.detail_address   as loadAddress,
               f.detail_address   as unloadAddress,
               a.billing_mode     as billingMode,
               a.loss,
               a.loss_unit        as lossUnit,
               a.goods_price      as goodsPrice,
               a.goods_price_unit as goodsPriceUnit,
               a.settlement_cycle as settlementCycle,
               c.contacts         as carrierContacts,
               c.phone            as carrierPhone,
               b.firm_name        as consignCompany,
               b.contacts         as consignContacts,
               b.phone            as consignPhone,
               e.contacts         as loadContacts,
               e.phone            as loadContacts,
               f.contacts         as unloadContacts,
               f.phone            as unloadPhone,
               a.start_time       as shipmentsDate,
               a.end_time         as receiveGoodsDate,
               a.remark,
               a.create_by        as createBy,
               a.create_time      as createTime,
               g.contract_id      as contractId
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_unit b ON a.id = b.l_order_id
            AND b.unit_type = 1
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id
            AND c.unit_type = 2
                 LEFT JOIN kwt_logistics_order_goods d ON a.id = d.l_order_id
                 LEFT JOIN kwt_logistics_order_address e ON a.id = e.l_order_id
            AND e.address_type = 1
                 LEFT JOIN kwt_logistics_order_address f ON a.id = f.l_order_id
            AND f.address_type = 2
                 LEFT JOIN kwt_logistics_order_contract g ON a.id = g.l_order_id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND c.del_flag = 0
          AND e.del_flag = 0
          AND f.del_flag = 0
          and a.pid = #{lOrderId}
        ORDER BY a.create_time desc
    </select>

    <select id="countSubcontractConsignmentById" resultType="java.util.Map">
        SELECT COUNT(0)           as total,
               a.id,
               a.pid,
               a.`status`         as status,
               a.l_order_no       as consignOrderNo,
               c.firm_name        as carrierContacts,
               d.goods_name       as goodsName,
               a.amount           as amount,
               a.price,
               a.price_type       as priceType,
               e.detail_address   as loadAddress,
               f.detail_address   as unloadAddress,
               a.billing_mode     as billingMode,
               a.loss,
               a.loss_unit        as lossUnit,
               a.goods_price      as goodsPrice,
               a.goods_price_unit as goodsPriceUnit,
               a.settlement_cycle as settlementCycle,
               c.contacts         as carrierContacts,
               c.phone            as carrierPhone,
               b.firm_name        as consignCompany,
               b.contacts         as consignContacts,
               b.phone            as consignPhone,
               e.contacts         as loadContacts,
               e.phone            as loadContacts,
               f.contacts         as unloadContacts,
               f.phone            as unloadPhone,
               a.start_time       as shipmentsDate,
               a.end_time         as receiveGoodsDate,
               a.remark,
               a.create_by        as createBy,
               a.create_time      as createTime,
               g.contract_id      as contractId
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_unit b ON a.id = b.l_order_id
            AND b.unit_type = 1
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id
            AND c.unit_type = 2
                 LEFT JOIN kwt_logistics_order_goods d ON a.id = d.l_order_id
                 LEFT JOIN kwt_logistics_order_address e ON a.id = e.l_order_id
            AND e.address_type = 1
                 LEFT JOIN kwt_logistics_order_address f ON a.id = f.l_order_id
            AND f.address_type = 2
                 LEFT JOIN kwt_logistics_order_contract g ON a.id = g.l_order_id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND c.del_flag = 0
          AND e.del_flag = 0
          AND f.del_flag = 0
          and a.pid = #{lOrderId}
        ORDER BY a.create_time
    </select>

    <select id="selectDriverListById" resultType="com.sckw.transport.model.vo.DriverListVo">
        SELECT a.id AS lOrderId,
        b.driver_id AS driverId,
        b.id AS wOrderId,
        b.w_order_no AS wOrderNo,
        b.driver_name AS driverName,
        b.driver_phone AS driverPhone,
        b.driver_idcard AS driverIdCard,
        b.truck_no AS truckNo,
        COUNT( b.id ) AS count,
        b.`status` as transportStatus
        FROM
        kwt_waybill_order b,
        kwt_logistics_order_circulate c,
        kwt_logistics_order a
        WHERE
        a.id = c.l_order_id
        AND a.id = b.l_order_id
        AND a.del_flag = 0
        AND b.del_flag = 0
        AND b.type = '2'
        AND c.`status` = '0'
        <if test="driverParam.lOrderId != null and driverParam.lOrderId != ''">
            AND a.id =#{driverParam.lOrderId}
        </if>
        <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 =#{driverParam.driverPhone}-->
            AND b.driver_phone like concat('%',#{driverParam.driverPhone},'%')
        </if>
        <if test="driverParam.truckNo != null and driverParam.truckNo != ''">
            <!--            AND b.truck_no = #{driverParam.truckNo}-->
            AND b.truck_no like concat('%',#{driverParam.truckNo},'%')
        </if>
        GROUP BY b.truck_no
    </select>

    <select id="selectLogisticOrderList" resultType="com.sckw.transport.api.model.vo.LogisticsOrderVO">
        SELECT
        a.id as lOrderId,
        a.l_order_no as lOrderNO,
        a.t_order_id as tOrderId,
        a.t_order_no as tOrderNO,
        a.payment as payment,
        a.billing_mode as billingMode,
        c.contract_id as contractId,
        b.goods_name as goodsName,
        a.amount ,
        a.price,
        a.goods_price as goodsPrice,
        a.loss ,
        a.load_amount as loadAmount,
        a.unload_amount as unloadAmount,
        a.start_time as startTime,
        a.end_time AS endTime,
        a.create_time AS createTime,
        a.create_by AS createBy,
        a.update_by AS updateBy,
        a.update_time AS updateTime
        FROM
        kwt_logistics_order a
        LEFT JOIN kwt_logistics_order_goods b ON a.id = b.l_order_id
        LEFT JOIN kwt_logistics_order_contract c ON a.id=c.l_order_id
        and
        a.del_flag = 0
        <where>
            <if test="ids != null and ids.size() > 0">
                and a.id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="selectOrderFinishData" resultType="com.sckw.transport.model.vo.OrderFinishVO">
        SELECT b.amount as amount,COUNT(a.id) AS number,
        SUM(IFNULL(a.load_amount, 0)) as loadAmount,
        SUM(IFNULL(a.unload_amount, 0)) AS unloadAmount
        FROM kwt_waybill_order a
        LEFT JOIN kwt_logistics_order b ON b.id = a.l_order_id
        AND a.del_flag = 0
        AND b.del_flag = 0
        <where>
            <if test="orderId != null and orderId != ''">
                and b.id=#{orderId}
            </if>
        </where>
    </select>

    <select id="findLogisticsOrder" resultType="com.sckw.transport.model.KwtLogisticsOrder" parameterType="java.util.Map">
        select
        id, ent_id entId, t_order_id tOrderId, t_order_no tOrderNo, l_order_no lOrderNo, pid, pids,
        settlement_cycle settlementCycle, price, price_type priceType, billing_mode billingMode,
        amount, unit, loss, loss_unit lossUnit, goods_price goodsPrice, goods_price_unit goodsPriceUnit,
        start_time startTime, end_time endTime, subcontract_amount subcontractAmount, entrust_amount entrustAmount,
        unload_amount unloadAmount, load_amount loadAmount, ignore_amount ignoreAmount, deficit_amount deficitAmount,
        deficit_price deficitPrice, payment, tax_rate taxRate, remark, `status`, create_by createBy,
        create_time createTime, update_by updateBy, update_time updateTime
        from kwt_logistics_order
        where del_flag = 0
        <if test="id != null and id != ''">
            and id = #{id, jdbcType=BIGINT}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, pids)
            and id != #{ownOrderId, jdbcType=BIGINT}
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(id, #{belowlOrderId, jdbcType=VARCHAR})
            and id != #{ownOrderId, jdbcType=BIGINT}
        </if>
    </select>
    <select id="selectOrderCountByTorderId" resultType="com.sckw.transport.model.dto.LogisticsOrderDTO">
        SELECT a.id          as lOrderId,
               a.l_order_no  as lOrderNo,
               b.`status`    as `status`,
               a.t_order_id  as tOrderId,
               a.t_order_no  as tOrderNo,
               a.payment     as payment,
               a.amount,
               a.price,
               a.loss,
               a.goods_price as goodsPrice,
               c.firm_name   as comanyName,
               c.contacts,
               c.phone,
               a.create_by   as createBy,
               a.create_time as createTime
        FROM kwt_logistics_order a
                 LEFT JOIN kwt_logistics_order_track b ON a.id = b.l_order_id
                 LEFT JOIN kwt_logistics_order_unit c ON a.id = c.l_order_id AND c.unit_type = #{type}
        WHERE a.del_flag = '0'
          AND b.del_flag = '0'
          AND a.t_order_id = #{id,jdbcType=VARCHAR}
    </select>
    <select id="getAddressList" resultType="java.lang.Integer">
        SELECT a.city_code
        FROM kwt_logistics_order_address a
                 LEFT JOIN kwt_logistics_order b ON a.l_order_id = b.id
        where a.city_code is not null AND b.ent_id =#{entId}
        GROUP BY a.city_code
    </select>
    <select id="getAddressDetailListByEntId" resultType="com.sckw.transport.api.model.dto.AddressResDTO">

    </select>
    <select id="selectMaxLevel" resultType="java.lang.Integer">
        SELECT MAX(LEVEL)
        FROM kwt_logistics_order
        WHERE del_flag = 0
          AND FIND_IN_SET(#{id}, pids)
    </select>

    <select id="countAmountByTradeOrderId" resultType="java.util.Map">
        SELECT
            SUM(
                    IFNULL( unload_amount, 0 )) as unloadAmount,
            SUM(
                    IFNULL( load_amount, 0 )) as loadAmount,
            SUM(
                    IFNULL( total_load_amount, 0 )) AS totalLoadAmount,
            SUM(
                    IFNULL( total_unload_amount, 0 )) AS totalUnloadAmount
        FROM
            kwt_logistics_order
        WHERE
            del_flag = 0
          AND t_order_id = #{tOrderId}
          AND `status`  IN (
            8
            )
    </select>
</mapper>