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

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.customer.CustomerEntity" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap">
            <result column="id" property="id" />
            <result column="customer_form" property="customerForm" />
            <result column="has_from_clue" property="hasFromClue" />
            <result column="has_public_customer" property="hasPublicCustomer" />
            <result column="customer_code" property="customerCode" />
            <result column="continent" property="continent" />
            <result column="country" property="country" />
            <result column="customer_name" property="customerName" />
            <result column="intended_products_ids" property="intendedProductsIds" />
            <result column="intended_products_names" property="intendedProductsNames" />
            <result column="customer_status_dict_value" property="customerStatusDictValue" />
            <result column="customer_source_dict_value" property="customerSourceDictValue" />
            <result column="customer_type_dict_value" property="customerTypeDictValue" />
            <result column="customer_level_dict_value" property="customerLevelDictValue" />
            <result column="customer_credit_level_dict_value" property="customerCreditLevelDictValue" />
            <result column="customer_economic_nature_dict_value" property="customerEconomicNatureDictValue" />
            <result column="mnemonic_name" property="mnemonicName" />
            <result column="remark" property="remark" />
            <result column="opening_bank" property="openingBank" />
            <result column="tax_number" property="taxNumber" />
            <result column="bank_account" property="bankAccount" />
            <result column="owner_time" property="ownerTime" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
                create_time,
                update_time,
                is_delete,
                enabled,
                create_by,
                owner_by,
                update_by,
                sort,
            id, customer_form, has_from_clue, has_public_customer, customer_code, continent, country, owner_name, customer_name, intended_products_ids, intended_products_names, customer_status_dict_id, customer_source_dict_id, customer_type_dict_id, customer_level_dict_id, customer_credit_level_dict_id, customer_economic_nature_dict_id, mnemonic_name, remarks, opening_bank, tax_number, bank_account
        </sql>
    <update id="updateCustomerNoFollowUpDate">
        UPDATE customer
        SET no_follow_up_date =
        CASE
        WHEN last_follow_up_time IS NULL THEN 0
        WHEN DATEDIFF(CURRENT_DATE(), last_follow_up_time) &lt; 0 THEN 0
        ELSE DATEDIFF(CURRENT_DATE(), last_follow_up_time)
        END
        <if test="idList != null and idList.size() > 0">
            WHERE id IN
            <foreach item="id" collection="idList" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </update>

    <select id="selectListVO" resultType="com.storlead.sales.pojo.vo.customer.CustomerListVO">
        select * from customer
    </select>
    <select id="errList" resultType="com.storlead.sales.pojo.vo.customer.CustomerCompanyListVO">
        SELECT
            id,
            id as customer_id,
            data_code,
            customer_name,
            last_follow_up_time,
            owner_by,
        create_time,
            1 as score
        <!-- similarityLikeNew(customer_name,"${customerName}") as score -->
        FROM
            customer
        WHERE
            <!-- similarityLikeNew(customer_name,"${customerName}") >= #{similarityValue} -->
            customer_name = #{customerName}
            and is_delete = 0
            and customer_form = #{customerForm}
            <if test="id != null">
                and id != #{id}
            </if>
        ORDER BY score DESC

    </select>

    <update id="replaceCollaborator">
        UPDATE customer
        SET collaborator = TRIM(BOTH ',' FROM REPLACE(
                CONCAT(',', IFNULL(collaborator, ''), ','),
                CONCAT(',', #{customer.oldOwnerBy}, ','),
                CONCAT(',', #{customer.newOwnerBy}, ',')
            ))
        WHERE FIND_IN_SET(#{customer.oldOwnerBy}, IFNULL(collaborator, '')) > 0
    </update>

</mapper>
