Browse Source

修改顺序问题

2024年9月5日14:05:00
thing_master
lishuai 1 year ago
parent
commit
6105ca330d
  1. 95
      modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java

95
modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java

@ -337,6 +337,14 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR
if (Objects.isNull(sourceEntity)) {
throw new SysException("当前移动节点关系不存在");
}
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("当前目标节点关系不存在");
@ -344,10 +352,12 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR
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(sourceEntity.getFromId().equals(targetEntity.getToId())){
mapper.update(sourceEntity);
}else{
//查询当前关系下所有节点信息
List<IotThingRelationDetailEntity> iotThingRelationDetailEntities = mapper.selectListByQuery(QueryWrapper.create().eq(IotThingRelationDetailEntity::getRootId, dto.getRootId()));
if (CollectionUtils.isEmpty(iotThingRelationDetailEntities)) {
throw new SysException("当前根关系下节点信息列表异常");
}
@ -356,51 +366,47 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR
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());
}else{
item.setRootThingId(targetEntity.getRootThingId());
}
resList.add(item);
});
//剩余的节点添加到list中
iotThingRelationDetailEntities.stream()
.filter(item -> item.getSort() > finalOptionalMax.get().getSort() && !sourceChildList.stream().map(IotThingRelationDetailEntity::getId).toList().contains(item.getId()))
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))
.forEach(resList::add);
.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);
resList.forEach(item ->
resultList.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;

Loading…
Cancel
Save