infra:标准化代码

This commit is contained in:
YunaiV 2023-02-04 09:16:33 +08:00
parent dd1ed7b81e
commit d5bd499899

View File

@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -75,7 +76,7 @@ public class CodegenServiceImpl implements CodegenService {
private Long createCodegen0(Long userId, Long dataSourceConfigId, TableInfo tableInfo) {
// 校验导入的表和字段非空
checkTableInfo(tableInfo);
validateTableInfo(tableInfo);
// 校验是否已经存在
if (codegenTableMapper.selectByTableNameAndDataSourceConfigId(tableInfo.getName(),
dataSourceConfigId) != null) {
@ -86,7 +87,7 @@ public class CodegenServiceImpl implements CodegenService {
CodegenTableDO table = codegenBuilder.buildTable(tableInfo);
table.setDataSourceConfigId(dataSourceConfigId);
table.setScene(CodegenSceneEnum.ADMIN.getScene()); // 默认配置下使用管理后台的模板
table.setAuthor(userApi.getUser(userId).getData().getNickname());
table.setAuthor(userApi.getUser(userId).getCheckedData().getNickname());
codegenTableMapper.insert(table);
// 构建 CodegenColumnDO 数组插入到 DB
@ -99,7 +100,7 @@ public class CodegenServiceImpl implements CodegenService {
return table.getId();
}
private void checkTableInfo(TableInfo tableInfo) {
private void validateTableInfo(TableInfo tableInfo) {
if (tableInfo == null) {
throw exception(CODEGEN_IMPORT_TABLE_NULL);
}
@ -148,18 +149,33 @@ public class CodegenServiceImpl implements CodegenService {
private void syncCodegen0(Long tableId, TableInfo tableInfo) {
// 校验导入的表和字段非空
checkTableInfo(tableInfo);
validateTableInfo(tableInfo);
List<TableField> tableFields = tableInfo.getFields();
// 构建 CodegenColumnDO 数组只同步新增的字段
List<CodegenColumnDO> codegenColumns = codegenColumnMapper.selectListByTableId(tableId);
Set<String> codegenColumnNames = CollectionUtils.convertSet(codegenColumns, CodegenColumnDO::getColumnName);
//计算需要修改的字段插入时重新插入删除时将原来的删除
BiPredicate<TableField, CodegenColumnDO> pr =
(tableField, codegenColumn) -> tableField.getType().equals(codegenColumn.getDataType())
&& tableField.getMetaInfo().isNullable() == codegenColumn.getNullable()
&& tableField.isKeyFlag() == codegenColumn.getPrimaryKey()
&& tableField.getComment().equals(codegenColumn.getColumnComment());
Map<String, CodegenColumnDO> codegenColumnDOMap = CollectionUtils.convertMap(codegenColumns, CodegenColumnDO::getColumnName);
//需要修改的字段
Set<String> modifyFieldNames = tableFields.stream()
.filter(tableField -> codegenColumnDOMap.get(tableField.getColumnName()) != null
&& !pr.test(tableField, codegenColumnDOMap.get(tableField.getColumnName())))
.map(TableField::getColumnName)
.collect(Collectors.toSet());
// 计算需要删除的字段
Set<String> tableFieldNames = CollectionUtils.convertSet(tableFields, TableField::getName);
Set<Long> deleteColumnIds = codegenColumns.stream().filter(column -> !tableFieldNames.contains(column.getColumnName()))
Set<Long> deleteColumnIds = codegenColumns.stream()
.filter(column -> (!tableFieldNames.contains(column.getColumnName())) || modifyFieldNames.contains(column.getColumnName()))
.map(CodegenColumnDO::getId).collect(Collectors.toSet());
// 移除已经存在的字段
tableFields.removeIf(column -> codegenColumnNames.contains(column.getColumnName()));
tableFields.removeIf(column -> codegenColumnNames.contains(column.getColumnName()) && (!modifyFieldNames.contains(column.getColumnName())));
if (CollUtil.isEmpty(tableFields) && CollUtil.isEmpty(deleteColumnIds)) {
throw exception(CODEGEN_SYNC_NONE_CHANGE);
}