Refactor user account management by replacing User entity with UserAccount, updating UserController and UserService for CRUD operations, and modifying MyBatis mappers accordingly.
This commit is contained in:
@@ -1,54 +1,92 @@
|
||||
package com.gameplatform.server.service;
|
||||
|
||||
import com.gameplatform.server.mapper.UserMapper;
|
||||
import com.gameplatform.server.model.User;
|
||||
import com.gameplatform.server.mapper.account.UserAccountMapper;
|
||||
import com.gameplatform.server.model.dto.account.AccountResponse;
|
||||
import com.gameplatform.server.model.dto.common.PageResult;
|
||||
import com.gameplatform.server.model.entity.account.UserAccount;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户服务类 - 基于UserAccount实体
|
||||
* 提供用户账户相关的业务逻辑
|
||||
*/
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserMapper userMapper;
|
||||
private final UserAccountMapper userAccountMapper;
|
||||
|
||||
public UserService(UserMapper userMapper) {
|
||||
this.userMapper = userMapper;
|
||||
public UserService(UserAccountMapper userAccountMapper) {
|
||||
this.userAccountMapper = userAccountMapper;
|
||||
}
|
||||
|
||||
public Mono<User> getById(Long id) {
|
||||
return Mono.fromCallable(() -> userMapper.findById(id))
|
||||
/**
|
||||
* 根据ID获取用户账户
|
||||
*/
|
||||
public Mono<AccountResponse> getById(Long id) {
|
||||
return Mono.fromCallable(() -> userAccountMapper.findById(id))
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.filter(Objects::nonNull)
|
||||
.map(this::toAccountResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名获取用户账户
|
||||
*/
|
||||
public Mono<UserAccount> getByUsername(String username) {
|
||||
return Mono.fromCallable(() -> userAccountMapper.findByUsername(username))
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
public Flux<User> listAll() {
|
||||
return Mono.fromCallable(userMapper::findAll)
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.flatMapMany(Flux::fromIterable);
|
||||
}
|
||||
|
||||
public Mono<User> create(User user) {
|
||||
/**
|
||||
* 分页查询用户列表
|
||||
*/
|
||||
public Mono<PageResult<AccountResponse>> list(String userType, String status, String role, String keyword,
|
||||
Integer page, Integer size) {
|
||||
int p = (page == null || page < 1) ? 1 : page;
|
||||
int s = (size == null || size < 1 || size > 200) ? 20 : size;
|
||||
int offset = (p - 1) * s;
|
||||
|
||||
return Mono.fromCallable(() -> {
|
||||
userMapper.insert(user);
|
||||
return user;
|
||||
long total = userAccountMapper.countByFilter(userType, status, role, keyword);
|
||||
List<UserAccount> list = userAccountMapper.listByFilter(userType, status, role, keyword, s, offset);
|
||||
List<AccountResponse> items = list.stream()
|
||||
.map(this::toAccountResponse)
|
||||
.collect(Collectors.toList());
|
||||
return new PageResult<>(items, total, p, s);
|
||||
})
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
public Mono<Boolean> deleteById(Long id) {
|
||||
return Mono.fromCallable(() -> userMapper.deleteById(id) > 0)
|
||||
/**
|
||||
* 检查用户名是否存在
|
||||
*/
|
||||
public Mono<Boolean> existsByUsername(String username) {
|
||||
return Mono.fromCallable(() -> userAccountMapper.findByUsername(username) != null)
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
public Mono<User> update(Long id, User user) {
|
||||
return Mono.fromCallable(() -> {
|
||||
user.setId(id);
|
||||
int n = userMapper.update(user);
|
||||
return n;
|
||||
})
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.flatMap(n -> n > 0 ? getById(id) : Mono.empty());
|
||||
/**
|
||||
* 将UserAccount实体转换为AccountResponse DTO
|
||||
*/
|
||||
private AccountResponse toAccountResponse(UserAccount account) {
|
||||
if (account == null) return null;
|
||||
|
||||
AccountResponse response = new AccountResponse();
|
||||
response.setId(account.getId());
|
||||
response.setUserType(account.getUserType());
|
||||
response.setUsername(account.getUsername());
|
||||
response.setDisplayName(account.getDisplayName());
|
||||
response.setRole(account.getRole());
|
||||
response.setStatus(account.getStatus());
|
||||
response.setPointsBalance(account.getPointsBalance());
|
||||
response.setCreatedAt(account.getCreatedAt());
|
||||
response.setUpdatedAt(account.getUpdatedAt());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user