package com.cico.nest.lcdp.boot.core.qxsd.adapter.config.runner;

import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwDeviceInfoDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwOrganizationId;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.ws.xlw.RealTimeEventWsClient;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.ws.xlw.RealTimeVehicleWsClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zws
 * @date 2024/8/15
 */
@Component
@Slf4j
public class WsApplicationRunner implements ApplicationRunner {

    @Resource
    private RealTimeEventWsClient realTimeEventWsClient;

    @Resource
    private RealTimeVehicleWsClient realTimeVehicleWsClient;

    @Resource
    private EventDetailMapper eventDetailMapper;

    @Resource
    private XlwClientFeign xlwClientFeign;


    @Value("${qxsd.xlw.ws}")
    private String xlwWsUrl;

    @Value("${qxsd.tunnel.down-id}")
    private String tunnelDownId;

    @Value("${qxsd.tunnel.up-id}")
    private String tunnelUpId;

    @Value("${qxsd.xlw.organizationId}")
    private String organizationId;
    //设备列表
    public static Map<String,String> deviceMap = null;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        //更新事件状态
        eventDetailMapper.updateThirdEventStatusFinish();
        //获取列表信息
        XlwOrganizationId xlwOrganizationId = new XlwOrganizationId();
        xlwOrganizationId.setOrganizationId(organizationId);
        try {
            List<XlwDeviceInfoDTO> deviceInfoList = xlwClientFeign.deviceInfo(xlwOrganizationId).getData();
            if (deviceInfoList!=null) {
                deviceMap = deviceInfoList.stream().filter(e -> e.getDeviceSn() != null && e.getMilestone() != null)
                        .collect(Collectors.toMap(XlwDeviceInfoDTO::getDeviceSn, XlwDeviceInfoDTO::getMilestone));
            }

            //事件
//            realTimeEventWsClient.connectAndHandleMessage(xlwWsUrl +"/client/event",tunnelDownId);
            realTimeEventWsClient.connectAndHandleMessage(xlwWsUrl +"/client/event",tunnelUpId);
            //558891
            //车流
//            realTimeVehicleWsClient.connectAndHandleMessage(xlwWsUrl + "/client/carInfo",tunnelDownId,"下行",214395);
            realTimeVehicleWsClient.connectAndHandleMessage(xlwWsUrl + "/client/carInfo",tunnelUpId,"上行",215808);

            //realTimeEventWsClient.connectAndHandleMessage("ws://ucloud.weisea.xyz:8888/pushx/client/event",tunnelDownId);
            //realTimeVehicleWsClient.connectAndHandleMessage(xlwWsUrl + "/client/carInfo",tunnelDownId,"下行");

            //将第三方事件设置为完成状态
        } catch (Exception e) {
            log.error("=============ws异常：{}==============",e.getMessage());
        }
    }
}
