package com.yushu.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yushu.common.utils.SecurityUtils;
import com.yushu.system.mapper.SysChatMessageMapper;
import com.yushu.system.mapper.SysConversationMapper;
import com.yushu.system.domain.SysChatMessage;
import com.yushu.system.service.ISysChatMessageService;

/**
 * 对话消息Service业务层处理
 * 
 * @author yushu
 * @date 2025-11-12
 */
@Service
public class SysChatMessageServiceImpl implements ISysChatMessageService 
{
    @Autowired
    private SysChatMessageMapper sysChatMessageMapper;

    @Autowired
    private SysConversationMapper sysConversationMapper;

    /**
     * 查询对话消息
     * 
     * @param messageId 对话消息主键
     * @return 对话消息
     */
    @Override
    public SysChatMessage selectSysChatMessageByMessageId(Long messageId)
    {
        return sysChatMessageMapper.selectSysChatMessageByMessageId(messageId);
    }

    /**
     * 查询对话消息列表
     * 
     * @param sysChatMessage 对话消息
     * @return 对话消息
     */
    @Override
    public List<SysChatMessage> selectSysChatMessageList(SysChatMessage sysChatMessage)
    {
        return sysChatMessageMapper.selectSysChatMessageList(sysChatMessage);
    }

    /**
     * 查询会话的消息列表
     * 
     * @param conversationId 会话ID
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 消息列表
     */
    @Override
    public List<SysChatMessage> selectConversationMessages(String conversationId, Integer pageNum, Integer pageSize)
    {
        // 暂时返回所有消息，避免分页插件干扰
        return sysChatMessageMapper.selectAllConversationMessages(conversationId);
    }

    /**
     * 新增对话消息
     * 
     * @param sysChatMessage 对话消息
     * @return 结果
     */
    @Override
    public int insertSysChatMessage(SysChatMessage sysChatMessage)
    {
        return sysChatMessageMapper.insertSysChatMessage(sysChatMessage);
    }

    /**
     * 修改对话消息
     * 
     * @param sysChatMessage 对话消息
     * @return 结果
     */
    @Override
    public int updateSysChatMessage(SysChatMessage sysChatMessage)
    {
        return sysChatMessageMapper.updateSysChatMessage(sysChatMessage);
    }

    /**
     * 批量删除对话消息
     * 
     * @param messageIds 需要删除的对话消息主键
     * @return 结果
     */
    @Override
    public int deleteSysChatMessageByMessageIds(Long[] messageIds)
    {
        return sysChatMessageMapper.deleteSysChatMessageByMessageIds(messageIds);
    }

    /**
     * 删除对话消息信息
     * 
     * @param messageId 对话消息主键
     * @return 结果
     */
    @Override
    public int deleteSysChatMessageByMessageId(Long messageId)
    {
        return sysChatMessageMapper.deleteSysChatMessageByMessageId(messageId);
    }

    /**
     * 发送消息
     * 
     * @param conversationId 会话ID
     * @param content 消息内容
     * @return 结果
     */
    @Override
    @Transactional
    public int sendMessage(String conversationId, String content)
    {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        
        // 创建消息
        SysChatMessage message = new SysChatMessage();
        message.setConversationId(conversationId);
        message.setSenderId(userId);
        message.setSenderName(userName);
        message.setMessageType("text");
        message.setContent(content);
        message.setStatus("1");
        
        int result = sysChatMessageMapper.insertSysChatMessage(message);
        
        // 更新会话的最后消息
        if (result > 0) {
            sysConversationMapper.updateLastMessage(conversationId, message.getMessageId());
        }
        
        return result;
    }

    /**
     * 统计会话未读消息数
     * 
     * @param conversationId 会话ID
     * @return 未读消息数
     */
    @Override
    public int countUnreadMessages(String conversationId)
    {
        Long userId = SecurityUtils.getUserId();
        return sysChatMessageMapper.countUnreadMessages(conversationId, userId);
    }

    /**
     * 标记会话为已读
     * 
     * @param conversationId 会话ID
     * @return 结果
     */
    @Override
    public int markConversationAsRead(String conversationId)
    {
        Long userId = SecurityUtils.getUserId();
        return sysChatMessageMapper.markConversationAsRead(conversationId, userId);
    }

    /**
     * 获取会话的最后一条消息
     * 
     * @param conversationId 会话ID
     * @return 最后一条消息
     */
    @Override
    public SysChatMessage selectLastMessage(String conversationId)
    {
        return sysChatMessageMapper.selectLastMessage(conversationId);
    }

    /**
     * 增加用户的未读消息数
     * 
     * @param conversationId 会话ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int incrementUnreadCount(String conversationId, Long userId)
    {
        return sysChatMessageMapper.incrementUnreadCount(conversationId, userId);
    }
}
