<?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.customerinformation.CustomerMapper">

    <insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="Customer">
       insert into enterprise_customer_info
       (
           enterprise_name,socialCreditCode,auth_status,supplier,user_create,synchronize_time
       )
       values
       (
           #{enterpriseName},#{socialCreditCode},#{authStatus},#{supplier},#{userCreate},#{synchronizeTime}
       )
    </insert>

    <!--获取业务经理创建的所有业务下关联的供应商-->
    <select id="getLocalUserSuppliers" resultType="com.sunxung.factoring.entity.customerinformation.Customer" parameterType="java.lang.Long">
        SELECT DISTINCT t1.*
        FROM
            enterprise_customer_info t1
                LEFT JOIN business_supplier_info t2 ON t1.id = t2.supplier_id
                LEFT JOIN business_basic_info t3 ON t3.id = t2.business_basic_info_id
        WHERE
            t3.manager_id =  #{userId}
    </select>

    <!--经理创建的业务中关联的某一个供应商的业务中的核心企业名称-->
    <select id="getLocalUserCustomers" resultType="com.sunxung.factoring.entity.customerinformation.Customer" parameterType="java.lang.Long">
        SELECT DISTINCT t1.*
        FROM
            enterprise_customer_info t1
                LEFT JOIN business_basic_info t2 ON t1.id = t2.enterprise_customer_info_id
                LEFT JOIN business_supplier_info t3 ON t2.id = t3.business_basic_info_id
        where
            t2.manager_id = #{userId} and t3.supplier_id = #{supplierId}
    </select>

    <!--供应商相关的的业务核心企业名称-->
    <select id="getLocalSupplierCustomers"
            resultType="com.sunxung.factoring.entity.customerinformation.Customer" parameterType="java.lang.Long">
        SELECT DISTINCT t1.*
        FROM
            enterprise_customer_info t1
                LEFT JOIN business_basic_info t2 ON t1.id = t2.enterprise_customer_info_id
                LEFT JOIN business_supplier_info t3 ON t2.id = t3.business_basic_info_id
        where
            t3.supplier_id = #{supplierId}
    </select>

    <!-- 经理创建的业务中的核心企业名称 -->
    <select id="getCustomersByUser" resultType="com.sunxung.factoring.entity.customerinformation.Customer">
        select DISTINCT t1.*
        from
            enterprise_customer_info t1
                LEFT JOIN business_basic_info t2 ON t1.id = t2.enterprise_customer_info_id
        where t2.manager_id = #{managerId}
    </select>
    <select id="getSuppliersByUser" resultType="com.sunxung.factoring.entity.customerinformation.Customer">
        select DISTINCT t1.*
        from
            enterprise_customer_info t1
                LEFT JOIN purch_contract_basic_info t2 ON t1.enterprise_name = t2.supplier_name
                LEFT JOIN purch_contract_management t3 ON t3.id = t2.purch_contract_management_id
        where t3.business_number = #{businessNumber}
    </select>

</mapper>