package com.muyu.car.gateway.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.car.domain.api.req.VehicleConnectionReq;
import com.muyu.car.domain.model.MqttServerModel;
import com.muyu.car.domain.model.VehicleInformation;
import com.muyu.car.gateway.service.VehicleInformationService;
import com.muyu.car.mapper.VehicleInformationMapper;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author：蓬叁
 * @Package：com.muyu.car.gateway.service.impl
 * @Project：cloud-server-8
 * @name：VehicleInformationServiceImpl
 * @Date：2024/10/6 下午2:41
 */
@Log4j2
@Service
public class VehicleInformationServiceImpl implements VehicleInformationService{

    @Autowired private VehicleInformationMapper vehicleInformationMapper;

    @Autowired private RedisService redisService;

    @Override
    public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) {
        log.info("车辆连接请求:[{}]",vehicleConnectionReq);
        vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());

        List<String> selectVehicle =vehicleInformationMapper.selectVehicleVin(vehicleConnectionReq.getVehicleVin());
        if(selectVehicle.isEmpty()) {
            vehicleInformationMapper.addVehicleConnection(vehicleConnectionReq);
            log.info("车辆预上线成功");
        }else {
            log.info("车辆无法重复预上线");
        }

        // 获取名为 "ipList" 的列表
        List<String> ipList = redisService.getCacheList("ipList");
        if (ipList.isEmpty()) {
            return null;
        }
        // 获取当前使用的索引位置
        String indexStr = redisService.getCacheObject("currentIndex");
        int index = indexStr!= null? Integer.parseInt(indexStr) : 0;
        String selectedIp = ipList.get(index);
        // 获取该 IP 的使用次数
        String countStr = redisService.getCacheMapValue("ipCounts", selectedIp);
        log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr);
        int count = countStr!= null? Integer.parseInt(countStr) : 0;
        if (count < 12) {
            // 使用次数加一
            count++;
            redisService.setCacheMapValue("ipCounts", selectedIp, String.valueOf(count));
            // 更新索引
            index = (index + 1) % ipList.size();
            redisService.setCacheObject("currentIndex", String.valueOf(index));
            return new MqttServerModel("tcp://"+selectedIp.substring(1,selectedIp.length()-1)+":1883","vehicle");
        } else {
            // 如果使用次数达到 12 次，跳过该 IP 并更新索引
            index = (index + 1) % ipList.size();
            redisService.setCacheObject("currentIndex", String.valueOf(index));
            return getVehicleData(vehicleConnectionReq);
        }
    }



}
