feat: 新增查询接口入参转换方法
This commit is contained in:
@@ -165,8 +165,9 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
protected R<T> info(@RequestAttribute() JSONObject requestParams,
|
protected R<T> info(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestParam() Long id,
|
@RequestParam() Long id,
|
||||||
@RequestAttribute(COOL_INFO_OP) CrudOption<T> option) {
|
@RequestAttribute(COOL_INFO_OP) CrudOption<T> option) {
|
||||||
|
invokerTransformParam(option, requestParams);
|
||||||
T info = (T) service.info(requestParams, id);
|
T info = (T) service.info(requestParams, id);
|
||||||
invokerTransform(option, info);
|
invokerTransformValue(option, info);
|
||||||
return R.ok(info);
|
return R.ok(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,13 +180,14 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
@PostMapping("/list")
|
@PostMapping("/list")
|
||||||
protected R<List<T>> list(@RequestAttribute() JSONObject requestParams,
|
protected R<List<T>> list(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestAttribute(COOL_LIST_OP) CrudOption<T> option) {
|
@RequestAttribute(COOL_LIST_OP) CrudOption<T> option) {
|
||||||
|
invokerTransformParam(option, requestParams);
|
||||||
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
||||||
List list = (List) switch (queryModeEnum) {
|
List list = (List) switch (queryModeEnum) {
|
||||||
case ENTITY_WITH_RELATIONS -> service.listWithRelations(requestParams, option.getQueryWrapper(currentEntityClass()));
|
case ENTITY_WITH_RELATIONS -> service.listWithRelations(requestParams, option.getQueryWrapper(currentEntityClass()));
|
||||||
case CUSTOM -> transformList(service.list(requestParams, option.getQueryWrapper(currentEntityClass()), option.getAsType()), option.getAsType());
|
case CUSTOM -> transformList(service.list(requestParams, option.getQueryWrapper(currentEntityClass()), option.getAsType()), option.getAsType());
|
||||||
default -> service.list(requestParams, option.getQueryWrapper(currentEntityClass()));
|
default -> service.list(requestParams, option.getQueryWrapper(currentEntityClass()));
|
||||||
};
|
};
|
||||||
invokerTransform(option, list);
|
invokerTransformValue(option, list);
|
||||||
return R.ok(list);
|
return R.ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,6 +200,7 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
@PostMapping("/page")
|
@PostMapping("/page")
|
||||||
protected R<PageResult<T>> page(@RequestAttribute() JSONObject requestParams,
|
protected R<PageResult<T>> page(@RequestAttribute() JSONObject requestParams,
|
||||||
@RequestAttribute(COOL_PAGE_OP) CrudOption<T> option) {
|
@RequestAttribute(COOL_PAGE_OP) CrudOption<T> option) {
|
||||||
|
invokerTransformParam(option, requestParams);
|
||||||
Integer page = requestParams.getInt("page", 1);
|
Integer page = requestParams.getInt("page", 1);
|
||||||
Integer size = requestParams.getInt("size", 20);
|
Integer size = requestParams.getInt("size", 20);
|
||||||
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
QueryModeEnum queryModeEnum = option.getQueryModeEnum();
|
||||||
@@ -207,25 +210,34 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
|
|||||||
default -> service.page(requestParams, new Page<>(page, size), option.getQueryWrapper(currentEntityClass()));
|
default -> service.page(requestParams, new Page<>(page, size), option.getQueryWrapper(currentEntityClass()));
|
||||||
};
|
};
|
||||||
Page pageResult = (Page) obj;
|
Page pageResult = (Page) obj;
|
||||||
invokerTransform(option, pageResult.getRecords());
|
invokerTransformValue(option, pageResult.getRecords());
|
||||||
return R.ok(pageResult(pageResult));
|
return R.ok(pageResult(pageResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换参数,组装数据
|
* 转换值,组装数据
|
||||||
*/
|
*/
|
||||||
private void invokerTransform(CrudOption<T> option, Object obj) {
|
private void invokerTransformValue(CrudOption<T> option, Object obj) {
|
||||||
if (ObjUtil.isNotEmpty(option.getTransform())) {
|
if (ObjUtil.isNotNull(option.getTransformValue())) {
|
||||||
if (obj instanceof List) {
|
if (obj instanceof List) {
|
||||||
((List)obj).forEach(o -> {
|
((List)obj).forEach(o -> {
|
||||||
option.getTransform().apply(o);
|
option.getTransformValue().apply(o);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
option.getTransform().apply(obj);
|
option.getTransformValue().apply(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换入参
|
||||||
|
*/
|
||||||
|
private void invokerTransformParam(CrudOption<T> option, JSONObject obj) {
|
||||||
|
if (ObjUtil.isNotNull(option.getTransformParam())) {
|
||||||
|
option.getTransformParam().apply(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页结果
|
* 分页结果
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -38,12 +38,18 @@ public class CrudOption<T> {
|
|||||||
|
|
||||||
private QueryModeEnum queryModeEnum;
|
private QueryModeEnum queryModeEnum;
|
||||||
|
|
||||||
private Transform<Object> transform;
|
private TransformValue<Object> transformValue;
|
||||||
|
|
||||||
public interface Transform<B> {
|
private TransformParam<Object> transformParam;
|
||||||
|
|
||||||
|
public interface TransformValue<B> {
|
||||||
void apply(B obj);
|
void apply(B obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface TransformParam<JSONObject> {
|
||||||
|
void apply(JSONObject obj);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* queryModeEnum 为 CUSTOM,可设置 默认为Map
|
* queryModeEnum 为 CUSTOM,可设置 默认为Map
|
||||||
*/
|
*/
|
||||||
@@ -113,10 +119,27 @@ public class CrudOption<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换参数,组装数据
|
* 转换值,组装数据
|
||||||
*/
|
*/
|
||||||
public CrudOption<T> transform(Transform<Object> transform) {
|
public CrudOption<T> transformValue(TransformValue<Object> transformValue) {
|
||||||
this.transform = transform;
|
this.transformValue = transformValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请使用 transformValue
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public CrudOption<T> transform(TransformValue<Object> transformValue) {
|
||||||
|
this.transformValue = transformValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换入参
|
||||||
|
*/
|
||||||
|
public CrudOption<T> transformParam(TransformParam<Object> transformParam) {
|
||||||
|
this.transformParam = transformParam;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class AdminBaseSysMenuController extends
|
|||||||
@Override
|
@Override
|
||||||
protected void init(HttpServletRequest request, JSONObject requestParams) {
|
protected void init(HttpServletRequest request, JSONObject requestParams) {
|
||||||
CrudOption<BaseSysMenuEntity> transform = createOp()
|
CrudOption<BaseSysMenuEntity> transform = createOp()
|
||||||
.transform(o -> {
|
.transformValue(o -> {
|
||||||
BaseSysMenuEntity entity = (BaseSysMenuEntity) o;
|
BaseSysMenuEntity entity = (BaseSysMenuEntity) o;
|
||||||
entity.setName(I18nUtil.getI18nMenu(entity.getName()));
|
entity.setName(I18nUtil.getI18nMenu(entity.getName()));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ public class AdminDictInfoController extends BaseController<DictInfoService, Dic
|
|||||||
setListOption(createOp().fieldEq(DictInfoEntityTableDef.DICT_INFO_ENTITY.TYPE_ID)
|
setListOption(createOp().fieldEq(DictInfoEntityTableDef.DICT_INFO_ENTITY.TYPE_ID)
|
||||||
.keyWordLikeFields(DictInfoEntityTableDef.DICT_INFO_ENTITY.NAME)
|
.keyWordLikeFields(DictInfoEntityTableDef.DICT_INFO_ENTITY.NAME)
|
||||||
.queryWrapper(QueryWrapper.create().orderBy(DictInfoEntityTableDef.DICT_INFO_ENTITY.CREATE_TIME, false))
|
.queryWrapper(QueryWrapper.create().orderBy(DictInfoEntityTableDef.DICT_INFO_ENTITY.CREATE_TIME, false))
|
||||||
.transform(o -> {
|
.transformValue(o -> {
|
||||||
DictInfoEntity entity = (DictInfoEntity) o;
|
DictInfoEntity entity = (DictInfoEntity) o;
|
||||||
entity.setName(I18nUtil.getI18nDictInfo(entity.getName()));
|
entity.setName(I18nUtil.getI18nDictInfo(entity.getName()));
|
||||||
}));
|
}));
|
||||||
CrudOption<DictInfoEntity> transform = createOp().transform(o -> {
|
CrudOption<DictInfoEntity> transform = createOp().transformValue(o -> {
|
||||||
DictInfoEntity entity = (DictInfoEntity) o;
|
DictInfoEntity entity = (DictInfoEntity) o;
|
||||||
entity.setName(I18nUtil.getI18nDictInfo(entity.getName()));
|
entity.setName(I18nUtil.getI18nDictInfo(entity.getName()));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ public class AdminDictTypeController extends BaseController<DictTypeService, Dic
|
|||||||
@Override
|
@Override
|
||||||
protected void init(HttpServletRequest request, JSONObject requestParams) {
|
protected void init(HttpServletRequest request, JSONObject requestParams) {
|
||||||
setPageOption(
|
setPageOption(
|
||||||
createOp().select(DICT_TYPE_ENTITY.ID, DICT_TYPE_ENTITY.KEY, DICT_TYPE_ENTITY.NAME).transform(o -> {
|
createOp().select(DICT_TYPE_ENTITY.ID, DICT_TYPE_ENTITY.KEY, DICT_TYPE_ENTITY.NAME).transformValue(o -> {
|
||||||
DictTypeEntity entity = (DictTypeEntity) o;
|
DictTypeEntity entity = (DictTypeEntity) o;
|
||||||
entity.setName(I18nUtil.getI18nDictType(entity.getName()));
|
entity.setName(I18nUtil.getI18nDictType(entity.getName()));
|
||||||
}));
|
}));
|
||||||
CrudOption<DictTypeEntity> transform = createOp().transform(o -> {
|
CrudOption<DictTypeEntity> transform = createOp().transformValue(o -> {
|
||||||
DictTypeEntity entity = (DictTypeEntity) o;
|
DictTypeEntity entity = (DictTypeEntity) o;
|
||||||
entity.setName(I18nUtil.getI18nDictType(entity.getName()));
|
entity.setName(I18nUtil.getI18nDictType(entity.getName()));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user