<?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.KwpSettlementTradeMapper">
    <resultMap id="BaseResultMap" type="com.sckw.payment.model.KwpSettlementTrade">
        <!--@mbg.generated-->
        <!--@Table kwp_settlement_trade-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="t_ledger_id" jdbcType="BIGINT" property="tLedgerId"/>
        <result column="st_order_no" jdbcType="VARCHAR" property="stOrderNo"/>
        <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="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="trading" jdbcType="VARCHAR" property="trading"/>
        <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">
        <!--@mbg.generated-->
        id,
        ent_id,
        t_ledger_id,
        st_order_no,
        `name`,
        total_price,
        actual_price,
        receipt_time,
        audit_user,
        audit_phone,
        audit_user_id,
        success_user_id,
        success_user,
        success_phone,
        trading,
        remark,
        `status`,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>
    <sql id="query">
        <where>
            kst.del_flag = 0
              and kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </sql>
    <select id="pageSelect" resultType="com.sckw.payment.model.dto.SettlementTradeDto">
        select kst.id,
               kst.status,
               kst.st_order_no,
               klt.t_ledger_no                                    tLedgerNo,
               kst.create_time,
               kst.receipt_time,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_time,
               kst.t_ledger_id,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               kltu.firm_name                                     firmName,
               kst.update_by
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type =
                                                           #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kltu2.top_ent_id = #{settlementReq.entId,jdbcType=INTEGER}
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or klt.t_ledger_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
        order by kst.create_time desc
    </select>
    <select id="list" resultType="com.sckw.payment.model.vo.SettlementTrade">
        select kst.id,
               kst.status,
               kst.st_order_no                                    stOrderNo,
               kst.receipt_time                                   receiptTime,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_by                                      createBy,
               kst.create_time                                    createTime,
               kst.t_ledger_id                                    tLedgerId,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               klt.t_ledger_no                                    tLedgerNo,
               klt.generate_time                                  generateTime,
               kltu.firm_name                                     sellFirmName,
               kltu2.firm_name                                    purchaseFirmName,
               kst.update_by                                      updateBy
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = 2
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type = 1
        <where>
            kst.del_flag = 0
            <if test="authEntIdList != null and authEntIdList.size() != 0">
                and kst.ent_id in
                <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
        order by kst.create_time desc
    </select>

    <select id="sumMoney" resultType="com.sckw.payment.model.vo.res.SettlementSumVo">
        select count(1)                                                allCount,
               IFNULL(sum(kst.total_price), 0)                         receiveAll,
               IFNULL(sum(kst.actual_price), 0)                        alreadyReceive,
               IFNULL(sum(kst.total_price) - sum(kst.actual_price), 0) waitReceive
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type =
                                                           #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kltu2.top_ent_id = #{settlementReq.entId,jdbcType=INTEGER}
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </select>
    <select id="sumMoney2" resultType="com.sckw.payment.model.vo.res.SettlementSumVo">
        select count(1)                                                allCount,
               IFNULL(sum(kst.total_price), 0)                         receiveAll,
               IFNULL(sum(kst.actual_price), 0)                        alreadyReceive,
               IFNULL(sum(kst.total_price) - sum(kst.actual_price), 0) waitReceive
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = 2
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type = 1
        <where>
            kst.del_flag = 0
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </select>
    <select id="countOrder" resultType="java.util.Map">
        select count(1)                           "0",
               count(if(kst.status = 3, 1, null)) "3",
               count(if(kst.status = 2, 1, null)) "2",
               count(if(kst.status = 1, 1, null)) "1"
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type =
                                                           #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kltu2.top_ent_id = #{settlementReq.entId,jdbcType=INTEGER}
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </select>
    <select id="count" resultType="java.util.Map">
        select count(1)                           "0",
               count(if(kst.status = 3, 1, null)) "3",
               count(if(kst.status = 2, 1, null)) "2",
               count(if(kst.status = 1, 1, null)) "1"
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = 2
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type = 1
        <where>
            kst.del_flag = 0
            <if test="authEntIdList != null and authEntIdList.size() != 0">
                and kst.ent_id in
                <foreach collection="authEntIdList" separator="," close=")" open="(" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                and (
                            klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kltu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                        or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="settlementReq.trading != null and settlementReq.trading != ''">
                and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
            </if>
            <if test="settlementReq.status != null">
                and kst.status = #{settlementReq.status,jdbcType=INTEGER}
            </if>
            <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
            </if>
        </where>
    </select>

    <select id="selectByIds" resultType="com.sckw.payment.model.dto.SettlementTradeDto">
        select kst.id,
               kst.status,
               kst.st_order_no,
               kst.create_time,
               kst.receipt_time,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_time,
               kst.t_ledger_id,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               kltu.firm_name
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            kst.id in
            <foreach collection="ids" open="(" close=")" item="item">
                #{item,jdbcType=BIGINT}
            </foreach>
            and kltu.unit_type = #{unitType,jdbcType=INTEGER}
        </where>
    </select>
    <select id="getById" resultType="com.sckw.payment.model.dto.SettlementTradeDto">
        select kst.id,
               kst.status,
               kst.st_order_no                                    stOrderNo,
               kst.create_time                                    createTime,
               kst.receipt_time,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_time,
               kst.t_ledger_id,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               kltu.firm_name
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            kst.del_flag = 0
              and kst.id = #{id,jdbcType=BIGINT}
              and kltu.unit_type = #{unitType,jdbcType=INTEGER}
        </where>
    </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_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            kst.del_flag = 0
              and kst.id = #{id,jdbcType=BIGINT}
        </where>
    </select>

    <select id="selectDetail" resultType="com.sckw.payment.model.dto.SettlementTradeDto">
        select kst.id,
               kst.status,
               kst.st_order_no,
               klt.t_ledger_no                                    tLedgerNo,
               klt.id                                             tLedgerId,
               kst.create_time,
               kst.receipt_time,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_time,
               kst.t_ledger_id,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               kltu.firm_name                                     firmName,
               kst.update_by                                      updateBy
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kst.id = #{id,jdbcType=BIGINT}
        </where>
    </select>

    <select id="selectJoin" resultType="java.lang.String">
        select kst.st_order_no
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = 1
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type = 2
        <where>
            kst.del_flag = 0
              and kst.status != 3
              and kltu.top_ent_id = #{entId,jdbcType=BIGINT}
              and kltu2.top_ent_id = #{entTarget,jdbcType=BIGINT}
        </where>
    </select>

    <select id="sumMoneyType" resultType="com.sckw.payment.model.dto.MoneyType">
        select ifnull(sum(kst.total_price - kst.actual_price), '0') money,
               1                                                    type
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kltu.top_ent_id = #{entId,jdbcType=BIGINT}
              and kst.create_time
            <foreach collection="cur" item="item" open="between" close="" separator="and">
                #{item,jdbcType=TIMESTAMP}
            </foreach>
        </where>
        union all
        select ifnull(sum(kst.total_price - kst.actual_price), '0') money,
               2                                                    type
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt
                            on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{unitType,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
              and kltu.top_ent_id = #{entId,jdbcType=BIGINT}
              and kst.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 kltu.top_ent_id                                                      entId,
               sum(IF(kltu.unit_type = 2, kst.total_price, 0.0))                    totalReceiveMoney,
               sum(IF(kltu.unit_type = 1, kst.total_price, 0.0))                    totalPayMoney,
               sum(IF(kltu.unit_type = 2, kst.actual_price, 0.0))                   receivedMoney,
               sum(IF(kltu.unit_type = 1, kst.actual_price, 0.0))                   paidMoney,
               sum(IF(kltu.unit_type = 2, kst.total_price - kst.actual_price, 0.0)) exactMoney,
               sum(IF(kltu.unit_type = 1, kst.total_price - kst.actual_price, 0.0)) unpaidMoney,
               sum(
                       IF
                           (kst.receipt_time &lt; now() &amp;&amp; kltu.unit_type = 2,
                            kst.total_price - kst.actual_price, 0.0)
                   )                                                                overdueExactMoney,
               sum(
                       IF
                           (kst.receipt_time &lt; now() &amp;&amp; kltu.unit_type = 2,
                            kst.total_price - kst.actual_price, 0.0)
                   )                                                                overduePaidMoney
        FROM `kwp_settlement_trade` kst
                 inner JOIN kwp_ledger_trade_unit kltu ON kst.t_ledger_id = kltu.t_ledger_id
            AND kltu.del_flag = 0
        WHERE kst.del_flag = 0
        GROUP BY kltu.top_ent_id
    </select>

    <select id="exportList" resultType="com.sckw.payment.model.dto.SettlementTradeDto">
        select kst.id,
               kst.status,
               kst.st_order_no,
               klt.t_ledger_no                                    tLedgerNo,
               kst.receipt_time,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               (case
                    when #{settlementReq.unitType,jdbcType=INTEGER} = 1 then klt.audit_user
                    else klt.success_user end)  as                contacts,
               (case
                    when #{settlementReq.unitType,jdbcType=INTEGER} = 1 then klt.audit_phone
                    else klt.success_phone end) as                phone,
               kst.create_by                                      createBy,
               kst.update_by                                      updateBy,
               kst.create_time                                    createTime,
               kst.update_time                                    updateTime,
               klt.name,
               klt.trading,
               kltu.firm_name                                     firmName
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type =
                                                           #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
            <choose>
                <when test="idList != null and idList.size() != 0">
                    and kst.id in
                    <foreach collection="idList" open="(" separator="," close=")" item="item">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </when>
                <otherwise>
                    and kltu2.top_ent_id = #{settlementReq.entId,jdbcType=INTEGER}
                    <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                        and (
                                    klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                                or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                                or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            )
                    </if>
                    <if test="settlementReq.trading != null and settlementReq.trading != ''">
                        and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
                    </if>
                    <if test="settlementReq.status != null and settlementReq.status != 0">
                        and kst.status = #{settlementReq.status,jdbcType=INTEGER}
                    </if>
                    <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                        and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
                    </if>
                    <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                        and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>
    <select id="operateExportList" resultType="com.sckw.payment.model.vo.SettlementTrade">
        select kst.id,
               kst.status,
               kst.st_order_no                                    stOrderNo,
               kst.receipt_time                                   receiptTime,
               cast(kst.actual_price as char)                     actualPrice,
               cast(kst.total_price as char)                      totalPrice,
               cast((kst.total_price - kst.actual_price) as char) waitPrice,
               kst.audit_user                                     auditUser,
               kst.audit_phone                                    auditPhone,
               kst.success_user                                   successUser,
               kst.success_phone                                  successPhone,
               kst.create_by                                      createBy,
               kst.create_time                                    createTime,
               kst.t_ledger_id                                    tLedgerId,
               klt.id                                             ledgerId,
               klt.name,
               klt.trading,
               klt.t_ledger_no                                    tLedgerNo,
               klt.generate_time                                  generateTime,
               kltu.firm_name                                     sellFirmName,
               kltu2.firm_name                                    purchaseFirmName,
               kst.update_by                                      updateBy
        from kwp_settlement_trade kst
                 inner join kwp_ledger_trade klt on kst.t_ledger_id = klt.id and klt.del_flag = 0
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                                                          kltu.unit_type = #{settlementReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2 on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and
                                                           kltu2.unit_type =
                                                           #{settlementReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kst.del_flag = 0
            <if test="authEntIdList != null and authEntIdList.size() != 0">
                and kst.ent_id in
                <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <choose>
                <when test="idList != null and idList.size() != 0">
                    and kst.id in
                    <foreach collection="idList" open="(" separator="," close=")" item="item">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </when>
                <otherwise>
                    <if test="settlementReq.keywords != null and settlementReq.keywords != ''">
                        and (
                                    klt.name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                                or kltu.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                                or kltu2.firm_name like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                                or kst.st_order_no like concat('%', #{settlementReq.keywords,jdbcType=VARCHAR}, '%')
                            )
                    </if>
                    <if test="settlementReq.trading != null and settlementReq.trading != ''">
                        and klt.trading = #{settlementReq.trading,jdbcType=VARCHAR}
                    </if>
                    <if test="settlementReq.status != null  and settlementReq.status != 0">
                        and kst.status = #{settlementReq.status,jdbcType=INTEGER}
                    </if>
                    <if test="settlementReq.startCreateTime != null and settlementReq.startCreateTime != '' and settlementReq.endCreateTime != null and settlementReq.endCreateTime != ''">
                        and kst.create_time between #{settlementReq.startCreateTime,jdbcType=TIMESTAMP} and #{settlementReq.endCreateTime,jdbcType=TIMESTAMP}
                    </if>
                    <if test="settlementReq.startReceiptTime != null and settlementReq.startReceiptTime != '' and settlementReq.endReceiptTime != null and settlementReq.endReceiptTime != ''">
                        and kst.receipt_time between #{settlementReq.startReceiptTime,jdbcType=TIMESTAMP} and #{settlementReq.endReceiptTime,jdbcType=TIMESTAMP}
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>
