EqptBuildTask.java

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

import com.fa.sesa.i18n.I18nUtils;
import com.mycim.framework.cache.utils.CacheUtils;
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.server.ems.service.EmsService;
import com.mycim.server.wip.service.WipQueryService;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.EventName;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.EntityGroup;
import com.mycim.valueobject.ems.Equipment;
import com.mycim.valueobject.security.Station;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.actions.operation.query.SelectEntityAction;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

public class EqptBuildTask implements Runnable {
    private final String filter;
    private final Map<String, Integer> countMap;
    private final Entity entity;
    private final Station station;
    private final EntityGroup entityGroup;
    private final WipQueryService wipQueryService;
    private final ConcurrentHashMap<String,Map<String,Object>> entityResultMap;
    public EqptBuildTask(String filter, Map<String, Integer> countMap, Entity entity,
                         Station station, EntityGroup entityGroup, WipQueryService wipQueryService,
                         ConcurrentHashMap<String, Map<String, Object>> entityResultMap) {
        this.filter = filter;
        this.countMap = countMap;
        this.entity = entity;
        this.station = station;
        this.entityGroup = entityGroup;
        this.wipQueryService = wipQueryService;
        this.entityResultMap = entityResultMap;
    }

    @Override
    public void run(){
        Map<String,Object> jsonEntity = new HashMap<>();
        String status = entity.getCurrentStatus();
        String chamberType = entity.getChamberType();
        // String isChamberEquip = entity.getIsChamberEquip();
        if (status != null && StringUtils.isBlank(chamberType)) {
            int waitLotCount = 0;
            if (MapUtils.isNotEmpty(countMap)) {
                waitLotCount = MapUtils.getIntValue(countMap, entity.getInstanceId());
            }
            jsonEntity.put("leaf", Boolean.TRUE);

            jsonEntity.put("waitingLotCount", waitLotCount);
            jsonEntity.put("id", String.valueOf(entity.getInstanceRrn()) + "#$#" +
                    String.valueOf(station.getInstanceRrn()) + "#$#" +
                    String.valueOf(entityGroup.getInstanceId()));
            jsonEntity.put("text", entity.getInstanceId() + " (" + status + ")" + "(" + waitLotCount + ")");
            jsonEntity.put("status", status);
            jsonEntity.put("objectType", ObjectList.ENTITY_KEY);
            jsonEntity.put("entityID", entity.getInstanceId());
            jsonEntity.put("pollutionLevel",
                           entity.getPollutionLevel() == null ? "" : entity.getPollutionLevel());
            jsonEntity.put("pmHold", String.valueOf(entity.getPmHoldFlag()));
            List<String> lotStatus = (List<String>) CacheUtils.get(String.valueOf(entity.getInstanceRrn()));
            if (CollectionUtils.isEmpty(lotStatus)) {
                lotStatus = wipQueryService.getLotsStatusEntity(entity.getInstanceRrn());
                CacheUtils.put(String.valueOf(entity.getInstanceRrn()), lotStatus);
            }
            Set<String> clsSet = new TreeSet<>();
            if (lotStatus != null && lotStatus.size() > 0) {
                if (lotStatus.contains(LotStatus.RUNNING)) {
                    clsSet.add("icon1_run");
                }
                if (lotStatus.contains(LotStatus.WAITING)) {
                    clsSet.add("icon2_wait");
                }
                if (lotStatus.contains(LotStatus.HOLD)) {
                    clsSet.add("icon3_hold");
                }
            }
            jsonEntity.put("cls", StringUtils.join(clsSet,""));
            jsonEntity.put("clsSet",clsSet);
            if ((status.equals(EventName.RUN)) || (status.equals(EventName.PT))) {
                jsonEntity.put("iconCls", "iconCls-status-run");
            } else {
                if (status.equals(EventName.IDLE)) {
                    jsonEntity.put("iconCls", "iconCls-status-idle");
                } else {
                    jsonEntity.put("iconCls", "iconCls-status-down");
                }
            }

            String entityDesc =
                    StringUtils.trim(entity.getInstanceDesc()) == null ? StringUtils.EMPTY : StringUtils
                            .trim(entity.getInstanceDesc());
            if (StringUtils.equalsIgnoreCase("CN", I18nUtils.getCurrentLanguage().toString())) {
                jsonEntity.put("qtip", "设备号:&nbsp;" + entity.getInstanceId() + "<br>" + "设备描述:&nbsp;" + entityDesc +
                                       "<br>设备状态:&nbsp;" + status);
            } else {
                jsonEntity.put("qtip",
                               "EQP ID:&nbsp;" + entity.getInstanceId() + "<br>" + "EQP Desc:&nbsp;" +
                                       entityDesc + "<br>EQP State:&nbsp;" + status);
            }
            jsonEntity.put("callback",callback);
            jsonEntity.put("cacheKey",entity.getInstanceRrn());
            jsonEntity.put("stationRrn",station.getInstanceRrn());
            jsonEntity.put("filter",filter);
            jsonEntity.put("entityGroupId",entityGroup.getInstanceId());
        }
        if(MapUtils.isNotEmpty(jsonEntity)){
            entityResultMap.put(String.format("%s_%s_%s",station.getInstanceId(),
                                              entityGroup.getInstanceId(),
                                              entity.getInstanceId()),jsonEntity);

        }
    }

    private BiConsumer<Map<Long,List<Equipment>>,Map<String,Object>> callback=(source, self)->{
        if(MapUtils.isEmpty(self)||MapUtils.isEmpty(source)){
            return;
        }
        Long instanceRrn =  MapUtils.getLong(self,"cacheKey");
        if(source.containsKey(instanceRrn)){
            List<Equipment> children =  source.get(instanceRrn);

            if(CollectionUtils.isNotEmpty(children)){
                self.put("children", executeChambers(children,MapUtils.getLong(self,"stationRrn"),
                                                 MapUtils.getString(self ,"entityGroupId"),
                                                 MapUtils.getString(self,"filter")));
                self.put("leaf", Boolean.FALSE);
            }
        }
        self.remove("callback");
        self.remove("cacheKey");
        self.remove("stationRrn");
        self.remove("entityGroupId");
        self.remove("filter");
    };
    private List<Map<String,Object>> executeChambers(List<Equipment> childrenEqpts, Long stationRrn, String entityGroupId,
                                                String filter) {
        List<Map<String, Object>> chambers = new ArrayList();
        for (Equipment equipment : childrenEqpts) {
            String status =equipment.getStatus();
            Map jsonEntity = new HashMap<>();
            String chamberType = equipment.getChamberType();
            //状态不为空并且是腔体设备
            if (status != null && StringUtils.isNotBlank(chamberType)) {
                jsonEntity.put("leaf", Boolean.TRUE);
                jsonEntity.put("id", String.valueOf(equipment.getInstanceRrn()) + "#$#" +
                        String.valueOf(stationRrn) + "#$#" + entityGroupId);
                jsonEntity.put("status", status);
                jsonEntity.put("objectType", ObjectList.ENTITY_KEY);
                jsonEntity.put("entityID", equipment.getInstanceId());
                jsonEntity.put("pollutionLevel",
                               equipment.getPollutionLevel() == null ? "" : equipment.getPollutionLevel());
                jsonEntity.put("pmHold", equipment.getPmHoldFlag() + "");

                // List lotStatus = (List<String>) CacheUtils.get(equipment.getInstanceRrn());
                // if (CollectionUtils.isEmpty(lotStatus) ) {
                //     // lotStatus = emsService.getLotsStatusEntity(equipment.getInstanceRrn());
                //     CacheUtils.put(equipment.getInstanceRrn(), lotStatus);
                // }

                if ((status.equals(EventName.RUN)) || (status.equals(EventName.PT))) {
                    jsonEntity.put("iconCls", "iconCls-status-run");
                } else {
                    if (status.equals(EventName.IDLE)) {
                        jsonEntity.put("iconCls", "iconCls-status-idle");
                    } else {
                        jsonEntity.put("iconCls", "iconCls-status-down");
                    }
                }
                jsonEntity.put("text", equipment.getInstanceId() + "(" + status + ")");
                String entityDesc =
                        StringUtils.trim(equipment.getInstanceDesc()) == null ? StringUtils.EMPTY : StringUtils
                                .trim(equipment.getInstanceDesc());
                if (StringUtils.equalsIgnoreCase("CN", I18nUtils.getCurrentLanguage().toString())) {
                    jsonEntity.put("qtip",
                                   "设备号:&nbsp;" + equipment.getInstanceId() + "<br>" + "设备描述:&nbsp;" + entityDesc +
                                           "<br>设备状态:&nbsp;" + status);
                } else {
                    jsonEntity.put("qtip", "EQP ID:&nbsp;" + equipment.getInstanceId() + "<br>" + "EQP Desc:&nbsp;" +
                            entityDesc + "<br>EQP State:&nbsp;" + status);
                }
                // 过滤条件
                if ("ALL".equalsIgnoreCase(filter)) {
                    chambers.add(jsonEntity);
                } else if (LotStatus.STATUSLIGTH_HAV.equals(filter) &&
                        "iconCls-statusLight-hav".equals(jsonEntity.get("cls"))) {
                    chambers.add(jsonEntity);
                } else if (LotStatus.STATUSLIGTH_HOLD_NO_RUN.equals(filter) &&
                        "iconCls-statusLight-waitNoRun".equals(jsonEntity.get("cls"))) {
                    chambers.add(jsonEntity);
                } else if (LotStatus.STATUSLIGTH_RUN.equals(filter) &&
                        "iconCls-statusLight-run".equals(jsonEntity.get("cls"))) {
                    chambers.add(jsonEntity);
                }
            }

        }
        sortChambersById(chambers);
        return chambers;
    }
    /**
     * 腔体设备排序
     *
     * @param chambers
     */
    private void sortChambersById(List<Map<String, Object>> chambers) {
        Collections.sort(chambers, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String name1 = (String) o1.get("entityID");
                String name2 = (String) o2.get("entityID");
                return name1.compareTo(name2);
            }
        });
    }
}