package com.huatek.busi.service.impl.monitor.helper;

import java.util.Map;

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.entity.*;
import com.huatek.frame.base.util.BusinessConstants;
import org.springframework.util.StringUtils;

import com.google.common.collect.Maps;
import com.huatek.busi.common.SiteConstants.GeneratorTreeEnum;
import com.huatek.busi.common.SiteConstants.CableTreeEnum;
import com.huatek.busi.common.SiteConstants.ArresterTreeEnum;
import com.huatek.busi.common.SiteConstants.GisTreeEnum;
import com.huatek.busi.common.SiteConstants.OtherTreeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.torch.frame.exception.OwlException;

/**
 * 监测数据表的映射关系工具类
 *
 * @author anker_cheng
 * @date 2019/09/11
 */
public final class MonitorDataTableMapping {

    /**
     * key: 主设备数类型编码+外挂设备树类型编码</br>
     * value: entity的全路径名
     */
    private static final Map<String, String> TABLE_MAPPING;

    static {
        TABLE_MAPPING = Maps.newHashMapWithExpectedSize(30);
        // 变压器
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.YSP_EQM, TransformerEpaData.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.JF_EQM, TransformerPd.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.TX_EQM, TransformerIcge.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.HW_EQM, TransformerInfraredTemperature.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.TP_EQM, TransformerReadTable.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.RX_EQM, CapacitanceEquipmentIm.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.QX_EQM, TransformerMeteorological.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.JJ_EQM, TransformerIcge.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.TG_EQM, null);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.CW_EQM, CwzzTemperature.class);
        put(TABLE_MAPPING, SiteTreeEnum.BYQ_EQM, TransfromerTreeEnum.RZCW_EQM, TransformerWindingTemperature.class);
        // GIS
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.JF_EQM, TransformerPd.class);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.SF6_EQM, GisSf6Pressure.class);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.JXTX_EQM, null);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.QX_EQM, TransformerMeteorological.class);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.HW_EQM, TransformerInfraredTemperature.class);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.TP_EQM, GisReadTable.class);
        put(TABLE_MAPPING, SiteTreeEnum.GIS_HL_EQM, GisTreeEnum.HJJC_EQM, GisSf6Env.class);
        // 避雷器
        put(TABLE_MAPPING, SiteTreeEnum.BLQ_HL_EQM, ArresterTreeEnum.JY_EQM, LightningrodIm.class);
        put(TABLE_MAPPING, SiteTreeEnum.BLQ_HL_EQM, ArresterTreeEnum.HW_EQM, TransformerInfraredTemperature.class);
        put(TABLE_MAPPING, SiteTreeEnum.BLQ_HL_EQM, ArresterTreeEnum.RX_EQM, CapacitanceEquipmentIm.class);
        put(TABLE_MAPPING, SiteTreeEnum.BLQ_HL_EQM, ArresterTreeEnum.QX_EQM, TransformerMeteorological.class);
        put(TABLE_MAPPING, SiteTreeEnum.BLQ_HL_EQM, ArresterTreeEnum.TP_EQM, ArresterReadTable.class);
        // 测温柜
        put(TABLE_MAPPING, SiteTreeEnum.CW_HL_EQM.getValue() + "000", CwgTemperature.class);

        // 电缆局放
        put(TABLE_MAPPING, SiteTreeEnum.OTHER_EQM, OtherTreeEnum.DL_EQM, TransformerPd.class);
        // 电缆环流
        put(TABLE_MAPPING, SiteTreeEnum.OTHER_EQM, OtherTreeEnum.DLHL_EQM, BusiCableCirculation.class);

        // 电缆
        put(TABLE_MAPPING, SiteTreeEnum.DL_EQM, CableTreeEnum.DLJF_EQM, TransformerPd.class);
        put(TABLE_MAPPING, SiteTreeEnum.DL_EQM, CableTreeEnum.DLHL_EQM, IBusiCable.class);
        put(TABLE_MAPPING, SiteTreeEnum.DL_EQM, CableTreeEnum.HW_EQM, TransformerInfraredTemperature.class);
        put(TABLE_MAPPING, SiteTreeEnum.DL_EQM, CableTreeEnum.CW_EQM, CwzzTemperature.class);

        // 发电机
        put(TABLE_MAPPING, SiteTreeEnum.GEN_EQM, GeneratorTreeEnum.ZD_EQM, BusiGeneratorAmpl.class);
        put(TABLE_MAPPING, SiteTreeEnum.GEN_EQM, GeneratorTreeEnum.YL_EQM, BusiGeneratorPressure.class);
    }



    /**
     * 获取表映射
     *
     * @param equipmentType
     *            主设备树类型编码
     * @param subEquipmentType
     *            外挂设备树类型编码
     * @return entity的全路径名
     */
    public static String getTableMapping(String equipmentType, String subEquipmentType) {
        String value = TABLE_MAPPING.get(equipmentType + subEquipmentType);
        if (StringUtils.isEmpty(value)) {
            throw new OwlException("不存在的配置：" + value);
        }
        return value;
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, TransfromerTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, GisTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, ArresterTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, OtherTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, CableTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static void put(Map<String, String> map, SiteTreeEnum siteTree, GeneratorTreeEnum tTree, Class<?> clazz) {
        put(map, siteTree.getValue() + tTree.getValue(), clazz);
    }

    static synchronized void put(Map<String, String> map, String key, Class<?> clazz) {
        if (clazz == null) {
            return;
        }
        String value = map.get(key);
        if (!StringUtils.isEmpty(value)) {
            throw new OwlException("重复配置：key = " + key + ", value = " + value);
        }
        map.put(key, clazz.getName());
    }
}
