提交 005ca75b 编写于 作者: tangyaologin's avatar tangyaologin

通过点击实体生成相应模型

上级 c17c7732
package cn.ibizlab.core.extensions.service;
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.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 cn.ibizlab.core.lite.extensions.util.LiteStorage;
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.util.ObjectUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* 实体[实体] 自定义服务对象
......@@ -41,6 +58,14 @@ 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)
{
......@@ -49,5 +74,133 @@ public class MetaEntityExService extends MetaEntityServiceImpl {
liteModelService.resetEntityModel(et.getSystemId(), et.getEntityName());
return true;
}
@Override
public MetaEntity initModels(MetaEntity et) {
todoBaseModel(baseMapper.selectById(et.getEntityId()));
return et;
}
/**
* 根据所选实体执行生成主模型
*
* @param et 所选实体
* @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);
}
/**
* 通过传入一个实体对象,生成对应的主从关系
*
* @param et 实体对象
* @return 主从关系组
*/
public LinkedHashSet<Property> findParRelation(MetaEntity et) {
Property property;
List<MetaEntity> parEntityList;
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());
parEntityList = baseMapper.selectBatchIds(refIds);
for (int i = 0; i < parEntityList.size(); i++) {
// 存储对象参数集合
property = new Property();
propertyMapping = new PropertyMapping();
property.setPropertyName(parEntityList.get(i).getEntityName() == null ? "" : parEntityList.get(i).getEntityName());
property.setSystem(parEntityList.get(i).getSystemId() == null ? "" : parEntityList.get(i).getSystemId());
property.setPropertyEntity(parEntityList.get(i).getTableName() == null ? "" : parEntityList.get(i).getTableName());
// 对应存储相同实体id关系
for (MetaEntity metaEntity : parEntityList) {
if (metaEntity.getEntityId().equals(parList.get(i).getRefEntityId())) {
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());
}
}
propertyMappingSet = new LinkedHashSet<>();
parPropertyList.add(property);
propertyMappingSet.add(propertyMapping);
property.setPropertyMappings(propertyMappingSet);
}
}
return parPropertyList;
}
/**
* 传入一个对象,返回一个数据模型Set集合
*
* @param et 实体对象
* @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;
}
/**
* 组合成一个主从模型
*
* @return 数据模型Set集合
*/
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()));
}
}
}
}
return dataModels;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册