Browse Source

refactor(common-generator): 优化在线表单头服务实现

- 在OnlFormHeadServiceImpl中设置sortFiled为parent_id
- 重构OnlineColumnServiceImpl中的包导入
- 在OnlineColumnServiceImpl中将列名转换为小写存储
- 扩展OnlineDatasourceRelationService接口定义
- 实现完整的OnlineDatasourceRelationServiceImpl业务逻辑
- 在OnlineDatasourceServiceImpl中设置数据源名称
- 重构OnlineFormController中的字段保存逻辑
- 优化OnlineFormController中的子表处理流程
- 更新OnlinePageController中表名生成规则为小写前缀格式
- 添加必要的依赖注入和缓存清理操作
online-form-direct-creation
chenchuchuan 1 week ago
parent
commit
3104f1b026
  1. 1
      common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java
  2. 323
      common/common-online/src/main/java/apelet/common/online/controller/OnlineFormController.java
  3. 16
      common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java
  4. 17
      common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java
  5. 6
      common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java
  6. 218
      common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java
  7. 6
      common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java

1
common/common-generator/src/main/java/apelet/common/generator/service/impl/OnlFormHeadServiceImpl.java

@ -1673,6 +1673,7 @@ public class OnlFormHeadServiceImpl extends ServiceImpl<OnlFormHeadMapper, OnlFo @@ -1673,6 +1673,7 @@ public class OnlFormHeadServiceImpl extends ServiceImpl<OnlFormHeadMapper, OnlFo
head.setLine(CustomUtil.MetaType_Entry.equals(metaType) ? 1 : 0);
if (StringUtils.isNotEmpty(sortFiled)) {
head.setSort(sortFiled);
head.setSortFiled("parent_id");
}
// 初始化审计字段(createUserId/updateUserId/createTime/updateTime/deletedFlag)

323
common/common-online/src/main/java/apelet/common/online/controller/OnlineFormController.java

@ -3,6 +3,7 @@ package apelet.common.online.controller; @@ -3,6 +3,7 @@ package apelet.common.online.controller;
import apelet.common.core.annotation.MyRequestBody;
import apelet.common.core.annotation.NoAuthInterface;
import apelet.common.core.cache.CacheConfig;
import apelet.common.core.cache.CacheKey;
import apelet.common.core.constant.AppDeviceType;
import apelet.common.core.constant.ErrorCodeEnum;
import apelet.common.core.object.*;
@ -17,6 +18,7 @@ import apelet.common.generator.model.OnlFormHead; @@ -17,6 +18,7 @@ import apelet.common.generator.model.OnlFormHead;
import apelet.common.generator.service.IOnlFormFieldService;
import apelet.common.generator.service.IOnlFormHeadService;
import apelet.common.generator.utils.CustomUtil;
import apelet.common.generator.utils.cache.MetaCacheService;
import apelet.common.log.annotation.OperationLog;
import apelet.common.log.model.constant.SysOperationLogType;
import apelet.common.online.config.OnlineProperties;
@ -53,6 +55,7 @@ import javax.annotation.Resource; @@ -53,6 +55,7 @@ import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@ -95,6 +98,9 @@ public class OnlineFormController { @@ -95,6 +98,9 @@ public class OnlineFormController {
@Resource(name = "caffeineCacheManager")
private CacheManager cacheManager;
@Autowired
private MetaCacheService metaCacheService;
/**
* 新增在线表单数据
*
@ -750,31 +756,9 @@ public class OnlineFormController { @@ -750,31 +756,9 @@ public class OnlineFormController {
for (String fieldName : validColumnNames) {
JSONObject widget = nameToWidget.get(fieldName);
Integer widgetType = widget.getInteger("widgetType");
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 设置字段备注
JSONObject bindData = widget.getJSONObject("bindData");
if (bindData != null && bindData.containsKey("columnComment")) {
field.setFieldRemark(bindData.getString("columnComment"));
}
String showName = widget.getString("showName");
if (StringUtils.isNotBlank(showName) && (StringUtils.isBlank(field.getFieldRemark()) || StringUtils.equals("null", field.getFieldRemark()))) {
field.setFieldRemark(showName);
}
onlFormFieldService.save(field);
this.saveField(widget.getInteger("widgetType"), bindData.getString("columnComment"), widget.getString("showName"), onlFormHead, fieldName);
}
// ALTER TABLE ADD COLUMN
@ -798,7 +782,8 @@ public class OnlineFormController { @@ -798,7 +782,8 @@ public class OnlineFormController {
if (sqlCol != null) {
// 回填 columnId 到 widgetJson
JSONObject widget = nameToWidget.get(fieldName);
long columnId = onlineColumnService.saveBySqlTable(sqlCol, onlineTable.getTableId(), widget.getString("showName"));
String showName = widget == null ? null : widget.getString("showName");
long columnId = onlineColumnService.saveBySqlTable(sqlCol, onlineTable.getTableId(), showName);
if (widget != null) {
JSONObject bindData2 = widget.getJSONObject("bindData");
if (bindData2 != null) {
@ -808,75 +793,141 @@ public class OnlineFormController { @@ -808,75 +793,141 @@ public class OnlineFormController {
}
}
}
redissonClient.getBucket(OnlineRedisKeyUtil.makeOnlineTableKey(onlineTable.getTableId())).delete();
redissonClient.getBucket(CacheKey.makeTableKey(onlineTable.getTableName())).delete();
redissonClient.getBucket(CacheKey.makeChildFieldListKey(onlFormHead.getId())).delete();
metaCacheService.removeData(onlineTable.getTableName());
changed = true;
}
// === Diff 新增子表 ===
Set<String> widgetSubTables = collectSubTableNames(widgetJson);
List<OnlFormHead> existingSubTables = onlFormHeadService.list(
new LambdaQueryWrapper<OnlFormHead>()
.eq(OnlFormHead::getSort, onlFormHead.getTableName())
.eq(OnlFormHead::getLine, 1));
Set<String> existingSubTableNames = existingSubTables.stream()
.map(OnlFormHead::getTableName).collect(Collectors.toSet());
Set<String> newSubTableNames = new HashSet<>(widgetSubTables);
newSubTableNames.removeAll(existingSubTableNames);
if (!newSubTableNames.isEmpty()) {
// 获取主表的主键字段(id)作为关联字段
OnlineColumn masterPkColumn = onlineColumnService.getOne(
new LambdaQueryWrapper<OnlineColumn>()
.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<OnlineTable>()
.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<String, JSONObject> 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<String> allTableIdSet = tableIdAndPropMap.keySet();
Set<String> newSubTableNames = new HashSet<>();
Set<Long> 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<OnlineTable> tableList = onlineTableService.listByIds(existingSubTableIdSet);
Map<Long, String> tableIdAndNameMap = tableList.stream().collect(Collectors.toMap(OnlineTable::getTableId, OnlineTable::getTableName));
List<String> tableNames = tableList.stream().map(OnlineTable::getTableName).collect(Collectors.toList());
List<OnlFormHead> onlFormHeadList = onlFormHeadService.list(new LambdaQueryWrapper<OnlFormHead>().in(OnlFormHead::getTableName, tableNames));
Map<String, OnlFormHead> tableNameAndHeadIdMap = onlFormHeadList.stream().collect(Collectors.toMap(OnlFormHead::getTableName, Function.identity()));
//因为这里是zz_online_column.column_id 的id和 列名的混合,我只要列名
Map<String, JSONObject> nameToWidget = mapColumnIdAndWidget(widgetJson);
Set<String> 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<SqlTableColumn> allColumns = onlineDblinkService.getDblinkTableColumnList(dblink, tableName);
Map<String, SqlTableColumn> 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<OnlineTable>().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 { @@ -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<String> collectSubTableNames(JSONObject widgetJson) {
Set<String> names = new HashSet<>();
private Map<String, JSONObject> collectSubTableAndPropMap(JSONObject widgetJson) {
Map<String, JSONObject> 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<String> collectSubTableName(JSONObject widgetJson) {
Set<String> 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 { @@ -930,15 +1025,53 @@ public class OnlineFormController {
return map;
}
private Map<String, JSONObject> mapColumnIdAndWidget(JSONObject widgetJson) {
Map<String, JSONObject> 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);
}
}
});

16
common/common-online/src/main/java/apelet/common/online/controller/OnlinePageController.java

@ -16,7 +16,10 @@ import apelet.common.generator.service.IOnlFormHeadService; @@ -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 { @@ -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 { @@ -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<Long> addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink) {
//历史数据 --做比较新数据写入,已存在的不用管,不存在的删除
OnlineDatasourceRelation onlineDatasourceRelations = new OnlineDatasourceRelation();
@ -442,7 +446,7 @@ public class OnlinePageController { @@ -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<OnlineColumn> onlineColumns = new ArrayList<>();
List<OnlFormField> filedByHeadIdCache = onlFormFieldService.getFiledByHeadIdCache(onlFormHeadList.get(0).getId());

17
common/common-online/src/main/java/apelet/common/online/service/OnlineDatasourceRelationService.java

@ -1,9 +1,13 @@ @@ -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<OnlineData @@ -82,4 +86,17 @@ public interface OnlineDatasourceRelationService extends IBaseService<OnlineData
*/
List<OnlineDatasourceRelation> getOnlineDatasourceRelationListWithRelation(
OnlineDatasourceRelation filter, String orderBy);
/***
* 功能: 迁移 OnlinePageController#addOnlineDatasourceRelation(OnlFormHead, OnlineDatasource, String, OnlineDblink)
* 方法创建数据表关联关系 先查出改元数据表的所有附表关系
*
* @author ${chenchuchuan}
* @param onlFormHead 主表信息
* @param onlineDatasource 数据库链接配置
* @param errorMessage 错误描述
* @param onlineDblink 数据库实际链接
* @return ResponseResult<Long>
*/
ResponseResult<Long> addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink);
}

6
common/common-online/src/main/java/apelet/common/online/service/impl/OnlineColumnServiceImpl.java

@ -38,7 +38,10 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -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<OnlineColumn, Long> imp @@ -124,6 +127,7 @@ public class OnlineColumnServiceImpl extends BaseService<OnlineColumn, Long> imp
}
this.setDefault(sqlTableColumn, onlineColumn);
if (super.save(onlineColumn)) {
sqlTableColumn.setColumnName(onlineColumn.getColumnName().toLowerCase());
return onlineColumn.getColumnId();
}
return 0;

218
common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceRelationServiceImpl.java

@ -4,21 +4,31 @@ import apelet.common.core.annotation.MyDataSourceResolver; @@ -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; @@ -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 @@ -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 @@ -289,4 +308,203 @@ public class OnlineDatasourceRelationServiceImpl
}
return CallResult.ok();
}
@Override
public ResponseResult<Long> addOnlineDatasourceRelation(OnlFormHead onlFormHead, OnlineDatasource onlineDatasource, String errorMessage, OnlineDblink onlineDblink) {
//历史数据 --做比较新数据写入,已存在的不用管,不存在的删除
OnlineDatasourceRelation onlineDatasourceRelations = new OnlineDatasourceRelation();
onlineDatasourceRelations.setDatasourceId(onlineDatasource.getDatasourceId());
List<OnlineDatasourceRelation> onlineDatasourceRelationList =
onlineDatasourceRelationService.getOnlineDatasourceRelationListWithRelation(onlineDatasourceRelations, null);
Map<String, OnlineDatasourceRelation> oldmap01 =
onlineDatasourceRelationList.stream().collect(Collectors.toMap(obj -> obj.getMasterColumnId() + "_" + obj.getRelationType() + "_" + obj.getSlaveTableId(), obj -> obj,
(key1, key2) -> key1
));
//查出表单元数据的所有附表关系
//主表id
List<OnlineTable> onlineTableList = onlineTableService.list(new LambdaQueryWrapper<OnlineTable>().eq(OnlineTable::getTableName, onlFormHead.getTableName().toLowerCase()));
//创建一对多关系:1
List<OnlFormHead> oneToMoreObjsList = iOnlFormHeadService.list(new LambdaQueryWrapper<OnlFormHead>().eq(OnlFormHead::getLine, 1).eq(OnlFormHead::getSort, onlFormHead.getTableName()));
if (!oneToMoreObjsList.isEmpty()) {
List<Long> tableIdList = onlineTableList.stream().map(OnlineTable::getTableId).collect(Collectors.toList());
// //主表列id对象
OnlineColumn onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper<OnlineColumn>()
.eq(OnlineColumn::getColumnName, "id")
.in(OnlineColumn::getTableId, tableIdList));
for (OnlFormHead formHead : oneToMoreObjsList) {
List<OnlineTable> onlineTableListfu = onlineTableService.list(new LambdaQueryWrapper<OnlineTable>().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<SqlTableColumn> sqlTableColumnList = slaveTable.getColumnList();
Set<Long> tableids = new HashSet<>();
tableids.add(onlineTableListfu.get(0).getTableId());
List<OnlineColumn> listColumn = onlineColumnService.getOnlineColumnListByTableIds(tableids);
List<String> 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<OnlFormField> onlFormFieldList = onlFormFieldService.list(new LambdaQueryWrapper<OnlFormField>()
.eq(OnlFormField::getHeadId, onlFormHead.getId()).isNotNull(OnlFormField::getRefPropert));
//List<OnlFormHead> oneToMoreObjsList = iOnlFormHeadService.list(new LambdaQueryWrapper<OnlFormHead>().eq(OnlFormHead::getLine, 1).eq(OnlFormHead::getSort, onlFormHead.getTableName()));
if (!onlFormFieldList.isEmpty()) {
Set<String> updatedTable = new HashSet<>();
//附表
for (OnlFormField onlFormField : onlFormFieldList) {
//附表
List<OnlFormHead> onlFormHeadList = iOnlFormHeadService.list(new LambdaQueryWrapper<OnlFormHead>().eq(OnlFormHead::getId, onlFormField.getRefPropert()));
if (onlFormHeadList.isEmpty()) {
continue;
}
OnlFormHead formHead = onlFormHeadList.get(0);
//根据表名去找在线附表
List<OnlineTable> fuonlineTableList = onlineTableService.list(new LambdaQueryWrapper<OnlineTable>().eq(OnlineTable::getTableName, onlFormHeadList.get(0).getTableName()));
//主表id对象
OnlineColumn onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper<OnlineColumn>()
.eq(OnlineColumn::getColumnName, onlFormField.getFieldName())
.in(OnlineColumn::getTableId, onlineTableList.get(0).getTableId()));
if (onlineColumn == null) {
onlineColumn = onlineColumnService.getOne(new LambdaQueryWrapper<OnlineColumn>()
.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<SqlTableColumn> sqlTableColumnList = slaveTable.getColumnList();
Set<Long> tableids = new HashSet<>();
tableids.add(fuonlineTableList.get(0).getTableId());
List<OnlineColumn> listColumn = onlineColumnService.getOnlineColumnListByTableIds(tableids);
List<String> 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;
}
}

6
common/common-online/src/main/java/apelet/common/online/service/impl/OnlineDatasourceServiceImpl.java

@ -11,7 +11,10 @@ import apelet.common.dbutil.object.SqlTable; @@ -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<OnlineDatasource, L @@ -83,6 +86,7 @@ public class OnlineDatasourceServiceImpl extends BaseService<OnlineDatasource, L
TokenData tokenData = TokenData.takeFromRequest();
OnlineTable onlineTable = onlineTableService.saveNewFromSqlTable(sqlTable);
onlineDatasource.setDatasourceId(idGenerator.nextLongId());
onlineDatasource.setDatasourceName(sqlTable.getTableName());
onlineDatasource.setAppCode(tokenData.getAppCode());
onlineDatasource.setMasterTableId(onlineTable.getTableId());
Date now = new Date();

Loading…
Cancel
Save