<?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.sunxung.factoring.mapper.creditresolution.CreditResolutionMapper">


    <insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO `credit_resolution` (
            `c_project_information_id`,
            `credit_type`,
            `domestic_factoring`,
            `implementing_factoring_products`,
            `implementing_factoring_type`,
            `proposed_credit_line`,
            `currency`,
            `credit_start_date`,
            `proposed_credit_term`,
            `credit_end_date`,
            `financing_ratio_low`,
            `financing_ratio_high`,
            `plan_irr`,
            `proposed_financing_ratio_two_low`,
            `proposed_financing_ratio_two_high`,
            `plan_irr_two`,
            `financing_rate_low`,
            `financing_rate_high`,
            `management_rate_low`,
            `management_rate_high`,
            `margin_ratio_low`,
            `margin_ratio_high`,
            `comprehensive_quotation`,
            `principal_payment_method`,
            `payment_method_for_interest_management_fee`,
            `payment_method_for_interest_management_fee_detail`,
            `margin_payment_method`,
            `other_fee_payment_method`,
            `upstream_accounting_period`,
            `upstream_accounting_period_chang_reason`,
            `factoring_financing_period`,
            `factoring_financing_period_chang_reason`,
            `credit_report`,
            `report_method`,
            `credit_enhancement_measures_one`,
            `credit_enhancement_measures_two`,
            `credit_enhancement_measures_three`,
            `credit_enhancement_measures_fore`,
            `credit_enhancement_measures_illustrate`,
            `has_condition`,
            `status`
        )
        VALUES
            (
                #{cProjectInformationId},
                #{creditType},
                #{domesticFactoring},
                #{implementingFactoringProducts},
                #{implementingFactoringType},
                #{proposedCreditLine},
                #{currency},
                #{creditStartDate},
                #{proposedCreditTerm},
                #{creditEndDate},
                #{financingRatioLow},
                #{financingRatioHigh},
                #{planIrr},
                #{proposedFinancingRatioTwoLow},
                #{proposedFinancingRatioTwoHigh},
                #{planIrrTwo},
                #{financingRateLow},
                #{financingRateHigh},
                #{managementRateLow},
                #{managementRateHigh},
                #{marginRatioLow},
                #{marginRatioHigh},
                #{comprehensiveQuotation},
                #{principalPaymentMethod},
                #{paymentMethodForInterestManagementFee},
                #{paymentMethodForInterestManagementFeeDetail},
                #{marginPaymentMethod},
                #{otherFeePaymentMethod},
                #{upstreamAccountingPeriod},
                #{upstreamAccountingPeriodChangReason},
                #{factoringFinancingPeriod},
                #{factoringFinancingPeriodChangReason},
                #{creditReport},
                #{reportMethod},
                #{creditEnhancementMeasuresOne},
                #{creditEnhancementMeasuresTwo},
                #{creditEnhancementMeasuresThree},
                #{creditEnhancementMeasuresFore},
                #{creditEnhancementMeasuresIllustrate},
                #{hasCondition},
                #{statusStr}
            );
    </insert>


    <select id="getByProjectInfoId"
            resultType="com.sunxung.factoring.entity.creditresolution.CreditResolution">
        select * from credit_resolution where c_project_information_id = #{value}
    </select>
    <select id="getById" resultType="com.sunxung.factoring.entity.creditresolution.CreditResolution">
        select
            `id`,
            `c_project_information_id`,
            `credit_type`,
            `domestic_factoring`,
            `implementing_factoring_products`,
            `implementing_factoring_type`,
            `proposed_credit_line`,
            `currency`,
            `credit_start_date`,
            `proposed_credit_term`,
            `credit_end_date`,
            `financing_ratio_low`,
            `financing_ratio_high`,
            `plan_irr`,
            `proposed_financing_ratio_two_low`,
            `proposed_financing_ratio_two_high`,
            `plan_irr_two`,
            `financing_rate_low`,
            `financing_rate_high`,
            `management_rate_low`,
            `management_rate_high`,
            `margin_ratio_low`,
            `margin_ratio_high`,
            `comprehensive_quotation`,
            `principal_payment_method`,
            `payment_method_for_interest_management_fee`,
            `payment_method_for_interest_management_fee_detail`,
            `margin_payment_method`,
            `other_fee_payment_method`,
            `upstream_accounting_period`,
            `upstream_accounting_period_chang_reason`,
            `factoring_financing_period`,
            `factoring_financing_period_chang_reason`,
            `credit_report`,
            `report_method`,
            `credit_enhancement_measures_one`,
            `credit_enhancement_measures_two`,
            `credit_enhancement_measures_three`,
            `credit_enhancement_measures_fore`,
            `credit_enhancement_measures_illustrate`,
            `has_condition`,
            `status` statusStr,
            gmt_modified
            from credit_resolution where id = #{value}
    </select>
    <select id="allList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultType="com.sunxung.factoring.entity.creditresolution.CreditResolution">
        select
            t1.id,
            t1.status statusStr,
            t2.business_number,
            t2.project_name,
            t8.corporate_name ownerName,
            t5.corporate_name customerName,
            t1.gmt_modified
        from credit_resolution t1
            left join c_project_information t2 on t1.c_project_information_id = t2.id
            left join c_enterprise t3 on t2.c_enterprise_id = t3.id
            left join c_project_core_enterprise t4 on t4.c_project_information_id = t2.id
            left join c_enterprise t5 on t4.c_enterprise_id =t5.id
            left join c_supplier_info t6 on t2.c_supplier_info_id = t6.id
            left join c_sys_user t7 on t7.id = t6.sys_user_id
            left join c_enterprise t8 on t8.id = t6.c_enterprise_id
        <where>
            <if test="fastSearch != null and fastSearch !=''">
                AND (t2.project_name like concat('%',#{fastSearch},'%') OR t8.corporate_name like concat('%',#{fastSearch},'%')
                OR t5.corporate_name like concat('%',#{fastSearch},'%') OR t2.business_number LIKE concat('%',#{fastSearch},'%'))
            </if>
            <if test="businessNo != null and businessNo !=''">
                AND t2.business_number = #{businessNo}
            </if>
            <if test="status != null and status !=''">
                AND t1.status = #{status}
            </if>
            <if test="userIds != null and userIds.size()>0">
                AND (t7.project_manager in
                <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
                    or t7.risk_manager in
                <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>)
            </if>
        </where>
            order by t1.gmt_modified desc
    </select>
</mapper>
