<?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.loanmanagement.ExemptionMattersMapper">


    <resultMap type="BusinessAssignee" id="assigneeResult">
        <result property="id" column="id"/>
        <result property="businessKey" column="business_key"/>
        <result property="businessType" column="business_type"/>
        <result property="assigneeId" column="assignee_id"/>
        <result property="assigneeType" column="assignee_type"/>
        <result property="userCreate" column="user_create"/>
        <result property="userModified" column="user_modified"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
        <result property="candidateId" column="candidate_id"/>
    </resultMap>

    <resultMap type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo" id="baseResultMap">
        <result property="id" column="id"/>
        <result property="businessNumber" column="business_number"/>
        <result property="projectName" column="project_name"/>
        <result property="customerName" column="customer_name"/>
        <result property="contractNumber" column="contract_no"/>
        <result property="contractNumber" column="contract_name"/>
        <result property="customerName" column="customer_name"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="platformName" column="platform_name"/>
        <result property="quotaDisburseBatch" column="quota_disburse_batch" />
        <result property="paymentNo" column="payment_no" />
        <result property="gmtModified" column="gmt_modified"/>
        <association property="status" column="status" resultMap="dictionaryResult"/>
        <association property="assignee" column="manager_id" resultMap="userResult"/>
    </resultMap>

    <resultMap id="dictionaryResult" type="Dictionary">
        <result property="id" column="did"/>
        <result property="name" column="dname"/>
        <result property="code" column="dcode"/>
    </resultMap>

    <resultMap id="userResult" type="User">
        <result property="id" column="uid"/>
        <result property="name" column="uname"/>
    </resultMap>


    <select id="findBySearch" resultMap="baseResultMap">
        SELECT DISTINCT
        t1.id,
        t3.business_no business_number,
        t3.contract_no,
        t3.quota_disburse_batch,
        t2.payment_no,
        t6.project_name,
        t8.corporate_name  supplier_name, -- 主企业名称
        t10.corporate_name customer_name, -- 核心企业名称
        t5.platform_name,
        t12.name dname,
        t12.code dcode,
        t12.id did
        FROM
        exemption_matters t1
        left join payment_apply t2 on t2.id = t1.payment_apply_id
        left join payment_info t3 on t3.id = t2.payment_info_id
        left join c_financing_basic_info t4 on t4.business_number = t3.business_no
        left join trading_platform t5 on t5.id = t4.trading_platform_id
        left join c_project_information t6 ON t6.business_number = t3.business_no
        left join c_supplier_info t7 ON t7.id = t6.c_supplier_info_id
        left join c_enterprise t8 on t8.id = t7.c_enterprise_id
        left join c_project_core_enterprise t9 on t9.c_project_information_id = t6.id
        left join c_enterprise t10 on t10.id = t9.c_enterprise_id
        left join c_sys_user t11 on t11.id = t7.sys_user_id
        left join t_dictionary t12 on t12.code = t1.exemption_status
        <where>
            <if test="null != userIds and userIds.size()>0">
                AND t11.project_manager IN
                <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="status != null and status !='' ">
                AND t1.exemption_status=#{status}
            </if>
            <if test="fastSearch !=null and fastSearch !=''">
                AND (
                t3.contract_no LIKE CONCAT( '%',#{fastSearch},'%' )
                OR t6.business_number LIKE CONCAT( '%',#{fastSearch},'%' )
                OR t8.corporate_name LIKE CONCAT( '%',#{fastSearch},'%' )
                OR t10.corporate_name LIKE CONCAT( '%',#{fastSearch},'%' )
                )
            </if>
            <if test="searchNumber !=null and searchNumber !=''">
                AND (
                t3.contract_no LIKE CONCAT( '%',#{searchNumber},'%' )
                OR t6.business_number LIKE CONCAT( '%',#{searchNumber},'%' )
                OR t2.payment_no LIKE CONCAT( '%',#{searchNumber},'%' )
                )
            </if>
        </where>
        ORDER BY
        t1.gmt_create DESC
    </select>
    <select id="getByPayBasicId"
            resultType="com.sunxung.factoring.entity.loanmanagement.ExemptionMatters">
        select *
        from exemption_matters
        where payment_apply_basic_id = #{payBasicId}
    </select>
    <select id="getByCreditSubject"
            resultType="com.sunxung.factoring.entity.loanmanagement.ExemptionMatters">
        SELECT t1.*
        FROM exemption_matters t1
                 LEFT JOIN payment_apply_basic t2 ON t2.id = t1.payment_apply_basic_id
                 LEFT JOIN payment_info t3 ON t3.id = t2.payment_info_id
                 LEFT JOIN review_quota_disburse t4 ON t4.id = t3.quota_disburse_id
        WHERE t4.social_credit_code = #{socialCreditCode}
          AND t1.statusId = 0;
    </select>
    <select id="getExemptionMattersById"
            resultType="com.sunxung.factoring.entity.loanmanagement.ExemptionMatters">

        SELECT DISTINCT
            t1.*,
            t3.business_no business_number,
            t3.contract_no,
            t3.quota_disburse_batch,
            t2.payment_no,
            t6.project_name,
            t8.corporate_name  supplier_name, -- 主企业名称
            t10.corporate_name customer_name, -- 核心企业名称
            t5.platform_name trading_platform_name
        FROM
            exemption_matters t1
                left join payment_apply t2 on t2.id = t1.payment_apply_id
                left join payment_info t3 on t3.id = t2.payment_info_id
                left join c_financing_basic_info t4 on t4.business_number = t3.business_no
                left join trading_platform t5 on t5.id = t4.trading_platform_id
                left join c_project_information t6 ON t6.business_number = t3.business_no
                left join c_supplier_info t7 ON t7.id = t6.c_supplier_info_id
                left join c_enterprise t8 on t8.id = t7.c_enterprise_id
                left join c_project_core_enterprise t9 on t9.c_project_information_id = t6.id
                left join c_enterprise t10 on t10.id = t9.c_enterprise_id
        where t1.id = #{id}
    </select>
    <select id="findAllByDate"
            resultType="com.sunxung.factoring.entity.loanmanagement.ExemptionMatters">
        SELECT DISTINCT
        t1.*,t6.manager_id
        FROM
        exemption_matters t1
        left join payment_apply_basic t2 on t2.id = t1.payment_apply_basic_id
        left join payment_info t3 on t3.id = t2.payment_info_id
        left join review_master_contract t4 on t4.review_quota_disburse_id = t3.quota_disburse_id
        left join review_master_contract_debtor t5 on t5.review_master_contract_id = t4.id
        LEFT JOIN ent_customer_marketing_info t6 on t6.business_no = t3.business_no
        <where>
            <if test="date != null">
                and DATE_FORMAT(t1.gmt_create ,'%Y-%m-%d' ) = DATE_FORMAT(#{date},'%Y-%m-%d')
            </if>
        </where>
        ORDER BY
        t1.gmt_create DESC
    </select>
</mapper>