master
you hang 3 months ago committed by youhang20021127
parent 2aae3d172a
commit af3088a023
  1. 14
      src/main/java/com/teaching/backend/controller/Know/KnowController.java
  2. 12
      src/main/java/com/teaching/backend/mapper/know/KnowRepository.java
  3. 116
      src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java

@ -17,8 +17,12 @@ import com.teaching.backend.model.vo.knowGraph.KnowVO1;
import com.teaching.backend.service.Know.KnowService; import com.teaching.backend.service.Know.KnowService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import java.util.Set; import java.util.Set;
@ -31,6 +35,9 @@ public class KnowController {
@Autowired @Autowired
private KnowRepository knowRepository; private KnowRepository knowRepository;
@Resource
private Neo4jClient neo4jClient;
@ -54,8 +61,11 @@ public class KnowController {
//通过courseId 显示图的JSON //通过courseId 显示图的JSON
@GetMapping("/test") @GetMapping("/test")
void test(@RequestParam String courseId){ void test(){
int res = knowRepository.addCourseRelationship(courseId,932L,"related"); String t = "related";
String str = "MATCH (p:Know), (j:Know) WHERE ID(p)= "+407+" AND ID(j)= "+389+" CREATE (p)-[r: "+t+" ]->(j) return count(r);";
Collection<Map<String, Object>> res = neo4jClient.query(str).fetch().all();
System.out.println(res);
} }
//通过courseId 显示图的JSON //通过courseId 显示图的JSON

@ -12,10 +12,12 @@ import com.teaching.backend.model.entity.know.Know;
import com.teaching.backend.model.entity.know.KnowChapter; import com.teaching.backend.model.entity.know.KnowChapter;
import com.teaching.backend.model.entity.know.KnowCourse; import com.teaching.backend.model.entity.know.KnowCourse;
import com.teaching.backend.model.entity.resource.Resources; import com.teaching.backend.model.entity.resource.Resources;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query; import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -101,12 +103,14 @@ public interface KnowRepository extends Neo4jRepository<Know, Long> {
//========== //==========
//建立关系 all -课程 //建立关系 all -课程
@Query("MATCH (p:Know {courseId: $courseId}), (j:Know {chapterId: $chapterId}) CREATE (p)-[r:顺序]->(j) return count(p);") @Query("MATCH (p:Know {courseId: $courseId}), (j:Know {chapterId: $chapterId}) CREATE (p)-[r:contain]->(j) return count(p);")
int addCourseRelationship(String courseId,Long chapterId,String type); Long addCourseRelationship(String courseId,Long chapterId);
//建立关系 all -章节 //建立关系 all -章节
@Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId1 AND ID(j)= $chapterId2 CREATE (p)-[r: "+"$type"+"]->(j) return count(r);") // @Query({str})
int addChapterRelationship(Long chapterId1, Long chapterId2, @Param("type")String type); // int addChapterRelationship(String str);
//建立关系 all -节 - 点 //建立关系 all -节 - 点
@Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId AND j.knowId= $knowId CREATE (p)-[r: $type]->(j) return count(r);") @Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId AND j.knowId= $knowId CREATE (p)-[r: $type]->(j) return count(r);")

@ -41,6 +41,7 @@ import org.neo4j.driver.types.Node;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.Neo4jClient; import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -420,6 +421,77 @@ public class knowServiceImpl implements KnowService {
} }
Long opResult( String source, String target, String type, Integer sta){
Long res = -1L;
String weight = null;
if("contain".equals(type)){
weight = " {weight:5}";
}else if("dependent".equals(type)){
weight = " {weight:9}";
}else if("order".equals(type)){
weight = " {weight: 1}";
}
if(sta != null && sta == 1){
res = -1L;
Long target1 = Long.parseLong(target);
res = knowRepository.addCourseRelationship(source,target1);
if (res< 0L) {
throw new RuntimeException("添加课程-章节 包含 失败");
}
return res;
}
else if (sta != null && sta == 2) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.chapterId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;";
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all();
Iterator<Map<String, Object>> iterator = result.iterator();
res = -1L;
while (iterator.hasNext()){
Map<String, Object> element = iterator.next();
res = (Long) element.get("count");
}
if (res<=0L) {
throw new RuntimeException("添加章节-章节"+type+"失败");
}
return res;
} else if (sta != null && sta == 3) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.knowId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;";
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all();
Iterator<Map<String, Object>> iterator = result.iterator();
res = -1L;
while (iterator.hasNext()){
Map<String, Object> element = iterator.next();
res = (Long) element.get("count");
}
if (res < 0) {
throw new RuntimeException("添加章节-知识点"+type+"失败");
}
return res;
} else if (sta != null && sta == 4) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.knowId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;";
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all();
Iterator<Map<String, Object>> iterator = result.iterator();
res = -1L;
while (iterator.hasNext()){
Map<String, Object> element = iterator.next();
res = (Long) element.get("count");
}
if (res < 0L) {
throw new RuntimeException("添加知识点-知识点 关系"+type+"失败");
}
return res;
} else {
throw new RuntimeException("执行 错误");
}
}
//todo 根据json串生成 图谱 保证事务 //todo 根据json串生成 图谱 保证事务
private BaseResponse<String> generateGraph(String jsonNodeCourse, String jsonNodeChapter, String jsonNodeKnow, String jsonEdges) { private BaseResponse<String> generateGraph(String jsonNodeCourse, String jsonNodeChapter, String jsonNodeKnow, String jsonEdges) {
//插入图 //插入图
@ -467,8 +539,7 @@ public class knowServiceImpl implements KnowService {
} }
} }
//插入关系 // 1 课程-章节 2 章节 - 章节 3 章节 - 知识点 4 点 - 点
//插入关系
JSONArray jsonArrayEdges = JSONUtil.parseArray(jsonEdges); JSONArray jsonArrayEdges = JSONUtil.parseArray(jsonEdges);
for (Object obj : jsonArrayEdges) { for (Object obj : jsonArrayEdges) {
JSONObject jsonObject = (JSONObject) obj; JSONObject jsonObject = (JSONObject) obj;
@ -476,43 +547,14 @@ public class knowServiceImpl implements KnowService {
String target = jsonObject.getStr("target"); String target = jsonObject.getStr("target");
String type = jsonObject.getStr("type"); String type = jsonObject.getStr("type");
Integer sta = jsonObject.getInt("sta"); Integer sta = jsonObject.getInt("sta");
//在图数据库中插入 try {
if (sta != null && sta == 1) { opResult(source,target,type,sta);
Long target1 = Long.parseLong(target); }catch (Exception e){
int res = knowRepository.addCourseRelationship(source, target1, type); e.printStackTrace();
if (res <= 0) { return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"添加节点关系失败");
throw new RuntimeException("添加关系 1 失败"); }
}
} else if (sta != null && sta == 2) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
int res = knowRepository.addChapterRelationship(source1, target1, type);
if (res <= 0) {
throw new RuntimeException("添加关系 2 失败");
}
} else if (sta != null && sta == 3) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
int res = knowRepository.addChapterToKnowRelationship(source1, target1, type);
if (res <= 0) {
throw new RuntimeException("添加关系 13失败");
}
} else if (sta != null && sta == 4) {
Long source1 = Long.parseLong(source);
Long target1 = Long.parseLong(target);
int res = knowRepository.addKnowRelationship(source1, target1, type);
if (res <= 0) {
throw new RuntimeException("添加关系 4 失败");
}
} else {
throw new RuntimeException("sta 错误");
} }
}
return ResultUtils.success("添加节点关系成功"); return ResultUtils.success("添加节点关系成功");
} }

Loading…
Cancel
Save