From 6105ca330d11970da646474f50ffafaaff3ae9f1 Mon Sep 17 00:00:00 2001 From: lishuai Date: Thu, 5 Sep 2024 14:05:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A1=BA=E5=BA=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98=202024=E5=B9=B49=E6=9C=885=E6=97=A514:05:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IotThingRelationDetailServiceImpl.java | 135 ++++++++++-------- 1 file changed, 72 insertions(+), 63 deletions(-) diff --git a/modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java b/modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java index 5a80001..adfd661 100644 --- a/modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java +++ b/modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java @@ -337,70 +337,76 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl iotThingRelationDetailEntities = mapper.selectListByQuery(queryWrapper); - if (CollectionUtils.isEmpty(iotThingRelationDetailEntities)) { - throw new SysException("当前根关系下节点信息列表异常"); - } - //找出当前移动节点的 所有子节点,即所有需要移动的节点集合 - List sourceChildList = Lists.newArrayList(); - findAllChild(iotThingRelationDetailEntities, sourceEntity, sourceChildList); - sourceChildList.add(sourceEntity); - - //获取前端的参数中 父节点的所有子节点的最大顺序的节点 - Optional optionalMax = iotThingRelationDetailEntities.stream() - .filter(item -> Objects.equals(item.getFromId(), targetEntity.getToId()) && Objects.equals(item.getRootThingId(), targetEntity.getRootThingId())) - .max(comparing(IotThingRelationDetailEntity::getSort)); - if(optionalMax.isEmpty()){ - //若没有子节点,那就自己为节点 - optionalMax = iotThingRelationDetailEntities.stream().filter(item -> - Objects.equals(item.getId(), dto.getPid()) - ).findFirst(); - } - //父节点上面的加入到list中 - Optional finalOptionalMax = optionalMax; - //找出目标节点上面的所有节点,但是不包含 移动节点和子节点 - List resList = new ArrayList<>(iotThingRelationDetailEntities.stream() - .filter(item -> item.getSort() <= finalOptionalMax.get().getSort() - && !sourceChildList.stream().map(IotThingRelationDetailEntity::getId).toList().contains(item.getId()) - ).sorted(Comparator.comparing(IotThingRelationDetailEntity::getSort)).toList()); - - sourceChildList.stream().sorted(Comparator.comparing(IotThingRelationDetailEntity::getSort)).forEach(item -> { - if(Objects.equals(item.getId(), sourceEntity.getId())){ - item.setFromId(targetEntity.getFromId()) - .setFromName(targetEntity.getFromName()) - .setFromCode(targetEntity.getFromCode()) - .setRootId(dto.getRootId()) - .setRootThingId(targetEntity.getRootThingId()) - .setToName(sourceEntity.getToName()) - .setTag(dto.getTag()); + + sourceEntity.setTag(StringUtils.isBlank(dto.getTag()) ? dto.getToName() : dto.getTag()); + + if(Objects.isNull(dto.getPid()) || Objects.equals(dto.getPid(), 0L)){ + mapper.update(sourceEntity); + } else { + + + IotThingRelationDetailEntity targetEntity = mapper.selectOneById(dto.getPid()); + if (Objects.isNull(targetEntity)) { + throw new SysException("当前目标节点关系不存在"); + } + if(!Objects.equals(sourceEntity.getRootThingId(), targetEntity.getRootThingId())){ + throw new SysException("当前移动节点和目标节点不在同一个根节点下,无法移动"); + } + + if(sourceEntity.getFromId().equals(targetEntity.getToId())){ + mapper.update(sourceEntity); }else{ - item.setRootThingId(targetEntity.getRootThingId()); + //查询当前关系下所有节点信息 + List iotThingRelationDetailEntities = mapper.selectListByQuery(QueryWrapper.create().eq(IotThingRelationDetailEntity::getRootId, dto.getRootId())); + if (CollectionUtils.isEmpty(iotThingRelationDetailEntities)) { + throw new SysException("当前根关系下节点信息列表异常"); + } + //找出当前移动节点的 所有子节点,即所有需要移动的节点集合 + List sourceChildList = Lists.newArrayList(); + findAllChild(iotThingRelationDetailEntities, sourceEntity, sourceChildList); + sourceChildList.add(sourceEntity); + + List sourceChildIds = sourceChildList.stream().map(IotThingRelationDetailEntity::getId).toList(); + //找出不包源节点 , 目标节点顺序的节点 ,并且顺序大于目标节点顺序的节点 + List dlist = iotThingRelationDetailEntities.stream() + .filter(d -> !sourceChildIds.contains(d.getId()) && targetEntity.getSort() > d.getSort()) + .sorted(Comparator.comparing(IotThingRelationDetailEntity::getSort)) + .toList(); + + List resultList = new ArrayList<>(dlist); + + //找出当前目标节点的所有子节点 + List targetChildList = Lists.newArrayList(); + findAllChild(iotThingRelationDetailEntities, targetEntity, targetChildList); + targetChildList.add(targetEntity); + //将当前目标节点的所有子节点,按照顺序排序 + List list = targetChildList.stream().sorted(comparing(IotThingRelationDetailEntity::getSort)).toList(); + resultList.addAll(list); + + sourceEntity.setFromId(targetEntity.getToId()); + sourceEntity.setFromCode(targetEntity.getToCode()); + sourceEntity.setFromName(targetEntity.getFromName()); + resultList.add(sourceEntity); + + + List targetChildIds = list.stream().map(IotThingRelationDetailEntity::getId).toList(); + + //找出不包源节点 , 目标节点顺序的节点 ,并且顺序小于目标节点顺序的节点 + List xlist = iotThingRelationDetailEntities.stream().filter(d -> !sourceChildIds.contains(d.getId()) && !targetChildIds.contains(d.getId()) + && list.get(list.size()-1).getSort() < d.getSort()).toList(); + + resultList.addAll(xlist); + + //更新排序 + AtomicLong sort = new AtomicLong(0); + resultList.forEach(item -> + { + item.setSort(sort.incrementAndGet()); + mapper.updateByQuery(item, QueryWrapper.create().eq(IotThingRelationDetailEntity::getId, item.getId())); + } + ); } - resList.add(item); - }); - //剩余的节点添加到list中 - iotThingRelationDetailEntities.stream() - .filter(item -> item.getSort() > finalOptionalMax.get().getSort() && !sourceChildList.stream().map(IotThingRelationDetailEntity::getId).toList().contains(item.getId())) - .sorted(Comparator.comparing(IotThingRelationDetailEntity::getSort)) - .forEach(resList::add); - //更新排序 - AtomicLong sort = new AtomicLong(0); - resList.forEach(item -> - { - item.setSort(sort.incrementAndGet()); - mapper.updateByQuery(item, QueryWrapper.create().eq(IotThingRelationDetailEntity::getId, item.getId())); } - ); //更新缓存 cache.clearTopic(CacheNameEnum.THING_DETAIL_RELATION); } @@ -555,11 +561,14 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl findTreeList(List rootIds) { //关系节点 - List rootList = cache.findAllKeyMap(CacheNameEnum.THING_ROOT_RELATION, rootIds.stream().map(String::valueOf).toList()); + String result = rootIds.stream() + .map(Object::toString) // 将每个元素转换为字符串 + .collect(Collectors.joining(",")); + List rootList = relationRootsService.findList(null, null, result, null, null, null); if (CollectionUtils.isEmpty(rootList)) { return Lists.newArrayList(); } - List detailList = cache.findAllKeyMap(CacheNameEnum.THING_DETAIL_RELATION, rootIds.stream().map(String::valueOf).toList()); + List detailList = findList(null, null, result, null, null, null); if (CollectionUtils.isEmpty(detailList)) { return rootList;