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

    <select id="findByAddress" parameterType="com.sckw.transport.model.KwtLogisticsOrderAddress"
            resultType="com.sckw.transport.model.KwtLogisticsOrderAmount" >
        select
        kloa1.id, kloa1.l_order_id lOrderId, kloa1.l_address_id lAddressId, kloa1.amount, kloa1.subcontract_amount subcontractAmount,
        kloa1.entrust_amount entrustAmount, kloa1.load_amount loadAmount, kloa1.unload_amount unloadAmount, kloa1.ignore_amount ignoreAmount,
        kloa1.load_time loadTime, kloa1.unload_time unloadTime, kloa1.total_load_amount totalLoadAmount, kloa1.total_unload_amount totalUnloadAmount
        from kwt_logistics_order_amount kloa1
        left join kwt_logistics_order_address kloa2 on kloa2.id = kloa1.l_address_id
        where kloa1.del_flag = 0 and kloa2.del_flag = 0
        <if test="lOrderId != null and lOrderId !=''">
            and kloa1.l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="addressType != null and addressType !=''">
            and kloa2.address_type = #{addressType, jdbcType=INTEGER}
        </if>
    </select>

    <select id="findByLAddressId" resultType="com.sckw.transport.model.KwtLogisticsOrderAmount" >
        select
        id, l_order_id lOrderId, l_address_id lAddressId, amount, subcontract_amount subcontractAmount,
        entrust_amount entrustAmount, load_amount loadAmount, unload_amount unloadAmount, ignore_amount ignoreAmount,
        load_time loadTime, unload_time unloadTime, total_load_amount totalLoadAmount, total_unload_amount totalUnloadAmount
        from kwt_logistics_order_amount
        where del_flag = 0
        and l_address_id = #{lAddressId, jdbcType=BIGINT}
    </select>

    <select id="findByLOrderId" resultType="com.sckw.transport.model.KwtLogisticsOrderAmount" >
        select
        id, l_order_id lOrderId, l_address_id lAddressId, amount, subcontract_amount subcontractAmount,
        entrust_amount entrustAmount, load_amount loadAmount, unload_amount unloadAmount, ignore_amount ignoreAmount,
        load_time loadTime, unload_time unloadTime, total_load_amount totalLoadAmount, total_unload_amount totalUnloadAmount
        from kwt_logistics_order_amount
        where del_flag = 0
        and l_order_id = #{lOrderId, jdbcType=BIGINT}
    </select>

    <select id="countAddressAmount" resultType="com.sckw.transport.model.vo.LogisticsOrderFinishAmountVo">
        SELECT
            a.l_order_id as lOrderId,
            a.t_address_id AS tOrderId,
            a.id AS lAddressId,
            a.t_address_id AS tAddressId,
            b.total_load_amount AS loadAmount,
            b.total_unload_amount AS unloadAmount
        FROM
            kwt_logistics_order_address a
                LEFT JOIN kwt_logistics_order_amount b ON a.id = b.l_address_id
        WHERE
            a.l_order_id = #{id}
        <if test="addressType != null">
            AND a.address_type = #{addressType,jdbcType=INTEGER}
        </if>
        AND a.del_flag = 0
    </select>

    <select id="cancelOrderToAmountByOrderId" resultType="com.sckw.transport.model.vo.LogisticsOrderCancelAmountVo">
        SELECT
        a.l_order_id as lOrderId,
        c.t_order_id AS tOrderId,
        a.id AS lAddressId,
        a.t_address_id AS tOrderAddressId,
        b.id as amountId,
        b.amount AS amount
        FROM
        kwt_logistics_order_address a
        LEFT JOIN kwt_logistics_order_amount b ON a.id = b.l_address_id
        left join kwt_logistics_order c on a.l_order_id = c.id
        WHERE
        a.l_order_id = #{orderId}
        <if test="addressType != null">
            AND a.address_type = #{addressType,jdbcType=INTEGER}
        </if>
        AND a.del_flag = 0
    </select>
</mapper>