<?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.salesManagement.dao.CSalesContractManagementMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.inkasso.factoring.salesManagement.entity.CSalesContractManagement">
        <id column="id" property="id"/>
        <result column="business_number" property="businessNumber"/>
        <result column="trading_platform_id" property="tradingPlatformId"/>
        <result column="contract_status" property="contractStatus"/>
        <result column="is_change" property="change"/>
        <result column="user_create" property="userCreate"/>
        <result column="user_modified" property="userModified"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
    </resultMap>

    <resultMap id="ListResultMap" type="com.inkasso.factoring.salesManagement.entity.vo.SalesContractListVo">
        <id column="id" property="id"/>
        <result column="business_number" property="businessNumber"/>
        <result column="project_name" property="projectName"/>
        <result column="contract_number" property="contractNumber"/>
        <result column="enterprise" property="enterprise"/>
        <result column="main_enterprise" property="mainEnterprise"/>
        <result column="platform_name" property="platformName"/>
        <result column="contract_status" property="status"/>
        <result column="task_name" property="stage"/>
        <result column="historic_task_id" property="taskId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , c_project_information_id, trading_platform_id, contract_status, stage, is_change, user_create, user_modified, gmt_create, gmt_modified
    </sql>
    <select id="getBasicInfoById"
            resultType="com.inkasso.factoring.salesManagement.entity.vo.SalesContractBasicVo">
        select c.id,
               t1.project_name,
               t1.business_number,
               t3.corporate_name AS main_enterprise, -- 主企业
               t5.corporate_name AS enterprise,      -- 核心企业
               t6.platform_name,
               t1.id             AS project_information_id,
               t6.id             AS platform_id
        from c_sales_contract_management c
                 left join c_project_information t1 on t1.business_number = c.business_number
                 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 t6 on t6.id = c.trading_platform_id
        where c.id = #{value}
    </select>
    <select id="getCreditProject"
            resultType="com.inkasso.factoring.salesManagement.entity.vo.SalesContractBasicVo">
        select distinct t1.id             AS project_Information_id,
                        t1.id,
                        t7.id             AS c_bidding_basic_info_id,
                        t1.project_name,
                        t1.business_number,
                        t3.corporate_name AS main_enterprise, -- 主企业
                        t5.corporate_name AS enterprise       -- 核心企业
        from c_project_information t1
                 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 c_bidding_basic_info t7 on t7.c_project_information_id = t1.id
        where t2.sys_user_id = #{value}
          and ((t1.bidding = 0 and t1.status = 'project_init_completed') -- 招投标否且授信通过
            or (t7.status = 'project_init_completed'))
          and t1.sales = 0
        order by t1.gmt_modified desc
    </select>

    <select id="findBySearch" resultType="com.inkasso.factoring.salesManagement.entity.vo.SalesContractListVo"
            resultMap="ListResultMap">
        select c.id,
        t1.project_name,
        t1.business_number,
        t3.corporate_name AS main_enterprise, -- 主企业
        t5.corporate_name AS enterprise, -- 核心企业
        t6.platform_name,
        t7.contract_number,
        t8.task_name,
        t8.historic_task_id,
        c.contract_status
        from c_sales_contract_management c
        left join c_project_information t1 on t1.business_number = c.business_number
        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 t6 on t6.id = c.trading_platform_id
        left join c_sales_contract_basic_info t7 on t7.c_sales_contract_management_id = c.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 "%SalesContract%" 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 = #{businessNumber} OR t7.contract_number = #{businessNumber})
            </if>
            <if test="status != null and status !='' ">
                AND c.contract_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="getCompletedSales"
            resultType="com.inkasso.factoring.salesManagement.entity.vo.SalesContractBasicVo">
        SELECT c.id,
               t1.project_name,
               t1.business_number,
               t3.corporate_name AS main_enterprise, -- 主企业
               t5.corporate_name AS enterprise,      -- 核心企业
               t6.platform_name,
               t2.id             AS supplier_id,
               t6.id             AS platform_id,
               t1.id             AS project_information_id
        FROM c_sales_contract_management c
                 LEFT JOIN c_project_information t1 ON c.business_number = t1.business_number
                 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 t6 ON t6.id = c.trading_platform_id

        WHERE t2.sys_user_id = #{value}
          AND c.contract_status = 'project_init_completed'
          AND t1.sales = 1
          AND c.purch = 0
        order by c.gmt_create desc
    </select>
    <select id="getCompletedSalesByBusinessNumber"
            resultType="com.inkasso.factoring.salesManagement.entity.vo.SalesContractBasicVo">
        SELECT c.id,
               t1.project_name,
               t1.business_number,
               t3.corporate_name AS main_enterprise, -- 主企业
               t5.corporate_name AS enterprise,      -- 核心企业
               t6.platform_name,
               t1.id             AS project_information_id,
               t2.id             AS supplier_id,
               t6.id             AS platform_id
        FROM c_sales_contract_management c
                 LEFT JOIN c_project_information t1 ON c.business_number = t1.business_number
                 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 t6 ON t6.id = c.trading_platform_id
        WHERE t1.business_number = #{value}
    </select>

</mapper>
