<?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.TbChatRoomMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbChatRoomDTO">
        <id column="room_id" property="roomId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <result column="room_name" property="roomName" jdbcType="VARCHAR"/>
        <result column="room_type" property="roomType" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="guest_id" property="guestId" jdbcType="VARCHAR"/>
        <result column="guest_name" property="guestName" jdbcType="VARCHAR"/>
        <result column="ai_session_id" property="aiSessionId" jdbcType="VARCHAR"/>
        <result column="message_count" property="messageCount" jdbcType="INTEGER"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="last_message_time" property="lastMessageTime" jdbcType="TIMESTAMP"/>
        <result column="last_message" property="lastMessage" jdbcType="VARCHAR"/>
        <result column="comment_level" property="commentLevel" jdbcType="INTEGER"/>
        <result column="closed_by" property="closedBy" jdbcType="VARCHAR"/>
        <result column="closed_time" property="closedTime" 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"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.ChatRoomVO">
        <id column="room_id" property="roomId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <result column="room_name" property="roomName" jdbcType="VARCHAR"/>
        <result column="room_type" property="roomType" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="guest_id" property="guestId" jdbcType="VARCHAR"/>
        <result column="guest_name" property="guestName" jdbcType="VARCHAR"/>
        <result column="ai_session_id" property="aiSessionId" jdbcType="VARCHAR"/>
        <result column="message_count" property="messageCount" jdbcType="INTEGER"/>
        <result column="device_code" property="deviceCode" jdbcType="VARCHAR"/>
        <result column="unread_count" property="unreadCount" jdbcType="INTEGER"/>
        <result column="last_message_time" property="lastMessageTime" jdbcType="TIMESTAMP"/>
        <result column="last_message" property="lastMessage" jdbcType="VARCHAR"/>
        <result column="comment_level" property="commentLevel" jdbcType="INTEGER"/>
        <result column="closed_by" property="closedBy" jdbcType="VARCHAR"/>
        <result column="closed_time" property="closedTime" 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"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        room_id, optsn, workcase_id, room_name, room_type, status, guest_id, guest_name,
        ai_session_id, message_count, device_code, last_message_time, last_message, comment_level, closed_by, closed_time,
        creator, create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertChatRoom" parameterType="org.xyzh.api.workcase.dto.TbChatRoomDTO">
        INSERT INTO workcase.tb_chat_room (
            optsn, room_id, workcase_id, room_name, guest_id, guest_name, device_code, creator
            <if test="roomType != null">, room_type</if>
            <if test="status != null">, status</if>
            <if test="aiSessionId != null">, ai_session_id</if>
            <if test="messageCount != null">, message_count</if>
            <if test="lastMessageTime != null">, last_message_time</if>
            <if test="lastMessage != null">, last_message</if>
        ) VALUES (
            #{optsn}, #{roomId}, #{workcaseId}, #{roomName}, #{guestId}, #{guestName}, #{deviceCode}, #{creator}
            <if test="roomType != null">, #{roomType}</if>
            <if test="status != null">, #{status}</if>
            <if test="aiSessionId != null">, #{aiSessionId}</if>
            <if test="messageCount != null">, #{messageCount}</if>
            <if test="lastMessageTime != null">, #{lastMessageTime}</if>
            <if test="lastMessage != null">, #{lastMessage}</if>
        )
    </insert>

    <update id="updateChatRoom" parameterType="org.xyzh.api.workcase.dto.TbChatRoomDTO">
        UPDATE workcase.tb_chat_room
        <set>
            <if test="workcaseId != null">workcase_id = #{workcaseId},</if>
            <if test="roomName != null and roomName != ''">room_name = #{roomName},</if>
            <if test="roomType != null and roomType != ''">room_type = #{roomType},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="aiSessionId != null">ai_session_id = #{aiSessionId},</if>
            <if test="messageCount != null">message_count = #{messageCount},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="lastMessageTime != null">last_message_time = #{lastMessageTime},</if>
            <if test="lastMessage != null">last_message = #{lastMessage},</if>
            <if test="commentLevel != null">comment_level = #{commentLevel},</if>
            <if test="closedBy != null">closed_by = #{closedBy},</if>
            <if test="closedTime != null">closed_time = #{closedTime},</if>
            update_time = now()
        </set>
        WHERE room_id = #{roomId} AND deleted = false
    </update>

    <update id="deleteChatRoom" parameterType="org.xyzh.api.workcase.dto.TbChatRoomDTO">
        UPDATE workcase.tb_chat_room
        SET deleted = true, delete_time = now()
        WHERE room_id = #{roomId} AND deleted = false
    </update>

    <select id="selectChatRoomById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room
        WHERE room_id = #{roomId} AND deleted = false
    </select>

    <select id="selectChatRoomList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_chat_room
        <where>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">AND workcase_id = #{filter.workcaseId}</if>
            <if test="filter.roomName != null and filter.roomName != ''">AND room_name LIKE CONCAT('%', #{filter.roomName}, '%')</if>
            <if test="filter.roomType != null and filter.roomType != ''">AND room_type = #{filter.roomType}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
            <if test="filter.guestId != null and filter.guestId != ''">AND guest_id = #{filter.guestId}</if>
            <if test="filter.guestName != null and filter.guestName != ''">AND guest_name LIKE CONCAT('%', #{filter.guestName}, '%')</if>
            AND deleted = false
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectChatRoomPage" resultMap="VOResultMap">
        SELECT r.room_id, r.optsn, r.workcase_id, r.room_name, r.room_type, r.status,
               r.guest_id, r.guest_name, r.ai_session_id, r.message_count, r.device_code,
               r.last_message_time, r.last_message, r.comment_level, r.closed_by, r.closed_time,
               r.creator, r.create_time, r.update_time, r.delete_time, r.deleted,
               COALESCE(m.unread_count, 0) as unread_count
        FROM workcase.tb_chat_room r
        LEFT JOIN workcase.tb_chat_room_member m ON r.room_id = m.room_id AND m.user_id = #{userId}
        <where>
            <if test="filter.roomId != null and filter.roomId != ''">AND r.room_id = #{filter.roomId}</if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">AND r.workcase_id = #{filter.workcaseId}</if>
            <if test="filter.roomName != null and filter.roomName != ''">AND r.room_name LIKE CONCAT('%', #{filter.roomName}, '%')</if>
            <if test="filter.roomType != null and filter.roomType != ''">AND r.room_type = #{filter.roomType}</if>
            <if test="filter.status != null and filter.status != ''">AND r.status = #{filter.status}</if>
            <if test="filter.guestId != null and filter.guestId != ''">AND r.guest_id = #{filter.guestId}</if>
            <if test="filter.guestName != null and filter.guestName != ''">AND r.guest_name LIKE CONCAT('%', #{filter.guestName}, '%')</if>
            AND r.deleted = false
        </where>
        ORDER BY r.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countChatRooms" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_chat_room
        <where>
            <if test="filter.roomId != null and filter.roomId != ''">AND room_id = #{filter.roomId}</if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">AND workcase_id = #{filter.workcaseId}</if>
            <if test="filter.roomName != null and filter.roomName != ''">AND room_name LIKE CONCAT('%', #{filter.roomName}, '%')</if>
            <if test="filter.roomType != null and filter.roomType != ''">AND room_type = #{filter.roomType}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
            <if test="filter.guestId != null and filter.guestId != ''">AND guest_id = #{filter.guestId}</if>
            <if test="filter.guestName != null and filter.guestName != ''">AND guest_name LIKE CONCAT('%', #{filter.guestName}, '%')</if>
            <if test="filter.startTime != null and filter.endTime != null">
                AND create_time BETWEEN #{filter.startTime} AND #{filter.endTime}
            </if>
            AND deleted = false
        </where>
    </select>

</mapper>
