提交 8a3081aa authored 作者: 黄承天's avatar 黄承天

[数据模型]修改为多层嵌套数据 增加了根据聚合对象id查询所有相关字段信息的方法

上级 62c2ba5f
...@@ -15,6 +15,6 @@ import java.util.List; ...@@ -15,6 +15,6 @@ import java.util.List;
*/ */
public interface AggregationDao extends JpaRepository<Aggregation, Integer>, JpaSpecificationExecutor<Aggregation> { public interface AggregationDao extends JpaRepository<Aggregation, Integer>, JpaSpecificationExecutor<Aggregation> {
List<Aggregation> findAllByTableInfoExId(Integer tableExId); List<Aggregation> findAllByTableInfoExIdAAndParentId(Integer tableExId, Integer parentId);
} }
package com.tykj.workflowcore.model_layer.entity.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@ApiModel("字段信息(聚合对象用)")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ColumnEXVO {
@ApiModelProperty("id")
private Integer id;
@ApiModelProperty("是否主键")
private Integer primaryKey;
@ApiModelProperty("字段类型")
private String fieldType;
@ApiModelProperty("字段名")
private String fieldName;
@ApiModelProperty("中文描述")
private String fieldTitle;
@ApiModelProperty("字段长度,有默认值")
private Integer fieldLength;
@ApiModelProperty("详细描述")
private String description;
@ApiModelProperty("聚合类型: -1无 0一对一 1一对多 2多对多")
private Integer aggregationType;
@ApiModelProperty("如果类型是个对象 该对象下的字段信息")
private List<ColumnEXVO> columns;
}
...@@ -9,14 +9,14 @@ import com.tykj.workflowcore.model_layer.dao.TableInfoDao; ...@@ -9,14 +9,14 @@ import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.dao.TableInfoExDao; import com.tykj.workflowcore.model_layer.dao.TableInfoExDao;
import com.tykj.workflowcore.model_layer.entity.Aggregation; import com.tykj.workflowcore.model_layer.entity.Aggregation;
import com.tykj.workflowcore.model_layer.entity.ColumnInfo; import com.tykj.workflowcore.model_layer.entity.ColumnInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfoEx; import com.tykj.workflowcore.model_layer.entity.TableInfoEx;
import com.tykj.workflowcore.model_layer.entity.vo.ColumnEXVO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap; import java.util.stream.Collectors;
import java.util.List;
import java.util.Map;
import static java.lang.String.format; import static java.lang.String.format;
...@@ -73,7 +73,18 @@ public class ModelHelper { ...@@ -73,7 +73,18 @@ public class ModelHelper {
result.put(columnInfo.getFieldName(), null); result.put(columnInfo.getFieldName(), null);
} }
//查出相关聚合信息并做处理 //查出相关聚合信息并做处理
List<Aggregation> aggregations = aggregationDao.findAllByTableInfoExId(tableInfoExId); List<Aggregation> aggregations = aggregationDao.findAllByTableInfoExIdAAndParentId(tableInfoExId, null);
getSubResult(result, aggregations);
//做json转换并返回
try {
return new ObjectMapper().writeValueAsString(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new ApiException("json转换时出现异常");
}
}
private void getSubResult(Map<String, Object> result, List<Aggregation> aggregations) {
for (Aggregation aggregation : aggregations) { for (Aggregation aggregation : aggregations) {
List<ColumnInfo> subColumnInfos = columnInfoDao.findAllByDbId(aggregation.getSideTableId()); List<ColumnInfo> subColumnInfos = columnInfoDao.findAllByDbId(aggregation.getSideTableId());
Map<String, Object> subResult = new HashMap<>(); Map<String, Object> subResult = new HashMap<>();
...@@ -83,6 +94,11 @@ public class ModelHelper { ...@@ -83,6 +94,11 @@ public class ModelHelper {
String subTableName = tableInfoDao.findById(aggregation.getSideTableId()) String subTableName = tableInfoDao.findById(aggregation.getSideTableId())
.orElseThrow(() -> new RuntimeException(format("未找到该id的数据:%s", aggregation.getSideTableId()))) .orElseThrow(() -> new RuntimeException(format("未找到该id的数据:%s", aggregation.getSideTableId())))
.getModelName(); .getModelName();
//如果聚合信息下还有子聚合信息 则往下递归处理
boolean hasSub = Objects.nonNull(aggregation.getAggregations()) && !aggregation.getAggregations().isEmpty();
if (hasSub) {
getSubResult(subResult, aggregation.getAggregations());
}
//判断关联类型 根据关联类型做不同处理 0:一对一 1:一对多 2:多对多 //判断关联类型 根据关联类型做不同处理 0:一对一 1:一对多 2:多对多
Integer relationshipType = aggregation.getRelationship(); Integer relationshipType = aggregation.getRelationship();
switch (relationshipType) { switch (relationshipType) {
...@@ -98,15 +114,78 @@ public class ModelHelper { ...@@ -98,15 +114,78 @@ public class ModelHelper {
default: default:
throw new ApiException(format("未识别的聚合类型:%s", relationshipType)); throw new ApiException(format("未识别的聚合类型:%s", relationshipType));
} }
}
//做json转换并返回
try {
return new ObjectMapper().writeValueAsString(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new ApiException("json转换时出现异常");
} }
} }
/**
* 根据聚合对象id 查询出其相关所有字段信息(包含对象嵌套字段)
*
* @param tableInfoExId 聚合对象id
* @return 聚合对象字段信息
*/
public List<ColumnEXVO> getColumnEXVOs(Integer tableInfoExId) {
List<ColumnEXVO> result = new ArrayList<>();
TableInfoEx tableInfoEX = tableInfoExDao.findById(tableInfoExId)
.orElseThrow(() -> new ApiException(format("未找到该id的数据:%s", tableInfoExId)));
//根据主表id查出主表部分字段信息 并加入Map结果
List<ColumnInfo> columnInfos = columnInfoDao.findAllByDbId(tableInfoEX.getMainTableId());
//普通字段部分
List<ColumnEXVO> part1 = columnInfos.stream()
.map(this::columnEXVO)
.collect(Collectors.toList());
//对象字段部分
List<Aggregation> aggregations = aggregationDao.findAllByTableInfoExIdAAndParentId(tableInfoExId, null);
List<ColumnEXVO> part2 = columnEXVOs(aggregations);
result.addAll(part1);
result.addAll(part2);
return result;
}
private ColumnEXVO columnEXVO(ColumnInfo columnInfo) {
return new ColumnEXVO(
columnInfo.getId(),
columnInfo.getPrimaryKey(),
columnInfo.getFieldType(),
columnInfo.getFieldName(),
columnInfo.getFieldTitle(),
columnInfo.getFieldLength(),
columnInfo.getDescription(),
-1,
new ArrayList<>()
);
}
private List<ColumnEXVO> columnEXVOs(List<Aggregation> aggregations) {
List<ColumnEXVO> result = new ArrayList<>();
for (Aggregation aggregation : aggregations) {
List<ColumnInfo> columnInfos = columnInfoDao.findAllByDbId(aggregation.getSideTableId());
List<ColumnEXVO> columns = columnInfos.stream()
.map(this::columnEXVO)
.collect(Collectors.toList());
TableInfo tableInfo = tableInfoDao.findById(aggregation.getSideTableId())
.orElseThrow(() -> new RuntimeException(format("未找到该id的数据:%s", aggregation.getSideTableId())));
//如果聚合信息下还有子聚合信息 则往下递归获取字段信息
boolean hasSub = Objects.nonNull(aggregation.getAggregations()) && !aggregation.getAggregations().isEmpty();
if (hasSub) {
List<ColumnEXVO> objColumns = columnEXVOs(aggregation.getAggregations());
columns.addAll(objColumns);
}
ColumnEXVO columnEXVO = new ColumnEXVO(
null,
0,
Object.class.getTypeName(),
tableInfo.getModelName(),
tableInfo.getModelTitle(),
-1,
tableInfo.getDescription(),
aggregation.getRelationship(),
columns
);
result.add(columnEXVO);
}
return result;
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论