java使用stream流构建自定义树形结构

实现效果:

查询基础数据做准备: 

 java实现:

package com.aa.web.controller.tree;

import com.alibaba.fastjson.JSONObject;
import com.aa.common.core.domain.AjaxResult;
import com.aa.system.domain.tree.MonitorCar;
import com.aa.system.domain.tree.MonitorParams;
import com.aa.system.domain.tree.MonitorTree;
import com.aa.system.service.IClJichuxxService;
import com.aa.system.service.ISysDeptService;
import com.aa.web.controller.tool.testMapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
public class MonitorTreeController {

    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    IClJichuxxService cljichuxxService;

    /**
     * 实时监控---树形结构数据
     * @param params
     * @return
     * @throws Exception
     */
    @PostMapping("/monitor/tree")
    public AjaxResult getMonitorTree(@RequestBody String params) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(params);
        String sign = jsonObject.getString("sign");
        String carType = jsonObject.getString("carType");
        try {
            MonitorParams params1 = new MonitorParams();
            params1.setSign(sign);
            params1.setCarType(carType);
            List<MonitorTree> deptlist = sysDeptService.getMonitorTree(params1);
            List<MonitorTree> deptlistwithcars = deptlist.stream()
                    .map(item -> {
                        List<MonitorTree> carsByDeptId = cljichuxxService.getCarsByDeptId(item.getId()).stream()
                                .map(caritem -> {
                                    if ("car".equals(caritem.getFlag())) {
                                        String online = caritem.getOnline();
                                        switch(online){
                                            case "0" :
                                                caritem.setOnline("离线");
                                                break;
                                            case "1" :
                                                caritem.setOnline("在线");
                                                break;
                                            default :
                                                break;
                                        }
                                        String city= caritem.getCity();
                                        caritem.setTotal(city);
                                    }
                                    return caritem;
                                }).collect(Collectors.toList());
                        item.setChildren(carsByDeptId);
                        item.setTotal(carsByDeptId.size()+"");
                        List<MonitorTree> carsOnline = carsByDeptId.stream()
                                .filter(item1 -> "在线".equals(item1.getOnline())).collect(Collectors.toList());
                        item.setOnline(carsOnline.size()+"");
                        return item;
                    })
                    .collect(Collectors.toList());
            List<MonitorTree> result = deptlistwithcars.stream()
                    .filter(item -> getRoot(item,deptlistwithcars))
                    .map(item -> {
                        item.setTotal(gettotal(item, deptlistwithcars));
                        item.setOnline(getonline(item,deptlistwithcars));
                        List<MonitorTree> childrencar = item.getChildren();
                        List<MonitorTree> childrendept = getChildren(item, deptlistwithcars);
                        childrendept.addAll(childrencar);
                        item.setChildren(childrendept);
                        return item;
                    })
                    .collect(Collectors.toList());
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取根节点(因为存在数据权限问题,根节点不固定)
     * @param item
     * @param list
     * @return
     */
    private boolean getRoot(MonitorTree item, List<MonitorTree> list) {
        List<Integer> arrayList = new ArrayList<Integer>();
        for (MonitorTree ele : list) {
            arrayList.add(ele.getId());
        }
        return  !arrayList.contains(item.getPcode());
    }

    /**
     * 获取children
     * @param root
     * @param all
     * @return
     */
    public List<MonitorTree> getChildren(MonitorTree root, List<MonitorTree> all) {
        List<MonitorTree> children = all.stream()
            .filter(item -> item.getPcode().equals(root.getCode()))
            .map(item -> {
                List<MonitorTree> childrencar = item.getChildren();
                List<MonitorTree> childrendept = getChildren(item, all);
                childrendept.addAll(childrencar);
                item.setChildren(childrendept);
                return item;
            }).collect(Collectors.toList());
        return children;
    }

    /**
     * 获取总数
     * @param root
     * @param all
     * @return
     */
    private String gettotal(MonitorTree root, List<MonitorTree> all) {
        int total = Integer.parseInt(root.getTotal());//先取本身的总数
        for (MonitorTree item : all){
            if(item.getPcode().equals(root.getCode())){//循环找下级
                item.setTotal(gettotal(item,all));
                int i = Integer.parseInt(item.getTotal());
                total += i;
            }
        }
        return  String.valueOf(total);
    }

    /**
     * 获取在线数
     * @param root
     * @param all
     * @return
     */
    private String getonline(MonitorTree root, List<MonitorTree> all) {
        int online = Integer.parseInt(root.getOnline());
        for (MonitorTree item : all){
            if(item.getPcode().equals(root.getCode())){//循环找下级
                item.setOnline(getonline(item,all));
                int i = Integer.parseInt(item.getOnline());
                online += i;
            }
        }
        return  String.valueOf(online);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜甜凉白开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值