StationTreeBuildTask.java

package com.mycim.webapp.actions.operation.query.threadtask;

import com.fa.sesa.i18n.I18nUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.ems.EntityGroup;
import com.mycim.valueobject.security.Station;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * 并行处理 每条station 数据,并构建成树结构
 */
public class StationTreeBuildTask implements Runnable{
    private final Station station;
    private final List<Map<String,Object>> result ;
    public StationTreeBuildTask(Station station, List<Map<String,Object>> result) {
        this.station = station;
        this.result = result;
    }

    @Override
    public void run() {
        Map<String, Object> jsonStation = new HashMap<>();
        jsonStation.put("id", String.valueOf(station.getInstanceRrn()));
        jsonStation.put("text", station.getInstanceId());
        jsonStation.put("leaf", Boolean.valueOf(false));
        jsonStation.put("iconCls", "iconCls-station");
        jsonStation.put("objectType", ObjectList.STATION_KEY);
        String stationDesc =
                station.getInstanceDesc() == null ? StringUtils.EMPTY : StringUtils.trim(station.getInstanceDesc());
        if (StringUtils.equalsIgnoreCase("CN", I18nUtils.getCurrentLanguage().toString())) {
            jsonStation.put("qtip",
                            "机台控制权限组号:&nbsp;" + station.getInstanceId() + "<br>" + "机台控制权限组描述:&nbsp;" + stationDesc);
        } else {
            jsonStation.put("qtip", "Station ID:&nbsp;" + station.getInstanceId() + "<br>" + "Station Desc.:&nbsp;" +
                    stationDesc);
        }
        // for每个工作组下的设备组
        List<Map<String,Object>> jsonEntityGroupArray = new ArrayList();
        Collection entityGroups = station.getEntityGroups();
        jsonStation.put("callback", callback);//传入函数指针 并行回调
        jsonStation.put("cacheKey",station.getInstanceId());
        jsonStation.put("entityGroups", entityGroups);
        jsonStation.put("children", jsonEntityGroupArray);
        result.add(jsonStation);
    }

    /**
     * Station 对象内的collection 没有类型
     * 1。不可为空
     * 2。类型必须是entity group
     * 3。(降低侵入性)没有实现equals hashCode情况下,同时需要避免重复数据
     */
    private BiConsumer<Map<String, Map<String,Object>>, Map<String, Object>> callback = (source, self) -> {
        if (MapUtils.isNotEmpty(self)) {
            Object entityGroupsObj = self.get("entityGroups");
            if (entityGroupsObj instanceof Collection) {//判断是否存在,并且类型是否对应
                //集合内对象必须是 EntityGroup class
                //只筛选出entity group 的类,强转安全
                //需要得到所有entity group 的id ,查询出来的时候 rrn为0 ,这里以id为作为唯一单位
                Set<String> entityGroupIds = (Set<String>) (
                        ((Collection) entityGroupsObj).stream() //instance of 保证类型
                        .filter(e -> e instanceof EntityGroup)//筛选EntityGroup类的数据 并不为null
                        .map(e -> ((EntityGroup) e).getInstanceId())//得到所有entity group 的  instance id
                        .collect(Collectors.toSet()) //set
                );
                if (CollectionUtils.isNotEmpty(entityGroupIds)) {//逐行写入entity group数据
                    for(String entityGroupId : entityGroupIds){
                        String cacheKey = String.format("%s_%s",self.get("cacheKey"),entityGroupId);
                        Map<String,Object> child =   source.get(cacheKey);
                        if(MapUtils.isEmpty(child)){
                            continue;
                        }
                        List<Map<String,Object>> jsonEntityGroupArray=
                                (List) self.computeIfAbsent("children",key->new ArrayList<Map<String,Object>>());
                        jsonEntityGroupArray.add(child);
                    }
                }
            }
        }
        //删除回调
        self.remove("callback");
        self.remove("entityGroups");
        self.remove("cacheKey");
    };

}