This commit is contained in:
牛子源 2025-05-22 13:34:37 +08:00
parent 5b1fcbaf57
commit 997c5dd5c9
1 changed files with 35 additions and 28 deletions

View File

@ -14,24 +14,29 @@ public abstract class BaseInputModel {
public Map<String, Object> buildToMap() {
Map<String, Object> map = new HashMap<>();
// 遍历所有字段包含父类
Class<?> clazz = this.getClass();
while (clazz != null) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
try {
Object value = field.get(this);
Object value = field.get(this); // 检查RequiredField注解
String mapKey = null;
if (field.isAnnotationPresent(IMField.class)) {
IMField annotation = field.getAnnotation(IMField.class);
mapKey = annotation.key();
if (annotation.required() && (value == null || (value instanceof String && ((String) value).isEmpty()))) {
throw new MessageException("[" + annotation.key() + ":" + annotation.name() + "] 不能为空");
if (annotation.required()) {
if (value == null || value.toString().isEmpty()) {
throw new MessageException("[" + annotation.key() + ":"+annotation.name()+"]不能为空");
}
}
if (mapKey != null) {
// 处理 BaseInputModel 子类
}
if(mapKey == null){
continue;
}
if (value instanceof BaseInputModel) {
map.put(mapKey, ((BaseInputModel) value).buildToMap());
}
@ -48,11 +53,14 @@ public abstract class BaseInputModel {
}
map.put(mapKey, mappedList);
}
// 其他非空值直接放入
else if (value != null && !(value instanceof String || value.toString().isEmpty())) {
else if (value == null || !value.toString().isEmpty()){
map.put(mapKey, value);
}else{
map.put(mapKey, value);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
@ -61,5 +69,4 @@ public abstract class BaseInputModel {
}
return map;
}
}