|
|
|
@ -275,6 +275,258 @@ public class ZhyFileManageController extends BaseController { |
|
|
|
// 默认 fallback
|
|
|
|
return isDefinitelyUtf8 ? "UTF-8" : "GBK"; |
|
|
|
} |
|
|
|
// @PostMapping("/insertRelationByFile")
|
|
|
|
// public AjaxResult insertRelationByFile(MultipartFile file) throws Throwable {
|
|
|
|
// // 1. 校验文件是否为空
|
|
|
|
// if (file.isEmpty()) {
|
|
|
|
// return AjaxResult.error("上传的文件为空");
|
|
|
|
// }
|
|
|
|
// String filePath1 =ARTICLE_DIR;
|
|
|
|
// // 2. 读取 CSV 文件(使用 GBK 编码)
|
|
|
|
// List<Map<String, String>> dataList = new ArrayList<>();
|
|
|
|
// String[] headers = null;
|
|
|
|
//
|
|
|
|
// InputStream inputStream = null;
|
|
|
|
// BufferedReader reader = null;
|
|
|
|
// try {
|
|
|
|
// inputStream = file.getInputStream();
|
|
|
|
//
|
|
|
|
// // 🔍 第一步:探测编码
|
|
|
|
// String encoding = detectCsvEncoding(inputStream);
|
|
|
|
// System.out.println("自动检测到编码: " + encoding);
|
|
|
|
//
|
|
|
|
// // 重新打开流(因为探测时已读取部分数据)
|
|
|
|
// inputStream.close();
|
|
|
|
// inputStream = file.getInputStream();
|
|
|
|
//
|
|
|
|
// reader = new BufferedReader(new InputStreamReader(inputStream, encoding));
|
|
|
|
//
|
|
|
|
// String line;
|
|
|
|
// int lineNumber = 0;
|
|
|
|
//
|
|
|
|
// while ((line = reader.readLine()) != null) {
|
|
|
|
// lineNumber++;
|
|
|
|
//
|
|
|
|
// // 去除 BOM(如果存在)
|
|
|
|
// if (lineNumber == 1 && line.startsWith("\uFEFF")) {
|
|
|
|
// line = line.substring(1);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // ✅ 替换 split(",") 为能处理引号内逗号的解析方法
|
|
|
|
// String[] row = parseCsvLine(line);
|
|
|
|
// for (int i = 0; i < row.length; i++) {
|
|
|
|
// row[i] = row[i].trim(); // 去空格
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (lineNumber == 1) {
|
|
|
|
// // 第一行是表头
|
|
|
|
// headers = row;
|
|
|
|
// continue; // 跳过第一行,不加入数据
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // 将每一行转为 Map<String, String>
|
|
|
|
// Map<String, String> rowMap = new HashMap<>();
|
|
|
|
// for (int i = 0; i < headers.length && i < row.length; i++) {
|
|
|
|
// String key = headers[i].trim();
|
|
|
|
// // 只保留你需要的字段
|
|
|
|
// if (Arrays.asList("ck", "main_keyword", "sub_keyword", "title", "authors", "keywords", "abstract", "url", "urls").contains(key)) {
|
|
|
|
// rowMap.put(key, row[i]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// dataList.add(rowMap);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// return AjaxResult.error("读取文件时发生错误:" + e.getMessage());
|
|
|
|
// } finally {
|
|
|
|
// try {
|
|
|
|
// if (reader != null) reader.close();
|
|
|
|
// if (inputStream != null) inputStream.close();
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// List<ZhyDocRelation> allRelation=new ArrayList<>();
|
|
|
|
// // 3. 处理每一行数据(和你原来逻辑一致)
|
|
|
|
// for (Map<String, String> data : dataList) {
|
|
|
|
// System.out.println("44444444444444");
|
|
|
|
// System.out.println(data);
|
|
|
|
// String mainKeyword = data.get("main_keyword");
|
|
|
|
// Pattern pattern = Pattern.compile("'([^']*)'");
|
|
|
|
// Matcher matcher = pattern.matcher(mainKeyword);
|
|
|
|
//
|
|
|
|
// ArrayList<String> mainKeywordList = new ArrayList<>();
|
|
|
|
// while (matcher.find()) {
|
|
|
|
// mainKeywordList.add(matcher.group(1)); // 提取引号内的内容
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//// System.out.println(mainKeywordList);
|
|
|
|
// Map mapLevel=new HashMap();
|
|
|
|
// for (int i=0;i<mainKeywordList.size();i++){
|
|
|
|
//// System.out.println(mainKeywordList.get(i));
|
|
|
|
// ZhyDoc doc=test1Mapper.selectDocByTitle(mainKeywordList.get(i));
|
|
|
|
// if(doc!=null){
|
|
|
|
// if(doc.getDocLevel()==1l){
|
|
|
|
// mapLevel.put("l1",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
// if(doc.getDocLevel()==2l){
|
|
|
|
// mapLevel.put("l2",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
// if(doc.getDocLevel()==3l){
|
|
|
|
// mapLevel.put("l3",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
// if(doc.getDocLevel()==4l){
|
|
|
|
// mapLevel.put("l4",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
// if(doc.getDocLevel()==5l){
|
|
|
|
// mapLevel.put("l5",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
// if(doc.getDocLevel()==6l){
|
|
|
|
// mapLevel.put("l6",mainKeywordList.get(i));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//// System.out.println(mapLevel);
|
|
|
|
// //如果选择了l1,则看是否选择l2、l3、l4、l5、l6
|
|
|
|
// String[] levels = {"l1", "l2", "l3", "l4", "l5", "l6"};
|
|
|
|
// int n = levels.length;
|
|
|
|
//
|
|
|
|
// for (int i = 0; i < n - 1; i++) {
|
|
|
|
// String parentKey = levels[i];
|
|
|
|
// if (mapLevel.containsKey(parentKey)) {
|
|
|
|
// String parentValue = mapLevel.get(parentKey).toString();
|
|
|
|
// for (int j = i + 1; j < n; j++) {
|
|
|
|
// String childKey = levels[j];
|
|
|
|
// if (mapLevel.containsKey(childKey)) {
|
|
|
|
// String childValue = mapLevel.get(childKey).toString();
|
|
|
|
//
|
|
|
|
// ZhyDocRelation relation=new ZhyDocRelation();
|
|
|
|
// relation.setSourceName(parentValue);
|
|
|
|
// relation.setTargetName(childValue);
|
|
|
|
// List<Map> relations=zhyDocRelationMapper.selectZhyRelationshipArticle(relation);
|
|
|
|
//// System.out.println(relations);
|
|
|
|
// if(relations.size()>0){
|
|
|
|
// boolean flag=false;
|
|
|
|
// for (int p=0;p<relations.size();p++){
|
|
|
|
// if(relations.get(p).get("articleName").equals(data.get("title"))) {
|
|
|
|
// flag=true;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if(!flag){
|
|
|
|
// createRelation(parentValue, childValue,data,filePath1);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }else{
|
|
|
|
// //插入关系
|
|
|
|
//
|
|
|
|
// createRelation(parentValue, childValue,data,filePath1);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
////
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// return AjaxResult.success().put("msg", "成功");
|
|
|
|
// }
|
|
|
|
public void createRelation(String parentValue,String childValue,Map<String, String> data,String local) throws IOException { |
|
|
|
ZhyDoc doc1 = test1Mapper.selectDocByTitle(parentValue); |
|
|
|
ZhyDoc doc2 = test1Mapper.selectDocByTitle(childValue); |
|
|
|
if (doc1 != null && doc2 != null) { |
|
|
|
List<ZhyDoc> allDocs = test1Mapper.selectAllDoc1(); |
|
|
|
for (ZhyDoc doc : allDocs) { |
|
|
|
if (doc.getDocTitle().equals(childValue)) { |
|
|
|
doc.setDocParentId(doc1.getId()); |
|
|
|
String updateCql = "MATCH (doc:Doc {docId: '" + doc.getId() + "'}) " + |
|
|
|
"SET doc.docParentId = '" + doc1.getId() + "' " + |
|
|
|
"RETURN doc"; |
|
|
|
neo4jUtil.excuteCypherSql(updateCql); |
|
|
|
} |
|
|
|
} |
|
|
|
// 插入文章
|
|
|
|
ZhyArticle article = new ZhyArticle(); |
|
|
|
article.setName(data.get("title")); |
|
|
|
// article.setOpenUrl(data.get("urls"));
|
|
|
|
File file = new File(ARTICLE_PDF_DIR, article.getName()+".pdf"); |
|
|
|
System.out.println("文件路径: " + file.getAbsolutePath()); |
|
|
|
article.setLocalUrl(file.getAbsolutePath()); |
|
|
|
article.setAuthors(data.get("authors")); |
|
|
|
article.setKeywords(data.get("keywords")); |
|
|
|
article.setSummary(data.get("abstract")); |
|
|
|
article.setUrls(data.get("urls")); |
|
|
|
article.setSourceId(doc1.getId()); |
|
|
|
article.setSourceName(doc1.getDocTitle()); |
|
|
|
article.setTargetId(doc2.getId()); |
|
|
|
article.setTagertName(doc2.getDocTitle()); |
|
|
|
|
|
|
|
zhyArticleMapper.insertZhyArticle(article); |
|
|
|
|
|
|
|
// 插入关系
|
|
|
|
ZhyDocRelation zz = new ZhyDocRelation(); |
|
|
|
zz.setRelationship("相关"); // 你可能需要从 CSV 中读取关系类型,或固定值
|
|
|
|
zz.setSource(doc1.getId()); |
|
|
|
zz.setTarget(doc2.getId()); |
|
|
|
zz.setSourceName(doc1.getDocTitle()); |
|
|
|
zz.setTargetName(doc2.getDocTitle()); |
|
|
|
zz.setCreateTime(new Date()); |
|
|
|
zz.setArticleId(article.getId()); |
|
|
|
System.out.println(zz); |
|
|
|
zhyDocRelationMapper.insertZhyRelationship(zz); |
|
|
|
List<String> ids = new ArrayList<>(); |
|
|
|
ids.add(String.valueOf(zz.getId())); |
|
|
|
ESDao esDao = new ESDao(); |
|
|
|
esDao.id = article.getId().toString(); |
|
|
|
esDao.DBid = article.getId().toString(); |
|
|
|
esDao.type = "line"; |
|
|
|
if(article.getLocalUrl()!=null){ |
|
|
|
System.out.println("333333"); |
|
|
|
try { |
|
|
|
String txt=article.getLocalUrl().replace("pdf", "txt"); |
|
|
|
List<String> content = extractContentUntilSecondPage(txt); |
|
|
|
if (content != null) { |
|
|
|
// 打印结果
|
|
|
|
for (String line : content) { |
|
|
|
System.out.println(line); |
|
|
|
} |
|
|
|
String result = String.join("", content); |
|
|
|
esDao.abstracts = result; |
|
|
|
} else { |
|
|
|
System.out.println("未找到相关内容或文件不存在。"); |
|
|
|
esDao.abstracts = article.getName()+article.getKeywords()+article.getSummary(); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
esDao.abstracts = article.getName()+article.getKeywords()+article.getSummary(); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
esDao.abstracts = article.getName()+article.getKeywords()+article.getSummary(); |
|
|
|
} |
|
|
|
esDao.data = ids.toString(); |
|
|
|
esService.insertDocById("links",esDao); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
// 4. 批量创建 Neo4j 关系
|
|
|
|
List<ZhyDocRelation> listR = zhyDocRelationMapper.getAllRelation(); |
|
|
|
for (ZhyDocRelation rel : listR) { |
|
|
|
String cql = "MATCH (a:Doc),(b:Doc) " + |
|
|
|
"WHERE a.docId = '" + rel.getSource() + "' AND b.docId = '" + rel.getTarget() + "' " + |
|
|
|
"CREATE (a)-[r:" + rel.getRelationship() + " {name: '" + rel.getRelationship() +"', DbId: '"+rel.getId()+ "'}]->(b) " + |
|
|
|
"RETURN r"; |
|
|
|
|
|
|
|
neo4jUtil.excuteCypherSql(cql); |
|
|
|
test1Mapper.updateGraphRelation(rel); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
@PostMapping("/insertRelationByFile") |
|
|
|
public AjaxResult insertRelationByFile(MultipartFile file) throws Throwable { |
|
|
|
// 1. 校验文件是否为空
|
|
|
|
@ -357,6 +609,14 @@ public class ZhyFileManageController extends BaseController { |
|
|
|
Map<String, String> data = dataList.get(i); |
|
|
|
|
|
|
|
String title1=data.get("main_keyword"); |
|
|
|
String mainKeyword = data.get("main_keyword"); |
|
|
|
Pattern pattern = Pattern.compile("'([^']*)'"); |
|
|
|
Matcher matcher = pattern.matcher(mainKeyword); |
|
|
|
|
|
|
|
ArrayList<String> mainKeywordList = new ArrayList<>(); |
|
|
|
while (matcher.find()) { |
|
|
|
mainKeywordList.add(matcher.group(1)); // 提取引号内的内容
|
|
|
|
} |
|
|
|
System.out.println(title1); |
|
|
|
String title2=data.get("sub_keyword"); |
|
|
|
System.out.println(title2); |
|
|
|
|