<?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.dk.mdm.mapper.mst.CusFollowStaffMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        cus_id, follow_staff, last_follow_id, last_follow_status, last_follow_time, follow_count, cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.CusFollowStaff">
        <id column="cus_id" property="cusId"/>
                <result column="follow_staff" property="followStaff" typeHandler="UuidTypeHandler"/>
                <result column="last_follow_id" property="lastFollowId" typeHandler="UuidTypeHandler"/>
                <result column="last_follow_status" property="lastFollowStatus"/>
            <result column="last_follow_time" property="lastFollowTime" typeHandler="TimestampTypeHandler"/>
                <result column="follow_count" property="followCount"/>
                <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="followStaff != null and followStaff != ''">
                AND follow_staff = #{followStaff}
            </if>
            <if test="lastFollowId != null and lastFollowId != ''">
                AND last_follow_id = #{lastFollowId}
            </if>
            <if test="lastFollowStatus != null and lastFollowStatus != ''">
                AND last_follow_status = #{lastFollowStatus}
            </if>
            <if test="lastFollowTime != null">
                AND last_follow_time = #{lastFollowTime}
            </if>
            <if test="followCount != null">
                AND follow_count = #{followCount}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键cusId批量操作 -->
        WHERE cus_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_crm_cus_follow_staff，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_crm_cus_follow_staff
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_crm_cus_follow_staff，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM t_crm_cus_follow_staff
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_crm_cus_follow_staff的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_crm_cus_follow_staff
        WHERE cus_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_crm_cus_follow_staff的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_crm_cus_follow_staff
        WHERE cus_id = #{cusId}
        for update
    </select>

    <!-- 根据主键锁定表t_crm_cus_follow_staff的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_crm_cus_follow_staff
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into t_crm_cus_follow_staff
        (
        <trim suffixOverrides=",">
            follow_staff,
            last_follow_id,
            last_follow_status,
            last_follow_time,
            follow_count,
            cp_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.followStaff}::uuid,
                #{item.lastFollowId}::uuid,
                #{item.lastFollowStatus},
                #{item.lastFollowTime},
                #{item.followCount},
                #{item.cpId},
            </trim>
            )
        </foreach>
    </insert>

    <select id="selectByCusFollowStaffId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_crm_cus_follow_staff
        where cus_id = #{cusId}::uuid and follow_staff = #{followStaff}::uuid
    </select>
</mapper>
