<?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.inkasso.factoring.purchManagement.dao.CPurchContractManagementMapper">

    <resultMap id="ListResultMap" type="com.inkasso.factoring.purchManagement.entity.vo.PurchContractCompletedVo">
        <id column="id" property="purchContractId"/>
        <result column="business_number" property="businessNumber"/>
        <result column="project_name" property="projectName"/>
        <result column="contract_number" property="purchContractNumber"/>
        <result column="contract_name" property="purchContractName"/>
        <result column="main_enterprise_id" property="mainEnterpriseId"/>
        <result column="c_project_core_enterprise_id" property="coreEnterpriseId"/>
        <result column="platform_id" property="platformId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="c_purch_contract_basic_info_id" property="cPurchContractBasicInfoId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , business_number, c_supplier_info_id, contract_status, is_change, user_create, user_modified, gmt_create, gmt_modified
    </sql>
    <select id="getCompletedPurchContractByBusinessNumber" resultMap="ListResultMap">
        SELECT DISTINCT c.business_number,
                        c1.id c_purch_contract_basic_info_id,
                        c1.contract_name,
                        c1.contract_number,
                        t2.project_name,
                        t5.id                  main_enterprise_id,
                        t6.id                  c_project_core_enterprise_id,
                        t1.trading_platform_id platform_id,
                        t4.id                  supplier_id
        FROM c_purch_contract_management c
                 LEFT JOIN c_purch_contract_basic_info c1 ON c1.c_purch_contract_management_id = c.id
                 LEFT JOIN c_sales_contract_management t1 ON t1.business_number = c.business_number
                 LEFT JOIN c_project_information t2 ON t2.business_number = c.business_number
                 LEFT JOIN c_project_core_enterprise t3 ON t3.c_project_information_id = t2.id
                 LEFT JOIN c_supplier_info t4 ON t4.id = t2.c_supplier_info_id
                 LEFT JOIN c_enterprise t5 ON t5.id = t4.c_enterprise_id
                 LEFT JOIN c_enterprise t6 ON t6.id = t3.c_enterprise_id
        where c.contract_status = 'project_init_completed'
          AND c.business_number = #{businessNumber}
          AND t4.sys_user_id = #{userId}
    </select>
    <select id="findContractInfo"
            resultType="com.inkasso.factoring.purchManagement.entity.vo.CPushContractInfoVo">
        select a.business_number
        ,a.c_supplier_info_id,a.contract_status,a.is_change,b.contract_number,b.contract_name,b.supplier_name,
        b.buyer_name,b.repurch_method,b.contract_plan_amount_type,b.contract_plan_amount,b.contract_id,b1.name
        operatorSupplier,
        d.id projectId,d.project_name,d.c_enterprise_id,e.corporate_name,
        <!--        e1.supplier_name,-->
        f.trading_platform_id,g.platform_name,h.social_credit_code,
        h.address,h.phone_number ,h.bank_account accountBankName,h.account accountNumber
        from
        c_purch_contract_management a left join c_purch_contract_basic_info b on a.id = b.c_purch_contract_management_id
        left join c_purch_contract_buyer_seller b1 on b1.c_purch_contract_basic_info_id = b.id and b1.buyer_or_seller =
        '2'
        left join t_dictionary c on c.code = a.contract_status
        left join c_project_information d on a.business_number = d.business_number
        left join c_enterprise e on e.id = d.c_enterprise_id
        <!--        left join (-->
        <!--           select  b.corporate_name supplier_name-->
        <!--           from  c_supplier_info a left join c_enterprise b on a.c_enterprise_id = b.is ) e1 on e.id = a.c_supplier_info_id-->
        left join c_acceptance_basic_info f on f.c_project_core_enterprise_id = e.id
        left join trading_platform g on f.trading_platform_id = g.id
        left join trading_platform_invoice_info h on h.trading_platform_id = g.id
        where
        c.code = 'project_init_completed'
    </select>
    <select id="findBySearch"
            resultType="com.inkasso.factoring.purchManagement.entity.CPurchContractBasicInfo">
        SELECT
        c.contract_number,
        c.contract_name,
        c.supplier_name,
        c.project_name,
        c.buyer_name,
        t1.business_number,
        t1.contract_status status,
        t2.platform_name,
        t8.task_name stage,
        t8.historic_task_id task_id,
        t1.id
        FROM
        c_purch_contract_basic_info c
        LEFT JOIN c_purch_contract_management t1 ON c.c_purch_contract_management_id = t1.id
        LEFT JOIN trading_platform t2 ON t2.id = c.trading_platform_id
        left join c_project_information t3 on t3.business_number = t1.business_number
        left join c_supplier_info t4 on t4.id = t1.c_supplier_info_id
        LEFT JOIN (
        SELECT
        m.*
        FROM
        (
        SELECT
        g.id,
        g.task_name,
        g.busness_key,
        g.business_type,
        g.historic_task_id
        FROM
        business_processing_log g
        WHERE
        g.business_type like "%PurchContract%"
        AND g.end_time is NULL
        ORDER BY
        id DESC
        LIMIT 999999
        ) m
        GROUP BY
        m.busness_key
        ) t8 ON t8.busness_key = c.id
        <where>
            <if test="businessNumber != null and businessNumber !=''  ">
                AND ( t1.business_number LIKE CONCAT('%', #{businessNumber},'%') OR c.contract_number LIKE CONCAT('%',
                #{businessNumber},'%'))
            </if>
            <if test="status != null and status !='' ">
                AND t1.contract_status = #{status}
            </if>
            <if test="fastSearch != null and fastSearch !='' ">
                AND (c.project_name LIKE CONCAT('%',#{fastSearch},'%') or c.supplier_name LIKE
                CONCAT('%',#{fastSearch},'%')
                or c.buyer_name LIKE CONCAT('%',#{fastSearch},'%'))
            </if>
            AND t4.sys_user_id = #{userId}
        </where>
        ORDER BY c.gmt_modified DESC
    </select>

</mapper>
