调整:优化代码
This commit is contained in:
@@ -210,9 +210,15 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
||||
/**
|
||||
* 转换参数,组装数据
|
||||
*/
|
||||
private void invokerTransform(CrudOption<T> option, List list) {
|
||||
private void invokerTransform(CrudOption<T> option, Object obj) {
|
||||
if (ObjUtil.isNotEmpty(option.getTransform())) {
|
||||
option.getTransform().apply(list);
|
||||
if (obj instanceof List) {
|
||||
((List)obj).forEach(o -> {
|
||||
option.getTransform().apply(o);
|
||||
});
|
||||
} else {
|
||||
option.getTransform().apply(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.cool.core.exception;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -15,20 +17,33 @@ public class CoolPreconditions {
|
||||
*/
|
||||
public static void check(boolean flag, int code, String message, Object... arguments) {
|
||||
if (flag) {
|
||||
throw new CoolException(formatMessage(message, arguments), code);
|
||||
throw getCoolException(message, code, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
public static void check(boolean flag, String message, Object... arguments) {
|
||||
if (flag) {
|
||||
throw new CoolException(formatMessage(message, arguments));
|
||||
throw getCoolException(message, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
public static void alwaysThrow(String message, Object... arguments) {
|
||||
throw new CoolException(formatMessage(message, arguments));
|
||||
throw getCoolException(message, arguments);
|
||||
}
|
||||
|
||||
private static CoolException getCoolException(String message, Object... arguments) {
|
||||
Optional<Object> first = Arrays.stream(arguments).filter(o -> o instanceof Throwable)
|
||||
.findFirst();
|
||||
return new CoolException(formatMessage(message, arguments), (Throwable) first.orElse(null));
|
||||
}
|
||||
|
||||
private static CoolException getCoolException(String message, int code, Object... arguments) {
|
||||
Optional<Object> first = Arrays.stream(arguments).filter(o -> o instanceof Throwable)
|
||||
.findFirst();
|
||||
return new CoolException(formatMessage(message, arguments), code, (Throwable) first.orElse(null));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回data
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.cool.core.enums.QueryModeEnum;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
@@ -36,10 +37,10 @@ public class CrudOption<T> {
|
||||
|
||||
private QueryModeEnum queryModeEnum;
|
||||
|
||||
private Transform<List> transform;
|
||||
private Transform<Object> transform;
|
||||
|
||||
public interface Transform<List> {
|
||||
void apply(List list);
|
||||
public interface Transform<B> {
|
||||
void apply(B obj);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,7 +114,7 @@ public class CrudOption<T> {
|
||||
/**
|
||||
* 转换参数,组装数据
|
||||
*/
|
||||
public CrudOption<T> transform(Transform<List> transform) {
|
||||
public CrudOption<T> transform(Transform<Object> transform) {
|
||||
this.transform = transform;
|
||||
return this;
|
||||
}
|
||||
@@ -125,8 +126,19 @@ public class CrudOption<T> {
|
||||
*/
|
||||
private QueryWrapper build(QueryWrapper queryWrapper, Class<T> entityClass) {
|
||||
if (ObjectUtil.isNotEmpty(fieldEq)) {
|
||||
Arrays.stream(fieldEq).toList().forEach(filed -> queryWrapper.and(
|
||||
filed.eq(requestParams.get(StrUtil.toCamelCase(filed.getName())))));
|
||||
Arrays.stream(fieldEq).toList().forEach(filed -> {
|
||||
Object obj = requestParams.get(StrUtil.toCamelCase(filed.getName()));
|
||||
if (ObjUtil.isEmpty(obj)) {
|
||||
return;
|
||||
}
|
||||
if (obj instanceof JSONArray) {
|
||||
// 集合
|
||||
queryWrapper.and(filed.in(((JSONArray)obj).toArray()));
|
||||
} else {
|
||||
// 对象
|
||||
queryWrapper.and(filed.eq(obj));
|
||||
}
|
||||
});
|
||||
}
|
||||
Object keyWord = requestParams.get("keyWord");
|
||||
if (ObjectUtil.isNotEmpty(this.keyWordLikeFields) && ObjectUtil.isNotEmpty(keyWord)) {
|
||||
|
||||
Reference in New Issue
Block a user