package com.kym.common.cache;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.kym.common.utils.PlatformConvertUtil;
import com.kym.entity.admin.Platform;
import com.kym.entity.common.RedisKeys;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 平台配置缓存
 *
 * @author skyline
 */
public enum PlatformCache {
    INSTANCE;


    /**
     * 保存站点id-平台映射
     *
     * @param map
     */
    public void putStationId2PlatformName(Map<String, String> map) {
        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.STATION_ID_PLATFORM_NAME + k, v));
    }

    /**
     * 站点ID获取平台名称
     *
     * @param stationId
     * @return
     */
    public String getPlatformNameByStationId(String stationId) {
        return PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_PLATFORM_NAME + stationId);
    }

    /**
     * 枪号-平台名称映射
     *
     * @param map
     */
    public void putConnectorId2PlatformName(Map<String, String> map) {
        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_PLATFORM_NAME + k, v));
    }

    /**
     * 根据枪号获取平台名称
     *
     * @param connectorId
     * @return
     */
    public String getPlatformNameByConnectorId(String connectorId) {
        connectorId = PlatformConvertUtil.parse2LocalConnectorId(connectorId);
        return PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_PLATFORM_NAME + connectorId);
    }


    /**
     * 保存平台名称-配置
     *
     * @param map
     */
    public void putPlatformName2Config(Map<String, Platform> map) {
        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.PLATFORM_NAME_CONFIG + k, JSONObject.toJSONString(v)));
    }


    public Platform getPlatformByName(String... platformName) {
        var name = "EN_PLUS";
        if (platformName.length > 0 && platformName[0] != null) {
            name = platformName[0];
        }
        return JSONObject.parseObject(PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.PLATFORM_NAME_CONFIG + name), Platform.class);
    }

    /**
     * 根据充电口ID获取平台配置
     *
     * @param connectorId
     * @return
     */
    public Platform getPlatformByConnectorId(String connectorId) {
        connectorId = PlatformConvertUtil.parse2LocalConnectorId(connectorId);
        var platformName = getPlatformNameByConnectorId(connectorId);
        return getPlatformByName(platformName);
    }

    @Component
    public static class PlatformCacheInjector implements ApplicationListener<ApplicationStartedEvent> {
        private static final StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);

        @Override
        public void onApplicationEvent(ApplicationStartedEvent event) {

        }
    }

}
