<?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.project.ProjectInformationMapper">


    <select id="findById" resultType="com.sunxung.factoring.entity.project.ProjectInformation">
        select
            id,
            c_supplier_info_id,
            business_number,
            project_type,
            project_name,
            c_enterprise_id,
            c_enterprise_history_id,
            company_for_project,
            project_address,
            project_cycle,
            project_nature,
            project_nature_detail,
            project_nature_detail_des,
            goods_category,
            goods_category_detail,
            goods_category_detail_des,
            purpose,
            purpose_detail,
            financial_funds,
            bank_loans,
            foreign_capital,
            raise_independently,
            special_fund,
            other_funds,
            `unknown`,
            bidding,
            cooperation_amount,
            peak_value,
            estimated_accounting_period,
            high_peak_usage,
            status statusStr,
            gmt_modified
        from c_project_information where id = #{value}
    </select>

    <select id="getByBusinessNo" resultType="com.sunxung.factoring.entity.project.vo.ProjectInformationVo">
        SELECT c.id AS project_information_id,
               c.project_name,
               c.business_number,
               t2.corporate_name AS main_enterprise, -- 主企业
               t4.corporate_name AS enterprise -- 核心企业
        FROM c_project_information c
                 LEFT JOIN c_supplier_info t1 ON t1.id = c.c_supplier_info_id
                 LEFT JOIN c_enterprise t2 ON t2.id = t1.c_enterprise_id
                 LEFT JOIN c_project_core_enterprise t3 ON t3.c_project_information_id = c.id
                 LEFT JOIN c_enterprise t4 ON t4.id = t3.c_enterprise_id
        WHERE c.business_number = #{value}
    </select>
    <select id="findBySearch" resultType="com.sunxung.factoring.entity.project.ProjectInformation">
        select
            t1.id ,
            t1.business_number,
            t1.project_name,
            t2.corporate_name ownerName,
            t4.corporate_name customerName,
            t7.name projectStatusStr,
            t8.name coreStatusStr,
            t9.corporate_name supplierName,
            t1.gmt_modified
        from c_project_information t1
            left join c_enterprise t2 on t1.c_enterprise_id = t2.id
            left join c_project_core_enterprise t3 on t3.c_project_information_id = t1.id
            left join c_enterprise t4 on t3.c_enterprise_id =t4.id
            left join c_supplier_info t5 on t1.c_supplier_info_id = t5.id
            left join c_sys_user t6 on t6.id = t5.sys_user_id
            left join t_dictionary t7 on t7.code = t1.status
            left join t_dictionary t8 on t8.code = t3.status
            left join c_enterprise t9 on t9.id = t5.c_enterprise_id
        where t1.c_enterprise_id is not null
            <if test="fastSearch != null and fastSearch !=''">
                AND (t1.project_name like concat('%',#{fastSearch},'%') OR t4.corporate_name like concat('%',#{fastSearch},'%'))
            </if>
            <if test="status != null and status !=''">
                AND t1.status = #{status}
            </if>
            <if test="userIds != null and userIds.size()>0">
                AND t6.risk_manager in
                <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            order by t1.gmt_modified
    </select>
</mapper>
