|
|
|
@ -337,70 +337,76 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR |
|
|
|
if (Objects.isNull(sourceEntity)) { |
|
|
|
throw new SysException("当前移动节点关系不存在"); |
|
|
|
} |
|
|
|
IotThingRelationDetailEntity targetEntity = mapper.selectOneById(dto.getPid()); |
|
|
|
if (Objects.isNull(targetEntity)) { |
|
|
|
throw new SysException("当前目标节点关系不存在"); |
|
|
|
} |
|
|
|
if(!Objects.equals(sourceEntity.getRootThingId(), targetEntity.getRootThingId())){ |
|
|
|
throw new SysException("当前移动节点和目标节点不在同一个根节点下,无法移动"); |
|
|
|
} |
|
|
|
//查询当前关系下所有节点信息,但是不包括当前节点 |
|
|
|
QueryWrapper queryWrapper = QueryWrapper.create().eq(IotThingRelationDetailEntity::getRootId, dto.getRootId()); |
|
|
|
//这里肯定有数据 |
|
|
|
List<IotThingRelationDetailEntity> iotThingRelationDetailEntities = mapper.selectListByQuery(queryWrapper); |
|
|
|
if (CollectionUtils.isEmpty(iotThingRelationDetailEntities)) { |
|
|
|
throw new SysException("当前根关系下节点信息列表异常"); |
|
|
|
} |
|
|
|
//找出当前移动节点的 所有子节点,即所有需要移动的节点集合 |
|
|
|
List<IotThingRelationDetailEntity> sourceChildList = Lists.newArrayList(); |
|
|
|
findAllChild(iotThingRelationDetailEntities, sourceEntity, sourceChildList); |
|
|
|
sourceChildList.add(sourceEntity); |
|
|
|
|
|
|
|
//获取前端的参数中 父节点的所有子节点的最大顺序的节点 |
|
|
|
Optional<IotThingRelationDetailEntity> 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<IotThingRelationDetailEntity> finalOptionalMax = optionalMax; |
|
|
|
//找出目标节点上面的所有节点,但是不包含 移动节点和子节点 |
|
|
|
List<IotThingRelationDetailEntity> 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<IotThingRelationDetailEntity> iotThingRelationDetailEntities = mapper.selectListByQuery(QueryWrapper.create().eq(IotThingRelationDetailEntity::getRootId, dto.getRootId())); |
|
|
|
if (CollectionUtils.isEmpty(iotThingRelationDetailEntities)) { |
|
|
|
throw new SysException("当前根关系下节点信息列表异常"); |
|
|
|
} |
|
|
|
//找出当前移动节点的 所有子节点,即所有需要移动的节点集合 |
|
|
|
List<IotThingRelationDetailEntity> sourceChildList = Lists.newArrayList(); |
|
|
|
findAllChild(iotThingRelationDetailEntities, sourceEntity, sourceChildList); |
|
|
|
sourceChildList.add(sourceEntity); |
|
|
|
|
|
|
|
List<Long> sourceChildIds = sourceChildList.stream().map(IotThingRelationDetailEntity::getId).toList(); |
|
|
|
//找出不包源节点 , 目标节点顺序的节点 ,并且顺序大于目标节点顺序的节点 |
|
|
|
List<IotThingRelationDetailEntity> dlist = iotThingRelationDetailEntities.stream() |
|
|
|
.filter(d -> !sourceChildIds.contains(d.getId()) && targetEntity.getSort() > d.getSort()) |
|
|
|
.sorted(Comparator.comparing(IotThingRelationDetailEntity::getSort)) |
|
|
|
.toList(); |
|
|
|
|
|
|
|
List<IotThingRelationDetailEntity> resultList = new ArrayList<>(dlist); |
|
|
|
|
|
|
|
//找出当前目标节点的所有子节点 |
|
|
|
List<IotThingRelationDetailEntity> targetChildList = Lists.newArrayList(); |
|
|
|
findAllChild(iotThingRelationDetailEntities, targetEntity, targetChildList); |
|
|
|
targetChildList.add(targetEntity); |
|
|
|
//将当前目标节点的所有子节点,按照顺序排序 |
|
|
|
List<IotThingRelationDetailEntity> 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<Long> targetChildIds = list.stream().map(IotThingRelationDetailEntity::getId).toList(); |
|
|
|
|
|
|
|
//找出不包源节点 , 目标节点顺序的节点 ,并且顺序小于目标节点顺序的节点 |
|
|
|
List<IotThingRelationDetailEntity> 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<IotThingR |
|
|
|
@Override |
|
|
|
public List<ObjectNode> findTreeList(List<Long> rootIds) { |
|
|
|
//关系节点 |
|
|
|
List<ObjectNode> rootList = cache.findAllKeyMap(CacheNameEnum.THING_ROOT_RELATION, rootIds.stream().map(String::valueOf).toList()); |
|
|
|
String result = rootIds.stream() |
|
|
|
.map(Object::toString) // 将每个元素转换为字符串 |
|
|
|
.collect(Collectors.joining(",")); |
|
|
|
List<ObjectNode> rootList = relationRootsService.findList(null, null, result, null, null, null); |
|
|
|
if (CollectionUtils.isEmpty(rootList)) { |
|
|
|
return Lists.newArrayList(); |
|
|
|
} |
|
|
|
List<ObjectNode> detailList = cache.findAllKeyMap(CacheNameEnum.THING_DETAIL_RELATION, rootIds.stream().map(String::valueOf).toList()); |
|
|
|
List<ObjectNode> detailList = findList(null, null, result, null, null, null); |
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(detailList)) { |
|
|
|
return rootList; |
|
|
|
|