<?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.storlead.sales.mapper.customer.LiaisonMapper">

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.customer.LiaisonEntity" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap">
            <id column="id" property="id" />
            <result column="customer_id" property="customerId" />
            <result column="has_primary_lialison" property="hasPrimaryLialison" />
            <result column="name" property="name" />
            <result column="call" property="call" />
            <result column="liaison_role_dict_value" property="liaisonRoleDictValue" />
            <result column="birthday" property="birthday" />
            <result column="department_position" property="departmentPosition" />
            <result column="telephone" property="telephone" />
            <result column="email" property="email" />
            <result column="fax_number" property="faxNumber" />
            <result column="postcode" property="postcode" />
            <result column="contact_information" property="contactInformation" />
            <result column="remark" property="remark" />
            <result column="qq" property="qq" />
            <result column="skype" property="skype" />
            <result column="wangwang" property="wangwang" />
            <result column="wechat" property="wechat" />
            <result column="twitter" property="twitter" />
            <result column="linkedin" property="linkedin" />
            <result column="facebook" property="facebook" />
            <result column="whatsapp" property="whatsapp" />
            <result column="international_area" property="internationalArea" />
            <result column="address" property="address" />
            <result column="resource_type" property="resourceType" />
            <result column="resource_type_id" property="resourceTypeId" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
                create_time,
                update_time,
                is_delete,
                enabled,
                create_by,
                owner_by,
                update_by,
                sort,
            id, customer_id, has_primary_lialison, name, call, liaison_role_dict_id, birthday, department_position
                    , telephone, email, niche_or_orders, fax_number, postcode, contact_information, remarks, qq
                    , skype, wangwang, wechat, twitter, linkedin, facebook, whatsapp, address,international_area,resource_type,resource_type_id
        </sql>

    <select id="list" resultType="com.storlead.sales.pojo.entity.customer.LiaisonEntity">
        select *
        from (
                 select c.customer_name as customerName,
                        c.country,
                        li.customer_id  as customerId,
                        li.email,
                        li.email1,
                        li.email2,
                        li.email3,
                        li.owner_by,
                        c.collaborator,
                        li.`name`,
                        c.is_delete
                 from liaison as li
                          left join customer as c on li.customer_id = c.id where c.is_delete = 0 and li.is_delete = 0 and li.customer_id is not null

                 union all

                 select c.customer_name as customerName,
                        c.country,
                        c.id as customerId,
                        cc.email,
                        cc.email1,
                        cc.email2,
                        cc.email3,
                        c.owner_by,
                        '' as collaborator,
                        '' as `name`,
                        c.is_delete
                 from customer as c
                          left join customer_company as cc
                                    on c.id = cc.customer_id where c.is_delete = 0

                 union all
                 select
                     '' as customer_name,
                     '' AS country,
                     null as customer_id,
                     li.email,
                     li.email1,
                     li.email2,
                     li.email3,
                     li.owner_by,
                     '' as collaborator,
                     li.`name`,
                     li.is_delete
                 from liaison as li where li.is_delete = 0 AND customer_id is null and li.is_delete = 0
             ) as mt
            ${ew.customSqlSegment}
    </select>
    <select id="errList" resultType="com.storlead.sales.pojo.vo.customer.LiaisonListVO">

        SELECT
        id,
        customer_id,
        email,
        email1,
        email2,
        email3,
        customer_name,
        `name`,
        customer_form,
        last_follow_up_time,
        owner_by,
        create_time,
        CASE
        WHEN type = 'cu' AND email = #{email} THEN '客户邮箱重复'
        WHEN type = 'cu' AND email1 = #{email} THEN '客户备用邮箱一重复'
        WHEN type = 'cu' AND email2 = #{email} THEN '客户备用邮箱二重复'
        WHEN type = 'cu' AND email3 = #{email} THEN '客户备用邮箱三重复'
        WHEN type = 'li' AND email = #{email} THEN '联系人邮箱重复'
        WHEN type = 'li' AND email1 = #{email} THEN '联系人备用邮箱一重复'
        WHEN type = 'li' AND email2 = #{email} THEN '联系人备用邮箱二重复'
        WHEN type = 'li' AND email3 = #{email} THEN '联系人备用邮箱三重复'
        ELSE ''
        END AS reason
        FROM (
        SELECT
        l.id,
        l.customer_id,
        l.email,
        l.email1,
        l.email2,
        l.email3,
        l.`name`,
        c.customer_name,
        c.customer_form,
        c.last_follow_up_time,
        l.type,
        c.is_delete,
        l.create_time,
        c.owner_by
        FROM (
        SELECT
        id,
        customer_id,
        '' as `name`,
        email,
        email1,
        email2,
        email3,
        create_time,"cu" as type
        FROM customer_company
        WHERE is_delete = 0
        UNION ALL
        SELECT
        id,
        customer_id,
        `name`,
        email,
        email1,
        email2,
        email3,
        create_time,"li" as type
        FROM liaison
        WHERE is_delete = 0
        ) AS l
        LEFT JOIN customer c
        ON l.customer_id = c.id
        AND c.is_delete = 0
        ) AS ccall
        WHERE
            ccall.is_delete = 0
            and
        (#{email} = email
        OR #{email} = email1
        OR #{email} = email2
        OR #{email} = email3)
        <if test="id != null">
            AND customer_id != #{id}
        </if>

        <!--SELECT
        id,
        customer_id,
        email,
        customer_name,
        customer_form,
        last_follow_up_time,
        score,
        score1,
        score2,
        score3
        FROM (
        SELECT
        l.id,
        l.customer_id,
        l.email,
        c.customer_name,
        c.customer_form,
        c.last_follow_up_time,
        similarityLikeNew(l.email, #{email}) AS score,
        similarityLikeNew(l.email1, #{email}) AS score1,
        similarityLikeNew(l.email2, #{email}) AS score2,
        similarityLikeNew(l.email3, #{email}) AS score3
        FROM (
        SELECT
        id,
        customer_id,
        email,
        email1,
        email2,
        email3
        FROM customer_company
        WHERE is_delete = 0
        UNION ALL
        SELECT
        id,
        customer_id,
        email,
        email1,
        email2,
        email3
        FROM liaison
        WHERE is_delete = 0
        ) AS l
        LEFT JOIN customer c
        ON l.customer_id = c.id
        AND c.is_delete = 0
        ) AS ccall
        WHERE
        (score >= #{similarityValue} OR
        score1 >= #{similarityValue} OR
        score2 >= #{similarityValue} OR
        score3 >= #{similarityValue})
        <if test="id != null">
            AND customer_id != #{id}
        </if>
        ORDER BY score DESC-->
    </select>

</mapper>
