更新账户更新请求DTO,添加用户类型、用户名和积分余额字段,并在AccountService中实现相应的验证和更新逻辑,确保用户名唯一性和积分余额的正确处理。
This commit is contained in:
@@ -2,15 +2,36 @@ package com.gameplatform.server.model.dto.account;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
@Schema(description = "账户更新请求")
|
||||
public class AccountUpdateRequest {
|
||||
@Schema(description = "用户类型", example = "AGENT", allowableValues = {"ADMIN", "AGENT"})
|
||||
@Pattern(regexp = "^(ADMIN|AGENT)$", message = "userType 只能是 ADMIN 或 AGENT")
|
||||
private String userType;
|
||||
|
||||
@Schema(description = "用户名", example = "newuser")
|
||||
@Size(min = 3, max = 64, message = "用户名长度必须在3-64字符之间")
|
||||
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字、下划线")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "账户状态", example = "ENABLED", allowableValues = {"ENABLED", "DISABLED"})
|
||||
@Pattern(regexp = "^(ENABLED|DISABLED)$", message = "status 只能是 ENABLED 或 DISABLED")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "积分余额(仅AGENT类型有效)", example = "100")
|
||||
private Long pointsBalance;
|
||||
|
||||
public String getUserType() { return userType; }
|
||||
public void setUserType(String userType) { this.userType = userType; }
|
||||
|
||||
public String getUsername() { return username; }
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
|
||||
public Long getPointsBalance() { return pointsBalance; }
|
||||
public void setPointsBalance(Long pointsBalance) { this.pointsBalance = pointsBalance; }
|
||||
}
|
||||
|
||||
|
||||
@@ -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