//package com.muyu.event.util;
//
//import com.muyu.iotDB.config.IotDBSessionConfig;
//import com.muyu.iotDB.service.IotDbServer;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.CacheManager;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.util.LinkedHashMap;
//
//@Slf4j
//@Service
//public class KafkaConsumer {
//    LinkedHashMap<String, EventStrategy> eventStrategyLinkedHashMap = new LinkedHashMap<>();
//    @Autowired
//    private CacheManager cacheManager;
//
//    @Autowired
//    private CarEventService carEventService;
//
//    @Autowired
//    private RedisTemplate<String,String> redisTemplate;
//    @Resource
//    private IotDbServer iotDbServer;
//    @Resource
//    private IotDBSessionConfig iotDBSessionConfig;
//
//    @PostConstruct
//    public void  initStrategyMap(){
//        eventStrategyLinkedHashMap.put("ELECTRONIC_FENCE",EventStrategy.ELECTRONIC_FENCE);
//        eventStrategyLinkedHashMap.put("FAULT_ALARM",EventStrategy.FAULT_ALARM);
//        eventStrategyLinkedHashMap.put("INDEX_WARNING",EventStrategy.INDEX_WARNING);
//        eventStrategyLinkedHashMap.put("REAL_TIME_DATA",EventStrategy.REAL_TIME_DATA);
//    }
//    @KafkaListener(topics = "topichyc", groupId = "firstGroup", containerFactory = "kafkaListenerContainerFactory",
//    errorHandler = "myKafkaListenerErrorHandler")
//    public void consume(List<ConsumerRecord<Object,Object>> consumerRecords, Acknowledgment acknowledgment) {
//        try {
//            //策略map集合
////            LinkedHashMap<String, Strategy> stringStrategyLinkedHashMap = new LinkedHashMap<>();
////            stringStrategyLinkedHashMap.put("存储数据", Strategy.STORE_DATA);
////            stringStrategyLinkedHashMap.put("实时数据",Strategy.REAL_TIME_DATA);
//
//            log.info("拉取到的记录数是:{}",consumerRecords.size());
//            for (ConsumerRecord<Object, Object> consumerRecord : consumerRecords) {
//                String value = (String) consumerRecord.value();
//
//                VehicleData vehicleData = JSONObject.parseObject(value, VehicleData.class);
//                log.error("消费者0得到的数据:{},所在分区:{}",vehicleData.toString(),consumerRecord.partition());
//
//                iotDbServer.insertData(vehicleData);
//                //得到小车绑定信息
//                String carEventByVin = this.getCarEventByVin(vehicleData.getVin());
//                //解析绑定的信息
//                String[] eventArr = carEventByVin.split(",");
//                for (String eventString : eventArr) {
//                    //处理事件
//                   eventStrategyLinkedHashMap.get(eventString).exe(cacheManager,redisTemplate,vehicleData);
//                }
//            }
//            //解析得到VIN
//
//
//
////            String dataMessage = value.toString();
////            JSONObject jsonObject = JSON.parseObject(dataMessage);
////            //根据VIN得到该小车拥有的事件
////            String vin = jsonObject.get("vin").toString();
////            List<String> eventList = getEvent(vin);
////            //循环事件集合,并执行响应的事件
////            for (String event : eventList) {
////                stringStrategyLinkedHashMap.get(event).exe(redisTemplate,dataMessage);
////            }
//
//        } catch (ServerException e) {
//            log.error("添加iotDb异常");
//            throw new RuntimeException(e);
//        } catch (IoTDBConnectionException e) {
//            throw new RuntimeException(e);
//        } catch (StatementExecutionException e) {
//            throw new RuntimeException(e);
//        } finally {
//            acknowledgment.acknowledge();
//        }
//    }
//
//    public List<String> getEvent(String vin){
//        ArrayList<String> strings = new ArrayList<>();
//
//        strings.add("存储数据");
//        int nextInt = new Random().nextInt(100);
//        if (nextInt % 2 ==0){
//            strings.add("实时数据");
//        }
//        return strings;
//    }
//
//    public String getCarEventByVin(String vin){
//
//
//        // 先从本地缓存获取
//        Cache cache = cacheManager.getCache("carEvent");
//        Cache.ValueWrapper valueWrapper = cache.get(vin);
//        if (valueWrapper != null) {
//            return valueWrapper.get().toString();
//        }
//
//        // 从 Redis 获取
//
//        String carEventString = redisTemplate.opsForValue().get(vin);
//        if (carEventString != null) {
//            // 将数据同步到本地缓存
//            cache.put(vin, carEventString);
//            return carEventString;
//        }
//
//        // 从 MySQL 获取
//        CacheCarEvent cacheCarEvent = carEventService.getCarEvent(vin);
//        if (cacheCarEvent != null) {
//            // 将数据存入 Redis 和本地缓存
//            redisTemplate.opsForValue().set(vin, cacheCarEvent.getEventValue());
//            cache.put(vin, cacheCarEvent.getEventValue());
//        }
//
//        return cacheCarEvent.getEventValue();
//
//    }
//
//    public void updateValueWithoutChangingTTL(String key, String newValue) {
//        // 获取当前的过期时间
//        Long currentTTL = redisTemplate.getExpire(key, TimeUnit.SECONDS);
//
//        // 更新键的值
//        redisTemplate.opsForList().rightPush( key,newValue);
//
//        // 重新设置过期时间
//        if (currentTTL != null && currentTTL > 0) {
//            redisTemplate.expire(key, currentTTL, TimeUnit.SECONDS);
//        }
//    }
//}
