<?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.payment.dao.KwpSettlementLogisticsMapper">
    <resultMap id="BaseResultMap" type="com.sckw.payment.model.KwpSettlementLogistics">
        <!--@mbg.generated-->
        <!--@Table kwp_settlement_logistics-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="l_ledger_id" jdbcType="BIGINT" property="lLedgerId"/>
        <result column="sl_order_no" jdbcType="VARCHAR" property="slOrderNo"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="total_price" jdbcType="DECIMAL" property="totalPrice"/>
        <result column="actual_price" jdbcType="DECIMAL" property="actualPrice"/>
        <result column="receipt_time" jdbcType="TIMESTAMP" property="receiptTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="audit_user" jdbcType="VARCHAR" property="auditUser"/>
        <result column="audit_phone" jdbcType="VARCHAR" property="auditPhone"/>
        <result column="success_user" jdbcType="VARCHAR" property="successUser"/>
        <result column="success_phone" jdbcType="VARCHAR" property="successPhone"/>
        <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,
        l_ledger_id,
        sl_order_no,
        name,
        total_price,
        actual_price,
        receipt_time,
        remark,
        audit_user,
        audit_phone,
        success_user,
        success_phone,
        status,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>

    <sql id="Join_Column_List">
        s.id,
        s.ent_id        as entId,
        s.l_ledger_id   as lLedgerId,
        s.sl_order_no   as slOrderNo,
        s.name,
        s.total_price   as totalPrice,
        s.actual_price  as actualPrice,
        s.receipt_time  as receiptTime,
        s.remark,
        s.audit_user    as auditUser,
        s.audit_phone   as auditPhone,
        s.success_user  as successUser,
        s.success_phone as successPhone,
        s.status,
        s.create_by     as createBy,
        s.create_time   as createTime,
        s.update_by     as updateBy,
        s.update_time   as updateTime,
        s.del_flag      as delFlag
    </sql>


    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from kwp_settlement_logistics
        where id = #{id,jdbcType=BIGINT}
    </select>

    <!--  连表筛选准备  -->
    <sql id="settlementLogisticsSelect">
        select l.name       as ledgerName,
               l.l_ledger_no  as lLedgerNo,
               l.trading      as ledgerTrading,
               l.settle_price as ledgerSettlePrice,
                l.url          as attachmentUrl,

               lu.unit_type   as unitType,
               lu.firm_name   as CustomerName,

        <include refid="Join_Column_List"/>
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
    </sql>
    <!--  连表筛选准备  -->
    <sql id="settlementLogisticsSelectSum">
        select count(l.id)                                    total,
               sum(l.settle_price)                         as actualReceivable,
               sum(s.actual_price)                         as currentReceipt
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
    </sql>

    <!--  根据连表，查询条件准备  -->
    <sql id="settlementLogisticsSelectWhere">
        <if test="settlementReq.status != null">
            and s.status = #{settlementReq.status}
        </if>

        <if test="settlementReq.startCreateTime != null and settlementReq.endCreateTime != null">
            and s.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
        </if>
        <if test="settlementReq.startReceiptTime != null and settlementReq.endReceiptTime != null">
            and s.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
        </if>

        <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
            and ( l.l_ledger_no like concat('%'
                 , #{settlementReq.keywords}
                 , '%')
                )
        </if>
    </sql>


    <!-- 结算运费收款(承运方)-物流订单列表  -->
    <select id="pageSelectCollection" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        <include refid="settlementLogisticsSelect"/>
        <where>
            lu.unit_type = 1
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
        order by s.update_time desc
    </select>

    <!--  结算运费收款(承运方)-物流订单列表合计  -->
    <select id="selectCollectionSum" resultType="com.sckw.payment.model.vo.res.SettlementLogisticsSumVo">
        select a.*, (a.actualReceivable - a.currentReceipt) AS remainingReceivables From (
        <include refid="settlementLogisticsSelectSum"/>
        <where>
            lu.unit_type = 1
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
        ) a
    </select>

    <!-- 结算运费收款(承运方)-物流订单列表头部汇总数  -->
    <select id="getCountListCollection" resultType="java.lang.Long">
        select count(s.id) as num
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
        <where>
            lu.unit_type = 1
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
    </select>

    <!--  根据结算(承运方)物流订单id查详情   -->
    <select id="detailCollection" parameterType="java.lang.Long" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        select l.name         as ledgerName,
               l.l_ledger_no  as lLedgerNo,
               l.trading      as ledgerTrading,
               l.settle_price as ledgerSettlePrice,
        l.url          as attachmentUrl,

               lu.unit_type   as unitType,
               lu.firm_name   as CustomerName,
        <include refid="Join_Column_List"/>
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
        where s.id = #{id,jdbcType=BIGINT}
          and lu.unit_type = 1
    </select>



    <!-- 结算运费付款(托运方)-物流订单列表  -->
    <select id="pageSelectPayment" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        <include refid="settlementLogisticsSelect"/>
        <where>
            lu.unit_type = 2
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
        order by s.update_time desc
    </select>

    <!-- 结算运费付款(托运方)-物流订单列表头部汇总数  -->
    <select id="getCountListPayment" resultType="java.lang.Long">
        select count(s.id) as num
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
        <where>
            lu.unit_type = 2
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
    </select>

    <!-- 根据结算(托运方)物流订单id查详情   -->
    <select id="detailPayment" parameterType="java.lang.Long" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        select l.name         as ledgerName,
               l.l_ledger_no  as lLedgerNo,
               l.trading      as ledgerTrading,
               l.settle_price as ledgerSettlePrice,
               l.url          as attachmentUrl,

               lu.unit_type   as unitType,
               lu.firm_name   as CustomerName,
        <include refid="Join_Column_List"/>
        from kwp_settlement_logistics s
                 left join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 left join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
        where s.id = #{id,jdbcType=BIGINT}
          and lu.unit_type = 2
    </select>


    <!--  根据多个结算运费收款(线下收款)物流订单id查订单，用于导出   -->
    <select id="selectIds" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        <include refid="settlementLogisticsSelect"/>
        <where>
            lu.unit_type = 2
            and l.del_flag = 0
            and s.del_flag = 0
            and lu.del_flag = 0
              and s.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
        order by s.update_time desc
    </select>

    <select id="selectList" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        <include refid="settlementLogisticsSelect"/>
        <where>
            lu.unit_type = #{unitType,jdbcType=INTEGER}
              and l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
        </where>
        order by s.update_time desc
    </select>
</mapper>