package com.muyu.cloud.vehicle.gateway.service.impl;

import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
import com.muyu.cloud.vehicle.gateway.mapper.VehicleConnectionMapper;
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class VehicleConnectionServiceImpl implements VehicleConnectionService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private VehicleConnectionMapper vehicleConnectionMapper;

    @Override
    public void getConnect(VehicleConnectionReq vehicleConnectionReq) {
        log.info("车辆连接请求：{}",vehicleConnectionReq.toString());

        //发送消息
        rabbitTemplate.convertAndSend("exchange_topics_inform","",vehicleConnectionReq.getVehicleVin());

        VehicleConnection vehicleConnection = new VehicleConnection();
        vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
        vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
        vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
        vehicleConnectionMapper.addConnect(vehicleConnection);
    }
}
