<?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.acceptanceManagement.dao.CAcceptanceBasicInfoMapper">

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , project_information_id, trading_platform_id, acceptance_number, expected_settlement_date, sign_confirm, specification, check_operator_supplier, check_operator_enterprise, check_operator_supplier_contact, check_operator_enterprise_contact, steel_waiter, acceptance_status, additional_remarks, is_settlement, is_receivables, user_create, user_modified, gmt_create, gmt_modified, signer
    </sql>

    <resultMap id="basicListResult" type="com.inkasso.factoring.acceptanceManagement.entity.vo.AcceptanceListVo">
        <result column="id" property="id"/>
        <result column="acceptance_number" property="acceptanceNumber"/>
        <result column="business_number" property="businessNumber"/>
        <result column="core_enterprise_name" property="coreEnterpriseName"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="platform_name" property="platformName"/>
        <result column="project_name" property="projectName"/>
        <result column="expected_settlement_date" property="expectedSettlementDate"/>
        <result column="stage" property="stage"/>
        <result column="status" property="status"/>
        <result column="taskId" property="taskId"/>
    </resultMap>


    <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>


    <select id="getPurchContractByBusinessNumber" 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.business_number = #{businessNumber}
          AND t4.sys_user_id = #{userId}
    </select>


    <select id="getBySearch"
            resultType="com.inkasso.factoring.acceptanceManagement.entity.vo.AcceptanceListVo"
            resultMap="basicListResult">
        SELECT
        c.id,
        c.acceptance_number,
        c.business_number,
        c.expected_settlement_date,
        c.acceptance_status,
        t2.corporate_name supplier_name, -- 主企业
        t3.corporate_name core_enterprise_name, -- 核心企业
        t4.platform_name platform_name,
        t1.project_name,
        t5.name status,
        t7.task_name stage,
        t7.historic_task_id  taskId
        FROM
        c_acceptance_basic_info c
        LEFT JOIN c_project_information t1 ON t1.business_number = c.business_number
        LEFT JOIN c_enterprise t2 ON t2.id = c.c_supplier_info_id
        LEFT JOIN c_enterprise t3 ON t3.id = c.c_project_core_enterprise_id
        LEFT JOIN trading_platform t4 ON t4.id = c.trading_platform_id
        LEFT JOIN t_dictionary t5 ON t5.code = c.acceptance_status
        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 "AcceptanceSheet%" 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="status != null and status != ''">
                c.acceptance_status = #{status}
            </if>
            <if test="businessNumber != null and businessNumber != ''">
                AND (c.acceptance_number = #{businessNumber} OR c.business_number = #{businessNumber})
            </if>
            <if test="fastSearch != null and fastSearch != ''">
                AND (t2.corporate_name = #{fastSearch} OR t3.corporate_name = #{fastSearch} OR t1.project_name =
                #{fastSearch})
            </if>
            AND c.sys_user_id = #{customerId}
        </where>
        ORDER BY c.gmt_create DESC

    </select>
    <select id="findByCondition" parameterType="com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceBasicInfo" resultType="com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceBasicInfo">
        SELECT
               t1.acceptance_goods_number acceptance_goods_number,
               t2.id,
               t2.business_number,
               t2.c_supplier_info_id,
               t2.c_project_core_enterprise_id,
               t2.trading_platform_id,
               t2.expected_settlement_date,
               t2.acceptance_status,
               t2.is_receivables receivables,
               t2.is_settlement settlement,
               t2.acceptance_number
        FROM c_acceptance_goods_info t1
            LEFT JOIN c_acceptance_basic_info t2 on t2.id = t1.c_acceptance_basic_info_id
        <where>
            <if test="businessNumber != null and businessNumber != ''">
                AND t2.business_number = #{businessNumber}
            </if>
            <if test="cSupplierInfoId != null">
                AND t2.c_supplier_info_id = #{cSupplierInfoId}
            </if>
            <if test="acceptanceStatus != null and acceptanceStatus != ''">
                AND t2.acceptance_status = #{acceptanceStatus}
            </if>
            <if test="receivables != null">
                AND t2.is_receivables = #{receivables}
            </if>
        </where>
        GROUP BY t1.acceptance_goods_number
    </select>

</mapper>
