diff --git a/common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java b/common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java index a6915a5..bb63a20 100644 --- a/common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java +++ b/common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java @@ -1673,6 +1673,7 @@ public class OnlFormHeadServiceImpl extends ServiceImpl widgetSubTables = collectSubTableNames(widgetJson); - List existingSubTables = onlFormHeadService.list( - new LambdaQueryWrapper() - .eq(OnlFormHead::getSort, onlFormHead.getTableName()) - .eq(OnlFormHead::getLine, 1)); - Set existingSubTableNames = existingSubTables.stream() - .map(OnlFormHead::getTableName).collect(Collectors.toSet()); - Set newSubTableNames = new HashSet<>(widgetSubTables); - newSubTableNames.removeAll(existingSubTableNames); - - if (!newSubTableNames.isEmpty()) { - // 获取主表的主键字段(id)作为关联字段 - OnlineColumn masterPkColumn = onlineColumnService.getOne( - new LambdaQueryWrapper() - .eq(OnlineColumn::getTableId, onlineTable.getTableId()) - .eq(OnlineColumn::getColumnName, "id")); - - for (String subTableName : newSubTableNames) { - OnlFormHead subHead = onlFormHeadService.createMetaAndSync( - subTableName, - onlFormHead.getTableTxt() + "附表", - CustomUtil.MetaType_Entry, - onlFormHead.getTableName()); - - // 获取子表的 OnlineTable - OnlineTable subOnlineTable = onlineTableService.getOne( - new LambdaQueryWrapper() - .eq(OnlineTable::getTableName, subTableName.toLowerCase())); - if (subOnlineTable == null) { - // 从DB获取表结构并创建 OnlineTable - SqlTable subSqlTable = onlineDblinkService.getDblinkTable( - onlineDblinkService.getById(datasource.getDblinkId()), subTableName); - if (subSqlTable != null) { - subOnlineTable = onlineTableService.saveNewFromSqlTable(subSqlTable); - } - } + // 取 widgetList->bindData -> tableId 在去 zz_online_table 查询,这样就可以判断哪些是新增,哪些是 已有的表 + // 如果存在附表 + Map tableIdAndPropMap = collectSubTableAndPropMap(widgetJson); - // 创建子表数据源 - OnlineDatasource subDatasource = new OnlineDatasource(); - subDatasource.setDblinkId(datasource.getDblinkId()); - if (subOnlineTable != null) { - subDatasource.setMasterTableId(subOnlineTable.getTableId()); - } - onlineDatasourceService.save(subDatasource); - - // 创建 1:N 关系 - OnlineDatasourceRelation relation = new OnlineDatasourceRelation(); - relation.setDatasourceId(datasource.getDatasourceId()); - relation.setRelationType(1); - if (subOnlineTable != null) { - relation.setSlaveTableId(subOnlineTable.getTableId()); + if (MapUtil.isNotEmpty(tableIdAndPropMap)) { + //搜集的所有tableId, 要么是 tableId(已存在),要么是 tableName(不存在) + Set allTableIdSet = tableIdAndPropMap.keySet(); + Set newSubTableNames = new HashSet<>(); + Set existingSubTableIdSet = new HashSet<>(); + for (String tableId : allTableIdSet) { + if (tableId.matches("\\d+")) { + existingSubTableIdSet.add(Long.parseLong(tableId)); + } else { + newSubTableNames.add(tableId); } - if (masterPkColumn != null) { - relation.setMasterColumnId(masterPkColumn.getColumnId()); + } + //分两步, + //step1 : 看看存在的附表,是否存在不存在的字段 + if (!existingSubTableIdSet.isEmpty()) { + // 拿到tableId 下面的所有字段,先查询 zz_online_table ,在根据 tableName 查询 onl_form_head 表 + List tableList = onlineTableService.listByIds(existingSubTableIdSet); + Map tableIdAndNameMap = tableList.stream().collect(Collectors.toMap(OnlineTable::getTableId, OnlineTable::getTableName)); + List tableNames = tableList.stream().map(OnlineTable::getTableName).collect(Collectors.toList()); + List onlFormHeadList = onlFormHeadService.list(new LambdaQueryWrapper().in(OnlFormHead::getTableName, tableNames)); + Map tableNameAndHeadIdMap = onlFormHeadList.stream().collect(Collectors.toMap(OnlFormHead::getTableName, Function.identity())); + //因为这里是zz_online_column.column_id 的id和 列名的混合,我只要列名 + Map nameToWidget = mapColumnIdAndWidget(widgetJson); + Set newSubColumnNameSet = nameToWidget.keySet().stream().filter(s -> !s.matches("\\d+")).collect(Collectors.toSet()); + //这里要走 新增逻辑 + if (!newSubColumnNameSet.isEmpty()) { + // ALTER TABLE ADD COLUMN, 这里取第一个就行,因为绑定的是一个表 + JSONObject columnInfo = nameToWidget.values().iterator().next(); + Long tableId = columnInfo.getLong("tableId"); + String tableName = tableIdAndNameMap.get(tableId); + OnlFormHead entryHead = tableNameAndHeadIdMap.get(tableName); + for (String newSubColumnName : newSubColumnNameSet) { + //这里取出来的结构是 widgetList -》 props -》 tableColumnList + JSONObject columnInfoJson = nameToWidget.get(newSubColumnName); + //step 1 : 先保存 field 表 + String showName = columnInfoJson.getString("showName"); + // 默认是字符串类型 + this.saveField(750, showName, null, entryHead, newSubColumnName); + } + onlFormHeadService.syncDB(entryHead, 0L); + //step 2 :在保存 OnlineColumn 表 + OnlineDblink dblink = onlineDblinkService.getById(datasource.getDblinkId()); + List allColumns = onlineDblinkService.getDblinkTableColumnList(dblink, tableName); + Map columnMap = new HashMap<>(); + if (CollUtil.isNotEmpty(allColumns)) { + columnMap = allColumns.stream().collect(Collectors.toMap(SqlTableColumn::getColumnName, Function.identity())); + } + for (String fieldName : newSubColumnNameSet) { + SqlTableColumn sqlCol = columnMap.get(fieldName.toLowerCase()); + if (sqlCol != null) { + // 回填 columnId 到 widgetJson + JSONObject widget = nameToWidget.get(fieldName); + String showName = widget == null ? null : widget.getString("showName"); + long columnId = onlineColumnService.saveBySqlTable(sqlCol, tableId, showName); + if (widget != null) { + //step 3 :回填 columnId + showFieldName 到 json里面去 + widget.put("columnId", columnId); + widget.put("showFieldName", fieldName); + } + } + } + //step 4 :删除缓存 + redissonClient.getBucket(OnlineRedisKeyUtil.makeOnlineTableKey(tableId)).delete(); + redissonClient.getBucket(CacheKey.makeTableKey(tableName)).delete(); + redissonClient.getBucket(CacheKey.makeChildFieldListKey(entryHead.getId())).delete(); + metaCacheService.removeData(tableName); + changed = true; } - onlineDatasourceRelationService.save(relation); + } + //step2 : 不存在的附表,新增 + if (newSubTableNames.isEmpty()) { + //如果他为空,说明我们走tableId取值没取到,我们就去一下 tableName; + newSubTableNames = this.collectSubTableName(widgetJson); + } + if (!newSubTableNames.isEmpty()) { + + for (String subTableName : newSubTableNames) { + OnlFormHead subHead = onlFormHeadService.createMetaAndSync(subTableName, onlFormHead.getTableTxt() + "附表", CustomUtil.MetaType_Entry, onlFormHead.getTableName()); + // 获取子表的 OnlineTable + OnlineTable subOnlineTable = onlineTableService.getOne(new LambdaQueryWrapper().eq(OnlineTable::getTableName, subTableName.toLowerCase())); + if (subOnlineTable == null) { + // 从DB获取表结构并创建 OnlineTable + SqlTable subSqlTable = onlineDblinkService.getDblinkTable(onlineDblinkService.getById(datasource.getDblinkId()), subTableName); + if (subSqlTable != null) { + subOnlineTable = onlineTableService.saveNewFromSqlTable(subSqlTable); + } + } + OnlineDblink dblink = onlineDblinkService.getById(datasource.getDblinkId()); + onlineDatasourceRelationService.addOnlineDatasourceRelation(onlFormHead, datasource, null, dblink); - backfillRelationId(widgetJson, subTableName, relation.getRelationId()); + if (subOnlineTable != null) { + backfillRelationId(widgetJson, subTableName, subOnlineTable.getTableId()); + } + } + changed = true; } - changed = true; } - return changed ? widgetJson : null; } + + private void saveField(Integer widgetType, String columnComment, String showName, OnlFormHead onlFormHead, String fieldName) { + OnlFormField field = new OnlFormField(); + field.setHeadId(onlFormHead.getId()); + field.setFieldName(fieldName); + field.setFieldType(WidgetFieldTypeMapping.getFieldTypeId(widgetType)); + Integer length = WidgetFieldTypeMapping.getDefaultLength(widgetType); + if (length != null) { + field.setFieldLength(length); + } + field.setIsEmpty(1); + field.setIsDefault(0); + field.setRowDisabled(0); + field.setSyncFlag(0); + + // 从 widget 的 bindData.columnComment 设置字段备注 + field.setFieldRemark(columnComment); + if (StringUtils.isNotBlank(showName) && (StringUtils.isBlank(field.getFieldRemark()) || StringUtils.equals("null", field.getFieldRemark()))) { + field.setFieldRemark(showName); + } + onlFormFieldService.save(field); + } + /** * 遍历 pc/mobile 的 widget 树,收集所有 bindData.columnName 值。 */ @@ -895,22 +946,66 @@ public class OnlineFormController { } /** - * 遍历 pc/mobile 的 widget 树,收集 widgetType=403 (TableContainer) 的 props.tableName 值。 + * 遍历 pc/mobile 的 widget 树,收集 widgetType=100 (TableContainer) 的 bindData 下的 tableId */ - private Set collectSubTableNames(JSONObject widgetJson) { - Set names = new HashSet<>(); + private Map collectSubTableAndPropMap(JSONObject widgetJson) { + Map resultMap = new HashMap<>(); walkAllModes(widgetJson, widget -> { - if (widget.getInteger("widgetType") != null && 403 == widget.getInteger("widgetType")) { - JSONObject props = widget.getJSONObject("props"); - if (props != null && props.containsKey("tableName")) { - String tn = props.getString("tableName"); - if (StrUtil.isNotEmpty(tn)) { - names.add(tn); + JSONArray widgetList = widget.getJSONArray("widgetList"); + if (widgetList != null) { + for (int i = 0; i < widgetList.size(); i++) { + JSONObject childWidget = widgetList.getJSONObject(i); + // 只处理widgetType为100的子表 + Integer widgetType = childWidget.getInteger("widgetType"); + if (widgetType == null || widgetType != 100) { + continue; + } + + JSONObject bindData = childWidget.getJSONObject("bindData"); + if (bindData == null) { + continue; + } + + String tableId = bindData.getString("tableId"); + if (StrUtil.isBlank(tableId)) { + continue; + } + + JSONObject props = childWidget.getJSONObject("props"); + if (props != null) { + resultMap.put(tableId, props); } } } }); - return names; + return resultMap; + } + + private Set collectSubTableName(JSONObject widgetJson) { + Set tableNameSet = new HashSet<>(); + walkAllModes(widgetJson, widget -> { + JSONArray widgetList = widget.getJSONArray("widgetList"); + if (widgetList != null) { + for (int i = 0; i < widgetList.size(); i++) { + JSONObject childWidget = widgetList.getJSONObject(i); + // 只处理widgetType为100的子表 + Integer widgetType = childWidget.getInteger("widgetType"); + if (widgetType == null || widgetType != 100) { + continue; + } + + JSONObject bindData = childWidget.getJSONObject("bindData"); + if (bindData == null) { + continue; + } + + if (bindData.containsKey("tableName")) { + tableNameSet.add(bindData.getString("tableName")); + } + } + } + }); + return tableNameSet; } /** @@ -930,15 +1025,53 @@ public class OnlineFormController { return map; } + private Map mapColumnIdAndWidget(JSONObject widgetJson) { + Map resultMap = new HashMap<>(); + walkAllModes(widgetJson, widget -> { + JSONArray widgetList = widget.getJSONArray("widgetList"); + if (widgetList != null) { + for (int i = 0; i < widgetList.size(); i++) { + JSONObject childWidget = widgetList.getJSONObject(i); + // 只处理widgetType为100的子表 + Integer widgetType = childWidget.getInteger("widgetType"); + if (widgetType == null || widgetType != 100) { + continue; + } + + JSONObject bindData = childWidget.getJSONObject("bindData"); + if (bindData == null) { + continue; + } + + String tableId = bindData.getString("tableId"); + if (StrUtil.isBlank(tableId)) { + continue; + } + + JSONObject props = childWidget.getJSONObject("props"); + if (props == null) { + continue; + } + JSONArray tableColumnList = props.getJSONArray("tableColumnList"); + for (int j = 0; j < tableColumnList.size(); j++) { + JSONObject tableColumn = tableColumnList.getJSONObject(j); + resultMap.put(tableColumn.getString("columnId"), tableColumn); + } + + } + } + }); + return resultMap; + } /** - * 在 widgetJson 中查找匹配 tableName 的 TableContainer 控件,回填 relationId。 + * 在 widgetJson 中查找匹配 tableName 的 TableContainer 控件,回填 tableId。 */ - private void backfillRelationId(JSONObject widgetJson, String tableName, Long relationId) { + private void backfillRelationId(JSONObject widgetJson, String tableName, Long tableId) { walkAllModes(widgetJson, widget -> { - if (widget.getInteger("widgetType") != null && 403 == widget.getInteger("widgetType")) { - JSONObject props = widget.getJSONObject("props"); + if (widget.getInteger("widgetType") != null && 100 == widget.getInteger("widgetType")) { + JSONObject props = widget.getJSONObject("bindData"); if (props != null && tableName.equals(props.getString("tableName"))) { - props.put("relationId", relationId); + props.put("tableId", tableId); } } }); diff --git a/common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java b/common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java index 1d928ae..8d137a7 100644 --- a/common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java +++ b/common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java @@ -16,7 +16,10 @@ import apelet.common.generator.service.IOnlFormHeadService; import apelet.common.generator.utils.CustomUtil; import apelet.common.log.annotation.OperationLog; import apelet.common.log.model.constant.SysOperationLogType; -import apelet.common.online.dto.*; +import apelet.common.online.dto.OnlineDatasourceDto; +import apelet.common.online.dto.OnlineDatasourceRelationDto; +import apelet.common.online.dto.OnlinePageDatasourceDto; +import apelet.common.online.dto.OnlinePageDto; import apelet.common.online.model.*; import apelet.common.online.model.constant.PageStatus; import apelet.common.online.service.*; @@ -124,7 +127,7 @@ public class OnlinePageController { if (META_SOURCE_NEW.equals(metaSource)) { // === New branch: create fresh metadata === String metaType = onlinePageDto.getMetaType(); - String tableName = onlinePageDto.getPageCode(); + String tableName = "t_" + onlinePageDto.getPageCode().toLowerCase(); String tableTxt = onlinePageDto.getPageName(); onlFormHead = iOnlFormHeadService.createMetaAndSync(tableName, tableTxt, metaType, null); @@ -179,16 +182,17 @@ public class OnlinePageController { * 创建数据表关联关系 先查出改元数据表的所有附表关系 * 一对多 */ - addOnlineDatasourceRelation(onlFormHead, onlineDatasource, errorMessage, onlineDblink); - + onlineDatasourceRelationService.addOnlineDatasourceRelation(onlFormHead, onlineDatasource, errorMessage, onlineDblink); + return ResponseResult.success(onlinePage); } /** *创建数据表关联关系 先查出改元数据表的所有附表关系 - * + * 方法迁移☞ onlineDatasourceRelationService.addOnlineDatasourceRelation */ + @Deprecated public ResponseResult addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink) { //历史数据 --做比较新数据写入,已存在的不用管,不存在的删除 OnlineDatasourceRelation onlineDatasourceRelations = new OnlineDatasourceRelation(); @@ -442,7 +446,7 @@ public class OnlinePageController { onlineColumnService.saveNewList(sqlTableColumnList, onlineDatasourceList.get(0).getMasterTableId()); //写表关联关系 --一对多,一对一 onlineDatasourceService.getOnlineDatasourceListByPageId(onlinePageDto.getPageId(), null, null); - addOnlineDatasourceRelation(onlFormHeadList.get(0), onlineDatasourceList.get(0), errorMessage, onlineDblink); + onlineDatasourceRelationService.addOnlineDatasourceRelation(onlFormHeadList.get(0), onlineDatasourceList.get(0), errorMessage, onlineDblink); ArrayList onlineColumns = new ArrayList<>(); List filedByHeadIdCache = onlFormFieldService.getFiledByHeadIdCache(onlFormHeadList.get(0).getId()); diff --git a/common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java b/common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java index afd9931..8a5afe9 100644 --- a/common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java +++ b/common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java @@ -1,9 +1,13 @@ package apelet.common.online.service; import apelet.common.core.base.service.IBaseService; +import apelet.common.core.object.ResponseResult; import apelet.common.dbutil.object.SqlTable; import apelet.common.dbutil.object.SqlTableColumn; +import apelet.common.generator.model.OnlFormHead; +import apelet.common.online.model.OnlineDatasource; import apelet.common.online.model.OnlineDatasourceRelation; +import apelet.common.online.model.OnlineDblink; import java.util.List; import java.util.Set; @@ -82,4 +86,17 @@ public interface OnlineDatasourceRelationService extends IBaseService getOnlineDatasourceRelationListWithRelation( OnlineDatasourceRelation filter, String orderBy); + + /*** + * 功能: 迁移 OnlinePageController#addOnlineDatasourceRelation(OnlFormHead, OnlineDatasource, String, OnlineDblink) + * 方法,创建数据表关联关系 先查出改元数据表的所有附表关系 + * + * @author ${chenchuchuan} + * @param onlFormHead 主表信息 + * @param onlineDatasource 数据库链接配置 + * @param errorMessage 错误描述 + * @param onlineDblink 数据库实际链接 + * @return ResponseResult + */ + ResponseResult addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink); } diff --git a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java index 15c7a3c..2756598 100644 --- a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java +++ b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java @@ -38,7 +38,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.*; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; /** * 字段数据数据操作服务类。 @@ -124,6 +127,7 @@ public class OnlineColumnServiceImpl extends BaseService imp } this.setDefault(sqlTableColumn, onlineColumn); if (super.save(onlineColumn)) { + sqlTableColumn.setColumnName(onlineColumn.getColumnName().toLowerCase()); return onlineColumn.getColumnId(); } return 0; diff --git a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java index 7dd4f38..799d821 100644 --- a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java +++ b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java @@ -4,21 +4,31 @@ import apelet.common.core.annotation.MyDataSourceResolver; import apelet.common.core.base.dao.BaseDaoMapper; import apelet.common.core.base.service.BaseService; import apelet.common.core.constant.ApplicationConstant; +import apelet.common.core.constant.ErrorCodeEnum; import apelet.common.core.object.CallResult; import apelet.common.core.object.MyRelationParam; +import apelet.common.core.object.ResponseResult; import apelet.common.core.object.TokenData; import apelet.common.core.util.DefaultDataSourceResolver; +import apelet.common.core.util.MyModelUtil; import apelet.common.dbutil.object.SqlTable; import apelet.common.dbutil.object.SqlTableColumn; +import apelet.common.generator.model.OnlFormField; +import apelet.common.generator.model.OnlFormHead; +import apelet.common.generator.service.IOnlFormFieldService; +import apelet.common.generator.service.IOnlFormHeadService; import apelet.common.online.dao.OnlineDatasourceRelationMapper; import apelet.common.online.dao.OnlineDatasourceTableMapper; +import apelet.common.online.dto.OnlineDatasourceRelationDto; import apelet.common.online.model.*; import apelet.common.online.service.*; import apelet.common.online.util.OnlineRedisKeyUtil; import apelet.common.redis.util.CommonRedisUtil; import apelet.common.sequence.wrapper.IdGeneratorWrapper; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.github.pagehelper.Page; @@ -30,6 +40,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; +import java.util.stream.Collectors; /** * 数据源关联数据操作服务类。 @@ -61,6 +72,14 @@ public class OnlineDatasourceRelationServiceImpl private RedissonClient redissonClient; @Autowired private CommonRedisUtil commonRedisUtil; + @Autowired + private OnlineDblinkService onlineDblinkService; + @Autowired + private IOnlFormHeadService iOnlFormHeadService; + @Autowired + private OnlineDatasourceRelationService onlineDatasourceRelationService; + @Autowired + private IOnlFormFieldService onlFormFieldService; /** * 返回当前Service的主表Mapper对象。 @@ -289,4 +308,203 @@ public class OnlineDatasourceRelationServiceImpl } return CallResult.ok(); } + + @Override + public ResponseResult addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink) { + //历史数据 --做比较新数据写入,已存在的不用管,不存在的删除 + OnlineDatasourceRelation onlineDatasourceRelations = new OnlineDatasourceRelation(); + onlineDatasourceRelations.setDatasourceId(onlineDatasource.getDatasourceId()); + List onlineDatasourceRelationList = + onlineDatasourceRelationService.getOnlineDatasourceRelationListWithRelation(onlineDatasourceRelations, null); + Map oldmap01 = + onlineDatasourceRelationList.stream().collect(Collectors.toMap(obj -> obj.getMasterColumnId() + "_" + obj.getRelationType() + "_" + obj.getSlaveTableId(), obj -> obj, + (key1, key2) -> key1 + )); + //查出表单元数据的所有附表关系 + //主表id + List onlineTableList = onlineTableService.list(new LambdaQueryWrapper().eq(OnlineTable::getTableName, onlFormHead.getTableName().toLowerCase())); + //创建一对多关系:1 + List oneToMoreObjsList = iOnlFormHeadService.list(new LambdaQueryWrapper().eq(OnlFormHead::getLine, 1).eq(OnlFormHead::getSort, onlFormHead.getTableName())); + if (!oneToMoreObjsList.isEmpty()) { + List tableIdList = onlineTableList.stream().map(OnlineTable::getTableId).collect(Collectors.toList()); +// //主表列id对象 + OnlineColumn onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper() + .eq(OnlineColumn::getColumnName, "id") + .in(OnlineColumn::getTableId, tableIdList)); + for (OnlFormHead formHead : oneToMoreObjsList) { + List onlineTableListfu = onlineTableService.list(new LambdaQueryWrapper().eq(OnlineTable::getTableName, formHead.getTableName().toLowerCase())); + //附表关联的主表字段+附表id + if ((onlineTableListfu == null || onlineTableListfu.size() < 1) || !oldmap01.containsKey(onlineColumn.getColumnId() + "_1_" + onlineTableListfu.get(0).getTableId())) { + OnlineDatasourceRelationDto onlineDatasourceRelationDto = new OnlineDatasourceRelationDto(); + onlineDatasourceRelationDto.setDatasourceId(onlineDatasource.getDatasourceId()); + //关联名称 + onlineDatasourceRelationDto.setRelationName(onlFormHead.getTableName() + "_" + formHead.getTableName()); + //关联标识 + onlineDatasourceRelationDto.setVariableName(onlFormHead.getTableName() + "_" + formHead.getTableName()); + //关联类型 + onlineDatasourceRelationDto.setRelationType(1); + //主键字段id + onlineDatasourceRelationDto.setMasterColumnId(onlineColumn.getColumnId()); + //从表名称 + onlineDatasourceRelationDto.setSlaveTableName(formHead.getTableName()); + //从表字段名称 + onlineDatasourceRelationDto.setSlaveColumnName(formHead.getSortFiled()); + //是否级联删除 + onlineDatasourceRelationDto.setCascadeDelete(true); + //是否左连接查询 + onlineDatasourceRelationDto.setLeftJoin(false); + OnlineDatasourceRelation onlineDatasourceRelation = + MyModelUtil.copyTo(onlineDatasourceRelationDto, OnlineDatasourceRelation.class); + String appCode = TokenData.takeFromRequest().getAppCode(); + if (!StrUtil.equals(onlineDatasource.getAppCode(), appCode)) { + errorMessage = "数据验证失败,当前应用并不包含该数据源Id!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } +// OnlineDblink onlineDblink = onlineDblinkService.getById(onlineDatasource.getDblinkId()); + SqlTable slaveTable = onlineDblinkService.getDblinkTable( + onlineDblink, onlineDatasourceRelationDto.getSlaveTableName()); + if (slaveTable == null) { + errorMessage = "数据验证失败,指定的数据表不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + SqlTableColumn slaveColumn = null; + for (SqlTableColumn column : slaveTable.getColumnList()) { + if (column.getColumnName().equals(onlineDatasourceRelationDto.getSlaveColumnName())) { + slaveColumn = column; + break; + } + } + if (slaveColumn == null) { + errorMessage = "数据验证失败,指定的数据表字段 [" + onlineDatasourceRelationDto.getSlaveColumnName() + "] 不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + // 验证关联Id的数据合法性 + CallResult callResult = + onlineDatasourceRelationService.verifyRelatedData(onlineDatasourceRelation, null); + if (!callResult.isSuccess()) { + errorMessage = callResult.getErrorMessage(); + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + onlineDatasourceRelation = onlineDatasourceRelationService.saveNew(onlineDatasourceRelation, slaveTable, slaveColumn); + } else { + //更新表字段 + SqlTable slaveTable = onlineDblinkService.getDblinkTable( + onlineDblink, formHead.getTableName()); + if (slaveTable == null) { + errorMessage = "数据验证失败,指定的数据表不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + List sqlTableColumnList = slaveTable.getColumnList(); + Set tableids = new HashSet<>(); + tableids.add(onlineTableListfu.get(0).getTableId()); + List listColumn = onlineColumnService.getOnlineColumnListByTableIds(tableids); + List columnNames = listColumn.stream().map(OnlineColumn::getColumnName).collect(Collectors.toList()); + sqlTableColumnList = sqlTableColumnList.stream().filter(x -> !columnNames.contains(x.getColumnName().toLowerCase())).collect(Collectors.toList()); + onlineColumnService.saveNewList(sqlTableColumnList, onlineTableListfu.get(0).getTableId()); + } + } + } + //创建一对一关系:0 + List onlFormFieldList = onlFormFieldService.list(new LambdaQueryWrapper() + .eq(OnlFormField::getHeadId, onlFormHead.getId()).isNotNull(OnlFormField::getRefPropert)); + //List oneToMoreObjsList = iOnlFormHeadService.list(new LambdaQueryWrapper().eq(OnlFormHead::getLine, 1).eq(OnlFormHead::getSort, onlFormHead.getTableName())); + if (!onlFormFieldList.isEmpty()) { + Set updatedTable = new HashSet<>(); + //附表 + for (OnlFormField onlFormField : onlFormFieldList) { + //附表 + List onlFormHeadList = iOnlFormHeadService.list(new LambdaQueryWrapper().eq(OnlFormHead::getId, onlFormField.getRefPropert())); + if (onlFormHeadList.isEmpty()) { + continue; + } + OnlFormHead formHead = onlFormHeadList.get(0); + //根据表名去找在线附表 + List fuonlineTableList = onlineTableService.list(new LambdaQueryWrapper().eq(OnlineTable::getTableName, onlFormHeadList.get(0).getTableName())); + //主表id对象 + OnlineColumn onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper() + .eq(OnlineColumn::getColumnName, onlFormField.getFieldName()) + .in(OnlineColumn::getTableId, onlineTableList.get(0).getTableId())); + if (onlineColumn == null) { + onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper() + .eq(OnlineColumn::getColumnName, onlFormField.getFieldName().toLowerCase()) + .in(OnlineColumn::getTableId, onlineTableList.get(0).getTableId())); + } + //附表关联的主表字段+附表id + if (fuonlineTableList == null || fuonlineTableList.isEmpty() || (!oldmap01.containsKey(onlineColumn.getColumnId() + "_0_" + fuonlineTableList.get(0).getTableId()))) { + OnlineDatasourceRelationDto onlineDatasourceRelationDto = new OnlineDatasourceRelationDto(); + onlineDatasourceRelationDto.setDatasourceId(onlineDatasource.getDatasourceId()); + //关联名称 + onlineDatasourceRelationDto.setRelationName(onlFormHead.getTableName() + "_" + onlFormField.getFieldName() + "_" + formHead.getTableName()); + //关联标识 + onlineDatasourceRelationDto.setVariableName(onlFormHead.getTableName() + "_" + onlFormField.getFieldName() + "_" + formHead.getTableName()); + //关联类型 + onlineDatasourceRelationDto.setRelationType(0); + //主键字段id + onlineDatasourceRelationDto.setMasterColumnId(onlineColumn.getColumnId()); + //从表名称 + onlineDatasourceRelationDto.setSlaveTableName(formHead.getTableName()); + //从表字段名称 + onlineDatasourceRelationDto.setSlaveColumnName("id"); + //是否级联删除 + onlineDatasourceRelationDto.setCascadeDelete(true); + //是否左连接查询 + onlineDatasourceRelationDto.setLeftJoin(false); + + OnlineDatasourceRelation onlineDatasourceRelation = + MyModelUtil.copyTo(onlineDatasourceRelationDto, OnlineDatasourceRelation.class); + String appCode = TokenData.takeFromRequest().getAppCode(); + if (!StrUtil.equals(onlineDatasource.getAppCode(), appCode)) { + errorMessage = "数据验证失败,当前应用并不包含该数据源Id!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } +// OnlineDblink onlineDblink = onlineDblinkService.getById(onlineDatasource.getDblinkId()); + SqlTable slaveTable = onlineDblinkService.getDblinkTable( + onlineDblink, onlineDatasourceRelationDto.getSlaveTableName()); + if (slaveTable == null) { + errorMessage = "数据验证失败,指定的数据表不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + SqlTableColumn slaveColumn = null; + for (SqlTableColumn column : slaveTable.getColumnList()) { + if (column.getColumnName().equals(onlineDatasourceRelationDto.getSlaveColumnName())) { + slaveColumn = column; + break; + } + } + if (slaveColumn == null) { + errorMessage = "数据验证失败,指定的数据表字段 [" + onlineDatasourceRelationDto.getSlaveColumnName() + "] 不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + // 验证关联Id的数据合法性 + CallResult callResult = + onlineDatasourceRelationService.verifyRelatedData(onlineDatasourceRelation, null); + if (!callResult.isSuccess()) { + errorMessage = callResult.getErrorMessage(); + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + onlineDatasourceRelation = onlineDatasourceRelationService.saveNew(onlineDatasourceRelation, slaveTable, slaveColumn); + } else { + if (updatedTable.contains(formHead.getTableName())) { + continue; + } + updatedTable.add(formHead.getTableName()); + //更新表字段 + SqlTable slaveTable = onlineDblinkService.getDblinkTable( + onlineDblink, formHead.getTableName()); + if (slaveTable == null) { + errorMessage = "数据验证失败,指定的数据表不存在!"; + return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage); + } + List sqlTableColumnList = slaveTable.getColumnList(); + Set tableids = new HashSet<>(); + tableids.add(fuonlineTableList.get(0).getTableId()); + List listColumn = onlineColumnService.getOnlineColumnListByTableIds(tableids); + List columnNames = listColumn.stream().map(OnlineColumn::getColumnName).collect(Collectors.toList()); + sqlTableColumnList = sqlTableColumnList.stream().filter(x -> !columnNames.contains(x.getColumnName().toLowerCase())).collect(Collectors.toList()); + onlineColumnService.saveNewList(sqlTableColumnList, fuonlineTableList.get(0).getTableId()); + } + } + } + return null; + } } diff --git a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java index 1009f9b..cfcd9f3 100644 --- a/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java +++ b/common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java @@ -11,7 +11,10 @@ import apelet.common.dbutil.object.SqlTable; import apelet.common.online.dao.OnlineDatasourceMapper; import apelet.common.online.dao.OnlineDatasourceTableMapper; import apelet.common.online.dao.OnlinePageDatasourceMapper; -import apelet.common.online.model.*; +import apelet.common.online.model.OnlineDatasource; +import apelet.common.online.model.OnlineDatasourceTable; +import apelet.common.online.model.OnlinePageDatasource; +import apelet.common.online.model.OnlineTable; import apelet.common.online.service.OnlineDatasourceRelationService; import apelet.common.online.service.OnlineDatasourceService; import apelet.common.online.service.OnlineTableService; @@ -83,6 +86,7 @@ public class OnlineDatasourceServiceImpl extends BaseService