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

优化初始化模型逻辑

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