package com.muyu.event.service.impl;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.muyu.demos.model.Result;
import com.muyu.event.common.Fault;
import com.muyu.event.common.VehicleEvent;
import com.muyu.event.mapper.EventMapper;
import com.muyu.event.service.EventService;
import com.muyu.mqtt.dao.MessageData;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * EventServiceImpl
 *
 * @author Yangle
 * Date 2024/6/17 19:57
 */
@Service
@Log4j2
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public List<VehicleEvent> getCarEventList(VehicleEvent vehicleEvent) {
        return eventMapper.getCarEventList(vehicleEvent);
    }

    @Override
    public Result<MessageData> selectEvent(VehicleEvent vehicleEvent) {

            if (vehicleEvent.getEvent() != null) {

                if (vehicleEvent.getEvent().contains("1")) {
                    KafkaConsumer<String, String> consumer = getStringStringKafkaConsumer();
                    // 订阅主题
                    consumer.subscribe(Collections.singletonList("test1"));
                    // 创建新线程来处理消息
                    new Thread(() -> {
                        while (true) {
                            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
                            for (ConsumerRecord<String, String> record : records) {
                                // 处理消息
                                String message = record.value();
                                MessageData messageData = JSON.parseObject(message, MessageData.class);
                                // 将毫秒级时间戳转换为LocalDateTime
                                LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(messageData.getTimestamp())), ZoneId.systemDefault());

                                // 格式化输出日期时间
                                Date formattedTime = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
                                    //判断车辆状态
                                vehicleStatus(messageData, formattedTime);

                                    //判断充电状态
                                chargeStatus(messageData, formattedTime);

                                //判断运行状态
                                runStatus(messageData, formattedTime);

                                //判断SOC
                                soc(messageData, formattedTime);

                                //判断充电工作状态
                                chargeWorkStatus(messageData, formattedTime);

                                //判断驱动电机状态
                                driveMotorStatus(messageData, formattedTime);

                                //判断位置
                                location(messageData, formattedTime);

                                //判断EAS
                                eas(messageData, formattedTime);

                                //判断PTC
                                ptc(messageData, formattedTime);

                                //判断EPS
                                eps(messageData, formattedTime);

                                //判断ABS
                                abs(messageData, formattedTime);

                                //判断MCU
                                mcu(messageData, formattedTime);

                                //判断动力电池加热
                                powerBatteryHeating(messageData, formattedTime);

                                //判断动力电池电流
                                powerBatteryCurrentStatus(messageData, formattedTime);

                                //判断动力电池保温
                                powerBatteryHeat(messageData, formattedTime);

                                //判断DCDC
                                dcdc(messageData, formattedTime);

                                //判断CHG
                                chg(messageData, formattedTime);

                            }
                        }
                    }).start();
                }
                if (vehicleEvent.getEvent().contains("2")) {
                    if(vehicleEvent.getStates()){
                        if (redisTemplate.hasKey(vehicleEvent.getVin())){
                            KafkaConsumer<String, String> consumer = getStringStringKafkaConsumer();
                            // 订阅主题
                            consumer.subscribe(Collections.singletonList("test1"));
                            // 创建新线程来处理消息
                            new Thread(() -> {
                                while (true) {
                                    // 发送拉取请求
                                    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); // 设置拉取超时时间为100毫秒
                                    // 处理消息
                                    for (ConsumerRecord<String, String> record : records) {
                                        redisTemplate.opsForValue().set(vehicleEvent.getVin(),record.value());
                                        System.out.println("Received message: " + record.value());
                                        // 在这里添加消息处理逻辑
                                    }
                                }
                            }).start();
                        }

                    }
                    return Result.success();
                }
                if (vehicleEvent.getEvent().contains("3")) {

                }
            }


            return Result.error("车辆没有绑定事件");
    }


    /**
     * 本地缓存
     */
    Cache<String, String> localCache = Caffeine.newBuilder()
            .initialCapacity(5)
            .maximumSize(10)
            //过期时间3秒钟
            .expireAfterWrite(5, TimeUnit.SECONDS)
            .build();
    /**
     * kafka消费者
     * @return
     */
    private static KafkaConsumer<String, String> getStringStringKafkaConsumer() {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092"); // Kafka服务器地址
        props.put("group.id", "Partitions"); // 消费者组ID
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 键反序列化器
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 值反序列化器
        // 创建Kafka消费者实例
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        return consumer;
    }

    @Override
    public void eventAddRedis(VehicleEvent vehicleEvent) {
        redisTemplate.opsForValue().set(vehicleEvent.getVin(),"");
    }

    @Override
    public Result getMaximumCoordinate(String vin) {
       //获取本地缓存
        String ifPresent = localCache.getIfPresent(vin);
        return Result.error(ifPresent);
    }

    @Override
    public List<VehicleEvent> selectVehicleEvent() {
        return  eventMapper.selectVehicleEvent();
    }


//    //创建分区
//    NewTopic newTopic = new NewTopic(topic, 8, (short) 1);
//        kafkaAdmin.createOrModifyTopics(newTopic);
//    ProducerRecord<String, String> producerRecord = new ProducerRecord<>(topic, main.getVin(), JSON.toJSONString(main));
//    // 发送消息
//        kafkaTemplate.send(producerRecord);


    //判断车辆状态
    public  void vehicleStatus(MessageData  messageData, Date formattedTime){
        if (messageData.getVehicleStatus().equals("0")) {

            if (messageData.getVehicleStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ001")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ001")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ001", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ001", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);
               log.info("该车故障GZ001信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ001", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ001信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }

    //判断充电状态
    private void chargeStatus(MessageData messageData, Date formattedTime) {
        if (messageData.getChargeStatus().equals("0")) {

            if (messageData.getChargeStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ002")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ002")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ002", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ002", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ002信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ002", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ002信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }

        }
    }

    //判断运行状态
    private void runStatus(MessageData messageData, Date formattedTime) {
        if (messageData.getRunStatus().equals("0")) {

            if (messageData.getRunStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ003")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ003")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ003", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ003", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ003信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ003", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ003信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断SOC
    private void soc(MessageData messageData, Date formattedTime) {
        if (messageData.getSoc().equals("0")) {

            if (messageData.getSoc().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ004")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ004")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ004", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ004", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ004信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ004", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ004信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }


    // 判断充电工作状态
    private void chargeWorkStatus(MessageData messageData, Date formattedTime) {
        if (messageData.getChargeWorkStatus().equals("0")) {

            if (messageData.getChargeWorkStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ005")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ005")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ005", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ005", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ005信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ005", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ005信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }

    //判断驱动电机状态
    private  void  driveMotorStatus(MessageData messageData,Date formattedTime){
        if (messageData.getDriveMotorStatus().equals("0")) {

            if (messageData.getDriveMotorStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ006")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ006")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ006", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ006", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ006信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ006", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ006信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断定位
    private  void  location(MessageData messageData,Date formattedTime){
        if (messageData.getLocation().equals("0")) {

            if (messageData.getLocation().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ007")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ007")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ007", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ007", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ007信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ007", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ007信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断EAS
    private  void  eas(MessageData messageData,Date formattedTime){
        if (messageData.getEas().equals("0")) {

            if (messageData.getVehicleStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ008")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ008")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ008", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ008", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ008信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ008", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ008信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断EPS
    private  void  ptc(MessageData messageData,Date formattedTime){
        if (messageData.getPtc().equals("0")) {

            if (messageData.getPtc().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ009")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ009")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ009", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ009", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ009信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ009", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ009信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断EPS
    private void eps(MessageData messageData,Date formattedTime){
        if (messageData.getEps().equals("0")) {

            if (messageData.getEps().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ010")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ010")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ010", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ010", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ010信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ010", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ010信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断ABS
    private void abs(MessageData messageData,Date formattedTime){
        if (messageData.getAbs().equals("0")) {

            if (messageData.getAbs().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ011")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz0011 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ011")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz0011);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ011", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ011", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz0011);

                log.info("该车故障GZ011信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ011", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ011信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }

    //判断MCU
    private void mcu(MessageData messageData,Date formattedTime){
        if (messageData.getMcu().equals("0")) {

            if (messageData.getMcu().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ012")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ012")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ012", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ012", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ012信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ012", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ012信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断动力电池加热
    private void powerBatteryHeating(MessageData messageData,Date formattedTime){
        if (messageData.getPowerBatteryHeating().equals("0")) {

            if (messageData.getPowerBatteryHeating().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ013")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ013")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ013", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ013", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ013信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ013", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ013信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断动力电池电流
    private void powerBatteryCurrentStatus(MessageData messageData,Date formattedTime){
        if (messageData.getPowerBatteryCurrentStatus().equals("0")) {

            if (messageData.getPowerBatteryCurrentStatus().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ014")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ014")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ014", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ014", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ014信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ014", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ014信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断动力电池保温
    private void powerBatteryHeat(MessageData messageData,Date formattedTime){
        if (messageData.getPowerBatteryHeat().equals("0")) {

            if (messageData.getPowerBatteryHeat().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ015")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ015")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ0015", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ015", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ015信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ015", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ015信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断DCDC
    private void dcdc(MessageData messageData,Date formattedTime){
        if (messageData.getDcdc().equals("0")) {

            if (messageData.getDcdc().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ016")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ016")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ016", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ016", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ016信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ016", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ016信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }
    //判断CHG
    private void chg(MessageData messageData,Date formattedTime){
        if (messageData.getChg().equals("0")) {

            if (messageData.getChg().equals("0") && !redisTemplate.hasKey(messageData.getVin() + "-" + "GZ017")) {
                // 车辆状态异常，第一次发生

                // 创建故障对象
                Fault gz001 = Fault.builder()
                        .vin(messageData.getVin())
                        .faultCode("GZ017")
                        .startTime(formattedTime)
                        .build();
                String gz001Json = JSON.toJSONString(gz001);

                // 存储故障信息到本地缓存和Redis
                localCache.put(messageData.getVin() + "-" + "GZ017", gz001Json);
                redisTemplate.opsForValue().set(messageData.getVin() + "-" + "GZ017", gz001Json, 10, TimeUnit.SECONDS);

                // 发送消息到RabbitMQ
                sendToRabbitMQ(gz001);

                log.info("该车故障GZ017信息首次存储到本地缓存和Redis中，并发送到RabbitMQ");
            } else if (messageData.getVehicleStatus().equals("0")) {

                // 刷新Redis中的故障信息过期时间
                redisTemplate.expire(messageData.getVin() + "-" + "GZ017", 10, TimeUnit.SECONDS);
                log.info("该车故障GZ017信息在Redis中的过期时间被刷新，不发送到RabbitMQ");
            }
        }
    }


    public void sendToRabbitMQ(Fault gz001){
        rabbitTemplate.convertAndSend("exchange_breakdown","discover_time",JSON.toJSONString(gz001));
         log.info("故障信息"+gz001.getVin()+"发送到RabbitMQ");
    }

}
