| @@ -0,0 +1,30 @@ | |||
| package com.ruoyi.common.core.enums; | |||
| /** | |||
| * 用户状态 | |||
| * | |||
| * @author ruoyi | |||
| */ | |||
| public enum KgStatus | |||
| { | |||
| ONLINE(1, "在线"), OFFLINE(0, "离线"); | |||
| private final Integer code; | |||
| private final String info; | |||
| KgStatus(Integer code, String info) | |||
| { | |||
| this.code = code; | |||
| this.info = info; | |||
| } | |||
| public Integer getCode() | |||
| { | |||
| return code; | |||
| } | |||
| public String getInfo() | |||
| { | |||
| return info; | |||
| } | |||
| } | |||
| @@ -0,0 +1,30 @@ | |||
| package com.ruoyi.common.core.enums; | |||
| /** | |||
| * 用户状态 | |||
| * | |||
| * @author ruoyi | |||
| */ | |||
| public enum KgUpdateMethod | |||
| { | |||
| PENDING_UPDATE(0, "待更新"), INCREMENTAL_UPDATE(1, "增量更新"), FULL_UPDATE(2, "全量更新"); | |||
| private final Integer code; | |||
| private final String info; | |||
| KgUpdateMethod(Integer code, String info) | |||
| { | |||
| this.code = code; | |||
| this.info = info; | |||
| } | |||
| public Integer getCode() | |||
| { | |||
| return code; | |||
| } | |||
| public String getInfo() | |||
| { | |||
| return info; | |||
| } | |||
| } | |||
| @@ -87,70 +87,4 @@ public class KgInfoController extends BaseController { | |||
| return R.ok(); | |||
| } | |||
| /** | |||
| * 图谱版本列表 | |||
| * | |||
| * @param kgInfoIdDTO | |||
| * @return | |||
| */ | |||
| @GetMapping("/version/list") | |||
| public TableDataInfo versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| startPage(); | |||
| List<KnowledgeGraphVersion> kgInfo = kgInfoService.versionList(kgInfoIdDTO); | |||
| return getDataTable(kgInfo); | |||
| } | |||
| /** | |||
| * 图谱版本导出 | |||
| * | |||
| * @param response | |||
| * @param kgInfoIdDTO | |||
| */ | |||
| @GetMapping("/version/export") | |||
| public void versionExport(HttpServletResponse response, KgInfoIdDTO kgInfoIdDTO) { | |||
| KgInfo KgInfo = kgInfoService.getKgInfo(kgInfoIdDTO); | |||
| ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class); | |||
| util.exportExcel(response, null, "图谱数据"); | |||
| } | |||
| /** | |||
| * 图谱版本导入 | |||
| * | |||
| * @param file | |||
| * @param kgId | |||
| */ | |||
| @PostMapping("/version/upload") | |||
| public R<String> uploadCsv(@RequestParam("file") MultipartFile file,@RequestParam("kgId") String kgId) { | |||
| if (file == null || file.isEmpty()) { | |||
| throw new IllegalArgumentException("文件不能为空"); | |||
| } | |||
| if (!file.getOriginalFilename().endsWith(".csv")) { | |||
| throw new IllegalArgumentException("仅支持 CSV 文件"); | |||
| } | |||
| kgInfoService.uploadCsv(file, kgId); | |||
| return R.ok(); | |||
| } | |||
| /** | |||
| * 图谱版本全量更新 | |||
| * | |||
| * @param id 版本id | |||
| */ | |||
| @PostMapping("/version/fullUpdate") | |||
| public R<String> fullUpdate(@RequestParam("id") Long id) { | |||
| kgInfoService.fullUpdate(id); | |||
| return R.ok(); | |||
| } | |||
| /** | |||
| * 图谱版本增量更新 | |||
| * | |||
| * @param id | |||
| */ | |||
| @PostMapping("/version/incrementalUpdate") | |||
| public void incrementalUpdate(@RequestParam("id") Long id) { | |||
| kgInfoService.incrementalUpdate(id); | |||
| } | |||
| } | |||
| @@ -0,0 +1,118 @@ | |||
| package com.ruoyi.platform.controller.kg; | |||
| import com.ruoyi.common.core.domain.R; | |||
| import com.ruoyi.common.core.utils.poi.ExcelUtil; | |||
| import com.ruoyi.common.core.web.controller.BaseController; | |||
| import com.ruoyi.common.core.web.page.TableDataInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfo; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import com.ruoyi.platform.service.KgVersionService; | |||
| import com.ruoyi.system.api.domain.SysDictData; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.util.List; | |||
| /** | |||
| * 知识图谱版本管理 | |||
| */ | |||
| @RestController | |||
| @RequestMapping("/kg/version") | |||
| public class KgVersionController extends BaseController { | |||
| @Autowired | |||
| private KgVersionService kgVersionService; | |||
| /** | |||
| * 图谱版本列表 | |||
| * | |||
| * @param kgInfoIdDTO | |||
| * @return | |||
| */ | |||
| @GetMapping("/list") | |||
| public TableDataInfo versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| startPage(); | |||
| List<KnowledgeGraphVersion> kgInfo = kgVersionService.versionList(kgInfoIdDTO); | |||
| return getDataTable(kgInfo); | |||
| } | |||
| /** | |||
| * 图谱版本导出 | |||
| * | |||
| * @param response | |||
| * @param kgInfoIdDTO | |||
| */ | |||
| @GetMapping("/export") | |||
| public void versionExport(HttpServletResponse response, KgInfoIdDTO kgInfoIdDTO) { | |||
| // KgInfo KgInfo = kgVersionService.getKgInfo(kgInfoIdDTO); | |||
| ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class); | |||
| util.exportExcel(response, null, "图谱数据"); | |||
| } | |||
| /** | |||
| * 图谱版本导入 | |||
| * | |||
| * @param file | |||
| * @param kgId | |||
| */ | |||
| @PostMapping("/upload") | |||
| public R<String> uploadCsv(@RequestParam("file") MultipartFile file,@RequestParam("kgId") String kgId) { | |||
| if (file == null || file.isEmpty()) { | |||
| throw new IllegalArgumentException("文件不能为空"); | |||
| } | |||
| if (!file.getOriginalFilename().endsWith(".csv")) { | |||
| throw new IllegalArgumentException("仅支持 CSV 文件"); | |||
| } | |||
| kgVersionService.uploadCsv(file, kgId); | |||
| return R.ok("操作成功"); | |||
| } | |||
| /** | |||
| * 图谱版本全量更新 | |||
| * | |||
| * @param id 版本id | |||
| */ | |||
| @PostMapping("/fullUpdate") | |||
| public R<String> fullUpdate(@RequestParam("id") Long id) { | |||
| kgVersionService.fullUpdate(id); | |||
| return R.ok("操作成功"); | |||
| } | |||
| /** | |||
| * 图谱版本增量更新 | |||
| * | |||
| * @param id | |||
| */ | |||
| @PostMapping("/incrementalUpdate") | |||
| public R<String> incrementalUpdate(@RequestParam("id") Long id) { | |||
| kgVersionService.incrementalUpdate(id); | |||
| return R.ok("操作成功"); | |||
| } | |||
| /** | |||
| * 图谱版本回退 | |||
| * | |||
| * @param versionId | |||
| */ | |||
| @PostMapping("/rollback") | |||
| public R<String> rollback(@RequestParam("id") Long versionId) { | |||
| kgVersionService.rollback(versionId); | |||
| return R.ok("操作成功"); | |||
| } | |||
| /** | |||
| * 图谱版本删除 | |||
| * | |||
| * @param versionId | |||
| */ | |||
| @DeleteMapping("/{versionId}") | |||
| public R<String> delete(@PathVariable Long versionId) { | |||
| kgVersionService.rollback(versionId); | |||
| return R.ok("操作成功"); | |||
| } | |||
| } | |||
| @@ -92,5 +92,8 @@ public interface KnowledgeGraphVersionDao { | |||
| int deleteById(Long id); | |||
| Integer queryMaxVersion(String kgId); | |||
| KnowledgeGraphVersion onlineFind(); | |||
| } | |||
| @@ -20,12 +20,4 @@ public interface KgInfoService { | |||
| KgInfo getKgInfo(KgInfoIdDTO kgInfoIdDTO); | |||
| void deleteKgInfoById(Long id); | |||
| List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO); | |||
| void uploadCsv(MultipartFile file,String kgId); | |||
| void fullUpdate(Long id); | |||
| void incrementalUpdate(Long id); | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.ruoyi.platform.service; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.util.List; | |||
| public interface KgVersionService { | |||
| List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO); | |||
| void uploadCsv(MultipartFile file,String kgId); | |||
| void fullUpdate(Long id); | |||
| void incrementalUpdate(Long id); | |||
| void rollback(Long versionId); | |||
| } | |||
| @@ -1,13 +1,15 @@ | |||
| package com.ruoyi.platform.service.impl; | |||
| import cn.hutool.core.collection.CollectionUtil; | |||
| import cn.hutool.core.date.DateTime; | |||
| import cn.hutool.core.date.DateUtil; | |||
| import cn.hutool.core.text.csv.*; | |||
| import cn.hutool.core.util.IdUtil; | |||
| import cn.hutool.core.util.ObjectUtil; | |||
| import cn.hutool.json.JSONArray; | |||
| import cn.hutool.json.JSONObject; | |||
| import cn.hutool.json.JSONUtil; | |||
| import com.ruoyi.common.core.enums.KgStatus; | |||
| import com.ruoyi.common.core.enums.KgUpdateMethod; | |||
| import com.ruoyi.platform.domain.kg.KgInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfoPageVo; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| @@ -39,25 +41,6 @@ public class KgInfoServiceImpl implements KgInfoService { | |||
| @Autowired | |||
| private KgInfoMapper kgInfoMapper; | |||
| @Autowired | |||
| private KnowledgeGraphVersionDao knowledgeGraphVersionDao; | |||
| @Autowired | |||
| private Neo4jClient neo4jClient; | |||
| @Autowired | |||
| private PersonRepository personRepository; | |||
| private static final Logger log = LoggerFactory.getLogger(KgInfoServiceImpl.class); | |||
| // 关系映射配置(可扩展) | |||
| private static final Map<String, String> RELATION_MAPPING = new HashMap<>(); | |||
| static { | |||
| RELATION_MAPPING.put("导演", "DIRECTED_BY"); | |||
| RELATION_MAPPING.put("主演", "ACTED_IN"); | |||
| } | |||
| @Override | |||
| public void insertKgInfo(KgInfo KgInfo) { | |||
| KgInfo.setVersion("1"); | |||
| @@ -88,98 +71,4 @@ public class KgInfoServiceImpl implements KgInfoService { | |||
| kgInfoMapper.deleteByPrimaryKey(id); | |||
| } | |||
| @Override | |||
| public List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| return knowledgeGraphVersionDao.queryAll(kgInfoIdDTO); | |||
| } | |||
| @Override | |||
| public void uploadCsv(MultipartFile file, String kgId) { | |||
| try (InputStreamReader reader = new InputStreamReader(file.getInputStream())) { | |||
| // 创建 CsvReader | |||
| CsvReader csvReader = CsvUtil.getReader(); | |||
| // 读取所有行 | |||
| CsvData rows = csvReader.read(reader); | |||
| KnowledgeGraphVersion knowledgeGraphVersion = new KnowledgeGraphVersion(); | |||
| knowledgeGraphVersion.setStatus(0); | |||
| knowledgeGraphVersion.setKgId(kgId); | |||
| knowledgeGraphVersion.setUpdateMethod(0); | |||
| knowledgeGraphVersion.setVersion(maxVersion(kgId)); | |||
| knowledgeGraphVersion.setName(file.getOriginalFilename()); | |||
| knowledgeGraphVersion.setTransactionId(IdUtil.simpleUUID()); | |||
| knowledgeGraphVersion.setContent(JSONUtil.toJsonStr(rows.getRows())); | |||
| knowledgeGraphVersionDao.insert(knowledgeGraphVersion); | |||
| } catch (Exception e) { | |||
| log.error("e={}",e.getMessage()); | |||
| } | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public void fullUpdate(Long id) { | |||
| KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.queryById(id); | |||
| String transactionId = knowledgeGraphVersion.getTransactionId(); | |||
| Integer version = knowledgeGraphVersion.getVersion(); | |||
| JSONArray neo4jContents = JSONUtil.parseArray(knowledgeGraphVersion.getContent()); | |||
| List<Object> objects = neo4jContents.subList(1, neo4jContents.size() - 1); | |||
| // 用于存储已解析的 Person 节点 | |||
| Map<String, Person> personMap = new HashMap<>(); | |||
| objects.forEach(item->{ | |||
| JSONArray lineData = JSONUtil.parseArray(item); | |||
| String name = String.valueOf(lineData.get(0)); | |||
| String age = String.valueOf((lineData.get(1))); | |||
| String relationType = String.valueOf(lineData.get(2)); | |||
| String relatedPerson = String.valueOf(lineData.get(3)); | |||
| String since = lineData.get(4) != null ? String.valueOf(lineData.get(4)) : "0"; | |||
| // 创建或获取 Person 节点 | |||
| Person person = personMap.computeIfAbsent(name, k -> new Person(name, age, new ArrayList<>())); | |||
| person.setVersion(version); | |||
| person.setTransactionId(transactionId); | |||
| // 如果存在关系,则创建关系 | |||
| if (relationType != null && relatedPerson != null ) { | |||
| Person related = personMap.computeIfAbsent(relatedPerson, k -> new Person(relatedPerson, "0", new ArrayList<>())); | |||
| FriendsWith relation = new FriendsWith(related, since); | |||
| related.setTransactionId(transactionId); | |||
| related.setVersion(version); | |||
| relation.setTransactionId(transactionId); | |||
| relation.setVersion(version); | |||
| relation.setRelationType(relationType); // 动态设置关系类型 | |||
| person.getRelations().add(relation); | |||
| } | |||
| // 保存 Person 节点 | |||
| personRepository.save(person); | |||
| }); | |||
| KnowledgeGraphVersion online = new KnowledgeGraphVersion(); | |||
| online.setStatus(1); | |||
| online.setUpdateMethod(1); | |||
| online.setId(id); | |||
| knowledgeGraphVersionDao.update(online); | |||
| } | |||
| @Override | |||
| public void incrementalUpdate(Long id) { | |||
| } | |||
| /** | |||
| * 根据名称和事务id生成实体和关系的唯一id | |||
| * @param input | |||
| * @return | |||
| */ | |||
| private Long createUUID(String input) { | |||
| CRC32 crc32 = new CRC32(); | |||
| crc32.update(input.getBytes()); | |||
| return crc32.getValue(); | |||
| } | |||
| private int maxVersion(String kgId) { | |||
| Integer maxVersion =knowledgeGraphVersionDao.queryMaxVersion(kgId); | |||
| return ObjectUtil.isNull(maxVersion)?1:++maxVersion; | |||
| } | |||
| } | |||
| @@ -0,0 +1,226 @@ | |||
| package com.ruoyi.platform.service.impl; | |||
| import cn.hutool.core.collection.CollectionUtil; | |||
| import cn.hutool.core.text.csv.CsvData; | |||
| import cn.hutool.core.text.csv.CsvReader; | |||
| import cn.hutool.core.text.csv.CsvUtil; | |||
| import cn.hutool.core.util.IdUtil; | |||
| import cn.hutool.core.util.ObjectUtil; | |||
| import cn.hutool.json.JSONArray; | |||
| import cn.hutool.json.JSONUtil; | |||
| import com.ruoyi.common.core.enums.KgStatus; | |||
| import com.ruoyi.common.core.enums.KgUpdateMethod; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import com.ruoyi.platform.mapper.kg.KnowledgeGraphVersionDao; | |||
| import com.ruoyi.platform.service.KgVersionService; | |||
| import com.ruoyi.platform.service.kg.FriendsWith; | |||
| import com.ruoyi.platform.service.kg.Person; | |||
| import com.ruoyi.platform.service.kg.PersonRepository; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import org.springframework.transaction.annotation.Transactional; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.io.InputStreamReader; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.zip.CRC32; | |||
| @Service | |||
| public class KgVersionServiceImpl implements KgVersionService { | |||
| @Autowired | |||
| private KnowledgeGraphVersionDao knowledgeGraphVersionDao; | |||
| @Autowired | |||
| private PersonRepository personRepository; | |||
| private static final Logger log = LoggerFactory.getLogger(KgVersionServiceImpl.class); | |||
| @Override | |||
| public List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| return knowledgeGraphVersionDao.queryAll(kgInfoIdDTO); | |||
| } | |||
| @Override | |||
| public void uploadCsv(MultipartFile file, String kgId) { | |||
| try (InputStreamReader reader = new InputStreamReader(file.getInputStream())) { | |||
| // 创建 CsvReader | |||
| CsvReader csvReader = CsvUtil.getReader(); | |||
| // 读取所有行 | |||
| CsvData rows = csvReader.read(reader); | |||
| KnowledgeGraphVersion knowledgeGraphVersion = new KnowledgeGraphVersion(); | |||
| knowledgeGraphVersion.setKgId(kgId); | |||
| knowledgeGraphVersion.setVersion(maxVersion(kgId)); | |||
| knowledgeGraphVersion.setName(file.getOriginalFilename()); | |||
| knowledgeGraphVersion.setTransactionId(IdUtil.simpleUUID()); | |||
| knowledgeGraphVersion.setStatus(KgStatus.OFFLINE.getCode()); | |||
| knowledgeGraphVersion.setContent(JSONUtil.toJsonStr(rows.getRows())); | |||
| knowledgeGraphVersion.setUpdateMethod(KgUpdateMethod.PENDING_UPDATE.getCode()); | |||
| knowledgeGraphVersionDao.insert(knowledgeGraphVersion); | |||
| } catch (Exception e) { | |||
| log.error("e={}", e.getMessage()); | |||
| } | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public void fullUpdate(Long id) { | |||
| KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.queryById(id); | |||
| String transactionId = knowledgeGraphVersion.getTransactionId(); | |||
| Integer version = knowledgeGraphVersion.getVersion(); | |||
| JSONArray neo4jContents = JSONUtil.parseArray(knowledgeGraphVersion.getContent()); | |||
| if (CollectionUtil.isEmpty(neo4jContents)||neo4jContents.size()<=1){ | |||
| return; | |||
| } | |||
| //去掉表头 | |||
| neo4jContents.remove(0); | |||
| // 用于存储已解析的 Person 节点 | |||
| Map<String, Person> personMap = new HashMap<>(); | |||
| neo4jContents.forEach(item -> { | |||
| JSONArray lineData = JSONUtil.parseArray(item); | |||
| String name = String.valueOf(lineData.get(0)); | |||
| String age = String.valueOf((lineData.get(1))); | |||
| String relationType = String.valueOf(lineData.get(2)); | |||
| String relatedPerson = String.valueOf(lineData.get(3)); | |||
| String since = lineData.get(4) != null ? String.valueOf(lineData.get(4)) : "0"; | |||
| // 创建或获取 Person 节点 | |||
| Person person = personMap.computeIfAbsent(name, k -> new Person(name, age, new ArrayList<>())); | |||
| person.setVersion(version); | |||
| person.setTransactionId(transactionId); | |||
| // 如果存在关系,则创建关系 | |||
| if (relationType != null && relatedPerson != null) { | |||
| Person related = personMap.computeIfAbsent(relatedPerson, k -> new Person(relatedPerson, "0", new ArrayList<>())); | |||
| FriendsWith relation = new FriendsWith(related, since); | |||
| related.setTransactionId(transactionId); | |||
| related.setVersion(version); | |||
| relation.setTransactionId(transactionId); | |||
| relation.setVersion(version); | |||
| relation.setRelationType(relationType); // 动态设置关系类型 | |||
| person.getRelations().add(relation); | |||
| } | |||
| // 保存 Person 节点 | |||
| personRepository.save(person); | |||
| }); | |||
| KnowledgeGraphVersion online = new KnowledgeGraphVersion(); | |||
| online.setStatus(KgStatus.ONLINE.getCode()); | |||
| online.setUpdateMethod(KgUpdateMethod.INCREMENTAL_UPDATE.getCode()); | |||
| online.setId(id); | |||
| knowledgeGraphVersionDao.update(online); | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public void incrementalUpdate(Long id) { | |||
| //当前版本 | |||
| KnowledgeGraphVersion currentVersion = knowledgeGraphVersionDao.queryById(id); | |||
| String transactionId = currentVersion.getTransactionId(); | |||
| Integer version = currentVersion.getVersion(); | |||
| // 找出在线的版本 | |||
| KnowledgeGraphVersion onlineVersion = knowledgeGraphVersionDao.onlineFind(); | |||
| if (ObjectUtil.isNotNull(onlineVersion)&&ObjectUtil.isNotEmpty(onlineVersion.getTransactionId()) | |||
| &&ObjectUtil.isNotNull(onlineVersion.getVersion())) { | |||
| // 存在在线的版本,则取在线版本的version和transactionId | |||
| transactionId = onlineVersion.getTransactionId(); | |||
| version = onlineVersion.getVersion(); | |||
| //当前版本更新方式改为增量更新 | |||
| currentVersion.setUpdateMethod(KgUpdateMethod.INCREMENTAL_UPDATE.getCode()); | |||
| currentVersion.setVersion(onlineVersion.getVersion()); | |||
| knowledgeGraphVersionDao.update(currentVersion); | |||
| }else { | |||
| //当前版本为在线版本 | |||
| currentVersion.setUpdateMethod(KgUpdateMethod.INCREMENTAL_UPDATE.getCode()); | |||
| currentVersion.setStatus(KgStatus.ONLINE.getCode()); | |||
| knowledgeGraphVersionDao.update(currentVersion); | |||
| } | |||
| //解析表格数据保存到neo4j | |||
| saveToNeo4j(currentVersion.getContent(), version, transactionId); | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public void rollback(Long versionId) { | |||
| //在线的版本置为离线 | |||
| KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.onlineFind(); | |||
| knowledgeGraphVersion.setStatus(KgStatus.OFFLINE.getCode()); | |||
| knowledgeGraphVersionDao.update(knowledgeGraphVersion); | |||
| //该版本置为在线 | |||
| KnowledgeGraphVersion onlineVersion = new KnowledgeGraphVersion(); | |||
| onlineVersion.setId(versionId); | |||
| onlineVersion.setStatus(KgStatus.ONLINE.getCode()); | |||
| knowledgeGraphVersionDao.update(knowledgeGraphVersion); | |||
| } | |||
| /** | |||
| * 解析表格数据保存到neo4j | |||
| */ | |||
| private void saveToNeo4j(String content,int version,String transactionId) { | |||
| JSONArray neo4jContents = JSONUtil.parseArray(content); | |||
| if (CollectionUtil.isEmpty(neo4jContents)||neo4jContents.size()<=1){ | |||
| return; | |||
| } | |||
| //去掉表头 | |||
| neo4jContents.remove(0); | |||
| Map<String, Person> personMap = new HashMap<>(); | |||
| // 把当前内容合并到在线版本 | |||
| neo4jContents.forEach(item -> { | |||
| JSONArray lineData = JSONUtil.parseArray(item); | |||
| String name = String.valueOf(lineData.get(0)); | |||
| String age = String.valueOf((lineData.get(1))); | |||
| String relationType = String.valueOf(lineData.get(2)); | |||
| String relatedPerson = String.valueOf(lineData.get(3)); | |||
| String since = lineData.get(4) != null ? String.valueOf(lineData.get(4)) : "0"; | |||
| // 创建或获取 Person 节点 | |||
| Person person = personMap.computeIfAbsent(name, k -> new Person(name, age, new ArrayList<>())); | |||
| person.setVersion(version); | |||
| person.setTransactionId(transactionId); | |||
| // 如果存在关系,则创建关系 | |||
| if (relationType != null && relatedPerson != null) { | |||
| Person related = personMap.computeIfAbsent(relatedPerson, k -> new Person(relatedPerson, "0", new ArrayList<>())); | |||
| FriendsWith relation = new FriendsWith(related, since); | |||
| related.setTransactionId(transactionId); | |||
| related.setVersion(version); | |||
| relation.setTransactionId(transactionId); | |||
| relation.setVersion(version); | |||
| relation.setRelationType(relationType); // 动态设置关系类型 | |||
| person.getRelations().add(relation); | |||
| } | |||
| // 保存 Person 节点 | |||
| personRepository.save(person); | |||
| }); | |||
| } | |||
| /** | |||
| * 根据名称和事务id生成实体和关系的唯一id | |||
| * | |||
| * @param input | |||
| * @return | |||
| */ | |||
| private Long createUUID(String input) { | |||
| CRC32 crc32 = new CRC32(); | |||
| crc32.update(input.getBytes()); | |||
| return crc32.getValue(); | |||
| } | |||
| /** | |||
| * 下一个升级的版本 | |||
| * @param kgId | |||
| * @return | |||
| */ | |||
| private int maxVersion(String kgId) { | |||
| Integer maxVersion = knowledgeGraphVersionDao.queryMaxVersion(kgId); | |||
| return ObjectUtil.isNull(maxVersion) ? 1 : ++maxVersion; | |||
| } | |||
| } | |||
| @@ -9,7 +9,7 @@ | |||
| <result property="transactionId" column="transaction_id" jdbcType="VARCHAR"/> | |||
| <result property="content" column="content" jdbcType="VARCHAR"/> | |||
| <result property="kgId" column="kg_id" jdbcType="VARCHAR"/> | |||
| <result property="status" column="status" jdbcType="VARCHAR"/> | |||
| <result property="status" column="status" jdbcType="INTEGER"/> | |||
| <result property="updateMethod" column="update_method" jdbcType="INTEGER"/> | |||
| <result property="conceptsCount" column="concepts_count" jdbcType="INTEGER"/> | |||
| <result property="relationsCount" column="relations_count" jdbcType="INTEGER"/> | |||
| @@ -20,18 +20,20 @@ | |||
| <result property="state" column="state" jdbcType="INTEGER"/> | |||
| </resultMap> | |||
| <!--查询单个--> | |||
| <select id="queryById" resultMap="KnowledgeGraphVersionMap"> | |||
| <sql id="selectVersionVo"> | |||
| select id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| </sql> | |||
| <!--查询单个--> | |||
| <select id="queryById" resultMap="KnowledgeGraphVersionMap"> | |||
| <include refid="selectVersionVo"/> | |||
| where id = #{id} | |||
| </select> | |||
| <!--查询指定行数据--> | |||
| <select id="queryAllByLimit" resultMap="KnowledgeGraphVersionMap"> | |||
| select | |||
| id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| <include refid="selectVersionVo"/> | |||
| <where> | |||
| <if test="id != null"> | |||
| and id = #{id} | |||
| @@ -76,9 +78,7 @@ | |||
| limit #{pageable.offset}, #{pageable.pageSize} | |||
| </select> | |||
| <select id="queryAll" resultMap="KnowledgeGraphVersionMap"> | |||
| select | |||
| id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| <include refid="selectVersionVo"/> | |||
| <where> | |||
| <if test="name != null"> | |||
| and `name` = #{name} | |||
| @@ -215,5 +215,10 @@ | |||
| <select id="queryMaxVersion" resultType="java.lang.Integer"> | |||
| select max(version) from knowledge_graph_version where kg_id=#{kgId}; | |||
| </select> | |||
| <select id="onlineFind" resultMap="KnowledgeGraphVersionMap"> | |||
| <include refid="selectVersionVo"/> | |||
| where status=1; | |||
| </select> | |||
| </mapper> | |||