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

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.customer.CustomerFollowUpEntity">
                    <id column="id" property="id" />
                <result column="create_time" property="createTime" />
                <result column="update_time" property="updateTime" />
                <result column="is_delete" property="isDelete" />
                <result column="enabled" property="enabled" />
                <result column="create_by" property="createBy" />
                <result column="owner_by" property="ownerBy" />
                <result column="update_by" property="updateBy" />
                <result column="sort" property="sort" />
                    <result column="follow_up_type_value" property="followUpTypeValue" />
                    <result column="content" property="content" />
                    <result column="customer_id" property="customerId" />
                    <result column="liaison_id" property="liaisonId" />
                    <result column="remind_user_ids" property="remindUserIds" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
                create_time,
                update_time,
                is_delete,
                enabled,
                create_by,
                owner_by,
                update_by,
                sort,
            id, follow_up_type_value, content, customer_id, liaison_id, remind_user_ids
        </sql>
    <select id="getFollowUpLastVOList" resultType="com.storlead.sales.pojo.vo.customer.CustomerFollowUpLastVO">
        SELECT
        cf.resource_type_id,
        cf.id AS id,
        cf.follow_up_time AS follow_up_time,
        <!-- DATEDIFF(CURDATE(), cf.follow_up_time) AS not_follow_up_date -->
        IF(DATEDIFF(CURDATE(), cf.follow_up_time) &lt; 0 , 0, DATEDIFF(CURDATE(), cf.follow_up_time)) AS not_follow_up_date
        <!-- IF(DATEDIFF(CURDATE(), STR_TO_DATE(cf.follow_up_time,  '%Y - %M - %D')) &lt; 0, 0, DATEDIFF(CURDATE(), STR_TO_DATE(cf.follow_up_time,  '%Y - %M - %D'))) AS not_follow_up_date -->
        FROM
        customer_follow_up cf
        INNER JOIN (
        SELECT
        resource_type_id,
        id,
        MAX(follow_up_time) AS follow_up_time
        FROM
        customer_follow_up
        WHERE
        resource_type_id IN
        <foreach collection="resourceTypeIdList" index="index" item="id" separator="," close=")" open="(">
            #{id}
        </foreach>
          AND resource_type = #{type} and is_delete = 0
        GROUP BY
        resource_type_id
        ORDER BY follow_up_time desc
        ) AS last_follow_ups
        ON cf.id =  last_follow_ups.id
            <!-- cf.resource_type_id = last_follow_ups.resource_type_id AND cf.follow_up_time = last_follow_ups.follow_up_time; -->

    </select>


    <select id="getBusinessFollowUpLastVOList" resultType="com.storlead.sales.pojo.vo.customer.CustomerFollowUpLastVO">
        select lastcf.id as id,cf.id as resource_type_id, IFNULL(lastcf.follow_up_time,cf.create_time) AS follow_up_time,
        IF(DATEDIFF(CURDATE(), IFNULL(lastcf.follow_up_time,cf.create_time)) &lt; 0 , 0, DATEDIFF(CURDATE(),IFNULL(lastcf.follow_up_time,cf.create_time))) AS not_follow_up_date
        from business as cf
        left join
        (
        SELECT
        resource_type_id,
        id,
        MAX(follow_up_time) AS follow_up_time
        FROM
        customer_follow_up
        WHERE resource_type_id IN
        <foreach collection="resourceTypeIdList" index="index" item="id" separator="," close=")" open="(">
            #{id}
        </foreach>
        AND resource_type = #{type} and is_delete = 0
        GROUP BY
        resource_type_id
        ORDER BY follow_up_time desc
        ) as lastcf
        on  cf.id =  lastcf.id
        where cf.id in
        <foreach collection="resourceTypeIdList" index="index" item="id" separator="," close=")" open="(">
          #{id}
        </foreach>
    </select>

    <select id="selectUserClickTitle" resultType="com.storlead.mail.pojo.vo.UserClickTitleVO">
        SELECT
            dataId,
            title,
            clickCount
        FROM
            (
                SELECT
                    a.id AS dataId,
                    a.SUBJECT AS title,
                    b.click_count AS clickCount,
                    ROW_NUMBER ( ) OVER ( PARTITION BY a.SUBJECT ORDER BY b.click_count DESC ) AS rn
                FROM
                    customer_follow_up AS a
                        LEFT JOIN user_click_record AS b ON a.id = b.data_id
                        AND b.click_type = 2
                WHERE
                    a.owner_by = #{ownerBy}
                    <if test="title != null and title != ''">
                         AND a.`subject` LIKE CONCAT('%',#{title},'%')
                    </if>
                  AND a.is_delete = 0
            ) AS t
        WHERE
            t.rn = 1
        ORDER BY
            clickCount DESC
            LIMIT 10;
    </select>

</mapper>
