package com.xhpc.evcs.utils;

import com.xhpc.evcs.dto.ConnectorStatusInfo;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 该全局类用于缓存上一次定时的数据
 */
public class ChangePoleStatus {

    public static Map<String, Integer> temp = new HashMap<>();

    public static Set<ConnectorStatusInfo> getChangeStatus(Set<ConnectorStatusInfo> freshInfoList) {

        if (temp.isEmpty()) {
            temp = transfer(freshInfoList);
            return freshInfoList;
        } else {
            Set<ConnectorStatusInfo> changeStatus = new HashSet<>();
            for (ConnectorStatusInfo newInfo : freshInfoList) {
                 if (temp.get(newInfo.getConnectorID()) == null || !temp.get(newInfo.getConnectorID()).equals(newInfo.getStatus())) {
                    ConnectorStatusInfo connectorStatusInfo = new ConnectorStatusInfo();
                    connectorStatusInfo.setConnectorID(newInfo.getConnectorID());
                    connectorStatusInfo.setStatus(newInfo.getStatus());
                    connectorStatusInfo.setOperatorID(newInfo.getOperatorID());
                    changeStatus.add(connectorStatusInfo);
                }
            }
            temp.putAll(transfer(freshInfoList));
            return changeStatus;
        }
    }

    private static Map<String, Integer> transfer(Set<ConnectorStatusInfo> connectorStatusInfoSet) {

        Map<String, Integer> tMap = new HashMap<>();
        for (ConnectorStatusInfo info : connectorStatusInfoSet) {
            tMap.put(info.getConnectorID(), info.getStatus());
        }
        return tMap;
    }

}
