<?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.gic.haoban.manage.service.dao.mapper.contact.TabContactFollowMapper">

    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.contact.TabContactFollow">
        <id column="follow_id" property="followId" />
        <result column="member_id" property="memberId" />
        <result column="clerk_id" property="clerkId" />
        <result column="clerk_code" property="clerkCode" />
        <result column="follow_remark" property="followRemark" />
        <result column="follow_material" property="followMaterial" />
        <result column="goods_info" property="goodsInfo" />
        <result column="follow_time" property="followTime" />
        <result column="enterprise_id" property="enterpriseId" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        follow_id, member_id, clerk_id, clerk_code, follow_remark, follow_material, goods_info, follow_time, enterprise_id, delete_flag, create_time, update_time
    </sql>
    <sql id="Alias_Base_Column_List">
        t.follow_id, t.member_id, t.clerk_id, t.clerk_code, t.follow_remark, t.follow_material, t.goods_info, t.follow_time, t.enterprise_id, t.delete_flag, t.create_time, t.update_time
    </sql>

    <select id="queryById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tab_contact_follow
        WHERE follow_id = #{followId}
    </select>

    <select id="queryByIds" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tab_contact_follow
        WHERE follow_id IN
        <foreach item="item" index="index" collection="followIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="count" resultType="long">
        SELECT COUNT(*)
        FROM tab_contact_follow
        <where>
            <if test="memberId != null">
                AND member_id = #{memberId}
            </if>
            <if test="clerkId != null">
                AND clerk_id = #{clerkId}
            </if>
            <if test="enterpriseId != null">
                AND enterprise_id = #{enterpriseId}
            </if>
        </where>
    </select>

    <select id="pageFollow" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tab_contact_follow
        <where>
            enterprise_id = #{enterpriseId} and delete_flag = 0
            <if test="search.clerkId != null and search.clerkId != ''">
                AND clerk_id = #{search.clerkId}
            </if>
            <if test="search.memberId != null and search.memberId != ''">
                AND member_id = #{search.memberId}
            </if>
            <if test="search.beginTime != null">
                AND follow_time >= #{search.beginTime}
            </if>
        </where>
        ORDER BY follow_time DESC,follow_id DESC
    </select>

    <select id="lastFollowByMemberId" resultMap="BaseResultMap">
        SELECT
            <include refid="Alias_Base_Column_List" />
        FROM
        tab_contact_follow t
        INNER JOIN ( SELECT clerk_id, member_id, MAX( follow_time ) AS max_follow_time FROM tab_contact_follow WHERE
        enterprise_id = #{enterpriseId}
        and delete_flag = 0
        and member_id IN
        <foreach item="item" index="index" collection="memberIdList" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY clerk_id, member_id )
        latest ON t.clerk_id = latest.clerk_id
        AND t.member_id = latest.member_id
        AND t.follow_time = latest.max_follow_time
        where t.enterprise_id = #{enterpriseId}
        and t.delete_flag = 0
        <if test="clerkId!=null and clerkId!=''">
            and t.clerk_id = #{clerkId}
        </if>
        and t.member_id IN
        <foreach item="item" index="index" collection="memberIdList" open="(" separator="," close=")">
            #{item}
        </foreach>
        order by t.follow_time DESC,t.follow_id DESC
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="followId">
        INSERT INTO tab_contact_follow (member_id, clerk_id, clerk_code, follow_remark, follow_material, goods_info, follow_time, enterprise_id, delete_flag, create_time, update_time)
        VALUES (#{memberId}, #{clerkId}, #{clerkCode}, #{followRemark}, #{followMaterial}, #{goodsInfo}, #{followTime}, #{enterpriseId}, #{deleteFlag}, #{createTime}, #{updateTime})
    </insert>

    <insert id="insertBatch">
        INSERT INTO tab_contact_follow (follow_id,member_id, clerk_id, clerk_code, follow_remark, follow_material, goods_info, follow_time, enterprise_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.followId},#{item.memberId}, #{item.clerkId}, #{item.clerkCode}, #{item.followRemark}, #{item.followMaterial}, #{item.goodsInfo}, #{item.followTime}, #{item.enterpriseId})
        </foreach>
    </insert>

    <update id="update">
        UPDATE tab_contact_follow
        <set>
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="clerkId != null">clerk_id = #{clerkId},</if>
            <if test="clerkCode != null">clerk_code = #{clerkCode},</if>
            <if test="followRemark != null">follow_remark = #{followRemark},</if>
            <if test="followMaterial != null">follow_material = #{followMaterial},</if>
            <if test="goodsInfo != null">goods_info = #{goodsInfo},</if>
            <if test="followTime != null">follow_time = #{followTime},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
            update_time = #{updateTime}
        </set>
        WHERE follow_id = #{followId}
    </update>

    <delete id="deleteById">
        DELETE FROM tab_contact_follow
        WHERE follow_id = #{followId}
    </delete>
</mapper>