优化标准数据模型.

This commit is contained in:
lijiahangmax
2024-06-12 00:21:25 +08:00
parent 3a5b84eec4
commit bfb80afee2
20 changed files with 205 additions and 224 deletions

View File

@@ -1,9 +1,8 @@
package com.orion.visor.framework.common.handler.data;
import com.alibaba.fastjson.JSON;
import com.orion.spring.SpringHolder;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy;
import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
/**
* 标准数据定义
@@ -12,45 +11,50 @@ import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy;
* @version 1.0.0
* @since 2023/12/21 0:07
*/
@SuppressWarnings("unchecked")
public interface GenericsDataDefinition {
/**
* 获取数据模型类型
*
* @return class
*/
Class<? extends GenericsDataModel> getModel();
/**
* 获取数据处理策略
*
* @return class
*/
Class<? extends MapDataStrategy<? extends GenericsDataModel>> getStrategy();
Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> getStrategyClass();
/**
* 获取数据模型策略处理器
*
* @param <Model> Model
* @param <Strategy> Strategy
* @return StrategyBean
* @param <M> Model
* @param <S> Strategy
* @return Strategy Bean
*/
@SuppressWarnings("unchecked")
default <Model extends GenericsDataModel, Strategy extends MapDataStrategy<Model>> Strategy getStrategyBean() {
return (Strategy) SpringHolder.getBean(this.getStrategy());
default <M extends GenericsDataModel, S extends GenericsDataStrategy<M>> S getStrategy() {
return (S) SpringHolder.getBean(this.getStrategyClass());
}
/**
* 反序列化对象
*
* @param json json
* @param <Model> Model
* @param serialModel serialModel
* @param <M> Model
* @return object
*/
@SuppressWarnings("unchecked")
default <Model extends GenericsDataModel> Model parse(String json) {
return (Model) JSON.parseObject(json, this.getModel());
default <M extends GenericsDataModel> M parse(String serialModel) {
return (M) this.getStrategy().parse(serialModel);
}
/**
* 转为视图对象
*
* @param serialModel serialModel
* @param <M> Model
* @return viewModel
*/
default <M extends GenericsDataModel> M toView(String serialModel) {
GenericsDataStrategy<GenericsDataModel> strategy = this.getStrategy();
GenericsDataModel model = strategy.parse(serialModel);
strategy.toView(model);
return (M) model;
}
}

View File

@@ -2,6 +2,8 @@ package com.orion.visor.framework.common.handler.data.model;
import com.alibaba.fastjson.JSON;
import java.util.Map;
/**
* 标准数据模型
*
@@ -20,4 +22,14 @@ public interface GenericsDataModel {
return JSON.toJSONString(this);
}
/**
* 转为 map
*
* @return map
*/
default Map<String, Object> toMap() {
return JSON.parseObject(this.serial());
}
}

View File

@@ -0,0 +1,65 @@
package com.orion.visor.framework.common.handler.data.strategy;
import com.alibaba.fastjson.JSON;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
/**
* 标准数据处理策略 基类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/6/11 21:44
*/
public abstract class AbstractGenericsDataStrategy<M extends GenericsDataModel> implements GenericsDataStrategy<M> {
protected final Class<M> modelClass;
public AbstractGenericsDataStrategy(Class<M> modelClass) {
this.modelClass = modelClass;
}
/**
* 更新填充
*
* @param beforeModel 修改前的配置
* @param afterModel 修改后的配置
*/
protected void updateFill(M beforeModel, M afterModel) {
}
/**
* 预校验参数
*
* @param model model
*/
protected void preValid(M model) {
}
/**
* 校验参数
*
* @param model model
*/
protected void valid(M model) {
}
@Override
public void doValid(M beforeModel, M afterModel) {
// 预校验参数
this.preValid(afterModel);
// 更新填充
this.updateFill(beforeModel, afterModel);
// 校验参数
this.valid(afterModel);
}
@Override
public M parse(String serialModel) {
return JSON.parseObject(serialModel, modelClass);
}
@Override
public void toView(M model) {
}
}

View File

@@ -9,60 +9,38 @@ import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
* @version 1.0.0
* @since 2023/12/20 22:09
*/
public interface GenericsDataStrategy<Model extends GenericsDataModel, View> {
public interface GenericsDataStrategy<M extends GenericsDataModel> {
/**
* 获取默认值
*
* @return 默认值
*/
Model getDefault();
/**
* 更新填充
*
* @param beforeModel 修改前的配置
* @param afterModel 修改后的配置
*/
void updateFill(Model beforeModel, Model afterModel);
/**
* 预校验参数
*
* @param model model
*/
void preValid(Model model);
/**
* 校验参数
*
* @param model model
*/
void valid(Model model);
M getDefault();
/**
* 执行完整验证链
* <p>
* preValid > updateFill > preValid
* preValid > updateFill > valid
*
* @param beforeModel beforeModel
* @param afterModel afterModel
*/
default void doValidChain(Model beforeModel, Model afterModel) {
// 预校验参数
this.preValid(afterModel);
// 更新填充
this.updateFill(beforeModel, afterModel);
// 校验参数
this.valid(afterModel);
}
void doValid(M beforeModel, M afterModel);
/**
* 解析数据
*
* @param serialModel serialModel
* @return model
*/
M parse(String serialModel);
/**
* 转为视图配置
*
* @param model model
* @return 视图配置
*/
View toView(String model);
void toView(M model);
}

View File

@@ -1,22 +0,0 @@
package com.orion.visor.framework.common.handler.data.strategy;
import com.alibaba.fastjson.JSONObject;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import java.util.Map;
/**
* map 数据处理策略
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/12/20 22:11
*/
public interface MapDataStrategy<Model extends GenericsDataModel> extends GenericsDataStrategy<Model, Map<String, Object>> {
@Override
default Map<String, Object> toView(String model) {
return JSONObject.parseObject(model);
}
}