<?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.ruoyi.system.mapper.ChatHistoryMapper">
    
    <resultMap type="ChatHistory" id="ChatHistoryResult">
        <result property="id"    column="id"    />
        <result property="messageId"    column="message_id"    />
        <result property="userId"    column="user_id"    />
        <result property="sessionId"    column="session_id"    />
        <result property="sessionIdLong"    column="session_id_long"    />
        <result property="messageType"    column="message_type"    />
        <result property="content"    column="content"    />
        <result property="messageContent"    column="message_content"    />
        <result property="isLink"    column="is_link"    />
        <result property="isButton"    column="is_button"    />
        <result property="senderId"    column="sender_id"    />
        <result property="receiverId"    column="receiver_id"    />
        <result property="sentAt"    column="sent_at"    />
        <result property="isRead"    column="is_read"    />
        <result property="senderType"    column="sender_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectChatHistoryVo">
        select id, message_id, user_id, session_id, session_id_long, message_type, content, message_content, is_link, is_button, sender_id, receiver_id, sent_at, is_read, sender_type, create_time, update_time from chat_history
    </sql>

    <select id="selectChatHistoryList" parameterType="ChatHistory" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        <where>  
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="sessionId != null  and sessionId != ''"> and session_id = #{sessionId}</if>
            <if test="messageType != null  and messageType != ''"> and message_type = #{messageType}</if>
            <if test="content != null  and content != ''"> and content like concat('%', #{content}, '%')</if>
            <if test="isLink != null "> and is_link = #{isLink}</if>
            <if test="isButton != null "> and is_button = #{isButton}</if>
        </where>
        order by create_time asc
    </select>
    
    <select id="selectChatHistoryById" parameterType="Long" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where id = #{id}
    </select>

    <select id="selectChatHistoryBySessionId" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where session_id = #{sessionId}
        order by create_time asc
    </select>

    <select id="selectChatHistoryByUserId" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where user_id = #{userId}
        order by create_time asc
    </select>

    <select id="selectChatHistoryByMessageId" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where message_id = #{messageId}
    </select>

    <select id="selectChatHistoryBySenderId" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where sender_id = #{senderId}
        order by create_time asc
    </select>

    <select id="selectChatHistoryByReceiverId" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where receiver_id = #{receiverId}
        order by create_time asc
    </select>

    <select id="selectChatHistoryBySenderType" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where sender_type = #{senderType}
        order by create_time asc
    </select>

    <select id="selectUnreadChatHistory" parameterType="String" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        where is_read = 0
        <if test="userId != null and userId != ''"> and user_id = #{userId}</if>
        order by create_time asc
    </select>

    <select id="selectChatHistoryByTimeRange" parameterType="map" resultMap="ChatHistoryResult">
        <include refid="selectChatHistoryVo"/>
        <where>
            <if test="startTime != null"> and sent_at &gt;= #{startTime}</if>
            <if test="endTime != null"> and sent_at &lt;= #{endTime}</if>
            <if test="userId != null and userId != ''"> and user_id = #{userId}</if>
        </where>
        order by sent_at asc
    </select>
        
    <insert id="insertChatHistory" parameterType="ChatHistory" useGeneratedKeys="true" keyProperty="id">
        insert into chat_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="messageId != null">message_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="sessionId != null">session_id,</if>
            <if test="sessionIdLong != null">session_id_long,</if>
            <if test="messageType != null">message_type,</if>
            <if test="content != null">content,</if>
            <if test="messageContent != null">message_content,</if>
            <if test="isLink != null">is_link,</if>
            <if test="isButton != null">is_button,</if>
            <if test="senderId != null">sender_id,</if>
            <if test="receiverId != null">receiver_id,</if>
            <if test="sentAt != null">sent_at,</if>
            <if test="isRead != null">is_read,</if>
            <if test="senderType != null">sender_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="messageId != null">#{messageId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="sessionId != null">#{sessionId},</if>
            <if test="sessionIdLong != null">#{sessionIdLong},</if>
            <if test="messageType != null">#{messageType},</if>
            <if test="content != null">#{content},</if>
            <if test="messageContent != null">#{messageContent},</if>
            <if test="isLink != null">#{isLink},</if>
            <if test="isButton != null">#{isButton},</if>
            <if test="senderId != null">#{senderId},</if>
            <if test="receiverId != null">#{receiverId},</if>
            <if test="sentAt != null">#{sentAt},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="senderType != null">#{senderType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertChatHistory" parameterType="java.util.List">
        insert into chat_history (message_id, user_id, session_id, session_id_long, message_type, content, message_content, is_link, is_button, sender_id, receiver_id, sent_at, is_read, sender_type, create_time, update_time) values
        <foreach collection="list" item="item" separator=",">
            (#{item.messageId}, #{item.userId}, #{item.sessionId}, #{item.sessionIdLong}, #{item.messageType}, #{item.content}, #{item.messageContent}, #{item.isLink}, #{item.isButton}, #{item.senderId}, #{item.receiverId}, #{item.sentAt}, #{item.isRead}, #{item.senderType}, #{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateChatHistory" parameterType="ChatHistory">
        update chat_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="messageId != null">message_id = #{messageId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="sessionId != null">session_id = #{sessionId},</if>
            <if test="sessionIdLong != null">session_id_long = #{sessionIdLong},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
            <if test="content != null">content = #{content},</if>
            <if test="messageContent != null">message_content = #{messageContent},</if>
            <if test="isLink != null">is_link = #{isLink},</if>
            <if test="isButton != null">is_button = #{isButton},</if>
            <if test="senderId != null">sender_id = #{senderId},</if>
            <if test="receiverId != null">receiver_id = #{receiverId},</if>
            <if test="sentAt != null">sent_at = #{sentAt},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="senderType != null">sender_type = #{senderType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteChatHistoryById" parameterType="Long">
        delete from chat_history where id = #{id}
    </delete>

    <delete id="deleteChatHistoryByIds" parameterType="String">
        delete from chat_history where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteChatHistoryBySessionId" parameterType="String">
        delete from chat_history where session_id = #{sessionId}
    </delete>

    <delete id="deleteChatHistoryByUserId" parameterType="String">
        delete from chat_history where user_id = #{userId}
    </delete>
</mapper>