文件关系网状图
This commit is contained in:
parent
8eb4d76f29
commit
965222bea2
|
|
@ -0,0 +1,40 @@
|
|||
package com.electromagnetic.industry.software.manage.pojo.models;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Data
|
||||
@Getter
|
||||
public class Edge {
|
||||
String source; // 起点
|
||||
String target; // 终点
|
||||
|
||||
public Edge(String source, String target) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return source + " -- " + target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Edge edge = (Edge) obj;
|
||||
// 无向边,两个方向都相等即认为是同一条边
|
||||
return (Objects.equals(source, edge.source) && Objects.equals(target, edge.target)) ||
|
||||
(Objects.equals(source, edge.target) && Objects.equals(target, edge.source));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// 无向边需要对 source 和 target 排序后计算哈希值
|
||||
return Objects.hash(Math.min(source.hashCode(), target.hashCode()),
|
||||
Math.max(source.hashCode(), target.hashCode()));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
package com.electromagnetic.industry.software.manage.pojo.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class FIleRelationVO {
|
||||
|
||||
/**
|
||||
* 关系主键id
|
||||
*/
|
||||
private String relationId;
|
||||
|
||||
/**
|
||||
* 关系描述
|
||||
*/
|
||||
private String relationship;
|
||||
|
||||
/**
|
||||
* 关联文件
|
||||
*/
|
||||
private FileSimpleInfoVO relatedFile;
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.electromagnetic.industry.software.manage.pojo.resp;
|
||||
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.Edge;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -8,12 +9,12 @@ import java.util.List;
|
|||
public class FileRelationViewVO {
|
||||
|
||||
/**
|
||||
* 主文件
|
||||
* 关联文件
|
||||
*/
|
||||
private FileSimpleInfoVO file;
|
||||
List<FileSimpleInfoVO> nodes;
|
||||
|
||||
/**
|
||||
* 关联文件关系列表
|
||||
* 边
|
||||
*/
|
||||
List<FIleRelationVO> relationList;
|
||||
List<Edge> edges;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@ import lombok.Data;
|
|||
@Data
|
||||
public class FileSimpleInfoVO {
|
||||
|
||||
/**
|
||||
* 主键id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 文件名称
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper;
|
|||
import com.electromagnetic.industry.software.manage.mapper.EdFileRelationMapper;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.EdFileInfo;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.EdFileRelation;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.FIleRelationVO;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.Edge;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.FileRelationViewVO;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.FileSimpleInfoVO;
|
||||
import com.electromagnetic.industry.software.manage.service.EdFileRelationService;
|
||||
|
|
@ -18,8 +18,7 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper, EdFileRelation> implements EdFileRelationService {
|
||||
|
|
@ -65,38 +64,69 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
|
|||
* 获取关系数据
|
||||
*/
|
||||
@Override
|
||||
public FileRelationViewVO listRelations (String id) {
|
||||
public FileRelationViewVO listRelations(String startId) {
|
||||
|
||||
FileRelationViewVO fileRelationViewVO = new FileRelationViewVO();
|
||||
|
||||
List<Edge> visitedEdges = new ArrayList<>();
|
||||
Set<String> visitedIds = new HashSet<>();
|
||||
Set<Edge> uniqueEdges = new HashSet<>();
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
|
||||
// 初始化 BFS
|
||||
queue.add(startId);
|
||||
visitedIds.add(startId);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
String currentId = queue.poll();
|
||||
List<Edge> neighbors = getEdges(currentId);
|
||||
|
||||
for (Edge edge : neighbors) {
|
||||
|
||||
// 添加边信息
|
||||
if (!uniqueEdges.contains(edge)) {
|
||||
visitedEdges.add(edge);
|
||||
uniqueEdges.add(edge);
|
||||
}
|
||||
// 如果目标节点未访问,记录边并继续搜索
|
||||
if (!visitedIds.contains(edge.getTarget())) {
|
||||
visitedIds.add(edge.getTarget()); // 标记目标节点为已访问
|
||||
queue.add(edge.getTarget());
|
||||
}
|
||||
}
|
||||
}
|
||||
fileRelationViewVO.setEdges(visitedEdges);
|
||||
List<FileSimpleInfoVO> nodes = new ArrayList<>();
|
||||
for (String id : visitedIds) {
|
||||
FileSimpleInfoVO fileSimpleInfoVO = new FileSimpleInfoVO();
|
||||
EdFileInfo fileInfo = edFileInfoMapper.selectById(id);
|
||||
BeanUtils.copyProperties(fileInfo, fileSimpleInfoVO);
|
||||
nodes.add(fileSimpleInfoVO);
|
||||
}
|
||||
fileRelationViewVO.setNodes(nodes);
|
||||
return fileRelationViewVO;
|
||||
}
|
||||
|
||||
private List<Edge> getEdges(String id) {
|
||||
LambdaQueryWrapper<EdFileRelation> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(EdFileRelation::getId1, id)
|
||||
.or()
|
||||
.eq(EdFileRelation::getId2, id);
|
||||
List<EdFileRelation> list = this.list(queryWrapper);
|
||||
List<FIleRelationVO> relationList = new ArrayList<>();
|
||||
List<Edge> edges = new ArrayList<>();
|
||||
for (EdFileRelation edFileRelation : list) {
|
||||
FIleRelationVO fIleRelationVO = new FIleRelationVO();
|
||||
fIleRelationVO.setRelationId(edFileRelation.getId());
|
||||
fIleRelationVO.setRelationship(edFileRelation.getRelationship());
|
||||
String queryId = "";
|
||||
if (edFileRelation.getId1().equals(id)) {
|
||||
queryId = edFileRelation.getId2();
|
||||
edges.add(new Edge(edFileRelation.getId1(), edFileRelation.getId2()));
|
||||
} else {
|
||||
queryId = edFileRelation.getId1();
|
||||
edges.add(new Edge(edFileRelation.getId2(), edFileRelation.getId1()));
|
||||
}
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
EdFileInfo info = edFileInfoMapper.selectById(queryId);
|
||||
FileSimpleInfoVO fileSimpleInfoVO = new FileSimpleInfoVO();
|
||||
BeanUtils.copyProperties(info, fileSimpleInfoVO);
|
||||
fIleRelationVO.setRelatedFile(fileSimpleInfoVO);
|
||||
relationList.add(fIleRelationVO);
|
||||
}
|
||||
|
||||
FileRelationViewVO fileRelationViewVO = new FileRelationViewVO();
|
||||
fileRelationViewVO.setRelationList(relationList);
|
||||
|
||||
EdFileInfo info = edFileInfoMapper.selectById(id);
|
||||
FileSimpleInfoVO fileSimpleInfoVO = new FileSimpleInfoVO();
|
||||
BeanUtils.copyProperties(info, fileSimpleInfoVO);
|
||||
fileRelationViewVO.setFile(fileSimpleInfoVO);
|
||||
|
||||
return fileRelationViewVO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue