<?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.CustomerCompanyMapper">

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.customer.CustomerCompanyEntity" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap">
            <id column="id" property="id" />
            <result column="customer_id" property="customerId" />
            <result column="name" property="name" />
            <result column="address" property="address" />
            <result column="scale_dict_id" property="scaleDictId" />
            <result column="website" property="website" />
            <result column="industry" property="industry" />
            <result column="communication_address" property="communicationAddress" />
            <result column="postcode" property="postcode" />
            <result column="fax_number" property="faxNumber" />
            <result column="email" property="email" />
            <result column="telephone" property="telephone" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
                create_time,
                update_time,
                is_delete,
                enabled,
                create_by,
                owner_by,
                update_by,
                sort,
            id, customer_id, name, address, scale_dict_id, website, industry, communication_address, postcode, fax_number, email, telephone
        </sql>
    <select id="errWebList" resultType="com.storlead.sales.pojo.vo.customer.CustomerCompanyListVO">
        SELECT
        id,
        customer_id,
        website,
        website1,
        website2,
        website3,
        customer_name,
        customer_form,
        last_follow_up_time,
        create_time,
        owner_by
        FROM (
        SELECT
        cc.id,
        cc.customer_id,
        cc.website,
        cc.website1,
        cc.website2,
        cc.website3,
        c.customer_name,
        c.customer_form,
        c.last_follow_up_time,
        c.create_time,
        c.owner_by
        FROM (
        SELECT
        com.id,
        com.customer_id,
        com.website,
        com.website1,
        com.website2,
        com.website3
        FROM customer_company AS com
        WHERE com.is_delete = 0

        ) AS cc
        LEFT JOIN customer c
        ON cc.customer_id = c.id
        AND c.is_delete = 0

        ) AS ccall
        WHERE
        (website = #{website} OR
        website1 = #{website} OR
        website2 = #{website} OR
        website3 = #{website})
        <if test="id != null">
            and customer_id != #{id}
        </if>
        ORDER BY customer_id DESC

    </select>
    <select id="errEmailList" resultType="com.storlead.sales.pojo.vo.customer.CustomerCompanyListVO">
        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 cc.id,cc.customer_id,cc.email,cc.email1,cc.email2,cc.email3,c.customer_name ,`name`,c.customer_form,c.last_follow_up_time,cc.type,c.create_time,c.is_delete,c.owner_by
        FROM
        (select id,customer_id,email,email1,email2,email3,'' as name,is_delete,"cu" as type from customer_company where is_delete = 0
        union all
        select id,customer_id,email,email1,email2,email3,name,is_delete,"li" as type from liaison where is_delete = 0)
        as cc
        LEFT JOIN customer c on cc.customer_id = c.id
        ) as ccall
        WHERE
        is_delete = 0
        and
        ( email = #{email}
        OR  email1 = #{email}
        OR  email2 = #{email}
        OR  email3 = #{email} )

        <if test="id != null">
            and customer_id != #{id}
        </if>



        <!--SELECT
        id,
        customer_id,
        email,
        email1,
        email2,
        email3,
        customer_name,
        customer_form,
        last_follow_up_time,
        similarityLikeNew(email,#{email}) as score,
        similarityLikeNew(email1,#{email}) as score1,
        similarityLikeNew(email2,#{email}) as score2,
        similarityLikeNew(email3,#{email}) as score3
        FROM
        (SELECT cc.id,cc.customer_id,cc.email,cc.email1,cc.email2,cc.email3,c.customer_name ,c.customer_form,c.last_follow_up_time
        FROM
            (select id,customer_id,email,email1,email2,email3,is_delete from customer_company
              union all
              select id,customer_id,email,email1,email2,email3,is_delete from liaison)
                                                               as cc
        LEFT JOIN customer c on cc.customer_id = c.id and c.is_delete = 0
        ) as ccall
        WHERE
        (similarityLikeNew(email,#{email}) >= #{similarityValue}
        OR similarityLikeNew(email1,#{email}) >= #{similarityValue}
        OR similarityLikeNew(email2,#{email}) >= #{similarityValue}
        OR similarityLikeNew(email3,#{email}) >= #{similarityValue})

        <if test="id != null">
            and customer_id != #{id}
        </if>

        ORDER BY
        score DESC-->
    </select>

</mapper>
