package com.zhentao.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.entity.ChatConversation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 聊天会话Mapper
 */
@Mapper
public interface ChatConversationMapper extends BaseMapper<ChatConversation> {

    /**
     * 查询用户与目标用户的会话
     */
    @Select("SELECT * FROM chat_conversation WHERE user_id = #{userId} AND target_user_id = #{targetUserId} AND is_deleted = 0 LIMIT 1")
    ChatConversation selectByUserIdAndTargetUserId(@Param("userId") Long userId, @Param("targetUserId") Long targetUserId);

    /**
     * 增加未读数
     */
    @Select("UPDATE chat_conversation SET unread_count = unread_count + 1 WHERE user_id = #{userId} AND target_user_id = #{targetUserId}")
    void incrementUnreadCount(@Param("userId") Long userId, @Param("targetUserId") Long targetUserId);

    /**
     * 清空未读数
     */
    @Select("UPDATE chat_conversation SET unread_count = 0 WHERE user_id = #{userId} AND target_user_id = #{targetUserId}")
    void clearUnreadCount(@Param("userId") Long userId, @Param("targetUserId") Long targetUserId);
    
    /**
     * 查询用户的所有会话列表（按最后消息时间倒序）
     */
    @Select("SELECT * FROM chat_conversation WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY last_message_time DESC")
    java.util.List<ChatConversation> selectByUserId(@Param("userId") Long userId);
}



