package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ScoreRoomUserMapper;
import com.ruoyi.system.domain.ScoreRoomUser;
import com.ruoyi.system.service.IScoreRoomUserService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-30
 */
@Service
public class ScoreRoomUserServiceImpl implements IScoreRoomUserService 
{
    @Autowired
    private ScoreRoomUserMapper scoreRoomUserMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param roomUserId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ScoreRoomUser selectScoreRoomUserByRoomUserId(Long roomUserId)
    {
        return scoreRoomUserMapper.selectScoreRoomUserByRoomUserId(roomUserId);
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param UserId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ScoreRoomUser selectScoreRoomUserByUserId(Long UserId)
    {
        return scoreRoomUserMapper.selectScoreRoomUserByUserId(UserId);
    }



    /**
     * 查询【请填写功能名称】列表
     * 
     * @param scoreRoomUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ScoreRoomUser> selectScoreRoomUserList(ScoreRoomUser scoreRoomUser)
    {
        return scoreRoomUserMapper.selectScoreRoomUserList(scoreRoomUser);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param roomId 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ScoreRoomUser> getUserScoresWithDetails(Long roomId) {
        return scoreRoomUserMapper.selectUserScoresWithDetails(roomId);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param scoreRoomUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertScoreRoomUser(ScoreRoomUser scoreRoomUser)
    {
        return scoreRoomUserMapper.insertScoreRoomUser(scoreRoomUser);
    }


    @Override
    public int updateScoreRoomUser(List<ScoreRoomUser> scoreRoomUsers)
    {
        List<ScoreRoomUser> updatedUsers = new ArrayList<>();

        for (ScoreRoomUser playerScore: scoreRoomUsers){
            // 处理单个用户的得分累加
            ScoreRoomUser updatedUser = processUserScoreUpdate(playerScore);
            updatedUsers.add(updatedUser);
        }

        return scoreRoomUserMapper.updateScoreRoomUser(updatedUsers);
    }

    private ScoreRoomUser processUserScoreUpdate(ScoreRoomUser newScore) {
        // 1. 首先检查用户是否已在房间中
        ScoreRoomUser existingUser = scoreRoomUserMapper.selectScoreRoomUserPrescore(newScore.getUserId(), newScore.getRoomId());

        Long newTotalScore;
        ScoreRoomUser userToUpdate;

        if (existingUser == null) {
            // 用户不在房间中，创建新记录
            userToUpdate = new ScoreRoomUser();
            userToUpdate.setRoomId(newScore.getRoomId());
            userToUpdate.setUserId(newScore.getUserId());
            userToUpdate.setPlayerType("user");
            userToUpdate.setNickName(newScore.getNickName());
            userToUpdate.setTotalScore(newScore.getTotalScore() != null ? newScore.getTotalScore() : 0L);

            // 插入新记录
            scoreRoomUserMapper.insertScoreRoomUser(userToUpdate);
            return userToUpdate;
        } else {
            // 2. 存在记录：历史总分 + 本次得分
            Long currentScore = existingUser.getTotalScore() != null ? existingUser.getTotalScore() : 0L;
            Long additionalScore = newScore.getTotalScore() != null ? newScore.getTotalScore() : 0L;

            newTotalScore = currentScore + additionalScore;

            // 使用现有记录进行更新
            userToUpdate = existingUser;
            userToUpdate.setTotalScore(newTotalScore);

            return userToUpdate;
        }
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param roomUserIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteScoreRoomUserByRoomUserIds(Long[] roomUserIds)
    {
        return scoreRoomUserMapper.deleteScoreRoomUserByRoomUserIds(roomUserIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param roomUserId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteScoreRoomUserByRoomUserId(Long roomUserId)
    {
        return scoreRoomUserMapper.deleteScoreRoomUserByRoomUserId(roomUserId);
    }



}
