<?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.fundmanagement.FinancialCustomerExpenditureMapper">
    <resultMap type="com.sunxung.factoring.entity.fundmanagement.FinancialCustomerExpenditureInfo" id="BaseResultMap">
        <result property="id" column="id"/>
        <result property="status.id" column="disburse_status"/>
        <result property="institutionName" column="institution_name"/>
        <result property="customerName" column="customer_name"/>
        <result property="contractNo" column="contract_no"/>
        <result property="quotaDisburseBatch" column="quota_disburse_batch"/>
        <result property="expireDate" column="expire_date"/>
        <result property="gmtModified" column="gmt_modified"/>
    </resultMap>

    <resultMap type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo" id="processingResultMap">
        <result property="id" column="id"/>
        <result property="institutionName" column="institution_name"/>
        <result property="status.id" column="disburse_status"/>
        <result property="businessNumber" column="business_no"/>
        <result property="customerName" column="customer_name"/>
        <result property="contractNumber" column="contract_no"/>
        <result property="quotaDisburseBatch" column="quota_disburse_batch"/>
        <result property="expireDate" column="expire_date"/>
    </resultMap>

    <select id="getDetail" parameterType="java.lang.Long" resultMap="BaseResultMap">
        SELECT DISTINCT
            t1.id,
            t1.disburse_status,
            t4.institution_name,
            t3.credit_subject_name customer_name,
            t2.contract_no,
            t2.quota_disburse_batch,
            t2.expire_date,
            t1.gmt_modified
        FROM financial_customer_expenditure_info t1
        LEFT JOIN financial_relation_customer_disburse t2 on t1.financial_relation_customer_disburse_id = t2.id
        LEFT JOIN financial_relation_customer t3 on t2.financial_relation_customer_id = t3.id
        LEFT JOIN financial_institution_info t4 on t3.financial_institution_info_id = t4.id
        WHERE t1.id = #{id}
        ORDER BY t1.gmt_create DESC
    </select>
    <select id="findAllList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask"
            resultMap="processingResultMap">
        SELECT
            t4.institution_name,
            t3.credit_subject_name customer_name,
            t2.business_no,
            t2.contract_no,
            t2.quota_disburse_batch,
            t2.expire_date,
            t1.disburse_status,
            t1.id,
            t5.id user_id
        FROM financial_customer_expenditure_info t1
        LEFT JOIN financial_relation_customer_disburse t2 ON t2.id = t1.financial_relation_customer_disburse_id
        LEFT JOIN financial_relation_customer t3 ON t3.id = t2.financial_relation_customer_id
        LEFT JOIN financial_institution_info t4 ON t4.id = t3.financial_institution_info_id
        LEFT JOIN sys_user t5 ON t4.customer_manager = t5.name
        <where>
            <if test="fastSearch != null and fastSearch !=''">
                (t4.institution_name LIKE concat('%', #{fastSearch}, '%') OR t3.credit_subject_name LIKE
                concat('%',#{fastSearch},'%'))
            </if>
            <if test="userIds != null and userIds.size() > 0">
                AND t5.id in
                <foreach item="item" collection="userIds" separator="," open="(" close=")"
                         index="">
                    #{item}
                </foreach>
            </if>
            <if test="backName != null and backName != ''">
                AND t4.institution_name = #{backName}
            </if>
            <if test="startDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.expire_date, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>
            </if>
            <if test="endDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.expire_date, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>
            </if>
        </where>
        ORDER BY t1.gmt_create DESC
    </select>
</mapper>
