提交 125a5084 编写于 作者: zhouweidong's avatar zhouweidong

优化初始化模型逻辑

上级 8c790a3a
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaField;
import cn.ibizlab.core.lite.domain.MetaModel;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import cn.ibizlab.core.lite.extensions.model.DataModel;
import cn.ibizlab.core.lite.extensions.model.LayerMapping;
import cn.ibizlab.core.lite.extensions.model.Property;
import cn.ibizlab.core.lite.extensions.model.PropertyMapping;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
import cn.ibizlab.core.lite.service.IMetaModelService;
import cn.ibizlab.core.lite.service.IMetaRelationshipService;
import cn.ibizlab.core.lite.service.impl.MetaEntityServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import liquibase.pro.packaged.D;
import liquibase.pro.packaged.M;
import lombok.extern.slf4j.Slf4j;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
import org.bouncycastle.pqc.crypto.rainbow.Layer;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.util.StringUtils;
import java.util.LinkedHashSet;
import java.util.List;
/**
* 实体[实体] 自定义服务对象
......@@ -38,6 +32,8 @@ import java.util.stream.Collectors;
@Service("MetaEntityExService")
public class MetaEntityExService extends MetaEntityServiceImpl {
private final int loop = 2;
@Override
protected Class currentModelClass() {
return com.baomidou.mybatisplus.core.toolkit.ReflectionKit.getSuperClassGenericType(this.getClass().getSuperclass(), 1);
......@@ -45,6 +41,7 @@ public class MetaEntityExService extends MetaEntityServiceImpl {
/**
* [GetDefaultModel:GetDefaultModel] 行为扩展
*
* @param et
* @return
*/
......@@ -58,156 +55,162 @@ public class MetaEntityExService extends MetaEntityServiceImpl {
@Lazy
private LiteModelService liteModelService;
@Autowired
@Lazy
private IMetaRelationshipService metaRelationshipService;
@Autowired
@Lazy
private IMetaModelService metaModelService;
@Override
public boolean update(MetaEntity et)
{
if(!super.update(et))
public boolean update(MetaEntity et) {
if (!super.update(et))
return false;
liteModelService.resetEntityModel(et.getSystemId(), et.getEntityName());
return true;
}
@Override
public MetaEntity initModels(MetaEntity et) {
todoBaseModel(baseMapper.selectById(et.getEntityId()));
et = get(et.getEntityId());
DataModel dm = getDataModel(et, loop, null);
String json = JSON.toJSONString(dm);
JSONObject modelCfg = JSON.parseObject(json);
modelCfg.remove("dataModelId");
MetaModel model = new MetaModel();
model.setId(et.getEntityId());
model.setName(et.getEntityName());
model.setCodeName(et.getCodeName());
model.setSystemId(et.getSystemId());
model.setConfig(modelCfg.toJSONString());
metaModelService.save(model);
return et;
}
/**
* 根据所选实体执行生成主模型
*
* @param et 所选实体
* @return 执行结果
* 获取实体模型结构
* @param et 实体
* @param loop 关系嵌套层级(默认只取2层),如:order --> orderDetail
* @param parent 父关系
* @return
*/
@Transactional
public Boolean todoBaseModel(MetaEntity et) {
DataModel dm = new DataModel();
dm.setDataModelName(et.getEntityName());
dm.setObjectProperties(findParRelation(et));
dm.setNestedDataModels(assembleModel(et));
String json = JSON.toJSONString(dm);
JSONObject jsonObject = JSON.parseObject(json);
jsonObject.remove("dataModelId");
MetaModel mm = new MetaModel();
mm.setName(et.getTableName() + "_模型:" + UUID.randomUUID().toString().substring(0, 5).toUpperCase());
mm.setCodeName(et.getTableName());
mm.setConfig(jsonObject.toJSONString());
mm.setSystemId(et.getSystemId());
return metaModelService.create(mm);
private DataModel getDataModel(MetaEntity et, int loop, MetaRelationship parent) {
DataModel model = new DataModel();
model.setDataModelName(et.getEntityName());
if (loop > 0) {
loop--;
model.setObjectProperties(getParentModel(et)); //获取主
model.setNestedDataModels(getNestedModel(et, loop)); //获取从
}
if (!ObjectUtils.isEmpty(parent)) {
model.setLayerMappings(getMapping(et, parent));//获取关系
}
return model;
}
/**
* 通过传入一个实体对象,生成对应的主从关系
*
* @param et 实体对象
* @return 主从关系组
* 获取子关系模型
* @param et
* @param loop
* @return
*/
public LinkedHashSet<Property> findParRelation(MetaEntity et) {
Property property;
Property temp;
MetaEntity parEntity;
PropertyMapping propertyMapping;
LinkedHashSet<PropertyMapping> propertyMappingSet;
LinkedHashSet<Property> parPropertyList = new LinkedHashSet<>();
// 查询所有从关系
List<MetaRelationship> parList = metaRelationshipService.selectByEntityId(et.getEntityId());
if (!ObjectUtils.isEmpty(parList)) {
List<String> refIds = parList.stream().map(MetaRelationship::getRefEntityId).collect(Collectors.toList());
for (int i = 0; i < refIds.size(); i++) {
parEntity = baseMapper.selectById(refIds.get(i));
private LinkedHashSet<DataModel> getNestedModel(MetaEntity et, int loop) {
LinkedHashSet<DataModel> models = new LinkedHashSet<>();
//主关系
List<MetaRelationship> subList = et.getSubEntitys();
if (!ObjectUtils.isEmpty(subList)) {
for (MetaRelationship relation : subList) {
DataModel model = null;
MetaEntity subEntity = relation.getEntity();
if (!ObjectUtils.isEmpty(subEntity)) {
model = getDataModel(subEntity, loop, relation);
models.add(model);
}
models.add(model);
}
}
return models;
}
/**
* 获取父关系模型
* @param et
* @return
*/
public LinkedHashSet<Property> getParentModel(MetaEntity et) {
LinkedHashSet<Property> properties = new LinkedHashSet<>();
List<MetaField> fields = et.getFields();
List<MetaRelationship> parentList = et.getParentEntitys();
if (!ObjectUtils.isEmpty(parentList)) {
Property master = new Property();
master.setPropertyName(et.getEntityName());
master.setSystem(et.getSystemId());
master.setPropertyEntity(et.getEntityName());
properties.add(master);
for (MetaRelationship relation : parentList) {
MetaField fkField = getForeignkey(fields, relation);
if (fkField == null) {
log.error("未能找到实体关系{}对应的外键属性", relation.getName());
continue;
}
// 存储对象参数集合
property = new Property();
propertyMapping = new PropertyMapping();
property.setPropertyName(parEntity.getEntityName() == null ? "" : parEntity.getEntityName());
property.setSystem(parEntity.getSystemId() == null ? "" : parEntity.getSystemId());
property.setPropertyEntity(parEntity.getTableName() == null ? "" : parEntity.getTableName());
Property property = new Property();
property.setPropertyName(relation.getRefEntityName() == null ? "" : relation.getRefEntityName());
property.setSystem(et.getSystemId() == null ? "" : et.getSystemId());
property.setPropertyEntity(relation.getRefEntityName() == null ? "" : relation.getRefEntityName());
// 对应存储相同实体id关系
propertyMapping.setSelfPropertyColumn(parList.get(i).getCodeName() == null ? "" : parList.get(i).getCodeName());
propertyMapping.setJoinPropertyName(parList.get(i).getEntityName() == null ? "" : parList.get(i).getEntityName());
propertyMapping.setJoinPropertyColumn(parList.get(i).getCodeName() == null ? "" : parList.get(i).getCodeName());
// 添加主实体在集合头部
if (i == 0) {
temp = new Property();
temp.setPropertyName(et.getEntityName());
temp.setSystem(et.getSystemId());
temp.setPropertyEntity(et.getTableName());
parPropertyList.add(temp);
}
propertyMappingSet = new LinkedHashSet<>();
parPropertyList.add(property);
propertyMappingSet.add(propertyMapping);
property.setPropertyMappings(propertyMappingSet);
PropertyMapping mapping = new PropertyMapping();
mapping.setSelfPropertyColumn(fkField.getRefFieldName() == null ? "" : fkField.getRefFieldName());
mapping.setJoinPropertyName(relation.getEntityName() == null ? "" : relation.getEntityName());
mapping.setJoinPropertyColumn(fkField.getFieldName() == null ? "" : fkField.getFieldName());
property.setPropertyMappings(new LinkedHashSet<PropertyMapping>() {{
add(mapping);
}});
properties.add(property);
}
}
return parPropertyList;
return properties;
}
/**
* 传入一个对象,返回一个数据模型Set集合
*
* @param et 实体对象
* @return 数据模型
* 获取父关系映射
* @param et
* @param relation
* @return
*/
public DataModel findSubEntityRelation(MetaEntity et, String son, String parent) {
DataModel dm = new DataModel();
LinkedHashSet<LayerMapping> layerMappings = new LinkedHashSet<>();
LayerMapping layerMapping = new LayerMapping();
layerMapping.setSelfPropertyColumn(son);
layerMapping.setParentPropertyColumn(parent);
layerMappings.add(layerMapping);
dm.setDataModelName(et.getEntityName());
dm.setObjectProperties(findParRelation(et));
// 添加递归在此
dm.setNestedDataModels(null);
dm.setLayerMappings(layerMappings);
return dm;
private LinkedHashSet<LayerMapping> getMapping(MetaEntity et, MetaRelationship relation) {
LinkedHashSet<LayerMapping> mappings = new LinkedHashSet<>();
List<MetaField> fields = et.getFields();
if (!ObjectUtils.isEmpty(fields)) {
MetaField fkField = getForeignkey(fields, relation);
if (fkField == null) {
log.error("未能找到实体关系{}对应的外键属性", relation.getName());
return mappings;
}
LayerMapping mapping = new LayerMapping();
mapping.setSelfPropertyColumn(fkField.getFieldName());
mapping.setParentPropertyColumn(fkField.getRefFieldName());
mappings.add(mapping);
}
return mappings;
}
/**
* 组合成一个主从模型
* 获取外键属性
*
* @return 数据模型Set集合
* @param fields
* @param relation
* @return
*/
public LinkedHashSet<DataModel> assembleModel(MetaEntity et) {
List<MetaEntity> subEntityList;
LinkedHashSet<DataModel> dataModels = new LinkedHashSet<>();
List<MetaRelationship> subList = metaRelationshipService.selectByRefEntityId(et.getEntityId());
if (!ObjectUtils.isEmpty(subList)) {
List<String> ids = subList.stream().map(MetaRelationship::getEntityId).collect(Collectors.toList());
subEntityList = baseMapper.selectBatchIds(ids);
for (MetaEntity metaEntity : subEntityList) {
for (int i = 0; i < subList.size(); i++) {
if (metaEntity.getEntityId().equals(subList.get(i).getEntityId())) {
dataModels.add(findSubEntityRelation(metaEntity, subList.get(i).getCodeName(), et.getCodeName()));
}
}
private MetaField getForeignkey(List<MetaField> fields, MetaRelationship relation) {
for (MetaField field : fields) {
String relationName = field.getRelationName();
if (!StringUtils.isEmpty(relationName) && relationName.equalsIgnoreCase(relation.getName()) && "PICKUP".equalsIgnoreCase(field.getFieldType())) {
return field;
}
}
return dataModels;
return null;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册