Browse Source

2025.1.8 导入

wzx
linsheng0116 1 year ago
parent
commit
a547dab6b4
  1. 765
      src/main/java/com/main/woka/Web/Service/impl/KcTlFileServiceImpl.java

765
src/main/java/com/main/woka/Web/Service/impl/KcTlFileServiceImpl.java

@ -0,0 +1,765 @@
package com.main.woka.Web.Service.impl;
import com.main.woka.Common.util.*;
import com.main.woka.Web.Mapper.KcFileMapper;
import com.main.woka.Web.Mapper.KcInfoRelationMapper;
import org.neo4j.driver.v1.StatementResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.util.*;
@Service
public class KcTlFileServiceImpl {
@Autowired
KcFileMapper kcFileMapper;
@Autowired
KcInfoRelationMapper kcInfoRelationMapper;
@Autowired
WordSplitter wordSplitter;
@Autowired
RelationUtil relationUtil;
@Autowired
Neo4jUtil neo4jUtil;
@Autowired
KcFileMapper kcFile1Mapper;
public String wordSplitter(FileInputStream file, String url, Integer fileId){
try {
wordSplitter.testWordOne(file,url,fileId);
return "a";
} catch (Exception e) {
e.printStackTrace();
}
return "b";
}
public Integer insertFile(KcTlFile kcTlFile){
return kcInfoRelationMapper.insertFile(kcTlFile);
}
public String getRelationShip(Integer fileId){
//先获取所有的第一级标题
List<KcInfo> list = kcFileMapper.getHeaderByLevel(1l,fileId);
String nameOld = String.valueOf(kcFile1Mapper.getFileById(fileId));
String name = nameOld.substring(0,nameOld.length()-5);
for(int a=0;a<list.size();a++){
String source = list.get(a).getName();
Long OneId = list.get(a).getId();
String name1 = name;
String name2 = source;
KcInfoRelation zz1 = new KcInfoRelation();
zz1.setRelation("属于");
zz1.setSourceId(Long.valueOf(fileId));
zz1.setTargetId(OneId);
zz1.setSourceName(name1);
zz1.setTargetName(name2);
zz1.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz1);
List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
for(int i=0;i<list1.size();i++){
String target = list1.get(i).getName();
// String aa = relationUtil.analyzeRelationship(source,target);
String aa = "属于";
// System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
KcInfoRelation zz = new KcInfoRelation();
zz.setRelation(aa);
zz.setSourceId(OneId);
zz.setTargetId(TwoId);
zz.setSourceName(source);
zz.setTargetName(target);
zz.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz);
}
}
//获取所有的二级标题
List<KcInfo> list2 = kcFileMapper.getHeaderByLevel(2l,fileId);
for(int a1=0;a1<list2.size();a1++){
Long OneId = list2.get(a1).getId();
List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
for(int i=0;i<list1.size();i++){
String source = list2.get(a1).getName();
String target = list1.get(i).getName();
// String aa = relationUtil.analyzeRelationship(source,target);
String aa = "属于";
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
KcInfoRelation zz = new KcInfoRelation();
zz.setRelation(aa);
zz.setSourceId(OneId);
zz.setTargetId(TwoId);
zz.setSourceName(source);
zz.setTargetName(target);
zz.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz);
}
}
//获取所有的三级标题
List<KcInfo> list3 = kcFileMapper.getHeaderByLevel(3l,fileId);
for(int a2=0;a2<list3.size();a2++){
Long OneId = list3.get(a2).getId();
List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
for(int i=0;i<list1.size();i++){
String source = list3.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
KcInfoRelation zz = new KcInfoRelation();
zz.setRelation(aa);
zz.setSourceId(OneId);
zz.setTargetId(TwoId);
zz.setSourceName(source);
zz.setTargetName(target);
zz.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz);
}
}
//获取所有的四级标题
List<KcInfo> list4 = kcFileMapper.getHeaderByLevel(4l,fileId);
for(int a2=0;a2<list4.size();a2++){
Long OneId = list4.get(a2).getId();
List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
for(int i=0;i<list1.size();i++){
String source = list4.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
KcInfoRelation zz = new KcInfoRelation();
zz.setRelation(aa);
zz.setSourceId(OneId);
zz.setTargetId(TwoId);
zz.setSourceName(source);
zz.setTargetName(target);
zz.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz);
}
}
//获取所有的五级文件
List<KcInfo> list5 = kcFileMapper.getHeaderByLevel(5l,fileId);
for(int a2=0;a2<list5.size();a2++){
Long OneId = list5.get(a2).getId();
List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
for(int i=0;i<list1.size();i++){
String source = list5.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
KcInfoRelation zz = new KcInfoRelation();
zz.setRelation(aa);
zz.setSourceId(OneId);
zz.setTargetId(TwoId);
zz.setSourceName(source);
zz.setTargetName(target);
zz.setCreateTime(new Date());
kcInfoRelationMapper.insertKcRelationship(zz);
}
}
return "a";
}
// public String getRelationShip1(Integer fileId,String parentId,String relation){
//
//
//
// //先获取所有的第一级标题
// List<KcInfo> list = kcFileMapper.getHeaderByLevel(1l,fileId);
//
// //查询选择的上级
// KcInfo aan = new KcInfo();
// aan.setId(Long.valueOf(parentId));
// KcInfo ddo = kcFile1Mapper.selectDocById(aan);
// String name = ddo.getName();
//
// Long leve = 0l;
//
//
//
//
// if(list.size()!=0){
// for(int a=0;a<list.size();a++){
// String source = list.get(a).getName();
// Long OneId = list.get(a).getId();
//
//
// String name1 = name;
// String name2 = source;
// KcInfoRelation zz1 = new KcInfoRelation();
// zz1.setRelation(relation);
// zz1.setSourceId(Long.valueOf(parentId));
// zz1.setTargetId(OneId);
// zz1.setSourceName(name1);
// zz1.setTargetName(name2);
// zz1.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz1);
//
//
// List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
//
// for(int i=0;i<list1.size();i++){
//
// String target = list1.get(i).getName();
// String aa = "属于";
//// String aa = relationUtil.analyzeRelationship(source,target);
// System.out.println(aa);
// //获取两个的Id
// Long TwoId = list1.get(i).getId();
// KcInfoRelation zz = new KcInfoRelation();
// zz.setRelation(aa);
// zz.setSourceId(OneId);
// zz.setTargetId(TwoId);
// zz.setSourceName(source);
// zz.setTargetName(target);
// zz.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz);
// }
// }
// leve =1l;
// }
//
//
// //获取所有的二级标题
// List<KcInfo> list2 = kcFileMapper.getHeaderByLevel(2l,fileId);
// if(list2.size()!=0){
//
// for(int a1=0;a1<list2.size();a1++){
// Long OneId = list2.get(a1).getId();
//
// if(leve==0){
// String source = list2.get(a1).getName();
// String name1 = name;
// String name2 = source;
// KcInfoRelation zz1 = new KcInfoRelation();
// zz1.setRelation(relation);
// zz1.setSourceId(Long.valueOf(parentId));
// zz1.setTargetId(OneId);
// zz1.setSourceName(name1);
// zz1.setTargetName(name2);
// zz1.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz1);
// }
// List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
// for(int i=0;i<list1.size();i++){
// String source = list2.get(a1).getName();
// String target = list1.get(i).getName();
// String aa = "属于";
//// String aa = relationUtil.analyzeRelationship(source,target);
// System.out.println(aa);
// //获取两个的Id
// Long TwoId = list1.get(i).getId();
// KcInfoRelation zz = new KcInfoRelation();
// zz.setRelation(aa);
// zz.setSourceId(OneId);
// zz.setTargetId(TwoId);
// zz.setSourceName(source);
// zz.setTargetName(target);
// zz.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz);
//
// }
// }
// leve = 2l;
//
// }
//
// //获取所有的三级标题
// List<KcInfo> list3 = kcFileMapper.getHeaderByLevel(3l,fileId);
// if(list3.size()!=0){
// for(int a2=0;a2<list3.size();a2++){
// Long OneId = list3.get(a2).getId();
// if(leve==0){
// String source = list3.get(a2).getName();
// String name1 = name;
// String name2 = source;
// KcInfoRelation zz1 = new KcInfoRelation();
// zz1.setRelation(relation);
// zz1.setSourceId(Long.valueOf(parentId));
// zz1.setTargetId(OneId);
// zz1.setSourceName(name1);
// zz1.setTargetName(name2);
// zz1.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz1);
// }
// List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
// for(int i=0;i<list1.size();i++){
// String source = list3.get(a2).getName();
// String target = list1.get(i).getName();
// String aa = "属于";
//// String aa = relationUtil.analyzeRelationship(source,target);
// System.out.println(aa);
// //获取两个的Id
// Long TwoId = list1.get(i).getId();
// KcInfoRelation zz = new KcInfoRelation();
// zz.setRelation(aa);
// zz.setSourceId(OneId);
// zz.setTargetId(TwoId);
// zz.setSourceName(source);
// zz.setTargetName(target);
// zz.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz);
//
// }
// }
// leve = 3l;
// }
//
//
//
// //获取所有的四级标题
// List<KcInfo> list4 = kcFileMapper.getHeaderByLevel(4l,fileId);
// if(list4.size()!=0){
// for(int a2=0;a2<list4.size();a2++){
// Long OneId = list4.get(a2).getId();
// if(leve==0){
// String source = list4.get(a2).getName();
// String name1 = name;
// String name2 = source;
// KcInfoRelation zz1 = new KcInfoRelation();
// zz1.setRelation(relation);
// zz1.setSourceId(Long.valueOf(parentId));
// zz1.setTargetId(OneId);
// zz1.setSourceName(name1);
// zz1.setTargetName(name2);
// zz1.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz1);
// }
// List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
// for(int i=0;i<list1.size();i++){
// String source = list4.get(a2).getName();
// String target = list1.get(i).getName();
// String aa = "属于";
//// String aa = relationUtil.analyzeRelationship(source,target);
// System.out.println(aa);
// //获取两个的Id
// Long TwoId = list1.get(i).getId();
// KcInfoRelation zz = new KcInfoRelation();
// zz.setRelation(aa);
// zz.setSourceId(OneId);
// zz.setTargetId(TwoId);
// zz.setSourceName(source);
// zz.setTargetName(target);
// zz.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz);
//
// }
// }
// leve=4l;
// }
//
//
// //获取所有的五级文件
// List<KcInfo> list5 = kcFileMapper.getHeaderByLevel(5l,fileId);
// if(list5.size()!=0){
// for(int a2=0;a2<list5.size();a2++){
// Long OneId = list5.get(a2).getId();
// if(leve==0){
// String source = list5.get(a2).getName();
// String name1 = name;
// String name2 = source;
// KcInfoRelation zz1 = new KcInfoRelation();
// zz1.setRelation(relation);
// zz1.setSourceId(Long.valueOf(parentId));
// zz1.setTargetId(OneId);
// zz1.setSourceName(name1);
// zz1.setTargetName(name2);
// zz1.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz1);
// }
// List<KcInfo> list1 = kcFileMapper.getOneHeader(OneId);
// for(int i=0;i<list1.size();i++){
// String source = list5.get(a2).getName();
// String target = list1.get(i).getName();
// String aa = "属于";
//// String aa = relationUtil.analyzeRelationship(source,target);
//
// System.out.println(aa);
// //获取两个的Id
// Long TwoId = list1.get(i).getId();
// KcInfoRelation zz = new KcInfoRelation();
// zz.setRelation(aa);
// zz.setSourceId(OneId);
// zz.setTargetId(TwoId);
// zz.setSourceName(source);
// zz.setTargetName(target);
// zz.setCreateTime(new Date());
// kcInfoRelationMapper.insertKcRelationship(zz);
//
// }
// }
// leve = 5l;
// }
//
//
// return "a";
// }
//生成图谱
public String createGraph(){
//先获取所有的文章实体
//查询对应的大文章
List<List<Map>> list2 = new ArrayList<>();
List<KcTlFile> ll = kcFile1Mapper.getFileList();
for(int b=0;b<ll.size();b++){
List<Map> list1 = new ArrayList<>();
String name = ll.get(b).getFileName().substring(0,ll.get(b).getFileName().length()-5);
//获取这个大文章下的所有文章;
name = name.replace("+","&");
name = name.replace("-","为");
name = name.replace(" ","");
name = name.replace(".","");
Map<String,String> bb1 = new HashMap<>();
bb1.put("key","name");
bb1.put("value",name);
list1.add(bb1);
Map<String,String> bb2 = new HashMap<>();
bb2.put("key","docId");
bb2.put("value", String.valueOf(ll.get(b).getId()));
list1.add(bb2);
Map<String,String> bb3 = new HashMap<>();
bb3.put("key","leve");
bb3.put("value","leve0");
list1.add(bb3);
// System.out.println(list1.size());
list2.add(list1);
List<KcInfo> list = kcFile1Mapper.selectAllDocByfileId(Math.toIntExact(ll.get(b).getId()));
for(int i=0;i<list.size();i++){
List<Map> listNew = new ArrayList<>();
KcInfo info = list.get(i);
Class<?> clazz = KcInfo.class;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
System.out.println("字段名称:" + fieldName);
Map<String,String> bb = new HashMap<>();
if(fieldName.equals("id")){
bb.put("key","docId");
bb.put("value", String.valueOf(info.getId()));
listNew.add(bb);
}
if(fieldName.equals("name")){
bb.put("key","name");
bb.put("value", info.getName());
listNew.add(bb);
}
if(fieldName.equals("parentId")){
bb.put("key","parentId");
bb.put("value", String.valueOf(info.getParentId()));
listNew.add(bb);
}
if(fieldName.equals("groupId")){
bb.put("key","groupId");
bb.put("value", String.valueOf(info.getGroupId()));
listNew.add(bb);
}
if(fieldName.equals("level")){
if(info.getLevel()==1){
bb.put("key","leve");
bb.put("value","leve1");
}
if(info.getLevel()==2){
bb.put("key","leve");
bb.put("value","leve2");
}
if(info.getLevel()==3){
bb.put("key","leve");
bb.put("value","leve3");
}
if(info.getLevel()==4){
bb.put("key","leve");
bb.put("value","leve4");
}
if(info.getLevel()==5){
bb.put("key","leve");
bb.put("value","leve5");
}
if(info.getLevel()==6){
bb.put("key","leve");
bb.put("value","leve6");
}
listNew.add(bb);
}
}
list2.add(listNew);
}
for(int bb=0;bb<list2.size();bb++){
String docId = "";
String cql = "create (doc: Doc{";
for(int a=0;a<list2.get(bb).size();a++){
if(list2.get(bb).get(a).get("key").equals("docId")){
docId = String.valueOf(list2.get(bb).get(a).get("value"));
}
if (list2.get(bb).size()>1){
if(a==0){
cql = cql + list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'";
}else if(a==(list2.get(bb).size()-1)){
cql = cql +","+list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'})";
}else {
cql = cql +","+ list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'";
}
}else {
cql = cql + list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'}) return doc";
}
}
System.out.println(cql);
StatementResult result = neo4jUtil.excuteCypherSql(cql);
//创建了之后将创建的实体和关系进行update
if(Long.valueOf(docId)>100){
int aa = kcFile1Mapper.updateGraphStatus(Long.valueOf(docId));
}
System.out.println(result.keys());
}
}
//获取所有关系
List<KcInfoRelation> listR = kcInfoRelationMapper.getAllRelation();
System.out.println(listR.size());
for(int i=0;i<listR.size();i++){
List<String> charList = new ArrayList<>();
for (int j = 0; j < listR.get(i).getRelation().length(); j++) {
if(!listR.get(i).getRelation().substring(j, j + 1).equals("、")){
charList.add(listR.get(i).getRelation().substring(j, j + 1));
}
}
String re = "";
for (int k=0;k<charList.size();k++){
re = re + charList.get(k);
}
listR.get(i).setRelation(re);
}
//创建
for(int i=0;i<listR.size();i++){
String cql = "MATCH (a:Doc),(b:Doc) WHERE a.docId = '"+listR.get(i).getSourceId() +"' AND " + "b.docId = '"+listR.get(i).getTargetId()+"'";
cql = cql + " CREATE (a)-[r:"+String.valueOf(listR.get(i).getRelation())+"{ name: '"+String.valueOf(listR.get(i).getRelation())+"'}] -> (b) RETURN r";
StatementResult result = neo4jUtil.excuteCypherSql(cql);
int bb = kcFile1Mapper.updateGraphRelation(listR.get(i));
}
return "aa";
}
// public String createGraph1(){
// //先获取所有的文章实体
//
//
// //查询对应的大文章
// List<List<Map>> list2 = new ArrayList<>();
// List<KcTlFile> ll = kcFile1Mapper.getFileList();
// for(int b=0;b<ll.size();b++){
// List<Map> list1 = new ArrayList<>();
//
// List<KcInfo> list = kcFile1Mapper.selectAllDocByfileId(Math.toIntExact(ll.get(b).getId()));
//
// for(int i=0;i<list.size();i++){
// List<Map> listNew = new ArrayList<>();
// KcInfo info = list.get(i);
// Class<?> clazz = KcInfo.class;
// Field[] fields = clazz.getDeclaredFields();
//
// for (Field field : fields) {
// String fieldName = field.getName();
// System.out.println("字段名称:" + fieldName);
// Map<String,String> bb = new HashMap<>();
// if(fieldName.equals("id")){
// bb.put("key","docId");
// bb.put("value", String.valueOf(info.getId()));
// listNew.add(bb);
// }
// if(fieldName.equals("name")){
// bb.put("key","name");
// bb.put("value", info.getName());
//
// listNew.add(bb);
// }
//// if(fieldName.equals("level")){
//// if(info.getLevel()==1){
//// bb.put("key","leve");
//// bb.put("value","leve1");
//// }
//// if(info.getLevel()==2){
//// bb.put("key","leve");
//// bb.put("value","leve2");
//// }
//// if(info.getLevel()==3){
//// bb.put("key","leve");
//// bb.put("value","leve3");
//// }
//// if(info.getLevel()==4){
//// bb.put("key","leve");
//// bb.put("value","leve4");
//// }
//// if(info.getLevel()==5){
//// bb.put("key","leve");
//// bb.put("value","leve5");
//// }
//// if(info.getLevel()==6){
//// bb.put("key","leve");
//// bb.put("value","leve6");
//// }
//// listNew.add(bb);
//// }
//
// }
// list2.add(listNew);
//
// }
//
// for(int bb=0;bb<list2.size();bb++){
// String docId = "";
// String cql = "create (doc: Doc{";
// for(int a=0;a<list2.get(bb).size();a++){
// System.out.println(list2.get(bb).size());
//// if(bb==0){
//// System.out.println(list2.get(bb).get(0));
//// System.out.println(list2.get(bb).get(1));
//// System.out.println(list2.get(bb).get(2));
//// }
// if(list2.get(bb).get(a).get("key").equals("docId")){
// docId = String.valueOf(list2.get(bb).get(a).get("value"));
// }
//
//
// if (list2.get(bb).size()>1){
// if(a==0){
// cql = cql + list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'";
// }else if(a==(list2.get(bb).size()-1)){
// cql = cql +","+list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'})";
// }else {
// cql = cql +","+ list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'";
// }
// }else {
// cql = cql + list2.get(bb).get(a).get("key")+":'"+list2.get(bb).get(a).get("value")+"'}) return doc";
// }
//
// }
//
// System.out.println(cql);
// StatementResult result = neo4jUtil.excuteCypherSql(cql);
// //创建了之后将创建的实体和关系进行update
// if(Long.valueOf(docId)>100){
// int aa = kcFile1Mapper.updateGraphStatus(Long.valueOf(docId));
// }
// System.out.println(result.keys());
//
//
//
// }
//
// }
//
//
//
//
//
//
// //获取所有关系
// List<KcInfoRelation> listR = kcInfoRelationMapper.getAllRelation();
// System.out.println(listR.size());
//
// for(int i=0;i<listR.size();i++){
// List<String> charList = new ArrayList<>();
//
// for (int j = 0; j < listR.get(i).getRelation().length(); j++) {
// if(!listR.get(i).getRelation().substring(j, j + 1).equals("、")){
// charList.add(listR.get(i).getRelation().substring(j, j + 1));
// }
// }
//
// String re = "";
// for (int k=0;k<charList.size();k++){
// re = re + charList.get(k);
// }
// listR.get(i).setRelation(re);
// }
//
//
// //创建
// for(int i=0;i<listR.size();i++){
//
// String cql = "MATCH (a:Doc),(b:Doc) WHERE a.docId = '"+listR.get(i).getSourceId() +"' AND " + "b.docId = '"+listR.get(i).getTargetId()+"'";
// cql = cql + " CREATE (a)-[r:"+String.valueOf(listR.get(i).getRelation())+"{ name: '"+String.valueOf(listR.get(i).getRelation())+"'}] -> (b) RETURN r";
// StatementResult result = neo4jUtil.excuteCypherSql(cql);
// int bb = kcFile1Mapper.updateGraphRelation(listR.get(i));
// }
//
//
//
//
//
// return "aa";
// }
}
Loading…
Cancel
Save