package com.muyu.processing.consumer;

import com.muyu.domain.Fence;
import com.muyu.domain.Vehicle;
import com.muyu.domain.WarnRule;
import com.muyu.domain.resp.VehicleManageResp;
import com.muyu.domain.resp.WarnRuleResp;
import com.muyu.enterprise.cache.*;
import com.muyu.processing.utils.CacheUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 上线监听
 * @Author：杨鹏
 * @Package：com.muyu.processing.consumer
 * @Project：car-cloud-server
 * @name：MQconsumer
 * @Date：2024/9/29 17:19
 */
@Log4j2
@Component
public class OnLineMonitoringConsumer {

    /**
     * 上线监听队列名称
     */
    private static final String ON_LINE_MONITORING = "MQ_ON_LINE_MONITORING";
    /**
     * 上线监听交换机名称
     */
    private static final String ONLINE_EXCHANGE = "ONLINE_EXCHANGE";

    @Resource
    private CacheUtil cacheUtil;

    @Resource
    private VehicleCacheService vehicleCacheService;

    @Resource
    private AllVehicleCacheService allVehicleCacheService;

    @Resource
    private FaultCacheService faultCacheService;

    @Resource
    private FenceCahceService fenceCahceService;

    @Resource
    private WarnRuleCacheService warnRuleCacheService;

    @Resource
    private WarnStrategyCacheService warnStrategyCacheService;

    /**
     * 上线监听车辆网关中车辆上线时
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = ON_LINE_MONITORING, declare = "true"),
            exchange = @Exchange(value = ONLINE_EXCHANGE, type = "fanout")))
    public void online(String vin){
        log.info("添加本地缓存,车辆vin: {}", vin);
        // 获取redis中的数据
        Fence fence = fenceCahceService.get(vin);
        Object breakdown = faultCacheService.get(vin);
        Vehicle vehicle = vehicleCacheService.get(vin);
        WarnRule warnRule = warnRuleCacheService.get(vin);
        WarnRuleResp warnRuleResp = warnStrategyCacheService.get(vin);
        VehicleManageResp vehicleManageResp = allVehicleCacheService.get(vin);
        // 封装从redis中获得的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("fence",fence);
        map.put("breakdown",breakdown);
        map.put("vehicle",vehicle);
        map.put("warnRule",warnRule);
        map.put("warnRuleResp",warnRuleResp);
        map.put("vehicleManageResp",vehicleManageResp);
        // 添加到本地缓存中
        cacheUtil.put(vin,map);
        log.info("vin码为: {}, 数据为: {}, 已完成本地缓存",vin,map);
    }

}
