|
@@ -14,8 +14,15 @@ package vip.xiaonuo.coldchain.modular.monitortargetregion.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollStreamUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.tree.Tree;
|
|
|
+import cn.hutool.core.lang.tree.TreeNode;
|
|
|
+import cn.hutool.core.lang.tree.TreeNodeConfig;
|
|
|
+import cn.hutool.core.lang.tree.TreeUtil;
|
|
|
+import cn.hutool.core.lang.tree.parser.DefaultNodeParser;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
@@ -39,6 +46,11 @@ import vip.xiaonuo.common.exception.CommonException;
|
|
|
import vip.xiaonuo.common.page.CommonPageRequest;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 区域管理Service接口实现类
|
|
@@ -134,4 +146,55 @@ public class MonitorTargetRegionServiceImpl extends ServiceImpl<MonitorTargetReg
|
|
|
queryWrapper.eq(MonitorTargetRegion::getMonitorTargetId, targetId);
|
|
|
return list(queryWrapper);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Tree<String>> tree() {
|
|
|
+ List<MonitorTargetRegion> monitorTargetRegionList = list();
|
|
|
+
|
|
|
+ this.fillParentLocationInfo(monitorTargetRegionList);
|
|
|
+
|
|
|
+ for (MonitorTargetRegion monitorTargetRegion : monitorTargetRegionList) {
|
|
|
+ MonitorTargetRegion byId = this.getById(monitorTargetRegion.getParentId());
|
|
|
+ if(ObjectUtil.isNotEmpty(byId)) {
|
|
|
+ monitorTargetRegion.setParentId(byId.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TreeNode<String>> treeNodeList = monitorTargetRegionList.stream().map(locationType ->
|
|
|
+ new TreeNode<>(locationType.getId(), locationType.getParentId(),
|
|
|
+ locationType.getName(), 0).setExtra(JSONUtil.parseObj(locationType)))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<Tree<String>> build = TreeUtil.build(treeNodeList, "-1");
|
|
|
+ return build;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillParentLocationInfo(List<MonitorTargetRegion> resourceList) {
|
|
|
+ if (CollUtil.isNotEmpty(resourceList)) {
|
|
|
+ List<MonitorTargetRegion> locationTypes = resourceList.stream().filter(distinctByKey(MonitorTargetRegion::getParentId)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<String> parentIds = null;
|
|
|
+ if (CollUtil.isNotEmpty(locationTypes)) {
|
|
|
+ parentIds = CollUtil.newArrayList();
|
|
|
+ for (MonitorTargetRegion locationType : locationTypes) {
|
|
|
+ if (!StrUtil.equals(locationType.getParentId(), "-1")) {
|
|
|
+ parentIds.add(locationType.getParentId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (CollUtil.isNotEmpty(parentIds)) {
|
|
|
+ LambdaQueryWrapper<MonitorTargetRegion> locationTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ locationTypeLambdaQueryWrapper.in(MonitorTargetRegion::getId, parentIds);
|
|
|
+ List<MonitorTargetRegion> parentLocationMenus = this.list(locationTypeLambdaQueryWrapper);
|
|
|
+ if (CollUtil.isNotEmpty(parentLocationMenus)) {
|
|
|
+ this.fillParentLocationInfo(parentLocationMenus);
|
|
|
+ resourceList.addAll(parentLocationMenus);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
|
|
|
+ Set<Object> seen = ConcurrentHashMap.newKeySet();
|
|
|
+ return t -> seen.add(keyExtractor.apply(t));
|
|
|
+ }
|
|
|
}
|