2026-06-15 18:23:41 +08:00
|
|
|
package com.webgame.webgamebackend.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
2026-06-22 10:09:40 +08:00
|
|
|
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
2026-06-15 18:23:41 +08:00
|
|
|
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
|
|
|
|
import com.webgame.webgamebackend.common.exception.BusinessException;
|
|
|
|
|
import com.webgame.webgamebackend.common.exception.ErrorCode;
|
|
|
|
|
import com.webgame.webgamebackend.entities.UserEntity;
|
|
|
|
|
import com.webgame.webgamebackend.repository.UserRepository;
|
|
|
|
|
import com.webgame.webgamebackend.service.UserService;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2026-06-22 10:09:40 +08:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2026-06-15 18:23:41 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户服务实现
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public UserInfoResponse getCurrentUserInfo() {
|
|
|
|
|
String accountId = StpUtil.getLoginIdAsString();
|
2026-06-16 18:20:11 +08:00
|
|
|
log.info("[用户服务] 获取当前登录用户信息, accountId: {}", accountId);
|
2026-06-15 18:23:41 +08:00
|
|
|
UserEntity user = userRepository.findByAccountId(accountId)
|
|
|
|
|
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
|
|
|
|
return UserInfoResponse.from(user);
|
|
|
|
|
}
|
2026-06-22 10:09:40 +08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional
|
|
|
|
|
public UserInfoResponse updateUserInfo(String accountId, UpdateUserInfoRequest request) {
|
|
|
|
|
UserEntity user = userRepository.findByAccountId(accountId)
|
|
|
|
|
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
|
|
|
|
|
|
|
|
|
// 仅更新非 null 字段
|
|
|
|
|
if (request.nickName() != null) {
|
|
|
|
|
user.setNickName(request.nickName());
|
|
|
|
|
}
|
|
|
|
|
if (request.avatar() != null) {
|
|
|
|
|
user.setAvatar(request.avatar());
|
|
|
|
|
}
|
|
|
|
|
if (request.signature() != null) {
|
|
|
|
|
user.setSignature(request.signature());
|
|
|
|
|
}
|
|
|
|
|
if (request.age() != null) {
|
|
|
|
|
user.setAge(request.age());
|
|
|
|
|
}
|
|
|
|
|
if (request.sex() != null) {
|
|
|
|
|
user.setSex(request.sex());
|
|
|
|
|
}
|
|
|
|
|
if (request.birthday() != null) {
|
|
|
|
|
user.setBirthday(request.birthday());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userRepository.save(user);
|
|
|
|
|
log.info("[用户服务] 更新用户信息, accountId={}", accountId);
|
|
|
|
|
return UserInfoResponse.from(user);
|
|
|
|
|
}
|
2026-06-15 18:23:41 +08:00
|
|
|
}
|