<?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.financing.apply.CFinancingBasicInfoMapper">

    <resultMap id="CFinancingBasicInfoResult" type="com.sunxung.factoring.entity.financing.apply.CFinancingBasicInfo">
        <result column="business_number" property="businessNumber" />
        <result column="contract_number" property="contractNumber" />
        <result column="quota_disburse_batch" property="quotaDisburseBatch" />
        <result column="project_name" property="projectName" />
        <result column="main_enterprise_name" property="supplierName" />
        <result column="core_enterprise_name" property="customerName" />
        <result column="platform_name" property="tradingPlatformName" />
        <result column="financing_amount" property="financingAmount" />
        <result column="financing_status" property="financingStatus" />
        <result column="current_stage" property="currentStage" />
        <result column="gmt_modified" property="gmtModified" />
    </resultMap>

    <resultMap id="businessProcessingTaskVo" type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo">
        <result column="business_number" property="businessNumber" />
        <result column="contract_number" property="contractNumber" />
        <result column="quota_disburse_batch" property="quotaDisburseBatch" />
        <result column="project_name" property="projectName" />
        <result column="main_enterprise_name" property="supplierName" />
        <result column="core_enterprise_name" property="customerName" />
        <result column="platform_name" property="platformName" />
        <result column="financing_amount" property="financingAmount" />
        <result column="financing_status" property="statusStr" />
        <result column="current_stage" property="stageStr" />
    </resultMap>

    <select id="getListInfo" parameterType="java.lang.Long" resultMap="CFinancingBasicInfoResult">
        SELECT DISTINCT
            t1.id,
            t1.business_number,
            t3.contract_no contract_number,
            t2.quota_disburse_batch,
            t1.project_name,
            t4.corporate_name main_enterprise_name,
            t5.corporate_name core_enterprise_name,
            t6.platform_name,
            t9.financing_amount,
            t7.name current_stage,
            t8.name financing_status,
            t1.gmt_modified
        FROM c_financing_basic_info t1
            LEFT JOIN review_quota_disburse t2 ON t1.id = t2.c_financing_basic_info_id
            LEFT JOIN review_master_contract t3 ON t2.id = t3.review_quota_disburse_id
            LEFT JOIN review_financing_info t9 ON t2.id = t9.review_quota_disburse_id
            LEFT JOIN c_enterprise t4 ON t4.id = t1.supplier_id
            LEFT JOIN c_enterprise t5 ON t5.id = t1.enterprise_customer_info_id
            LEFT JOIN trading_platform t6 ON t1.trading_platform_id = t6.id
            LEFT JOIN t_dictionary t7 ON t1.current_stage = t7.code
            LEFT JOIN t_dictionary t8 ON t1.financing_status = t8.code
        WHERE t1.id = #{id}
    </select>
    <select id="findAll" resultMap="businessProcessingTaskVo">
        SELECT
            t1.id,
            t1.business_number,
            t3.contract_no contract_number,
            t2.quota_disburse_batch,
            t1.project_name,
            t4.corporate_name main_enterprise_name,
            t5.corporate_name core_enterprise_name,
            t6.platform_name,
            t9.financing_amount,
            t7.name current_stage,
            t8.name financing_status
        FROM c_financing_basic_info t1
                 LEFT JOIN review_quota_disburse t2 ON t1.id = t2.c_financing_basic_info_id
                 LEFT JOIN review_master_contract t3 ON t2.id = t3.review_quota_disburse_id
                 LEFT JOIN review_financing_info t9 ON t2.id = t9.review_quota_disburse_id
                 LEFT JOIN c_enterprise t4 ON t4.id = t1.supplier_id
                 LEFT JOIN c_enterprise t5 ON t5.id = t1.enterprise_customer_info_id
                 LEFT JOIN trading_platform t6 ON t1.trading_platform_id = t6.id
                 LEFT JOIN t_dictionary t7 ON t1.current_stage = t7.code
                 LEFT JOIN t_dictionary t8 ON t1.financing_status = t8.code
                LEFT JOIN c_supplier_info t10 ON t1.supplier_id = t10.c_enterprise_id
                LEFT JOIN c_sys_user t11 ON t10.sys_user_id = t11.id
        <where>
            <if test="userIds != null and userIds.size()>0">
                AND t11.project_manager IN
                <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="searchNumber != null and searchNumber != ''">
                AND ( t1.business_number LIKE CONCAT('%', #{businessNo},'%') OR t3.contract_no LIKE CONCAT('%',#{businessNo},'%'))
            </if>
            <if test="fastSearch != null and fastSearch != ''">
                AND (t1.project_name LIKE CONCAT('%',#{fastSearch},'%') OR t4.corporate_name LIKE CONCAT('%',#{fastSearch},'%') OR
                t5.corporate_name LIKE CONCAT('%',#{fastSearch},'%'))
            </if>
            <if test="status != null and status != ''">
                AND t1.financing_status = #{status}
            </if>
        </where>
    </select>
</mapper>
