<?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.trade.service.finance.paymentcollection.mapper.BillingIncomeMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.finance.paymentcollection.domain.BillingIncome">
        <id column="actual_amount" property="actualAmount"/>
        <id column="actual_retained_amount" property="actualRetainedAmount"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="agency_enterprise_name" property="agencyEnterpriseName"/>
        <id column="apply_amount" property="applyAmount"/>
        <id column="contract_name" property="contractName"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="deleted" property="deleted"/>
        <id column="apply_entry_date" property="applyEntryDate"/>
        <id column="financier_identifier" property="financierIdentifier"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="id" property="id"/>
        <id column="importer_cert_no" property="importerCertNo"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="payment_type" property="paymentType"/>
        <id column="payment_way" property="paymentWay"/>
        <id column="remark" property="remark"/>
        <id column="sales_contract_no" property="salesContractNo"/>
        <id column="settlement_amount" property="settlementAmount"/>
        <id column="status" property="status"/>
        <id column="transaction_number" property="transactionNumber"/>
        <id column="apply_currency" property="applyCurrency"/>
        <id column="settlement_currency" property="settlementCurrency"/>
        <id column="actual_currency" property="actualCurrency"/>
        <id column="actual_retained_currency" property="actualRetainedCurrency"/>
        <id column="actual_entry_date" property="actualEntryDate"/>
        <id column="sale_order_no" property="saleOrderNo"/>
        <id column="business_number" property="businessNumber"/>
        <id column="payee_type" property="payeeType"/>
        <id column="payee_name" property="payeeName"/>
        <id column="payer_type" property="payerType"/>
        <id column="payer_name" property="payerName"/>
        <id column="trade_receivable_currency" property="tradeReceivableCurrency"/>
        <id column="trade_receivable_amount" property="tradeReceivableAmount"/>
    </resultMap>

    <resultMap id="PageResultMap" type="com.trade.service.finance.paymentcollection.vo.BillingIncomePageVo">
        <id column="actual_amount" property="actualAmount"/>
        <id column="apply_amount" property="applyAmount"/>
        <id column="financier_identifier" property="financierIdentifier"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="id" property="id"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="payment_type" property="paymentType"/>
        <id column="payment_way" property="paymentWay"/>
        <id column="sales_contract_no" property="contractNo"/>
        <id column="status" property="status"/>
        <id column="transaction_number" property="transactionNumber"/>
        <id column="apply_currency" property="applyCurrency"/>
        <id column="actual_currency" property="actualCurrency"/>
        <id column="enterprise_name" property="customerName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="business_number" property="businessNumber"/>
        <id column="voucher_no" property="voucherNo"/>
        <id column="payee_type" property="payeeType"/>
        <id column="payee_name" property="payeeName"/>
        <id column="payer_type" property="payerType"/>
        <id column="payer_name" property="payerName"/>
    </resultMap>
    <sql id="Base_Column_List">
        actual_amount
        , business_number, payer_type, payer_name, payee_type, payee_name, trade_receivable_amount, trade_receivable_currency
        , actual_retained_amount, agency_cert_no, agency_enterprise_name, apply_amount, contract_name, customer_user_id, deleted, entry_date, financier_identifier, gmt_create, gmt_modified, id, importer_cert_no, importer_enterprise_name, payment_type, payment_way, remark, sales_contract_no, settlement_amount, status, transaction_number, currency_code,
        actual_currency, settlement_currency, actual_retained_currency, actual_entry_date, sale_order_no
    </sql>
    <select id="selectByPage"
            parameterType="com.trade.service.finance.paymentcollection.dto.BillingIncomeSearchDto"
            resultMap="PageResultMap">
        select t1.actual_amount, t1.apply_amount, t1.financier_identifier, t1.gmt_create, t1.id,
        t1.importer_enterprise_name, t1.payment_type, t1.payment_way, t1.sales_contract_no,
        t1.status, t1.transaction_number, t1.apply_currency, t1.actual_currency, t1.business_number,
        t1.payer_type, t1.payer_name, t1.payee_type, t1.payee_name,
        e.name AS enterprise_name, e.customer_identifier_no, t2.voucher_no
        from billing_income t1 left join enterprise e on t1.customer_user_id = e.customer_id
        left join billing_income_info t2 on t1.id = t2.billing_income_id
        where t1.deleted = 0
        <if test="paymentType != null and paymentType != ''">
            AND t1.payment_type = #{paymentType}
        </if>
        <if test="customerUserInfo != null and customerUserInfo != ''">
            AND e.name LIKE CONCAT ('%',#{customerUserInfo},'%') OR e.customer_identifier_no = #{customerUserInfo}
        </if>
        <if test="financierIdentifier != null and financierIdentifier != ''">
            AND t1.financier_identifier = #{financierIdentifier}
        </if>
        <if test="importerCertNo != null and importerCertNo != ''">
            AND t1.importer_cert_no = #{importerCertNo}
        </if>
        <if test="status != null and status != ''">
            AND t1.status = #{status}
        </if>
        <if test="contractNo != null and contractNo != ''">
            AND t1.sales_contract_no = #{contractNo}
        </if>
        <if test="beginTime != null">
            AND t1.gmt_create <![CDATA[ >= ]]> #{beginTime}
        </if>
        <if test="endTime != null">
            AND t1.gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="transactionNumber != null and transactionNumber != ''">
            AND t1.transaction_number = #{transactionNumber}
        </if>
        <if test="businessNumber != null and businessNumber != ''">
            AND t1.business_number = #{businessNumber}
        </if>
        <if test="voucherNo != null and voucherNo != ''">
            AND t2.voucher_no = #{voucherNo}
        </if>
        order by t1.gmt_create desc
    </select>

</mapper>