package com.yushu.system.service.impl;

import java.util.List;
import java.util.Map;
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.SysConversationMapper;
import com.yushu.system.mapper.SysUserMapper;
import com.yushu.system.domain.SysConversation;
import com.yushu.common.core.domain.entity.SysUser;
import com.yushu.system.service.ISysConversationService;

/**
 * 会话Service业务层处理
 * 
 * @author yushu
 * @date 2025-11-12
 */
@Service
public class SysConversationServiceImpl implements ISysConversationService 
{
    @Autowired
    private SysConversationMapper sysConversationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询会话
     * 
     * @param conversationId 会话主键
     * @return 会话
     */
    @Override
    public SysConversation selectSysConversationByConversationId(String conversationId)
    {
        return sysConversationMapper.selectSysConversationByConversationId(conversationId);
    }

    /**
     * 查询会话列表
     * 
     * @param sysConversation 会话
     * @return 会话
     */
    @Override
    public List<SysConversation> selectSysConversationList(SysConversation sysConversation)
    {
        return sysConversationMapper.selectSysConversationList(sysConversation);
    }

    /**
     * 获取用户的会话列表
     * 
     * @return 会话列表
     */
    @Override
    public List<Map<String, Object>> getUserConversationList()
    {
        Long userId = SecurityUtils.getUserId();
        return sysConversationMapper.selectUserConversationList(userId);
    }

    /**
     * 新增会话
     * 
     * @param sysConversation 会话
     * @return 结果
     */
    @Override
    public int insertSysConversation(SysConversation sysConversation)
    {
        return sysConversationMapper.insertSysConversation(sysConversation);
    }

    /**
     * 修改会话
     * 
     * @param sysConversation 会话
     * @return 结果
     */
    @Override
    public int updateSysConversation(SysConversation sysConversation)
    {
        return sysConversationMapper.updateSysConversation(sysConversation);
    }

    /**
     * 批量删除会话
     * 
     * @param conversationIds 需要删除的会话主键
     * @return 结果
     */
    @Override
    public int deleteSysConversationByConversationIds(String[] conversationIds)
    {
        return sysConversationMapper.deleteSysConversationByConversationIds(conversationIds);
    }

    /**
     * 删除会话信息
     * 
     * @param conversationId 会话主键
     * @return 结果
     */
    @Override
    public int deleteSysConversationByConversationId(String conversationId)
    {
        return sysConversationMapper.deleteSysConversationByConversationId(conversationId);
    }

    /**
     * 创建或获取私聊会话
     * 
     * @param otherUserId 对方用户ID
     * @return 会话ID
     */
    @Override
    @Transactional
    public String createOrGetPrivateConversation(Long otherUserId)
    {
        Long currentUserId = SecurityUtils.getUserId();
        String currentUserName = SecurityUtils.getUsername();
        
        // 生成会话ID（确保两个用户之间只有一个会话）
        String conversationId = sysConversationMapper.createPrivateConversation(currentUserId, otherUserId);
        
        // 检查会话是否已存在
        SysConversation existingConversation = sysConversationMapper.selectSysConversationByConversationId(conversationId);
        
        if (existingConversation == null) {
            // 创建新会话
            SysConversation conversation = new SysConversation();
            conversation.setConversationId(conversationId);
            conversation.setConversationType("1"); // 私聊
            conversation.setCreatorId(currentUserId);
            conversation.setCreatorName(currentUserName);
            conversation.setStatus("1");
            
            sysConversationMapper.insertSysConversation(conversation);
            
            // 添加当前用户到会话
            SysUser currentUser = sysUserMapper.selectUserById(currentUserId);
            sysConversationMapper.addConversationMember(conversationId, currentUserId, 
                currentUser.getUserName(), currentUser.getNickName());
            
            // 添加对方用户到会话
            SysUser otherUser = sysUserMapper.selectUserById(otherUserId);
            if (otherUser != null) {
                sysConversationMapper.addConversationMember(conversationId, otherUserId, 
                    otherUser.getUserName(), otherUser.getNickName());
            }
        }
        
        return conversationId;
    }

    /**
     * 更新会话成员备注
     * 
     * @param conversationId 会话ID
     * @param remarkName 备注名称
     * @return 结果
     */
    @Override
    public int updateMemberRemark(String conversationId, String remarkName)
    {
        Long userId = SecurityUtils.getUserId();
        return sysConversationMapper.updateMemberRemark(conversationId, userId, remarkName);
    }

    /**
     * 获取会话的所有成员ID
     * 
     * @param conversationId 会话ID
     * @return 成员ID列表
     */
    @Override
    public List<Long> getConversationMemberIds(String conversationId)
    {
        return sysConversationMapper.selectConversationMemberIds(conversationId);
    }
}
