<?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="trading" jdbcType="VARCHAR" property="trading"/>
        <result column="audit_user" jdbcType="VARCHAR" property="auditUser"/>
        <result column="audit_phone" jdbcType="VARCHAR" property="auditPhone"/>
        <result column="audit_user_id" jdbcType="VARCHAR" property="auditUserId"/>
        <result column="success_user_id" jdbcType="VARCHAR" property="successUserId"/>
        <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,
        trading,
        audit_user,
        audit_phone,
        audit_user_id,
        success_user_id,
        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>

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

               lu2.unit_type  as unitType,
               lu2.firm_name  as firmName,

        <include refid="Join_Column_List"/>
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.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
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.l_ledger_id
    </sql>

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

        <if test="settlementReq.unitType != null">
            and lu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
        </if>
        <if test="settlementReq.unitTypeTwo != null">
            and lu2.unit_type = #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        </if>
        <if test="settlementReq.entId != null">
            and lu2.top_ent_id = #{settlementReq.entId,jdbcType=INTEGER}
        </if>
        <if test="settlementReq.trading != null and settlementReq.trading != ''">
            and l.trading = #{settlementReq.trading,jdbcType=VARCHAR}
        </if>
        <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != ''  and settlementReq.endCreateTime != null  and settlementReq.endCreateTime != ''">
            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.name like concat('%'
            , #{settlementReq.keywords,jdbcType=VARCHAR}
            , '%')
                or l.l_ledger_no like concat('%'
            , #{settlementReq.keywords,jdbcType=VARCHAR}
            , '%')
                or lu2.firm_name like concat('%'
            , #{settlementReq.keywords,jdbcType=VARCHAR}
            , '%')
                or s.sl_order_no like concat('%'
            , #{settlementReq.keywords,jdbcType=VARCHAR}
            , '%')
                )
        </if>
    </sql>

    <!-- 结算运费-物流订单列表  -->
    <select id="pageSelect" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        <include refid="settlementLogisticsSelect"/>
        <where>
            l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
              and lu2.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
        order by s.update_time desc
    </select>
    <select id="list" resultType="com.sckw.payment.model.vo.SettlementLogistics">
        select s.id,
               s.ent_id                       as entId,
               s.l_ledger_id                  as lLedgerId,
               s.sl_order_no                  as slOrderNo,
               s.total_price                  as totalPrice,
               s.actual_price                 as actualPrice,
               s.total_price - s.actual_price as waitPrice,
               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,
               l.name,
               l.generate_time                as generateTime,
               l.l_ledger_no                  as lLedgerNo,
               l.trading                      as trading,
               l.settle_price                 as settlePrice,
               l.url                          as url,
               lu.firm_name                   as shipperFirmName,
               lu2.firm_name                  as carrierFirmName
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id and lu.del_flag = 0 and
                                                            lu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit lu2
                            on s.l_ledger_id = lu2.l_ledger_id and lu2.del_flag = 0
                                and lu2.unit_type = #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            s.del_flag = 0
            <if test="settlementReq.status != null and settlementReq.status != 0">
                and s.status = #{settlementReq.status}
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and l.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != ''  and settlementReq.endCreateTime != null  and settlementReq.endCreateTime != ''">
                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.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or lu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or lu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or s.sl_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
        order by s.update_time desc
    </select>

    <!--  结算运费-物流订单列表合计  -->
    <select id="selectSum" resultType="com.sckw.payment.model.vo.res.SettlementLogisticsSumVo">
        select a.*, (a.actualReceivable - a.currentReceipt) AS remainingReceivables From (
        <include refid="settlementLogisticsSelectSum"/>
        <where>
            l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
              and lu2.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
        ) a
    </select>
    <select id="sum" resultType="com.sckw.payment.model.vo.res.SettlementLogisticsSumVo">
        select a.*, (a.actualReceivable - a.currentReceipt) AS remainingReceivables From (
        select count(l.id)            total,
               sum(l.settle_price) as actualReceivable,
               sum(s.actual_price) as currentReceipt
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id and lu.del_flag = 0 and
                                                            lu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit lu2
                            on s.l_ledger_id = lu2.l_ledger_id and lu2.del_flag = 0
                                and lu2.unit_type = #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            s.del_flag = 0
            <if test="settlementReq.status != null and settlementReq.status != 0">
                and s.status = #{settlementReq.status}
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and l.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != ''  and settlementReq.endCreateTime != null  and settlementReq.endCreateTime != ''">
                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.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or lu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or lu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    or s.sl_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
        ) a
    </select>

    <!-- 结算运费-物流订单列表头部汇总数  -->
    <select id="getCountList" resultType="java.lang.Long">
        select count(s.id) as num
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.l_ledger_id
        <where>
            l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
              and lu2.del_flag = 0
            <include refid="settlementLogisticsSelectWhere"/>
        </where>
    </select>
    <select id="count" resultType="java.util.Map">
        select count(1)                           "0",
               count(if(ksl.status = 3, 1, null)) "3",
               count(if(ksl.status = 2, 1, null)) "2",
               count(if(ksl.status = 1, 1, null)) "1"
        from kwp_settlement_logistics ksl
                 inner join kwp_ledger_logistics l on ksl.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on ksl.l_ledger_id = lu.l_ledger_id and lu.del_flag = 0
            and lu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit lu2 on ksl.l_ledger_id = lu2.l_ledger_id and lu2.del_flag = 0
            and lu2.unit_type = #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            ksl.del_flag = 0
            <if test="settlementReq.status != null and settlementReq.status != 0">
                and ksl.status = #{settlementReq.status}
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and l.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != ''  and settlementReq.endCreateTime != null  and settlementReq.endCreateTime != ''">
                and ksl.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.endReceiptTime != null">
                and ksl.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (l.name like concat('%'
                    , #{settlementReq.keywords,jdbcType=VARCHAR}
                    , '%')
                    or lu.firm_name like concat('%'
                        , #{settlementReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    or lu2.firm_name like concat('%'
                        , #{settlementReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    or ksl.sl_order_no like concat('%'
                        , #{settlementReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    )
            </if>
        </where>
    </select>
    <!-- 根据结算物流订单id查详情   -->
    <select id="detail" 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 trading,
               l.settle_price as settlePrice,
               l.url          as attachmentUrl,

               lu.unit_type   as unitType,
               lu2.firm_name  as firmName,
        <include refid="Join_Column_List"/>
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.l_ledger_id
        where s.id = #{id,jdbcType=BIGINT}
        <if test="unitType != null">
            and lu.unit_type = #{unitType,jdbcType=INTEGER}
        </if>
        <if test="unitTypeTwo != null">
            and lu2.unit_type = #{unitTypeTwo,jdbcType=INTEGER}
        </if>
    </select>
    <select id="selectDetail" parameterType="java.lang.Long"
            resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        select s.id,
               s.ent_id                       as entId,
               s.l_ledger_id                  as lLedgerId,
               s.sl_order_no                  as slOrderNo,
               s.total_price                  as totalPrice,
               s.actual_price                 as actualPrice,
               s.total_price - s.actual_price as waitPrice,
               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,
               l.name,
               l.generate_time                as generateTime,
               l.l_ledger_no                  as lLedgerNo,
               l.trading                      as trading,
               l.settle_price                 as settlePrice,
               l.url                          as url,
               lu.firm_name                   as firm_name
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id and lu.del_flag = 0 and
                                                            lu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            s.del_flag = 0
              and s.id = #{id,jdbcType=BIGINT}
        </where>
    </select>

    <!--  根据多个结算运费物流订单id查订单，用于导出   -->
    <select id="selectLogisticsList" resultType="com.sckw.payment.model.dto.SettlementLogisticsDto">
        select l.name                           as name,
               l.l_ledger_no                    as lLedgerNo,
               l.trading                        as trading,
               l.settle_price                   as settlePrice,
               (s.total_price - s.actual_price) as waitPrice,
               lu2.firm_name                    as firmName,
               s.id,
               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,
               (case
                    when #{settlementReq.unitType,jdbcType=INTEGER} = 2 then s.audit_user
                    else s.success_user end)    as contacts,
               (case
                    when #{settlementReq.unitType,jdbcType=INTEGER} = 2 then s.audit_phone
                    else s.success_phone end)   as phone,
               s.status,
               s.create_by                      as createBy,
               s.update_by                      as updateBy,
               s.create_time                    as createTime,
               s.update_time                    as updateTime
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.l_ledger_id
        <where>
            l.del_flag = 0
              and s.del_flag = 0
              and lu.del_flag = 0
              and lu2.del_flag = 0
            <choose>
                <when test="ids != null and ids.size() > 0">
                    and s.id in
                    <foreach collection="ids" item="id" close=")" open="(" separator=",">
                        #{id,jdbcType=BIGINT}
                    </foreach>
                </when>
                <otherwise>
                    <include refid="settlementLogisticsSelectWhere"/>
                </otherwise>
            </choose>
        </where>
        order by s.update_time desc
    </select>
    <select id="exportList" resultType="com.sckw.payment.model.vo.SettlementLogistics">
        select s.id,
               s.ent_id                       as entId,
               s.l_ledger_id                  as lLedgerId,
               s.sl_order_no                  as slOrderNo,
               s.total_price                  as totalPrice,
               s.actual_price                 as actualPrice,
               s.total_price - s.actual_price as waitPrice,
               l.settle_price                 as settlePrice,
               l.name,
               l.l_ledger_no                  as lLedgerNo,
               l.trading                      as trading,
               l.url,
               lu.firm_name                   as shipperFirmName,
               lu2.firm_name                  as carrierFirmName,
               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.receipt_time                 as receiptTime,
               s.create_by                    as createBy,
               s.create_time                  as createTime,
               s.update_by                    as updateBy,
               s.update_time                  as updateTime
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id and lu.del_flag = 0 and
                                                            lu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit lu2
                            on s.l_ledger_id = lu2.l_ledger_id and lu2.del_flag = 0 and lu2.unit_type =
                                                                                        #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            s.del_flag = 0
            <choose>
                <when test="ids != null and ids.size() > 0">
                    and s.id in
                    <foreach collection="ids" item="id" close=")" open="(" separator=",">
                        #{id,jdbcType=BIGINT}
                    </foreach>
                </when>
                <otherwise>
                    <if test="settlementReq.status != null and settlementReq.status != 0">
                        and s.status = #{settlementReq.status}
                    </if>
                    <if test="settlementReq.trading != null and settlementReq.trading != ''">
                        and l.trading = #{settlementReq.trading,jdbcType=VARCHAR}
                    </if>
                    <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != ''  and settlementReq.endCreateTime != null  and settlementReq.endCreateTime != ''">
                        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.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            or lu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            or lu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            or s.sl_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            )
                    </if>
                </otherwise>
            </choose>
        </where>
        order by s.update_time desc
    </select>

    <select id="getListById" resultType="com.sckw.payment.model.dto.LedgerUnitDto">
        select kltu.top_ent_id,
               kltu.ent_id,
               kltu.unit_type
        from kwp_settlement_logistics kst
                 inner join kwp_ledger_logistics klt on kst.l_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_logistics_unit kltu on klt.id = kltu.l_ledger_id and kltu.del_flag = 0
        <where>
            kst.del_flag = 0
              and kst.id = #{id,jdbcType=BIGINT}
        </where>
    </select>

    <select id="selectJoin" resultType="java.lang.String">
        select s.sl_order_no
        from kwp_settlement_logistics s
                 inner join kwp_ledger_logistics l on s.l_ledger_id = l.id and l.del_flag = 0
                 inner join kwp_ledger_logistics_unit lu on s.l_ledger_id = lu.l_ledger_id
            and lu.del_flag = 0 and lu.unit_type = 1
                 inner join kwp_ledger_logistics_unit lu2 on s.l_ledger_id = lu2.l_ledger_id and lu2.del_flag = 0
            and lu2.unit_type = 2
        <where>
            s.del_flag = 0
              and s.status != 3
              and lu.top_ent_id = #{entId,jdbcType=BIGINT}
              and lu2.top_ent_id = #{entTarget,jdbcType=BIGINT}
        </where>
    </select>

    <select id="sumMoneyType" resultType="com.sckw.payment.model.dto.MoneyType">
        select ifnull(sum(ksl.total_price - ksl.actual_price), '0') money,
               1                                                    type
        from kwp_settlement_logistics ksl
                 inner join kwp_ledger_logistics kll on ksl.l_ledger_id = kll.id and kll.del_flag = 0
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                                                              kllu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            ksl.del_flag = 0
              and kllu.top_ent_id = #{entId,jdbcType=BIGINT}
              and ksl.create_time

            <foreach collection="cur" item="item" open="between" close="" separator="and">
                #{item,jdbcType=TIMESTAMP}
            </foreach>
        </where>
        union all
        select ifnull(sum(ksl.total_price - ksl.actual_price), '0') money,
               2                                                    type
        from kwp_settlement_logistics ksl
                 inner join kwp_ledger_logistics kll on ksl.l_ledger_id = kll.id and kll.del_flag = 0
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                                                              kllu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            ksl.del_flag = 0
              and kllu.top_ent_id = #{entId,jdbcType=BIGINT}
              and ksl.create_time
            <foreach collection="pre" item="item" open="between" close="" separator="and">
                #{item,jdbcType=TIMESTAMP}
            </foreach>
        </where>
    </select>

    <select id="queryRecord" resultType="com.sckw.payment.model.KwpSettlementRecord">
        SELECT kllu.top_ent_id                                                      entId,
               sum(IF(kllu.unit_type = 2, ksl.total_price, 0.0))                    totalReceiveMoney,
               sum(IF(kllu.unit_type = 1, ksl.total_price, 0.0))                    totalPayMoney,
               sum(IF(kllu.unit_type = 2, ksl.actual_price, 0.0))                   receivedMoney,
               sum(IF(kllu.unit_type = 1, ksl.actual_price, 0.0))                   paidMoney,
               sum(IF(kllu.unit_type = 2, ksl.total_price - ksl.actual_price, 0.0)) exactMoney,
               sum(IF(kllu.unit_type = 1, ksl.total_price - ksl.actual_price, 0.0)) unpaidMoney,
               sum(
                       IF
                           (ksl.receipt_time &lt; now() &amp;&amp; kllu.unit_type = 2,
                            ksl.total_price - ksl.actual_price, 0.0)
                   )                                                                overdueExactMoney,
               sum(
                       IF
                           (ksl.receipt_time &lt; now() &amp;&amp; kllu.unit_type = 2,
                            ksl.total_price - ksl.actual_price, 0.0)
                   )                                                                overduePaidMoney
        FROM kwp_settlement_logistics ksl
                 inner JOIN kwp_ledger_logistics_unit kllu ON ksl.l_ledger_id = kllu.l_ledger_id
            AND kllu.del_flag = 0
        WHERE ksl.del_flag = 0
        GROUP BY kllu.top_ent_id
    </select>
</mapper>
