<?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.biddingManagement.dao.CBiddingBasicInfoMapper">

    <resultMap id="BiddingBasicInfoResultVoResult"
               type="com.inkasso.factoring.biddingManagement.entity.vo.BiddingBasicInfoResultVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="business_number" property="businessNumber"/>
        <result column="project_name" property="projectName"/>
        <result column="enterprise" property="enterprise"/>
        <result column="main_enterprise" property="mainEnterprise"/>
        <result column="platform_name" property="platform"/>
        <result column="stage" property="stage"/>
        <result column="status" property="status"/>
        <result column="task_id" property="taskId" />
    </resultMap>

    <select id="findBySearch"
            resultType="com.inkasso.factoring.biddingManagement.entity.vo.BiddingBasicInfoResultVo"
            parameterType="com.inkasso.factoring.biddingManagement.entity.SearchBiddingBasic"
            resultMap="BiddingBasicInfoResultVoResult">
        SELECT
        c.id,
        t1.business_number,
        t1.project_name,
        t3.corporate_name AS main_enterprise,
        t5.corporate_name AS enterprise,
        t8.platform_name,
        c.stage,
        c.STATUS ,
        t7.historic_task_id task_id,
        t7.task_name
        FROM c_bidding_basic_info c
        LEFT JOIN c_project_information t1 ON t1.id = c.c_project_information_id
        LEFT JOIN c_supplier_info t2 ON t2.id = t1.c_supplier_info_id
        LEFT JOIN c_enterprise t3 ON t3.id = t2.c_enterprise_id
        LEFT JOIN c_project_core_enterprise t4 ON t4.c_project_information_id = t1.id
        LEFT JOIN c_enterprise t5 ON t5.id = t4.c_enterprise_id
        LEFT JOIN business_processing_log t6 ON t6.business_type = c.id
        LEFT JOIN trading_platform t8 ON t8.id = c.trading_platform_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 = "BiddingBasicInfo" and g.end_time is null order by id desc limit 999999) m
            group BY  m.busness_key )  t7 ON t7.busness_key = c.id
        <where>
            <if test="businessNumber != null and businessNumber !=''  ">
                AND t1.business_number = #{businessNumber }
            </if>
            <if test="status != null and status !='' ">
                AND c.status = #{status}
            </if>
            <if test="fastSearch != null and fastSearch !='' ">
                AND (t1.project_name LIKE CONCAT('%',#{fastSearch},'%') or t3.corporate_name LIKE
                CONCAT('%',#{fastSearch},'%')
                or t5.corporate_name LIKE CONCAT('%',#{fastSearch},'%'))
            </if>
            AND t2.sys_user_id = #{userId}
        </where>
        ORDER BY c.gmt_modified DESC
    </select>
    <select id="getCreditProjectByBiddingId"
            resultType="com.inkasso.factoring.biddingManagement.entity.vo.BiddingBasicInfoResultVo">
        SELECT
            c.id,
            t1.business_number,
            t1.project_name,
            t3.corporate_name,
            t5.corporate_name AS main_enterprise,
            t8.platform_name AS platform_name,
            c.stage,
            c.status
        FROM c_bidding_basic_info c
                 LEFT JOIN c_project_information t1 ON t1.id = c.c_project_information_id
                 LEFT JOIN c_supplier_info t2 ON t2.id = t1.c_supplier_info_id
                 LEFT JOIN c_enterprise t3 ON t3.id = t2.c_enterprise_id
                 LEFT JOIN c_project_core_enterprise t4 ON  t4.c_project_information_id = t1.id
                 LEFT JOIN c_enterprise t5 ON t5.id = t4.c_enterprise_id
                 LEFT JOIN trading_platform t8 ON t8.id = c.trading_platform_id
        WHERE c.id = #{value}
    </select>
    <select id="getBasicInfo"
            resultType="com.inkasso.factoring.creditmanagement.entity.vo.UserBiddingProjectsVo">
        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_bidding_basic_info m ON m.c_project_information_id = c.id
                 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 m.id = #{value}
    </select>
</mapper>
