<?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="org.xyzh.workcase.mapper.TbChatMessageMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbChatRoomMessageDTO">
        <id column="message_id" property="messageId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="room_id" property="roomId" jdbcType="VARCHAR"/>
        <result column="sender_id" property="senderId" jdbcType="VARCHAR"/>
        <result column="sender_type" property="senderType" jdbcType="VARCHAR"/>
        <result column="sender_name" property="senderName" jdbcType="VARCHAR"/>
        <result column="message_type" property="messageType" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="files" property="files" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="content_extra" property="contentExtra" jdbcType="OTHER" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="reply_to_msg_id" property="replyToMsgId" jdbcType="VARCHAR"/>
        <result column="is_ai_message" property="isAiMessage" jdbcType="BOOLEAN"/>
        <result column="ai_message_id" property="aiMessageId" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="read_count" property="readCount" jdbcType="INTEGER"/>
        <result column="send_time" property="sendTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.ChatRoomMessageVO">
        <id column="message_id" property="messageId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="room_id" property="roomId" jdbcType="VARCHAR"/>
        <result column="sender_id" property="senderId" jdbcType="VARCHAR"/>
        <result column="sender_type" property="senderType" jdbcType="VARCHAR"/>
        <result column="sender_name" property="senderName" jdbcType="VARCHAR"/>
        <result column="message_type" property="messageType" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="files" property="files" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="content_extra" property="contentExtra" jdbcType="OTHER" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="reply_to_msg_id" property="replyToMsgId" jdbcType="VARCHAR"/>
        <result column="is_ai_message" property="isAiMessage" jdbcType="BOOLEAN"/>
        <result column="ai_message_id" property="aiMessageId" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="read_count" property="readCount" jdbcType="INTEGER"/>
        <result column="send_time" property="sendTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        message_id, optsn, room_id, sender_id, sender_type, sender_name, message_type,
        content, files, content_extra, reply_to_msg_id, is_ai_message, ai_message_id,
        status, read_count, send_time, creator, create_time, update_time
    </sql>

    <insert id="insertChatMessage" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMessageDTO">
        INSERT INTO workcase.tb_chat_room_message (
            optsn, message_id, room_id, sender_id, sender_type, sender_name, content, creator,send_time
            <if test="messageType != null">, message_type</if>
            <if test="files != null">, files</if>
            <if test="contentExtra != null">, content_extra</if>
            <if test="replyToMsgId != null">, reply_to_msg_id</if>
            <if test="isAiMessage != null">, is_ai_message</if>
            <if test="aiMessageId != null">, ai_message_id</if>
            <if test="status != null">, status</if>
            <if test="createTime != null">, create_time</if>
        ) VALUES (
            #{optsn}, #{messageId}, #{roomId}, #{senderId}, #{senderType}, #{senderName}, #{content}, #{creator},#{sendTime}
            <if test="messageType != null">, #{messageType}</if>
            <if test="files != null">, #{files, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="contentExtra != null">, #{contentExtra, typeHandler=org.xyzh.common.jdbc.handler.FastJson2TypeHandler}::jsonb</if>
            <if test="replyToMsgId != null">, #{replyToMsgId}</if>
            <if test="isAiMessage != null">, #{isAiMessage}</if>
            <if test="aiMessageId != null">, #{aiMessageId}</if>
            <if test="status != null">, #{status}</if>
            <if test="createTime != null">, #{createTime}</if>
        )
    </insert>

    <update id="updateChatMessage" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMessageDTO">
        UPDATE workcase.tb_chat_room_message
        <set>
            <if test="content != null">content = #{content},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="readCount != null">read_count = #{readCount},</if>
            update_time = now()
        </set>
        WHERE message_id = #{messageId}
    </update>

    <delete id="deleteChatMessage" parameterType="org.xyzh.api.workcase.dto.TbChatRoomMessageDTO">
        DELETE FROM workcase.tb_chat_room_message
        WHERE message_id = #{messageId}
    </delete>

    <select id="selectChatMessageById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_message
        WHERE message_id = #{messageId}
    </select>

    <select id="selectChatMessageList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_message
        <where>
            <if test="filter.messageId != null and filter.messageId != ''">AND message_id = #{filter.messageId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</if>
            <if test="filter.senderId != null and filter.senderId != ''">AND sender_id = #{filter.senderId}</if>
            <if test="filter.senderType != null and filter.senderType != ''">AND sender_type = #{filter.senderType}</if>
            <if test="filter.messageType != null and filter.messageType != ''">AND message_type = #{filter.messageType}</if>
            <if test="filter.isAiMessage != null">AND is_ai_message = #{filter.isAiMessage}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
        ORDER BY send_time DESC
    </select>

    <select id="selectChatMessagePage" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_message
        <where>
            <if test="filter.messageId != null and filter.messageId != ''">AND message_id = #{filter.messageId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</if>
            <if test="filter.senderId != null and filter.senderId != ''">AND sender_id = #{filter.senderId}</if>
            <if test="filter.senderType != null and filter.senderType != ''">AND sender_type = #{filter.senderType}</if>
            <if test="filter.messageType != null and filter.messageType != ''">AND message_type = #{filter.messageType}</if>
            <if test="filter.isAiMessage != null">AND is_ai_message = #{filter.isAiMessage}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
        ORDER BY send_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countChatMessages" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_chat_room_message
        <where>
            <if test="filter.messageId != null and filter.messageId != ''">AND message_id = #{filter.messageId}</if>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</if>
            <if test="filter.senderId != null and filter.senderId != ''">AND sender_id = #{filter.senderId}</if>
            <if test="filter.senderType != null and filter.senderType != ''">AND sender_type = #{filter.senderType}</if>
            <if test="filter.messageType != null and filter.messageType != ''">AND message_type = #{filter.messageType}</if>
            <if test="filter.isAiMessage != null">AND is_ai_message = #{filter.isAiMessage}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
        </where>
    </select>

    <select id="selectChatMessageVOById" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room_message
        WHERE message_id = #{messageId}
    </select>

</mapper>
