package com.fs.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserPlayer;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.mapper.FsUserPlayerMapper;
import com.fs.his.service.IFsUserPlayerService;
import com.fs.his.vo.FsUserPlayerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户游戏记录Service业务层处理
 *
 * @author fs
 * @date 2026-02-25
 */
@Service
public class FsUserPlayerServiceImpl extends ServiceImpl<FsUserPlayerMapper, FsUserPlayer> implements IFsUserPlayerService {

    @Autowired
    private FsUserMapper fsUserMapper;


    /**
     * 查询用户游戏记录
     *
     * @param id 用户游戏记录主键
     * @return 用户游戏记录
     */
    @Override
    public FsUserPlayer selectFsUserPlayerById(Long id)
    {
        return baseMapper.selectFsUserPlayerById(id);
    }

    /**
     * 查询用户游戏记录列表
     *
     * @param fsUserPlayer 用户游戏记录
     * @return 用户游戏记录
     */
    @Override
    public List<FsUserPlayer> selectFsUserPlayerList(FsUserPlayer fsUserPlayer)
    {
        return baseMapper.selectFsUserPlayerList(fsUserPlayer);
    }

    /**
     * 新增用户游戏记录
     *
     * @param fsUserPlayer 用户游戏记录
     * @return 结果
     */
    @Override
    public int insertFsUserPlayer(FsUserPlayer fsUserPlayer)
    {
        fsUserPlayer.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsUserPlayer(fsUserPlayer);
    }

    /**
     * 修改用户游戏记录
     *
     * @param fsUserPlayer 用户游戏记录
     * @return 结果
     */
    @Override
    public int updateFsUserPlayer(FsUserPlayer fsUserPlayer)
    {
        fsUserPlayer.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsUserPlayer(fsUserPlayer);
    }

    /**
     * 批量删除用户游戏记录
     *
     * @param ids 需要删除的用户游戏记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserPlayerByIds(Long[] ids)
    {
        return baseMapper.deleteFsUserPlayerByIds(ids);
    }

    /**
     * 删除用户游戏记录信息
     *
     * @param id 用户游戏记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserPlayerById(Long id)
    {
        return baseMapper.deleteFsUserPlayerById(id);
    }

    @Override
    public FsUserPlayerVo selectFsUserByUserIdAndGameId(Long userId,Integer gameId) {
        if (userId == null || gameId == null) {
            return null;
        }
        FsUserPlayerVo fsUserPlayerVo = baseMapper.selectFsUserByUserIdAndGameId(userId, gameId);
        if(fsUserPlayerVo == null){
            //查询用户是否存在
            FsUser fsUser = fsUserMapper.selectFsUserByUserId(userId);
            if(fsUser == null){
                return null;
            }
            FsUserPlayer fsUserPlayer = new FsUserPlayer();
            fsUserPlayer.setGameId(gameId);
            fsUserPlayer.setUserId(userId);
            fsUserPlayer.setLevel(1); //等级
            fsUserPlayer.setStar("{}"); //星星数
            insertFsUserPlayer(fsUserPlayer);
            fsUserPlayerVo = baseMapper.selectFsUserByUserIdAndGameId(userId, gameId);
        }
        return fsUserPlayerVo;
    }

    @Override
    public int updateFsUserPlayerByUserIdAndGameId(FsUserPlayer fsUserPlayer) {
        fsUserPlayer.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsUserPlayerByUserIdAndGameId(fsUserPlayer);
    }
}
