You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.5 KiB
156 lines
5.5 KiB
11 months ago
|
package com.teaching.backend.service.KnowGraph;
|
||
|
|
||
|
import com.teaching.backend.mapper.KnowGraph.KnowRepository;
|
||
|
import com.teaching.backend.model.dto.KnowGraph.BaseKnowReturn;
|
||
|
import com.teaching.backend.model.dto.KnowGraph.KnowRequest;
|
||
|
import com.teaching.backend.model.dto.KnowGraph.KnowUpdateRequest;
|
||
|
import com.teaching.backend.model.dto.KnowGraph.RelationshipKnowRequest;
|
||
|
import com.teaching.backend.model.entity.KnowGraph.Know;
|
||
|
import com.teaching.backend.model.entity.KnowGraph.Links;
|
||
|
import com.teaching.backend.model.vo.knowGraph.KnowVO;
|
||
|
import org.neo4j.driver.internal.InternalRelationship;
|
||
|
import org.neo4j.driver.types.Node;
|
||
|
import org.springframework.beans.BeanUtils;
|
||
|
import org.springframework.data.neo4j.core.Neo4jClient;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
|
||
|
import javax.annotation.Resource;
|
||
|
import java.util.*;
|
||
|
|
||
|
/**
|
||
|
* @Author:youhang
|
||
|
* @Date:2024-07-21-14:46
|
||
|
* @Description:
|
||
|
*/
|
||
|
@Service
|
||
|
public class KnowService {
|
||
|
|
||
|
private KnowRepository knowRepository;
|
||
|
|
||
|
@Resource
|
||
|
private Neo4jClient neo4jClient;
|
||
|
|
||
|
public KnowService(KnowRepository knowRepository) {
|
||
|
this.knowRepository = knowRepository;
|
||
|
}
|
||
|
|
||
|
|
||
|
public List<Know> queryKnowAllKnowById(Long id) {
|
||
|
return knowRepository.queryKnowAllKnowById(id);
|
||
|
}
|
||
|
|
||
|
|
||
|
public Know createKnow(KnowRequest knowRequest ) {
|
||
|
Know know = new Know();
|
||
|
BeanUtils.copyProperties(knowRequest,know);
|
||
|
return knowRepository.save(know);
|
||
|
}
|
||
|
|
||
|
|
||
|
public void deleteKnow(Long id) {
|
||
|
knowRepository.deleteById(id);
|
||
|
}
|
||
|
|
||
|
public Know updateKnow(KnowUpdateRequest knowUpdateRequest ) {
|
||
|
Know know = new Know();
|
||
|
know = knowRepository.findById(knowUpdateRequest.getId()).orElseThrow(() -> new RuntimeException("知识点 not found"));
|
||
|
BeanUtils.copyProperties(knowUpdateRequest,know);
|
||
|
return knowRepository.save(know);
|
||
|
}
|
||
|
|
||
|
|
||
|
public void addKnowRelatedKnow( RelationshipKnowRequest relationshipKnowRequest) {
|
||
|
knowRepository.addKnowRelatedKnow(relationshipKnowRequest.getId(),relationshipKnowRequest.getKnowIds());
|
||
|
}
|
||
|
public void addKnowFatherAndSonKnow( RelationshipKnowRequest relationshipKnowRequest) {
|
||
|
knowRepository.addKnowFatherAndSonKnow(relationshipKnowRequest.getId(),relationshipKnowRequest.getKnowIds());
|
||
|
}
|
||
|
|
||
|
|
||
|
public BaseKnowReturn getKnowAll(Long id) {
|
||
|
Collection<Map<String, Object>> all =
|
||
|
neo4jClient.query( "match(n:Know)-[r*0..]->(p:Know) where ID(n) = "+id+" return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all();
|
||
|
|
||
|
Iterator<Map<String, Object>> iterator = all.iterator();
|
||
|
Set<KnowVO> knowList = new HashSet<>();
|
||
|
Set<Links>linksList = new HashSet<>();
|
||
|
KnowVO knowVO;
|
||
|
List node2 = new ArrayList<>();
|
||
|
Links links;
|
||
|
int p = 0;
|
||
|
while (iterator.hasNext()) {
|
||
|
Map<String, Object> element = iterator.next();
|
||
|
Long group = (Long) element.get("d");
|
||
|
knowVO = new KnowVO();
|
||
|
Node node1 = (Node) element.get("p");
|
||
|
Long id1 = node1.id();
|
||
|
String name1 = node1.get("name").asString();
|
||
|
// String info1 = node1.get("info").asString();
|
||
|
knowVO.setId(id1);
|
||
|
knowVO.setLabel(name1);
|
||
|
knowVO.setGroup(group);
|
||
|
knowList.add(knowVO);
|
||
|
|
||
|
node2 = (List) element.get("r");
|
||
|
for (int i = 0; i < node2.size(); i++) {
|
||
|
InternalRelationship e = (InternalRelationship) node2.get(i);
|
||
|
links = new Links();
|
||
|
links.setId(e.id());
|
||
|
links.setSource(e.startNodeId());
|
||
|
links.setTarget(e.endNodeId());
|
||
|
links.setLabel(e.type());
|
||
|
linksList.add(links);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList,linksList);
|
||
|
System.out.println(baseKnowReturn);
|
||
|
return baseKnowReturn;
|
||
|
}
|
||
|
|
||
|
public BaseKnowReturn getKnowById(Long id) {
|
||
|
Collection<Map<String, Object>> all =
|
||
|
neo4jClient.query( "match(n:Know)-[r*0..2]->(p:Know) where ID(n) = "+id+" return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all();
|
||
|
|
||
|
Iterator<Map<String, Object>> iterator = all.iterator();
|
||
|
Set<KnowVO> knowList = new HashSet<>();
|
||
|
Set<Links>linksList = new HashSet<>();
|
||
|
KnowVO knowVO;
|
||
|
List node2 = new ArrayList<>();
|
||
|
Links links;
|
||
|
int p = 0;
|
||
|
while (iterator.hasNext()) {
|
||
|
Map<String, Object> element = iterator.next();
|
||
|
|
||
|
Long group = (Long) element.get("d");
|
||
|
knowVO = new KnowVO();
|
||
|
Node node1 = (Node) element.get("p");
|
||
|
Long id1 = node1.id();
|
||
|
String name1 = node1.get("name").asString();
|
||
|
// String info1 = node1.get("info").asString();
|
||
|
knowVO.setId(id1);
|
||
|
knowVO.setLabel(name1);
|
||
|
knowVO.setGroup(group);
|
||
|
knowList.add(knowVO);
|
||
|
|
||
|
node2 = (List) element.get("r");
|
||
|
for (int i = 0; i < node2.size(); i++) {
|
||
|
InternalRelationship e = (InternalRelationship) node2.get(i);
|
||
|
links = new Links();
|
||
|
links.setId(e.id());
|
||
|
links.setSource(e.startNodeId());
|
||
|
links.setTarget(e.endNodeId());
|
||
|
links.setLabel(e.type());
|
||
|
linksList.add(links);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList,linksList);
|
||
|
System.out.println(baseKnowReturn);
|
||
|
return baseKnowReturn;
|
||
|
}
|
||
|
|
||
|
}
|