package com.xydl.cac.model;

import com.beanit.iec61850bean.BasicDataAttribute;
import com.xydl.cac.entity.*;
import com.xydl.cac.iec.IecClient;
import com.xydl.cac.iec.IecServer;
import com.xydl.cac.util.DateUtil;

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

public class StaticVariable {

    public static HashMap<Integer, IecServer> iecServerMap = new HashMap<>();
    public static HashMap<Integer, HashMap<String, String>> sensorLastDataMap = new HashMap<>();
    public static HashMap<String, String> paramRelationMap = new HashMap<>();
    public static HashMap<String, String> doneWarnMap = new HashMap<>();
    public static HashMap<Integer, IecClient> realTimeClientMap = new HashMap<>();
    public static List<IcdIed> iedTestList = new ArrayList<>();
    public static int shutdown = 0;
    public static HashMap<String, String> rptFromActiveMap = new HashMap<>();
    public static HashMap<String, String> rptToActiveMap = new HashMap<>();

    public static HashMap<String, String> unit_Cache = new HashMap<>();
    public static List<ModevType> modevType_Cache = null;
    public static List<Jg> jg_Cache = null;
    public static List<Zsb> zsb_Cache = null;
    public static ConcurrentHashMap<Integer, WarnRule> rule_Cache = new ConcurrentHashMap<>();


    // 更新服务端
    public static void updateServerNodeValue(BasicDataAttribute bda) {
        Iterator<Integer> it = StaticVariable.iecServerMap.keySet().iterator();
        while (it.hasNext()) {
            Integer key = it.next();
            IecServer iecServer = StaticVariable.iecServerMap.get(key);
            iecServer.updateBda(bda);
        }
    }

    // 更新最新数据缓存
    public static void updateLastData(Integer eqmid, String colname, String value, String time) {
        HashMap<String, String> map = StaticVariable.sensorLastDataMap.get(eqmid);
        if (map == null) {
            map = new HashMap<>();
            StaticVariable.sensorLastDataMap.put(eqmid, map);
        }
        map.put(colname, value);
        if (time != null) {
            map.put("acquisitionTime", time);
        }
    }

    // 更新最新数据缓存
    public static void updateLastData(BasicDataAttribute bda) {
        try {
            String ref = bda.getReference().toString();
            String key = ref + "_" + bda.getFc().toString();
            if (StaticVariable.paramRelationMap.containsKey(key)) {
                String value = StaticVariable.paramRelationMap.get(key);
                String[] str = value.split(",");
                Integer eqmid = Integer.parseInt(str[0]);
                String colname = str[1];
                value = bda.getValueString();
                if ("acquisitionTime".equals(colname)) {
                    value = DateUtil.fromZoneUTCToLocal(value);
                    if (value == null) {
                        return;
                    }
                }
                updateLastData(eqmid, colname, value, null);
            }
        } catch (Exception ignore) {
        }
    }
}
