|
@@ -0,0 +1,395 @@
|
|
|
+package com.diagbot.facade;
|
|
|
+
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.diagbot.dto.RelationNameDTO;
|
|
|
+import com.diagbot.dto.RelationNamesDTO;
|
|
|
+import com.diagbot.dto.RelationNodeDTO;
|
|
|
+import com.diagbot.entity.KlConcept;
|
|
|
+import com.diagbot.entity.KlRelation;
|
|
|
+import com.diagbot.entity.KlRelationOrder;
|
|
|
+import com.diagbot.enums.IsDeleteEnum;
|
|
|
+import com.diagbot.enums.LexiconEnum;
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
+import com.diagbot.service.KlRelationService;
|
|
|
+import com.diagbot.service.impl.KlRelationOrderServiceImpl;
|
|
|
+import com.diagbot.service.impl.KlRelationServiceImpl;
|
|
|
+import com.diagbot.util.DateUtil;
|
|
|
+import com.diagbot.util.ListUtil;
|
|
|
+import com.diagbot.util.UserUtils;
|
|
|
+import com.diagbot.vo.KlRelationNodeVO;
|
|
|
+import com.diagbot.vo.KlRelationSaveVO;
|
|
|
+import com.diagbot.vo.KlRelationVO;
|
|
|
+import com.diagbot.vo.RelationContactDetailVO;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import io.swagger.models.auth.In;
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author kwz
|
|
|
+ * @date 2021/3/3
|
|
|
+ * @time 15:42
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class RelationContactFacade extends KlRelationServiceImpl {
|
|
|
+ @Autowired
|
|
|
+ private KlConceptFacade klConceptFacade;
|
|
|
+ @Autowired
|
|
|
+ private KlRelationOrderFacade klRelationOrderFacade;
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("klRelationServiceImpl")
|
|
|
+ private KlRelationService klRelationService;
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("klRelationOrderServiceImpl")
|
|
|
+ private KlRelationOrderServiceImpl klRelationOrderServiceImpl;
|
|
|
+
|
|
|
+ public RelationNodeDTO relationContactDetail(RelationContactDetailVO relationContactDetailVO) {
|
|
|
+ RelationNodeDTO relationNodeDTO = new RelationNodeDTO();
|
|
|
+
|
|
|
+ KlConcept klConcept = klConceptFacade.getById(relationContactDetailVO.getConceptId());
|
|
|
+ relationNodeDTO.setConceptId(klConcept.getId());
|
|
|
+ relationNodeDTO.setConceptName(klConcept.getLibName());
|
|
|
+ relationNodeDTO.setIsDeletedConcept(klConcept.getIsDeleted());
|
|
|
+ relationNodeDTO.setConceptTypeId(new Long(klConcept.getLibType()));
|
|
|
+
|
|
|
+ relationNodeDTO.setNodeList(repairRelationDataForQuery(relationContactDetailVO.getConceptId(),
|
|
|
+ relationContactDetailVO.getRelationIds(), relationContactDetailVO.getTypeIds(),
|
|
|
+ null, klConcept.getIsDeleted()));
|
|
|
+
|
|
|
+ return relationNodeDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ public RelationNodeDTO relationContactDetail(KlRelationVO klRelationVO) {
|
|
|
+ RelationNamesDTO relationNamesDTO = new RelationNamesDTO();
|
|
|
+ RelationNodeDTO relationNodeDTO = new RelationNodeDTO();
|
|
|
+ List<RelationNameDTO> relationNameDTOs_first = this.getRelationNameDTOs(klRelationVO.getConceptIds(), klRelationVO.getRelationId());
|
|
|
+
|
|
|
+
|
|
|
+ return relationNodeDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理关系节点-查询事件
|
|
|
+ *
|
|
|
+ * @param conceptId
|
|
|
+ * @param relationIds
|
|
|
+ * @param typeIds
|
|
|
+ * @param hookConceptIds
|
|
|
+ * @param isDeletedParent
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<RelationNodeDTO> repairRelationDataForQuery(Long conceptId, List<Long> relationIds,
|
|
|
+ List<Long> typeIds, List<Long> hookConceptIds,
|
|
|
+ String isDeletedParent) {
|
|
|
+ if (hookConceptIds == null) {
|
|
|
+ hookConceptIds = new ArrayList<>();
|
|
|
+ hookConceptIds.add(conceptId);
|
|
|
+ }
|
|
|
+
|
|
|
+ Long relationId = null, endTypeId = null;
|
|
|
+ if (ListUtil.isNotEmpty(relationIds)) {
|
|
|
+ relationId = relationIds.remove(0);
|
|
|
+ }
|
|
|
+ if (relationId == null) {
|
|
|
+ relationId = -999999l;
|
|
|
+ }
|
|
|
+ if (ListUtil.isNotEmpty(typeIds)) {
|
|
|
+ endTypeId = typeIds.remove(0);
|
|
|
+ }
|
|
|
+ List<RelationNodeDTO> relationNodeDTOList = this.baseMapper.getRelationNodeDTOs(conceptId, relationId, endTypeId);
|
|
|
+
|
|
|
+ for (RelationNodeDTO i : relationNodeDTOList) {
|
|
|
+ i.setConceptName(i.getConceptName());
|
|
|
+ i.setParentConceptName(i.getParentConceptName());
|
|
|
+ i.setConceptTypeName(LexiconEnum.getName(i.getConceptTypeId().intValue()));
|
|
|
+ i.setConceptNameType(i.getConceptName() + "(" + LexiconEnum.getName(i.getConceptTypeId().intValue()) + ")");
|
|
|
+ i.setParentConceptTypeName(LexiconEnum.getName(i.getParentConceptTypeId().intValue()));
|
|
|
+ i.setParentConceptNameType(i.getParentConceptName() + "("
|
|
|
+ + LexiconEnum.getName(i.getParentConceptTypeId().intValue()) + ")");
|
|
|
+ i.setIsDeletedConcept(
|
|
|
+ isDeletedParent.equals(IsDeleteEnum.Y.getKey()) ? IsDeleteEnum.Y.getKey() : i.getIsDeletedConcept()
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!hookConceptIds.contains(i.getConceptId())) {
|
|
|
+ List<Long> hookConceptIds_ = new ArrayList<>();
|
|
|
+ hookConceptIds_.addAll(hookConceptIds);
|
|
|
+ hookConceptIds_.add(i.getConceptId());
|
|
|
+ i.setNodeList(repairRelationDataForQuery(i.getConceptId(),
|
|
|
+ Lists.newArrayList(relationIds), Lists.newArrayList(typeIds),
|
|
|
+ hookConceptIds_, i.getIsDeletedConcept()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return relationNodeDTOList;
|
|
|
+ }
|
|
|
+ @DS("med")
|
|
|
+ @DSTransactional
|
|
|
+ public Boolean addRelation(List<KlRelationSaveVO> klRelationSaveVOs) {
|
|
|
+ if (ListUtil.isNotEmpty(klRelationSaveVOs)) {
|
|
|
+ KlRelationSaveVO klRelationSaveVO = klRelationSaveVOs.get(0);
|
|
|
+ Long sid = klRelationSaveVO.getSid();
|
|
|
+ List<Long> conceptid = Lists.newArrayList(sid);
|
|
|
+ Integer rid = klRelationSaveVO.getRid();
|
|
|
+ List<Long> sids = new ArrayList<>();
|
|
|
+ List<RelationNameDTO> relationNameDTOs = this.getRelationNameDTOs(conceptid, rid);
|
|
|
+ if (ListUtil.isNotEmpty(relationNameDTOs)) {
|
|
|
+ for (RelationNameDTO red : relationNameDTOs) {
|
|
|
+ Long startId = red.getStartId();
|
|
|
+ Long medId = red.getEndId();
|
|
|
+ if (!sids.contains(startId)) {
|
|
|
+ sids.add(startId);
|
|
|
+ }
|
|
|
+ if (!sids.contains(medId)) {
|
|
|
+ sids.add(medId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<KlRelation> relationList = this.list(new QueryWrapper<KlRelation>()
|
|
|
+ .in("start_id", sids)
|
|
|
+ .eq("relation_id", rid));
|
|
|
+ List<Long> ids = relationList.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
+ //先删除
|
|
|
+ if (ListUtil.isNotEmpty(ids)) {
|
|
|
+ this.removeByIds(ids);
|
|
|
+
|
|
|
+ QueryWrapper<KlRelationOrder> relationOrderQe = new QueryWrapper<>();
|
|
|
+ relationOrderQe.in("t_relation_id", ids);
|
|
|
+ klRelationOrderFacade.remove(relationOrderQe);
|
|
|
+ }
|
|
|
+ List<List<KlRelation>> relationGroupList = relationDataForAdd(klRelationSaveVOs);
|
|
|
+
|
|
|
+// String currentUser = UserUtils.getCurrentPrincipleID();
|
|
|
+ Date now = DateUtil.now();
|
|
|
+ List<KlRelation> relationLists = Lists.newArrayList();
|
|
|
+ relationGroupList.forEach(i -> {
|
|
|
+ i.forEach(j -> {
|
|
|
+// j.setCreator(currentUser);
|
|
|
+ j.setGmtCreate(now);
|
|
|
+// j.setModifier(currentUser);
|
|
|
+ j.setGmtModified(now);
|
|
|
+ relationLists.add(j);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ klRelationService.saveBatch(relationLists);
|
|
|
+
|
|
|
+ List<KlRelationOrder> relationOrderList = Lists.newArrayList();
|
|
|
+ relationGroupList.forEach(i -> {
|
|
|
+ int orderNo = 0;
|
|
|
+ for (KlRelation j : i) {
|
|
|
+ orderNo++;
|
|
|
+ KlRelationOrder relationOrder = new KlRelationOrder();
|
|
|
+ relationOrder.setOrderNo(orderNo);
|
|
|
+ relationOrder.settRelationId(j.getId());
|
|
|
+ relationOrder.setGmtCreate(now);
|
|
|
+ relationOrder.setGmtModified(now);
|
|
|
+// relationOrder.setCreator(currentUser);
|
|
|
+// relationOrder.setModifier(currentUser);
|
|
|
+ relationOrderList.add(relationOrder);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ klRelationOrderServiceImpl.saveBatch(relationOrderList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<List<KlRelation>> relationDataForAdd(List<KlRelationSaveVO> klRelationSaveVOs) {
|
|
|
+ List<List<KlRelation>> relationList = Lists.newArrayList();
|
|
|
+ Map<String, List<KlRelation>> relationMap = Maps.newHashMap();
|
|
|
+ if (ListUtil.isNotEmpty(klRelationSaveVOs)) {
|
|
|
+
|
|
|
+ for (KlRelationSaveVO kls : klRelationSaveVOs) {
|
|
|
+ Long sid = kls.getSid();
|
|
|
+ Integer rid = kls.getRid();
|
|
|
+ Long eid = kls.getEid();
|
|
|
+ Integer srid = kls.getSrid();
|
|
|
+ List<Long> eids = kls.getEids();
|
|
|
+ if(relationMap.containsKey(sid.toString()+"&"+rid.toString())){
|
|
|
+ List<KlRelation> klRelations_big = relationMap.get(sid.toString() + "&" + rid.toString());
|
|
|
+ KlRelation klRelation = new KlRelation();
|
|
|
+ klRelation.setStartId(sid);
|
|
|
+ klRelation.setRelationId(rid);
|
|
|
+ klRelation.setEndId(eid);
|
|
|
+ klRelation.setStatus(1);
|
|
|
+ klRelations_big.add(klRelation);
|
|
|
+ relationMap.put(sid.toString() + "&" + rid.toString(),klRelations_big);
|
|
|
+ }else {
|
|
|
+ List<KlRelation> klRelations_big = Lists.newArrayList();
|
|
|
+ KlRelation klRelation = new KlRelation();
|
|
|
+ klRelation.setStartId(sid);
|
|
|
+ klRelation.setRelationId(rid);
|
|
|
+ klRelation.setEndId(eid);
|
|
|
+ klRelation.setStatus(1);
|
|
|
+ klRelations_big.add(klRelation);
|
|
|
+ relationMap.put(sid.toString() + "&" + rid.toString(),klRelations_big);
|
|
|
+ }
|
|
|
+ if(srid != null && ListUtil.isNotEmpty(eids)){
|
|
|
+ List<KlRelation> klRelations_sub = Lists.newArrayList();
|
|
|
+ for (Long id:eids) {
|
|
|
+ KlRelation klRelation = new KlRelation();
|
|
|
+ klRelation.setStartId(eid);
|
|
|
+ klRelation.setRelationId(srid);
|
|
|
+ klRelation.setEndId(id);
|
|
|
+ klRelation.setStatus(1);
|
|
|
+ klRelations_sub.add(klRelation);
|
|
|
+ }
|
|
|
+ relationMap.put(eid.toString() + "&" + srid.toString(),klRelations_sub);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(MapUtils.isNotEmpty(relationMap)){
|
|
|
+ relationMap.forEach((x,y)->{
|
|
|
+ relationList.add(y);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return relationList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 医学术语关联维护/医学术语多层关联维护-添加或者编辑
|
|
|
+ *
|
|
|
+ * @param klRelationNodeVO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean addRelation(KlRelationNodeVO klRelationNodeVO) {
|
|
|
+ if (klRelationNodeVO.getConceptId() == null) {
|
|
|
+ throw new CommonException(CommonErrorCode.RPC_ERROR, "conceptId必填!");
|
|
|
+ }
|
|
|
+ if (ListUtil.isEmpty(klRelationNodeVO.getNodeList())) {
|
|
|
+ throw new CommonException(CommonErrorCode.RPC_ERROR, "nodeList不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //先删除
|
|
|
+ List<Long> relationIdList = repairRelationDataForDelBeforeAdd(klRelationNodeVO.getConceptId(), klRelationNodeVO);
|
|
|
+ if (ListUtil.isNotEmpty(relationIdList)) {
|
|
|
+ removeByIds(relationIdList);
|
|
|
+
|
|
|
+ QueryWrapper<KlRelationOrder> relationOrderQe = new QueryWrapper<>();
|
|
|
+ relationOrderQe.in("t_relation_id", relationIdList);
|
|
|
+ klRelationOrderFacade.remove(relationOrderQe);
|
|
|
+ }
|
|
|
+
|
|
|
+ //后添加
|
|
|
+ List<List<KlRelation>> relationGroupList = repairRelationDataForAdd(klRelationNodeVO);
|
|
|
+ String currentUser = UserUtils.getCurrentPrincipleID();
|
|
|
+ Date now = DateUtil.now();
|
|
|
+ List<KlRelation> relationList = Lists.newArrayList();
|
|
|
+ relationGroupList.forEach(i -> {
|
|
|
+ i.forEach(j -> {
|
|
|
+ j.setCreator(currentUser);
|
|
|
+ j.setGmtCreate(now);
|
|
|
+ j.setModifier(currentUser);
|
|
|
+ j.setGmtModified(now);
|
|
|
+ relationList.add(j);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ klRelationService.saveOrUpdateBatch(relationList);
|
|
|
+
|
|
|
+ if (klRelationNodeVO.getIsOrderBy() == 1) {
|
|
|
+ List<KlRelationOrder> relationOrderList = Lists.newArrayList();
|
|
|
+ relationGroupList.forEach(i -> {
|
|
|
+ int orderNo = 0;
|
|
|
+ for (KlRelation j : i) {
|
|
|
+ orderNo++;
|
|
|
+ KlRelationOrder relationOrder = new KlRelationOrder();
|
|
|
+ relationOrder.setOrderNo(orderNo);
|
|
|
+ relationOrder.settRelationId(j.getId());
|
|
|
+ relationOrder.setGmtCreate(now);
|
|
|
+ relationOrder.setGmtModified(now);
|
|
|
+ relationOrder.setCreator(currentUser);
|
|
|
+ relationOrder.setModifier(currentUser);
|
|
|
+ relationOrderList.add(relationOrder);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ klRelationOrderServiceImpl.saveBatch(relationOrderList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理关系节点-添加或者编辑前先删除掉
|
|
|
+ *
|
|
|
+ * @param conceptId 当前概念id
|
|
|
+ * @param klRelationNodeVO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<Long> repairRelationDataForDelBeforeAdd(Long conceptId, KlRelationNodeVO klRelationNodeVO) {
|
|
|
+ List<Long> relationIdList = Lists.newArrayList();
|
|
|
+ List<KlRelation> relationList = this.list(new QueryWrapper<KlRelation>()
|
|
|
+ .eq("start_id", conceptId)
|
|
|
+ .eq("relation_id", klRelationNodeVO != null && klRelationNodeVO.getSonRelationId() != null ?
|
|
|
+ klRelationNodeVO.getSonRelationId() : -999999l));
|
|
|
+
|
|
|
+ if (ListUtil.isNotEmpty(relationList)) {
|
|
|
+ if (klRelationNodeVO != null && klRelationNodeVO.getSonTypeId() != null) {
|
|
|
+ List<Long> conceptIdList = klConceptFacade.getCompatibleTypeConceptIds(klRelationNodeVO.getSonTypeId(),
|
|
|
+ relationList.stream().map(i -> i.getEndId()).collect(Collectors.toList()));
|
|
|
+ if (conceptIdList != null) {
|
|
|
+ relationList = relationList.stream()
|
|
|
+ .filter(i -> conceptIdList.contains(i.getEndId())).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ListUtil.isNotEmpty(relationList)) {
|
|
|
+ relationIdList.addAll(relationList.stream().map(i -> i.getId()).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ Map<Long, KlRelationNodeVO> conceptIdRnMap = Maps.newHashMap();
|
|
|
+ if (klRelationNodeVO != null && ListUtil.isNotEmpty(klRelationNodeVO.getNodeList())) {
|
|
|
+ conceptIdRnMap = klRelationNodeVO.getNodeList()
|
|
|
+ .stream().collect(Collectors.toMap(KlRelationNodeVO::getConceptId, i -> i));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (KlRelation i : relationList) {
|
|
|
+ relationIdList.addAll(repairRelationDataForDelBeforeAdd(i.getEndId(),
|
|
|
+ conceptIdRnMap.get(i.getEndId())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return relationIdList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理关系节点-添加或者编辑事件
|
|
|
+ *
|
|
|
+ * @param klRelationNodeVO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<List<KlRelation>> repairRelationDataForAdd(KlRelationNodeVO klRelationNodeVO) {
|
|
|
+ List<List<KlRelation>> retList = Lists.newArrayList();
|
|
|
+
|
|
|
+ if (ListUtil.isNotEmpty(klRelationNodeVO.getNodeList())) {
|
|
|
+ List<KlRelation> relationList = Lists.newArrayList();
|
|
|
+ klRelationNodeVO.getNodeList().forEach(i -> {
|
|
|
+ KlRelation relation = new KlRelation();
|
|
|
+ relation.setStartId(klRelationNodeVO.getConceptId());
|
|
|
+ relation.setEndId(i.getConceptId());
|
|
|
+ relation.setRelationId(Integer.parseInt(String.valueOf(i.getRelationId())));
|
|
|
+ relationList.add(relation);
|
|
|
+
|
|
|
+ if (ListUtil.isNotEmpty(i.getNodeList())) {
|
|
|
+ retList.addAll(repairRelationDataForAdd(i));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ retList.add(relationList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return retList;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|