package com.xzzn.ems.enums;

import com.xzzn.ems.domain.EmsAmmeterData;
import com.xzzn.ems.domain.EmsBatteryCluster;
import com.xzzn.ems.domain.EmsBatteryData;
import com.xzzn.ems.domain.EmsBatteryGroup;
import com.xzzn.ems.domain.EmsBatteryStack;
import com.xzzn.ems.domain.EmsClusterAlarmData;
import com.xzzn.ems.domain.EmsCoolingAlarmData;
import com.xzzn.ems.domain.EmsCoolingData;
import com.xzzn.ems.domain.EmsDhData;
import com.xzzn.ems.domain.EmsEmsData;
import com.xzzn.ems.domain.EmsPcsAlarmData;
import com.xzzn.ems.domain.EmsPcsBranchData;
import com.xzzn.ems.domain.EmsPcsData;
import com.xzzn.ems.domain.EmsStackAlarmData;
import com.xzzn.ems.domain.EmsXfData;

import java.util.HashMap;
import java.util.Map;

/**
 * 设备匹配表枚举类
 */
public enum DeviceMatchTable
{
    PCS("PCS", "ems_pcs_data", EmsPcsData.class),
    BRANCH("BRANCH", "ems_pcs_branch_data", EmsPcsBranchData.class),
    STACK("STACK", "ems_battery_stack", EmsBatteryStack.class),
    CLUSTER("CLUSTER", "ems_battery_cluster", EmsBatteryCluster.class),
    BATTERY("BATTERY", "ems_battery_data", EmsBatteryData.class),
    AMMETER("AMMETER", "ems_ammeter_data", EmsAmmeterData.class),
    COOLING("COOLING", "ems_cooling_data", EmsCoolingData.class),
    DH("DH", "ems_dh_data", EmsDhData.class),
    XF("XF", "ems_xf_data", EmsXfData.class),
    BATTERY_GROUP("BATTERY_GROUP", "ems_battery_group", EmsBatteryGroup.class),
    EMS("EMS", "ems_ems_data", EmsEmsData.class),

    /** 告警点位 */
    COOLING_ALARM("COOLING_ALARM", "ems_cooling_alarm_data", EmsCoolingAlarmData.class),
    STACK_ALARM("STACK_ALARM", "ems_stack_alarm_data", EmsStackAlarmData.class),
    CLUSTER_ALARM("CLUSTER_ALARM", "ems_cluster_alarm_data", EmsClusterAlarmData.class),
    PCS_ALARM("PCS_ALARM", "ems_pcs_alarm_data", EmsPcsAlarmData.class),
    ;

    private final String code;
    private final String matchTable;

    private final Class<?> matchTableClass;

    DeviceMatchTable(String code, String matchTable, Class<?> matchTableClass)
    {
        this.code = code;
        this.matchTable = matchTable;
        this.matchTableClass = matchTableClass;
    }

    public String getCode()
    {
        return code;
    }

    public String getMatchTable() {
        return matchTable;
    }

    public Class<?> getMatchTableClass() {
        return matchTableClass;
    }

    // 缓存code与matchTable的映射（优化查询效率）
    private static final Map<String, String> DEVICE_MATCH_TABLE_MAP = new HashMap<>();

    // 缓存table与实体类的映射（优化查询效率）
    private static final Map<String, Class<?>> TABLE_TO_CLASS_MAP = new HashMap<>();

    // 静态块初始化缓存
    static {
        for (DeviceMatchTable category : DeviceMatchTable.values()) {
            DEVICE_MATCH_TABLE_MAP.put(category.code, category.matchTable);
            TABLE_TO_CLASS_MAP.put(category.matchTable, category.matchTableClass);
        }
    }


    // 通过code获取matchTable的方法
    public static String getMatchTableByCode(String code) {
        return DEVICE_MATCH_TABLE_MAP.get(code); // 从缓存中直接获取，效率高
    }

    // 通过table获取实体类的方法
    public static Class<?> getClassByTable(String matchTable) {
        return TABLE_TO_CLASS_MAP.get(matchTable); // 从缓存中直接获取，效率高
    }

    public static String getAlarmMatchTableByCode(String code) {
        return DEVICE_MATCH_TABLE_MAP.get(code + "_ALARM"); // 从缓存中直接获取，效率高
    }

}
