|
|
@@ -1,133 +0,0 @@
|
|
|
-package com.lingyue.project.entity.service;
|
|
|
-
|
|
|
-import com.lingyue.common.core.Constants;
|
|
|
-import com.lingyue.common.exception.BusinessException;
|
|
|
-import com.lingyue.graph.entity.Node;
|
|
|
-import com.lingyue.graph.service.NodeService;
|
|
|
-import com.lingyue.graph.service.PropertyService;
|
|
|
-import com.lingyue.project.attachment.viewmapper.AttachmentViewMapper;
|
|
|
-import com.lingyue.project.attachment.viewmapper.VAttachment;
|
|
|
-import com.lingyue.project.entity.dto.EntityMergeDTO;
|
|
|
-import com.lingyue.project.entity.dto.EntityUpdateDTO;
|
|
|
-import com.lingyue.project.entity.dto.EntityVO;
|
|
|
-import com.lingyue.project.entity.viewmapper.EntityViewMapper;
|
|
|
-import com.lingyue.project.entity.viewmapper.VEntity;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service
|
|
|
-@RequiredArgsConstructor
|
|
|
-public class EntityService {
|
|
|
-
|
|
|
- private final NodeService nodeService;
|
|
|
- private final PropertyService propertyService;
|
|
|
- private final EntityViewMapper entityViewMapper;
|
|
|
- private final AttachmentViewMapper attachmentViewMapper;
|
|
|
-
|
|
|
- public List<EntityVO> listByAttachment(Long attachmentId, String entityType, String keyword) {
|
|
|
- List<VEntity> entities = entityViewMapper.selectByAttachmentId(attachmentId);
|
|
|
- return filterAndConvert(entities, entityType, keyword);
|
|
|
- }
|
|
|
-
|
|
|
- public List<EntityVO> listByProject(Long projectId, String entityType, String keyword) {
|
|
|
- List<VEntity> entities = entityViewMapper.selectByProjectId(projectId);
|
|
|
- return filterAndConvert(entities, entityType, keyword);
|
|
|
- }
|
|
|
-
|
|
|
- public EntityVO getEntity(Long entityId) {
|
|
|
- VEntity ve = entityViewMapper.selectById(entityId);
|
|
|
- if (ve == null) {
|
|
|
- throw new BusinessException(404, "实体不存在");
|
|
|
- }
|
|
|
- return toEntityVO(ve);
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public EntityVO updateEntity(Long entityId, EntityUpdateDTO dto) {
|
|
|
- Node node = nodeService.getById(entityId);
|
|
|
- if (node == null || !Constants.NODE_ENTITY.equals(node.getNodeType())) {
|
|
|
- throw new BusinessException(404, "实体不存在");
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.hasText(dto.getEntityText())) {
|
|
|
- nodeService.updateNodeName(entityId, dto.getEntityText());
|
|
|
- }
|
|
|
- if (StringUtils.hasText(dto.getEntityType())) {
|
|
|
- propertyService.setNodeProperty(entityId, "entity_type", dto.getEntityType());
|
|
|
- }
|
|
|
- if (dto.getBusinessLabel() != null) {
|
|
|
- propertyService.setNodeProperty(entityId, "business_label", dto.getBusinessLabel());
|
|
|
- }
|
|
|
- if (dto.getConfidence() != null) {
|
|
|
- propertyService.setNodePropertyNumber(entityId, "confidence", dto.getConfidence());
|
|
|
- }
|
|
|
-
|
|
|
- VEntity ve = entityViewMapper.selectById(entityId);
|
|
|
- return toEntityVO(ve);
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public EntityVO mergeEntities(EntityMergeDTO dto) {
|
|
|
- Node target = nodeService.getById(dto.getTargetEntityId());
|
|
|
- if (target == null || !Constants.NODE_ENTITY.equals(target.getNodeType())) {
|
|
|
- throw new BusinessException(404, "目标实体不存在");
|
|
|
- }
|
|
|
-
|
|
|
- BigDecimal currentCount = propertyService.getNodePropertyNumber(dto.getTargetEntityId(), "occurrence_count");
|
|
|
- int totalCount = currentCount != null ? currentCount.intValue() : 1;
|
|
|
-
|
|
|
- for (Long sourceId : dto.getSourceEntityIds()) {
|
|
|
- if (sourceId.equals(dto.getTargetEntityId())) continue;
|
|
|
-
|
|
|
- Node source = nodeService.getById(sourceId);
|
|
|
- if (source == null || !Constants.NODE_ENTITY.equals(source.getNodeType())) continue;
|
|
|
-
|
|
|
- BigDecimal srcCount = propertyService.getNodePropertyNumber(sourceId, "occurrence_count");
|
|
|
- totalCount += (srcCount != null ? srcCount.intValue() : 1);
|
|
|
-
|
|
|
- nodeService.deleteNodeCascade(sourceId);
|
|
|
- }
|
|
|
-
|
|
|
- propertyService.setNodePropertyNumber(dto.getTargetEntityId(), "occurrence_count",
|
|
|
- BigDecimal.valueOf(totalCount));
|
|
|
-
|
|
|
- VEntity ve = entityViewMapper.selectById(dto.getTargetEntityId());
|
|
|
- return toEntityVO(ve);
|
|
|
- }
|
|
|
-
|
|
|
- private List<EntityVO> filterAndConvert(List<VEntity> entities, String entityType, String keyword) {
|
|
|
- return entities.stream()
|
|
|
- .filter(e -> !StringUtils.hasText(entityType) || entityType.equals(e.getEntityType()))
|
|
|
- .filter(e -> !StringUtils.hasText(keyword) || (e.getEntityText() != null && e.getEntityText().contains(keyword)))
|
|
|
- .map(this::toEntityVO)
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- private EntityVO toEntityVO(VEntity ve) {
|
|
|
- EntityVO vo = new EntityVO();
|
|
|
- vo.setId(ve.getId());
|
|
|
- vo.setEntityText(ve.getEntityText());
|
|
|
- vo.setEntityKey(ve.getEntityKey());
|
|
|
- vo.setEntityType(ve.getEntityType());
|
|
|
- vo.setBusinessLabel(ve.getBusinessLabel());
|
|
|
- vo.setConfidence(ve.getConfidence());
|
|
|
- vo.setOccurrenceCount(ve.getOccurrenceCount());
|
|
|
- vo.setAttachmentId(ve.getAttachmentId());
|
|
|
- vo.setCreatedAt(ve.getCreatedAt());
|
|
|
- vo.setUpdatedAt(ve.getUpdatedAt());
|
|
|
-
|
|
|
- if (ve.getAttachmentId() != null) {
|
|
|
- VAttachment va = attachmentViewMapper.selectById(ve.getAttachmentId());
|
|
|
- if (va != null) {
|
|
|
- vo.setAttachmentName(va.getDisplayName());
|
|
|
- }
|
|
|
- }
|
|
|
- return vo;
|
|
|
- }
|
|
|
-}
|