更新账户更新请求DTO,添加用户类型、用户名和积分余额字段,并在AccountService中实现相应的验证和更新逻辑,确保用户名唯一性和积分余额的正确处理。
This commit is contained in:
@@ -78,10 +78,48 @@ public class AccountService {
|
||||
return Mono.fromCallable(() -> {
|
||||
UserAccount db = mapper.findById(id);
|
||||
if (db == null) return null;
|
||||
|
||||
// 验证用户名唯一性(如果要更新用户名)
|
||||
if (req.getUsername() != null && !req.getUsername().equals(db.getUsername())) {
|
||||
UserAccount existing = mapper.findByUsername(req.getUsername());
|
||||
if (existing != null) {
|
||||
throw new IllegalArgumentException("用户名已存在");
|
||||
}
|
||||
}
|
||||
|
||||
UserAccount patch = new UserAccount();
|
||||
patch.setId(id);
|
||||
|
||||
patch.setStatus(req.getStatus());
|
||||
// 更新用户类型
|
||||
if (req.getUserType() != null) {
|
||||
String type = req.getUserType().toUpperCase();
|
||||
if (!type.equals("ADMIN") && !type.equals("AGENT")) {
|
||||
throw new IllegalArgumentException("userType 只能是 ADMIN 或 AGENT");
|
||||
}
|
||||
patch.setUserType(type);
|
||||
}
|
||||
|
||||
// 更新用户名
|
||||
if (req.getUsername() != null) {
|
||||
patch.setUsername(req.getUsername());
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
if (req.getStatus() != null) {
|
||||
patch.setStatus(req.getStatus());
|
||||
}
|
||||
|
||||
// 更新积分余额(仅对AGENT类型有效)
|
||||
if (req.getPointsBalance() != null) {
|
||||
String userType = req.getUserType() != null ? req.getUserType() : db.getUserType();
|
||||
if ("AGENT".equals(userType)) {
|
||||
patch.setPointsBalance(req.getPointsBalance());
|
||||
} else if ("ADMIN".equals(userType)) {
|
||||
// 管理员账户积分余额固定为0
|
||||
patch.setPointsBalance(0L);
|
||||
}
|
||||
}
|
||||
|
||||
mapper.update(patch);
|
||||
return mapper.findById(id);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user