<?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.business.separateaccounts.mapper.SeparateAccountsMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.business.separateaccounts.domain.SeparateAccounts">
        <id column="id" property="id"/>
        <id column="business_number" property="businessNumber"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="billing_income_id" property="billingIncomeId"/>
        <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_amount" property="entryAmount"/>
        <id column="entry_currency" property="entryCurrency"/>
        <id column="entry_date" property="entryDate"/>
        <id column="actual_retained_currency" property="actualRetainedCurrency"/>
        <id column="settlement_currency" property="settlementCurrency"/>
        <id column="actual_retained_amount" property="actualRetainedAmount"/>
        <id column="settlement_amount" property="settlementAmount"/>
        <id column="deleted" property="deleted"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
    </resultMap>

    <resultMap id="PageResultMap" type="com.trade.service.business.separateaccounts.vo.SeparateAccountsPageVo">
        <id column="id" property="id"/>
        <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_amount" property="entryAmount"/>
        <id column="entry_currency" property="entryCurrency"/>
        <id column="entry_date" property="entryDate"/>
        <id column="enterprise_name" property="customerName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="business_number" property="businessNumber"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , business_number
        , customer_user_id, billing_income_id, sales_contract_no, contract_name, importer_cert_no, importer_enterprise_name, agency_cert_no, agency_enterprise_name, financier_identifier, payment_type, status, transaction_number,
        entry_amount, entry_currency, settlement_currency, actual_retained_currency, actual_retained_amount, settlement_amount, entry_date, deleted, gmt_create, gmt_modified
    </sql>
    <select id="selectByPage" resultMap="PageResultMap"
            parameterType="com.trade.service.business.separateaccounts.dto.SeparateAccountsSearchDto">
        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.entry_amount, t1.entry_currency, t1.entry_date, t1.business_number,
        e.name AS enterprise_name, e.customer_identifier_no
        from separate_accounts t1 left join enterprise e on t1.customer_user_id = e.customer_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="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.entry_date <![CDATA[ >= ]]> #{beginTime}
        </if>
        <if test="endTime != null">
            AND t1.entry_date <![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="status != null and status == '1'">
            ORDER BY t1.entry_date DESC
        </if>
        <if test="status != null and status == '2'">
            ORDER BY t1.entry_date ASC
        </if>
    </select>

</mapper>