<?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.FinancialCustomerMapper">
    <resultMap type="com.sunxung.factoring.entity.fundmanagement.FinancialCustomerInfo" id="BaseResultMap">
        <result property="id" column="id"/>
        <result property="financialRelationCustomerId" column="financial_institution_info_id"/>
        <result property="customerStatus" column="customer_status"/>
        <result property="userCreate" column="user_create"/>
        <result property="userModified" column="user_modified"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
    </resultMap>

    <resultMap type="com.sunxung.factoring.entity.fundmanagement.vo.FinancialCustomerVo" id="ResultMap">
        <result property="id" column="id"/>
        <result property="institutionName" column="institution_name"/>
        <result property="status.id" column="customer_status"/>
        <result property="customerName" column="credit_subject_name"/>
        <result property="userCreate" column="user_create"/>
        <result property="userModified" column="user_modified"/>
        <result property="gmtCreate" column="gmt_create"/>
        <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="customer_status"/>
        <result property="customerName" column="customer_name"/>
    </resultMap>

    <select id="findMyCompletedList"
            resultMap="processingResultMap"
            parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask">
        SELECT distinct
        c.id,
        c.customer_status,
        t1.institution_name,
        t2.credit_subject_name customer_name
        FROM financial_customer_info c
        LEFT JOIN financial_relation_customer t2 ON t2.id = c.financial_relation_customer_id
        left join financial_institution_info t1 on t1.id = t2.financial_institution_info_id
        <where>

            <if test="fastSearch != null  and fastSearch !=''">
                AND t1.institution_name LIKE concat('%', #{fastSearch}, '%') OR t2.credit_subject_name LIKE concat('%',
                #{fastSearch},
                '%')
            </if>
        </where>
        order by c.gmt_create desc
    </select>
    <select id="findByFinancialCustomerId"
            resultType="com.sunxung.factoring.entity.fundmanagement.vo.FinancialBasicFileVo">
        select t2.id financial_basic_file_dic_info_id,
            t2.profile_name,
            t2.remark,
            t1.id
            from financial_basic_file_info t1 LEFT JOIN financial_basic_file_dic_info t2 ON t2.id = t1.financial_basic_file_dic_info_id
            where financial_customer_info_id =#{value}
    </select>
    <select id="findVoById" resultMap="ResultMap">
        SELECT distinct
        c.id,
        c.customer_status,
        t2.institution_name,
        t1.credit_subject_name,
        c.user_create,
        c.user_modified,
        c.gmt_create,
        c.gmt_modified
        FROM financial_customer_info c
        left join financial_relation_customer t1 on t1.id = c.financial_relation_customer_id
        left join financial_institution_info t2 on t2.id = t1.financial_institution_info_id
        where c.id = #{value}
        order by c.gmt_create desc
    </select>
    <select id="findAllList" resultMap="processingResultMap"
            parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask">
        SELECT distinct
        c.id,
        c.customer_status,
        t1.institution_name,
        t2.credit_subject_name customer_name
        FROM financial_customer_info c
        LEFT JOIN financial_relation_customer t2 ON t2.id = c.financial_relation_customer_id
        left join financial_institution_info t1 on t1.id = t2.financial_institution_info_id
        left join sys_user t3 on t3.name = t1.customer_manager
        <where>
            <if test="fastSearch != null and fastSearch !=''">
                t1.institution_name LIKE concat('%', #{fastSearch}, '%') OR t2.credit_subject_name LIKE concat('%',
                #{fastSearch},
                '%')
            </if>
            <if test="userIds != null and userIds.size() > 0">
                AND t3.id in
                <foreach item="item" collection="userIds" separator="," open="(" close=")"
                         index="">
                                #{item}
                </foreach>
            </if>
            <if test="backName != null and backName != ''">
                AND t1.institution_name = #{backName}
            </if>
        </where>
        order by c.gmt_create desc
    </select>
    <select id="findHisVoById" resultMap="ResultMap">
        SELECT distinct
        t1.id,
        t1.customer_status,
        t3.institution_name,
        t2.credit_subject_name,
        t1.user_create,
        t1.user_modified,
        t1.gmt_create,
        t1.gmt_modified
        FROM financial_customer_his_info c
        left join financial_customer_info t1 on c.financial_customer_info_id = t1.id
        left join financial_relation_customer t2 on t2.id = t1.financial_relation_customer_id
        left join financial_institution_info t3 on t3.id = t2.financial_institution_info_id
        where c.id = #{value}
        order by c.gmt_create desc
    </select>

</mapper>
