<?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.huyi.service.credit.mapper.OwnCreditMapper">

    <select id="selectCreditList" parameterType="map" resultType="map">
        SELECT
                f.zfi_id                                        'zfiId',
                f.zfi_supplier_id                               'zfiSupplierId',
                f.zfi_core_id                                   'zfiCoreId',
                f.zfi_product_id                                'zfiProductId',
                f.zfi_create_type                               'zfiCreateType',
                f.zfi_amount                                    'zfiAmount',
                DATE_FORMAT(f.zfi_effective_date,'%Y-%m-%d')    'zfiEffectiveDate',
                DATE_FORMAT(f.zfi_sign_date,'%Y-%m-%d')         'zfiSignDate',
                DATE_FORMAT(f.zfi_expire_date,'%Y-%m-%d')       'zfiExpireDate',
                f.zfi_status                                    'zfiStatus',
                y.scy_name                                      'receiveName',
                s.scy_name                                      'openName',
                p.zfp_name                                      'zfpName',
                f.zfi_number                                    'zfiNumber',
                f.zfi_core_quota_id                             'zfiCoreQuotaId',
                DATE_FORMAT(f.create_time ,'%Y-%m-%d %H:%i:%s') 'createTime',
                f.zfi_supplier_quota_id                         'zfiSupplierQuotaId',
                f.zfi_rate                                       'zfiRate',
                p.zfp_platform                                  'zfpPlatform'

            FROM  zc_finance_inf f
            LEFT JOIN  sys_company y
            ON y.scy_id = f.zfi_supplier_id
            LEFT JOIN  sys_company s
            ON s.scy_id = f.zfi_core_id
            LEFT JOIN zc_finance_product p
            ON p.zfp_id = f.zfi_product_id
            WHERE 1=1
        <if test="paramMap.zfiId != null  and paramMap.zfiId != ''">
            and f.zfi_id = #{paramMap.zfiId}
        </if>
        <if test="paramMap.zfiNumber != null  and paramMap.zfiNumber != ''">
            and f.zfi_number like concat('%', #{paramMap.zfiNumber}, '%')
        </if>

        <if test="paramMap.zfiStatus != null  and paramMap.zfiStatus != ''">
            and f.zfi_status = #{paramMap.zfiStatus}
        </if>

        <if test="paramMap.openCompany != null  and paramMap.openCompany != ''">
            and s.scy_name like concat('%', #{paramMap.openCompany}, '%')
        </if>

        <if test="paramMap.receiveCompany != null  and paramMap.receiveCompany != ''">
            and y.scy_name like concat('%', #{paramMap.receiveCompany}, '%')
        </if>

        <if test="paramMap.zfiRate != null  and paramMap.zfiRate != ''">
            and f.zfi_rate = #{paramMap.zfiRate}
        </if>

        <if test="paramMap.zfpName != null  and paramMap.zfpName != ''">
            and p.zfp_name like concat('%', #{paramMap.zfpName}, '%')
        </if>

        <if test="paramMap.startTime != null  and paramMap.startTime != ''">
            and DATE_FORMAT(f.zfi_sign_date, '%Y-%m-%d') &gt;= #{paramMap.startTime}
        </if>

        <if test="paramMap.endTime != null  and paramMap.endTime != ''">
            and DATE_FORMAT(f.zfi_sign_date, '%Y-%m-%d') &lt;=  #{paramMap.endTime}
        </if>
        <if test="paramMap.zfiExpireDateStart != null  and paramMap.zfiExpireDateStart != ''">
            and DATE_FORMAT(f.zfi_expire_date, '%Y-%m-%d') &gt;=  #{paramMap.zfiExpireDateStart}
        </if>
        <if test="paramMap.zfiExpireDateEnd != null  and paramMap.zfiExpireDateEnd != ''">
            and DATE_FORMAT(f.zfi_expire_date, '%Y-%m-%d') &lt;=  #{paramMap.zfiExpireDateEnd}
        </if>
        <if test="paramMap.startAmt != null  and paramMap.startAmt != ''">
            and f.zfi_amount >= #{paramMap.startAmt}
        </if>
        <if test="paramMap.endAmt != null  and paramMap.endAmt != ''">
            and f.zfi_amount &lt;= #{paramMap.endAmt}
        </if>
        order by f.create_time desc

    </select>


    <select id="selectCreditLineList" parameterType="map" resultType="map">
            SELECT
                    rel.zfpcr_id                                      'zfpcrId',
                    p.zfp_name                                        'zfpName',
                    p.zfp_split                                       'zfpSplit',
                    rel.zfpcr_loan_type                               'zfpcrLoanType',
                    rel.zfpcr_date_type                               'zfpcrDateType',
                    DATE_FORMAT(rel.zfpcr_end_date,'%Y-%m-%d')        'zfpcrEndDate',
                    p.zfp_id                                          'zfpId',
                    rel.zfpcr_rate                                    'zfpcrRate',
                    p.zfp_auth_type                                   'zfpAuthType'
             FROM   zc_finance_pro_com_rel rel
             LEFT JOIN zc_finance_product p
             ON p.zfp_id = rel.zfpcr_product_id
             WHERE 1 = 1
            <if test="zfpcrId == null  or zfpcrId == ''">
                AND rel.zfpcr_status = '00'
            </if>
            <if test="zfpcrCompanyId != null  and zfpcrCompanyId != ''">
                AND rel.zfpcr_company_id = #{zfpcrCompanyId}
            </if>
            <if test="type != null  and type != ''">
                AND p.zfp_type = #{type}
            </if>
            <if test="zfpcrId != null  and zfpcrId != ''">
                AND (rel.zfpcr_id = #{zfpcrId} OR rel.zfpcr_status = '00')
            </if>


    </select>

    <select id="selectSupplierCreditLineList" parameterType="map" resultType="map">
            SELECT
                    q.zfsq_id                                         'zfsqId',
                    p.zfp_name                                        'zfpName',
                    p.zfp_split                                       'zfpSplit',
                    rel.zfpcr_loan_type                               'zfpcrLoanType',
                    rel.zfpcr_date_type                               'zfpcrDateType',
                    DATE_FORMAT(rel.zfpcr_end_date,'%Y-%m-%d')        'zfpcrEndDate',
                    p.zfp_id                                          'zfpId',
                    rel.zfpcr_rate                                    'zfpcrRate',
                    p.zfp_auth_type                                   'zfpAuthType'

             FROM   zc_finance_supplier_quota q
             LEFT JOIN zc_finance_pro_com_rel rel
             ON rel.zfpcr_id = q.zfsq_core_quota_id
             LEFT JOIN zc_finance_product p
             ON p.zfp_id = rel.zfpcr_product_id
             WHERE 1 = 1
            <if test="zfsqId == null  or zfsqId == ''">
                AND q.zfsq_status = '00'
            </if>
            <if test="zfsqCompanyId != null  and zfsqCompanyId != ''">
                AND q.zfsq_company_id = #{zfsqCompanyId}
            </if>
            <if test="zfsqId != null  and zfsqId != ''">
                AND (q.zfsq_id = #{zfsqId} OR q.zfsq_status = '00')
            </if>


    </select>

    <select id="getReceiverList" parameterType="map" resultType="map">
            SELECT     scy_name     'scyName',
                       scy_id       'scyId',
                      CASE  WHEN scy_id =#{paramMap.zfiSupplierId}  THEN 0  ELSE  1  END  'flag'
            FROM sys_company_rel
            INNER JOIN sys_company
            ON scr_launch_type = '01'
            AND scr_receive_company_id = scy_id
            WHERE  1=1
            <if test="paramMap.companyId != null  and paramMap.companyId != ''">
                AND scr_launch_company_id = #{paramMap.companyId}
            </if>
            <if test="paramMap.zfiSupplierId != null  and paramMap.zfiSupplierId != ''">
                AND (scy_id = #{paramMap.zfiSupplierId} or  (scr_status = '01' and  scy_status= '00'))
            </if>
            <if test="paramMap.zfiSupplierId == null  or paramMap.zfiSupplierId == ''">
                and scr_status = '01' and scy_status= '00'
            </if>

            UNION

            SELECT     scy_name     'scyName',
                       scy_id       'scyId',
                       CASE  WHEN scy_id =#{paramMap.zfiSupplierId}  THEN 0  ELSE  1  END  'flag'
            FROM sys_company_rel
            INNER JOIN sys_company
            ON scr_receive_type = '01'
            AND scr_launch_company_id = scy_id
            WHERE  1=1
            <if test="paramMap.companyId != null  and paramMap.companyId != ''">
                AND scr_receive_company_id = #{paramMap.companyId}
            </if>
            <if test="paramMap.zfiSupplierId != null  and paramMap.zfiSupplierId != ''">
                AND (scy_id = #{paramMap.zfiSupplierId} or  (scr_status = '01' and  scy_status= '00'))
            </if>
            <if test="paramMap.zfiSupplierId == null  or paramMap.zfiSupplierId == ''">
                and scr_status = '01' and scy_status= '00'
            </if>
            ORDER BY  flag asc
    </select>
    <select id="getAccountsPay" parameterType="map" resultType="map">
            SELECT
                    b.zbi_id                                        'zbiId',
                    b.zbi_name                                      'zbiName',
                    DATE_FORMAT(b.zbi_pay_date,'%Y-%m-%d')          'zbiPayDate',
                    DATE_FORMAT(b.zbi_date,'%Y-%m-%d')              'zbiDate',
                    b.zbi_amount                                    'zbiAmount',
                    b.zbi_service                                   'zbiService',
                    y.scy_name                                      'payName',
                    s.scy_name                                      'receiveName'

            FROM   zc_bill_inf b
            LEFT JOIN  sys_company y
            ON y.scy_id = b.zbi_payer_id
            LEFT JOIN  sys_company s
            ON s.scy_id = b.zbi_payee_id
            WHERE b.zbi_status = '00'
            <if test="paramMap.zbiPayerId != null  and paramMap.zbiPayerId != ''">
                AND b.zbi_payer_id = #{paramMap.zbiPayerId}
            </if>
            <if test="paramMap.zbiName != null  and paramMap.zbiName != ''">
                and b.zbi_name like concat('%', #{paramMap.zbiName}, '%')
            </if>
            <if test="paramMap.receiveName != null  and paramMap.receiveName != ''">
                and s.scy_name like concat('%', #{paramMap.receiveName}, '%')
            </if>
            <if test="paramMap.payIdList != null  and paramMap.payIdList != ''">
                and b.zbi_id  IN (${paramMap.payIdList})
            </if>
            <if test="paramMap.billIdList != null  and paramMap.billIdList != ''">
                and b.zbi_id  NOT IN (${paramMap.billIdList})
            </if>

    </select>

    <select id="getAccountsCollection" parameterType="map" resultType="map">
            SELECT
                    b.zbi_id                                        'zbiId',
                    b.zbi_name                                      'zbiName',
                    DATE_FORMAT(b.zbi_pay_date,'%Y-%m-%d')          'zbiPayDate',
                    DATE_FORMAT(b.zbi_date,'%Y-%m-%d')              'zbiDate',
                    b.zbi_amount                                    'zbiAmount',
                    b.zbi_service                                   'zbiService',
                    y.scy_name                                      'payName',
                    s.scy_name                                      'receiveName',
                    b.zbi_payer_id                                  'zbiPayerId'

            FROM   zc_bill_inf b
            LEFT JOIN  sys_company y
            ON y.scy_id = b.zbi_payer_id
            LEFT JOIN  sys_company s
            ON s.scy_id = b.zbi_payee_id
            WHERE b.zbi_status = '00'
            <if test="paramMap.zbiPayeeId != null  and paramMap.zbiPayeeId != ''">
                AND b.zbi_payee_id = #{paramMap.zbiPayeeId}
            </if>
            <if test="paramMap.zbiName != null  and paramMap.zbiName != ''">
                and b.zbi_name like concat('%', #{paramMap.zbiName}, '%')
            </if>
            <if test="paramMap.payName != null  and paramMap.payName != ''">
                and y.scy_name like concat('%', #{paramMap.payName}, '%')
            </if>
            <if test="paramMap.payId != null  and paramMap.payId != ''">
                and b.zbi_payer_id = #{paramMap.payId}
            </if>
            <if test="paramMap.payIdList != null  and paramMap.payIdList != ''">
                and b.zbi_id  IN (${paramMap.payIdList})
            </if>
            <if test="paramMap.billIdList != null  and paramMap.billIdList != ''">
                and b.zbi_id  NOT IN (${paramMap.billIdList})
            </if>

    </select>

    <!---核心开立已使用额度-->
    <select id="getUsedBalance" parameterType="map" resultType="java.lang.String">
            SELECT
                 CAST(ROUND(IFNULL(SUM(f.zfi_amount),0),2) AS CHAR)              'allAmount'

            FROM   zc_finance_inf f
            WHERE f.zfi_status IN ('01','02','04','05')
            <if test="companyId != null  and companyId != ''">
                AND f.zfi_core_id = #{companyId}
            </if>
            <if test="zfpcrId != null  and zfpcrId != ''">
                AND f.zfi_core_quota_id = #{zfpcrId}
            </if>

    </select>

    <!--供应商已使用额度-->
    <select id="getSupplierUsedBalance" parameterType="map" resultType="java.lang.String">
        SELECT
        CAST(ROUND(IFNULL(SUM(f.zfi_amount),0),2) AS CHAR)              'allAmount'

        FROM   zc_finance_inf f
        WHERE f.zfi_status IN ('01','02','04','05')
        <if test="companyId != null  and companyId != ''">
            AND f.zfi_supplier_id = #{companyId}
        </if>
        <if test="zfsqId != null  and zfsqId != ''">
            AND f.zfi_supplier_quota_id = #{zfsqId}
        </if>

    </select>



</mapper>