package com.muyu.car.redis;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.muyu.car.constant.RedisConstant;
import com.muyu.car.domain.VehicleMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

@Component
public class RedisInitialize {


    @Autowired
    private RedisTemplate<String,String>redisTemplate;

    @PostConstruct
    public void a() {

        new Thread(()->{
           try {
               Thread.sleep(1000);
           }catch (Exception exception){
               throw new RuntimeException(exception);
           }
        });
        VehicleMessage message1 = new VehicleMessage();
        message1.setStartTime(System.currentTimeMillis());
        message1.setSpeed("50");
        message1.setLongitude("126.397428");
        message1.setLatitude("37.90923");
        message1.setTotalMileage("1010");
        message1.setTotalVoltage("22.5");
        message1.setAcceleratorPedalTravelValue("1.5");
        message1.setBrakePedalTravelValue("1.2");
        message1.setSpecificFuelConsumption("1.8");
        message1.setMotorControllerTemperature("59");
        message1.setMotorSpeed("850");
        message1.setMotorTorque("110");
        message1.setMotorTemperature("53");
        message1.setMotorVoltage("12.5");
        message1.setMotorCurrent("1.1");
        message1.setPowerBatteryRemainingSOC("88");
        message1.setMaximumPower("999");
        message1.setMaximumDischargePower("950");
        message1.setDcdc("2");
        message1.setChg("2");
        message1.setBMSSelfCheckCounter("2");
        message1.setElectricCurrent("2.3");
        message1.setTotalVoltageV3("13.1");
        message1.setSingleMaximumVoltage("14.1");
        message1.setMinimumVoltageOfABattery("12.2");
        message1.setMaximumBatteryTemperature("85");
        message1.setMinimumBatteryTemperature("51");
        message1.setPowerBatteryAvailableCapacity("560");
        message1.setCombinedCurrent("1.1");
        message1.setRunningState("2");
        message1.setWorkStatus("2");
        message1.setDriveMotorCondition("1");
        message1.setVehicleStatus("1");
        message1.setChargingState("1");
        message1.setHeatingState("1");
        message1.setCarVin("1HGCM826X3A004352");

        redisTemplate.opsForValue().set(RedisConstant.VEHICLE_ENTERPRISE + message1.getCarVin(), JSON.toJSONString(message1));
    }

    @PostConstruct
    public void initialize() {


//
//        new Thread(() -> {
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//                List<VehicleMessageMiddle> vehicleMessageMiddleList = vehicleMessageMiddleService.list();
//                vehicleMessageMiddleList.forEach(vehicleMessageMiddle -> {
//                    List<MessageDetail> messageDetailList = messageDetailService.list(new LambdaQueryWrapper<>() {{
//                        in(MessageDetail::getId, Arrays.asList(vehicleMessageMiddle.getMessageIds().split(",")));
//                    }});
//                    String jsonString = JSON.toJSONString(messageDetailList);
//                    redisTemplate.opsForHash().put(RedisConstant.MESSAGE_DETAIL, vehicleMessageMiddle.getCarVin(), jsonString);
//                });
//            });

//        MessageDetail messageDetail = new MessageDetail();
//        messageDetail.setKeyCode("1");
//        messageDetail.setLabel("测试");
//        messageDetail.setStartBit(0);
//        messageDetail.setStopBit(8);
//        messageDetail.setType("1");

//        List<VehicleMessageMiddle> list = vehicleMessageMiddleService.list();
//        list.forEach(vehicleMessageMiddle -> {
//            List<MessageDetail> messageDetailList = messageDetailService.list(new LambdaQueryWrapper<>() {{
//                in(MessageDetail::getId, Arrays.asList(vehicleMessageMiddle.getMessageIds().split(",")));
//        });
//        String jsonString = JSON.toJSONString(messageDetailList);
//        redisTemplate.opsForHash().put(RedisConstant.VEHICLE_ENTERPRISE, message1.getCarVin(), String.valueOf(jsonString));

        new Thread(()->{
            try {
                Thread.sleep(500);
            }catch (Exception exception){
             throw new RuntimeException(exception);
            }
        });
        VehicleMessage message1 = new VehicleMessage();
        message1.setStartTime(System.currentTimeMillis());
        message1.setSpeed("50");
        message1.setLongitude("116.397428");
        message1.setLatitude("39.90923");
        message1.setTotalMileage("1000");
        message1.setTotalVoltage("12.5");
        message1.setAcceleratorPedalTravelValue("0.5");
        message1.setBrakePedalTravelValue("0.2");
        message1.setSpecificFuelConsumption("0.8");
        message1.setMotorControllerTemperature("60");
        message1.setMotorSpeed("800");
        message1.setMotorTorque("100");
        message1.setMotorTemperature("70");
        message1.setMotorVoltage("12.6");
        message1.setMotorCurrent("1.2");
        message1.setPowerBatteryRemainingSOC("80");
        message1.setMaximumPower("1000");
        message1.setMaximumDischargePower("900");
        message1.setDcdc("1");
        message1.setChg("1");
        message1.setBMSSelfCheckCounter("1");
        message1.setElectricCurrent("2.5");
        message1.setTotalVoltageV3("13.5");
        message1.setSingleMaximumVoltage("14.5");
        message1.setMinimumVoltageOfABattery("12.0");
        message1.setMaximumBatteryTemperature("80");
        message1.setMinimumBatteryTemperature("50");
        message1.setPowerBatteryAvailableCapacity("800");
        message1.setCombinedCurrent("1.5");
        message1.setRunningState("1");
        message1.setWorkStatus("1");
        message1.setDriveMotorCondition("1");
        message1.setVehicleStatus("1");
        message1.setChargingState("1");
        message1.setHeatingState("1");
        message1.setCarVin("1HGCM826X3A004352");

        redisTemplate.opsForValue().set(RedisConstant.VEHICLE_ENTERPRISE + message1.getCarVin(), JSON.toJSONString(message1));
    }
}
