<?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.ym.im.mapper.ChatRecordMapper">
    <resultMap id="BaseResultMap" type="com.ym.im.entity.ChatRecord">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="staff_id" jdbcType="BIGINT" property="staffId"/>
        <result column="msg_type" jdbcType="TINYINT" property="msgType"/>
        <result column="msg_info" jdbcType="VARCHAR" property="msgInfo"/>
        <result column="send_receive" jdbcType="TINYINT" property="sendReceive"/>
        <result column="scope" jdbcType="TINYINT" property="scope"/>
        <result column="send_time" jdbcType="TIMESTAMP" property="sendTime"/>
        <result column="receive_time" jdbcType="TIMESTAMP" property="receiveTime"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, user_id, staff_id, msg_type, msg_info, send_receive, scope, send_time, receive_time, create_time, modify_time
    </sql>

    <insert id="insert">
        INSERT INTO chat_record_${index} (<include refid="Base_Column_List"/>)
        VALUES (
        #{chat.id},
        #{chat.userId},
        #{chat.staffId},
        #{chat.msgType},
        #{chat.msgInfo},
        #{chat.sendReceive},
        #{chat.scope},
        #{chat.sendTime},
        #{chat.receiveTime},
        #{chat.createTime},
        #{chat.modifyTime})
    </insert>

    <insert id="insertSelective">
        INSERT INTO chat_record_${index}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chat.id != null">
                id,
            </if>
            <if test="chat.userId != null">
                user_id,
            </if>
            <if test="chat.staffId != null">
                staff_id,
            </if>
            <if test="chat.msgType != null">
                msg_type,
            </if>
            <if test="chat.msgInfo != null">
                msg_info,
            </if>
            <if test="chat.sendReceive != null">
                send_receive,
            </if>
            <if test="chat.scope != null">
                scope,
            </if>
            <if test="chat.sendTime != null">
                send_time,
            </if>
            <if test="chat.receiveTime != null">
                receive_time,
            </if>
            <if test="chat.createTime != null">
                create_time,
            </if>
            <if test="chat.modifyTime != null">
                modify_time,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="chat.id != null">
                #{chat.id},
            </if>
            <if test="chat.userId != null">
                #{chat.userId},
            </if>
            <if test="chat.staffId != null">
                #{chat.staffId},
            </if>
            <if test="chat.msgType != null">
                #{chat.msgType},
            </if>
            <if test="chat.msgInfo != null">
                #{chat.msgInfo},
            </if>
            <if test="chat.sendReceive != null">
                #{chat.sendReceive},
            </if>
            <if test="chat.scope != null">
                #{chat.scope},
            </if>
            <if test="chat.sendTime != null">
                #{chat.sendTime},
            </if>
            <if test="chat.receiveTime != null">
                #{chat.receiveTime},
            </if>
            <if test="chat.createTime != null">
                #{chat.createTime},
            </if>
            <if test="chat.modifyTime != null">
                #{chat.modifyTime},
            </if>
        </trim>
    </insert>

    <update id="updateById">
        UPDATE chat_record_${index}
        SET user_id = #{chat.userId},
            staff_id = #{chat.staffId},
            msg_type = #{chat.msgType},
            msg_info = #{chat.msgInfo},
            send_receive = #{chat.sendReceive},
            scope = #{chat.scope},
            send_time = #{chat.sendTime},
            receive_time = #{chat.receiveTime},
            create_time = #{chat.createTime},
            modify_time = #{chat.modifyTime}
        WHERE id = #{chat.id}
    </update>

    <update id="updateByIdSelective">
        UPDATE chat_record_${index}
        <set>
            <if test="chat.id != null">
                #{chat.id}
            </if>
            <if test="chat.userId != null">
                #{chat.userId}
            </if>
            <if test="chat.staffId != null">
                #{chat.staffId}
            </if>
            <if test="chat.msgType != null">
                #{chat.msgType}
            </if>
            <if test="chat.msgInfo != null">
                #{chat.msgInfo}
            </if>
            <if test="chat.sendReceive != null">
                #{chat.sendReceive}
            </if>
            <if test="chat.scope != null">
                #{chat.scope}
            </if>
            <if test="chat.sendTime != null">
                #{chat.sendTime}
            </if>
            <if test="chat.receiveTime != null">
                #{chat.receiveTime}
            </if>
            <if test="chat.createTime != null">
                #{chat.createTime}
            </if>
            <if test="chat.modifyTime != null">
                #{chat.modifyTime}
            </if>
        </set>
    </update>

    <update id="updateBatchReceiveTimeById">
        UPDATE chat_record_${index}
        <set>
            receive_time =
            <foreach collection="chats" item="chat" index="index" separator=" " open="CASE" close="END">
                WHEN id = #{chat.id} THEN #{chat.receiveTime}
            </foreach>
        </set>
        WHERE id IN
        <foreach collection="chats" item="chat" index="index" separator="," open="(" close=")">
            #{chat.id}
        </foreach>
    </update>

    <update id="updateReceiveTime">
        UPDATE chat_record_${index}
        <set>
            receive_time = #{chat.receiveTime},
            modify_time = #{chat.modifyTime}
        </set>
        WHERE id = #{chat.id}
    </update>

    <select id="selectById" resultType="com.ym.im.entity.ChatRecord">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_record_${index} WHERE id = #{id}
    </select>

    <select id="getChatRecord" resultType="com.ym.im.entity.ChatRecord">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_record_${index}
        <trim prefix="WHERE" suffixOverrides="AND">
            <if test="pull.userId != null">
                user_id = #{pull.userId} AND
            </if>
            <if test="pull.direction == 'forward' and pull.chatRecordId != null">
                --id小于传入id
                id &lt; #{pull.chatRecordId}
            </if>
            <if test="pull.direction == 'backward' and pull.chatRecordId != null">
                --id大于传入id
                id &gt; #{pull.chatRecordId}
            </if>
        </trim>
    </select>


</mapper>
