package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw;

import com.alibaba.fastjson2.JSONObject;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelSectionCongestionStatusVO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.result.XlwResultData;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TrafficFlowMetricDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.MetricMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwSectionCongestionStatusDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwTrafficFlowMetricDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwTunnelIdDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zws
 * @date 2024/8/8
 */
@Service
@Slf4j
public class XlwMetricMapper implements MetricMapper {
    @Resource
    private XlwClientFeign xlwClientFeign;

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

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

    @Override
    public TrafficFlowMetricDO getTrafficFlowMetric(String tunnelId) {
        String xlwTunnelId = tunnelId;
        if(tunnelId.equals(tunnelDownId)){
            xlwTunnelId = "tunnel_down";
        }else if(tunnelId.equals(tunnelUpId)){
            xlwTunnelId = "tunnel_up";
        }
        XlwResultData<XlwTrafficFlowMetricDTO> metricResultData = xlwClientFeign.getTrafficFlowMetric(new XlwTunnelIdDTO(xlwTunnelId));
        XlwTrafficFlowMetricDTO metricData = metricResultData.getData();
        log.info("统计数据tunnelId：{},数据{}",tunnelId, JSONObject.toJSONString(metricData));
        return XlwTrafficFlowMetricDTO.convert2DO(metricData,tunnelId);
    }

    @Override
    public List<TunnelSectionCongestionStatusVO> getSectionCongestion(String tunnelId) {
        String xlwTunnelId = tunnelId;
        if(tunnelId.equals(tunnelDownId)){
            xlwTunnelId = "tunnel_down";
        }else if(tunnelId.equals(tunnelUpId)){
            xlwTunnelId = "tunnel_up";
        }
        XlwResultData<XlwSectionCongestionStatusDTO> resultData = xlwClientFeign.sectionCongestionStatus(new XlwTunnelIdDTO(xlwTunnelId));
        XlwSectionCongestionStatusDTO sectionCongestionStatusDTOS = resultData.getData();
        if (sectionCongestionStatusDTOS !=null) {
            return XlwSectionCongestionStatusDTO.dto2VO(sectionCongestionStatusDTOS.getSituations());
        }
        return null;
    }


}
