<?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.disbursement.mapper.BillingDisbursementMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.finance.disbursement.domain.BillingDisbursement">
        <id column="id" property="id"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="payment_basic_id" property="paymentBasicId"/>
        <id column="business_number" property="businessNumber"/>
        <id column="sales_contract_no" property="salesContractNo"/>
        <id column="contract_name" property="contractName"/>
        <id column="importer_cert_no" property="importerCertNo"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="agency_enterprise_name" property="agencyEnterpriseName"/>
        <id column="financier_identifier" property="financierIdentifier"/>
        <id column="payment_type" property="paymentType"/>
        <id column="status" property="status"/>
        <id column="transaction_number" property="transactionNumber"/>
        <id column="payment_amount" property="paymentAmount"/>
        <id column="payment_currency" property="paymentCurrency"/>
        <id column="payment_date" property="paymentDate"/>
        <id column="deleted" property="deleted"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="entry_business_number" property="transactionNumber"/>
        <id column="payee_type" property="payeeType"/>
        <id column="source" property="source"/>
        <id column="separate_accounts_info_id" property="separateAccountsInfoId"/>
        <id column="voucher_no" property="voucherNo"/>
        <id column="account_number" property="accountNumber"/>
        <id column="payer_type" property="payerType"/>
        <id column="payer_name" property="payerName"/>
        <id column="trade_participants_identifier" property="tradeParticipantsIdentifier"/>
    </resultMap>

    <resultMap id="PageResultMap" type="com.trade.service.finance.disbursement.vo.BillingDisbursementPageVo">
        <id column="id" property="id"/>
        <id column="business_number" property="businessNumber"/>
        <id column="sales_contract_no" property="salesContractNo"/>
        <id column="contract_name" property="contractName"/>
        <id column="importer_cert_no" property="importerCertNo"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="agency_enterprise_name" property="agencyEnterpriseName"/>
        <id column="financier_identifier" property="financierIdentifier"/>
        <id column="payment_type" property="paymentType"/>
        <id column="status" property="status"/>
        <id column="transaction_number" property="transactionNumber"/>
        <id column="entry_business_number" property="entryBusinessNumber"/>
        <id column="payee_type" property="payeeType"/>
        <id column="payment_amount" property="paymentAmount"/>
        <id column="payment_currency" property="paymentCurrency"/>
        <id column="payment_date" property="paymentDate"/>
        <id column="enterprise_name" property="customerName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="trade_participants_identifier" property="tradeParticipantsIdentifier"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , voucher_no, account_number, payer_type, payer_name, trade_participants_identifier
        , customer_user_id, payment_basic_id, separate_accounts_info_id, business_number, entry_business_number, sales_contract_no, contract_name, importer_cert_no, importer_enterprise_name, agency_cert_no, agency_enterprise_name, financier_identifier, payment_type, status, transaction_number, payment_amount, payment_currency, payment_date, payee_type, source, deleted, gmt_create, gmt_modified
    </sql>
    <select id="selectByPage"
            resultMap="PageResultMap"
            resultType="com.trade.service.finance.disbursement.dto.DisbursementSearchDto">
        select
        t1.id, t1.sales_contract_no, t1.contract_name, t1.importer_cert_no, t1.importer_enterprise_name,
        t1.agency_cert_no, t1.agency_enterprise_name, t1.financier_identifier, t1.payment_type, t1.status,
        t1.transaction_number, t1.business_number, t1.payment_amount, t1.payment_currency, t1.payment_date,
        t1.entry_business_number, t1.payee_type, t1.gmt_create, t1.trade_participants_identifier,
        e.name AS enterprise_name, e.customer_identifier_no
        from billing_disbursement t1 left join enterprise e on t1.customer_user_id = e.customer_id
        where t1.deleted = 0 and source = 1
        <if test="paymentType != null and paymentType != ''">
            AND t1.payment_type = #{paymentType}
        </if>
        <if test="payeeType != null and payeeType != ''">
            AND t1.payee_type = #{payeeType}
        </if>
        <if test="payeeName != null and payeeName != ''">
            AND t1.trade_participants_identifier = #{payeeName}
        </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="agencyCertNo != null and agencyCertNo != ''">
            AND t1.agency_cert_no = #{agencyCertNo}
        </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="number != null and number != ''">
            AND t1.entry_business_number = #{number} OR AND t1.business_number = #{number}
        </if>
        ORDER BY t1.gmt_create DESC
    </select>


    <resultMap id="PageCResultMap" type="com.trade.service.finance.disbursement.vo.ReceivePaymentPageVo">
        <id column="id" property="id"/>
        <id column="business_number" property="businessNumber"/>
        <id column="sales_contract_no" property="salesContractNo"/>
        <id column="contract_name" property="contractName"/>
        <id column="importer_cert_no" property="importerCertNo"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="payer_name" property="payerName"/>
        <id column="payment_type" property="paymentType"/>
        <id column="payer_type" property="payerType"/>
        <id column="payment_amount" property="receivePaymentAmount"/>
        <id column="payment_currency" property="receivePaymentCurrency"/>
        <id column="payment_date" property="paymentDate"/>
    </resultMap>

    <select id="selectByPageForCustomer"
            resultMap="PageCResultMap"
            resultType="com.trade.service.finance.disbursement.dto.ReceivePaymentSearchDto">
        select
        t1.id, t1.sales_contract_no, t1.contract_name, t1.importer_cert_no, t1.importer_enterprise_name,
        t1.payer_name, t1.payment_type, t1.payer_type,
        t1.business_number, t1.payment_amount, t1.payment_currency, t1.payment_date
        from billing_disbursement t1 where t1.deleted = 0
        <if test="customerUserId != null">
            AND t1.customer_user_id = #{customerUserId}
        </if>
        <if test="businessNumber != null and businessNumber != ''">
            AND t1.business_number = #{businessNumber}
        </if>
        <if test="paymentType != null and paymentType != ''">
            AND t1.payment_type = #{paymentType}
        </if>
        <if test="importerCertNo != null and importerCertNo != ''">
            AND t1.importer_cert_no = #{importerCertNo}
        </if>
        <if test="payerType != null and payerType != ''">
            AND t1.payer_type = #{payerType}
        </if>
        <if test="salesContractNo != null and salesContractNo != ''">
            AND t1.sales_contract_no = #{salesContractNo}
        </if>
        <if test="payerName != null and payerName != ''">
            AND t1.payer_name = #{payerName}
        </if>
        <if test="beginTime != null">
            AND t1.payment_date <![CDATA[ >= ]]> #{beginTime}
        </if>
        <if test="endTime != null">
            AND t1.payment_date <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY t1.payment_date DESC
    </select>
</mapper>