提交 5ab098a5 编写于 作者: sq3536's avatar sq3536

modify

上级 cf0db727
......@@ -171,14 +171,18 @@ public class DstDataSourceExService extends DstDataSourceServiceImpl {
if(ObjectUtils.isEmpty(datasourceMap.get("defaultSchema"))){
throw new BadRequestAlertException("数据源默认数据库不能为空","","ibzInit");
}
if(ObjectUtils.isEmpty(datasourceMap.get("defaultSystemId"))){
throw new BadRequestAlertException("逆向工程归属系统为空","","ibzInit");
}
LiquibaseProp lp = new LiquibaseProp();
lp.setUsername(datasourceMap.get("username").toString());
lp.setPassword(datasourceMap.get("password").toString());
lp.setUrl(datasourceMap.get("url").toString());
lp.setDefaultSchema(datasourceMap.get("defaultSchema").toString());
// 调用liquibase逆向工程的逻辑,生成一个xml,并返回一个SysModel对象
SysModel sysModel = dstLiquibaseService.generateLiquibase(lp,ds.getDsId());
liteCoreService.syncSysModel(sysModel);
SysModel sysModel = dstLiquibaseService.generateLiquibase(lp,datasourceMap.get("defaultSystemId").toString());
liteCoreService.syncSysModel(sysModel,1);
return et;
}
}
......
......@@ -27,6 +27,11 @@ import java.util.*;
@Slf4j
public class LiteCoreService {
public final static int ONLYCREATE=1;
public final static int ONLYUPDATE=2;
public final static int CREATEORUPDATE=3;
@Autowired
@Lazy
IMetaEntityService entityService;
......@@ -57,18 +62,24 @@ public class LiteCoreService {
@Transactional
public boolean syncSysModel(SysModel sysModel) {
proxyService.asyncSysModel(sysModel);
proxyService.asyncSysModel(sysModel,CREATEORUPDATE);
return true;
}
@Transactional
public boolean syncSysModel(SysModel sysModel,int saveMode) {
proxyService.asyncSysModel(sysModel,saveMode);
return true;
}
@Transactional
@Async("asyncExecutor")
public void asyncSysModel(SysModel sysModel) {
public void asyncSysModel(SysModel sysModel,int saveMode) {
long start = System.currentTimeMillis();
log.info("开始同步[{}]系统模型",sysModel.getSystemid());
syncDE(sysModel);
syncDEField(sysModel);
syncDERelation(sysModel);
syncDE(sysModel,saveMode);
syncDEField(sysModel,saveMode);
syncDERelation(sysModel,saveMode);
syncDataSource(sysModel);
long end = System.currentTimeMillis();
log.info("完成[{}]系统同步模型任务,总耗时[{}]秒",sysModel.getSystemid(),(end-start)/1000);
......@@ -85,23 +96,37 @@ public class LiteCoreService {
dataSourceService.save(dataSource);
}
private Map<String,String> gloabEntity = new HashMap<>();
/**
* 同步实体
* @param sysModel
*/
private void syncDE(SysModel sysModel) {
Map<String,Integer> delDE = new HashMap<>();
private void syncDE(SysModel sysModel,int saveMode) {
Map<String,String> delDE = new HashMap<>();
Map<String,Integer> delModule = new HashMap<>();
entityService.list(new QueryWrapper<MetaEntity>().select("entityid").eq("systemid",sysModel.getSystemid())).forEach(entity -> delDE.put(entity.getEntityId(),1));
entityService.list(new QueryWrapper<MetaEntity>().select("entityid","entityname").eq("systemid",sysModel.getSystemid())).forEach(entity -> delDE.put(entity.getEntityName(),entity.getEntityId()));
moduleService.list(new QueryWrapper<MetaModule>().select("codename").eq("systemid",sysModel.getSystemid())).forEach(module -> delModule.put(module.getCodeName(),1));
List<MetaEntityModel> metaEntityModels = sysModel.getEntities();
Set<MetaEntity> entities=new HashSet<>();
List<MetaEntity> create=new ArrayList<>();
List<MetaEntity> update=new ArrayList<>();
Map<Object,MetaModule> modulesMap = new HashMap<>();
if(!ObjectUtils.isEmpty(metaEntityModels)){
entities.addAll(entityMapping.toDomain(metaEntityModels));
}
entities.forEach(entity -> {
delDE.remove(entity.getEntityId());
if(delDE.containsKey(entity.getEntityName())) {
entity.setEntityId(delDE.get(entity.getEntityName()));
update.add(entity);
delDE.remove(entity.getEntityName());
}
else
{
entity.setEntityId(entity.getDefaultKey(true).toString());
create.add(entity);
}
gloabEntity.put(sysModel.getSystemid()+"."+entity.getEntityName(),entity.getEntityId());
String moduleId=entity.getModuleId();
String moduleName=entity.getModuleName();
if(!ObjectUtils.isEmpty(moduleId) && !ObjectUtils.isEmpty(moduleName)){
......@@ -119,13 +144,16 @@ public class LiteCoreService {
}
});
//移除无效资源
if(delDE.size()>0)
entityService.removeBatch(delDE.keySet());
if(delModule.size()>0)
moduleService.removeBatch(delModule.keySet());
// if(delDE.size()>0)
// entityService.removeBatch(delDE.values());
// if(delModule.size()>0)
// moduleService.removeBatch(delModule.keySet());
//存储或更新资源saveOrUpdate
if(entities.size()>0)
entityService.saveBatch(entities);
if(create.size()>0&&saveMode!=ONLYUPDATE)
entityService.createBatch(create);
if(update.size()>0&&saveMode!=ONLYCREATE)
entityService.updateBatch(update);
if(modulesMap.size()>0)
moduleService.saveBatch(modulesMap.values());
}
......@@ -134,31 +162,47 @@ public class LiteCoreService {
* 同步实体属性
* @param sysModel
*/
private void syncDEField(SysModel sysModel) {
Map<String,Integer> delField = new HashMap<>();
private void syncDEField(SysModel sysModel,int saveMode) {
Map<String,String> delField = new HashMap<>();
Map param=new HashMap();
param.put("id",sysModel.getSystemid());
List<JSONObject> oldField=fieldService.select("select fieldid from ibzfield t inner join ibzentity t1 on t.entityid =t1.entityid where t1.systemid = #{et.id}",param);
List<JSONObject> oldField=fieldService.select("select fieldid as \"FIELDID\",fieldname as \"FIELDNAME\",t.entityid as \"ENTITYID\",t.entityname as \"ENTITYNAME\" from ibzfield t inner join ibzentity t1 on t.entityid =t1.entityid where t1.systemid = #{et.id}",param);
for(JSONObject field: oldField){
delField.put(field.getString("FIELDID"),1);
delField.put(field.getString("ENTITYNAME")+"."+field.getString("FIELDNAME"),field.getString("FIELDID"));
}
List<MetaField> create=new ArrayList<>();
List<MetaField> update=new ArrayList<>();
Set<MetaField> list = sysModel.getField();
list.forEach(field -> {
delField.remove(field.getFieldId());
if(gloabEntity.containsKey(sysModel.getSystemid()+"."+field.getEntityName()))
field.setEntityId(gloabEntity.get(sysModel.getSystemid()+"."+field.getEntityName()));
if(delField.containsKey(field.getEntityName()+"."+field.getFieldName()))
{
field.setFieldId(delField.get(field.getEntityName()+"."+field.getFieldName()));
update.add(field);
delField.remove(field.getEntityName()+"."+field.getFieldName());
}
else {
field.setFieldId(field.getDefaultKey(true).toString());
create.add(field);
}
});
//移除无效资源
if(delField.size()>0)
fieldService.removeBatch(delField.keySet());
// if(delField.size()>0)
// fieldService.removeBatch(delField.values());
//存储或更新资源saveOrUpdate
if(list.size()>0)
fieldService.saveBatch(list);
if(create.size()>0&&saveMode!=ONLYUPDATE)
fieldService.createBatch(create);
if(update.size()>0&&saveMode!=ONLYCREATE)
fieldService.updateBatch(update);
}
/**
* 同步实体关系
* @param sysModel
*/
private void syncDERelation(SysModel sysModel) {
private void syncDERelation(SysModel sysModel,int saveMode) {
Map<String,Integer> delRelation = new HashMap<>();
Map param=new HashMap();
param.put("id",sysModel.getSystemid());
......@@ -166,16 +210,28 @@ public class LiteCoreService {
for(JSONObject relation: oldRelation){
delRelation.put(relation.getString("RELATIONID"),1);
}
List<MetaRelationship> create=new ArrayList<>();
List<MetaRelationship> update=new ArrayList<>();
Collection<MetaRelationship> list = sysModel.getRelation();
list.forEach(relation -> {
delRelation.remove(relation.getId());
if(delRelation.containsKey(relation.getId()))
{
delRelation.remove(relation.getId());
update.add(relation);
}
else
create.add(relation);
});
//移除无效资源
if(delRelation.size()>0)
relationService.removeBatch(delRelation.keySet());
// if(delRelation.size()>0)
// relationService.removeBatch(delRelation.keySet());
//存储或更新资源saveOrUpdate
if(list.size()>0)
relationService.saveBatch(list);
if(create.size()>0&&saveMode!=ONLYUPDATE)
relationService.createBatch(create);
if(update.size()>0&&saveMode!=ONLYCREATE)
relationService.updateBatch(update);
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册