package com.cico.nest.lcdp.boot.core.qxsd.adapter.web;

import cn.hutool.core.date.DateUtil;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.request.TunnelIdRequest;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.ReportCarTypeFlowVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.TableStatsReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.TrafficFlowMetricVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.tunnelFacility.TunnelSectionCongestionStatusVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.StatisticalReportsService;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.TunnelService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.TunnelGateway;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.TrafficFlowMetricDO;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 隧道
 * @author zws
 * @date 2024/8/12
 */

@Tag(name = "隧道--数据统计")
@RestController
@RequestMapping("/qxsd/tunnel")
public class TunnelController {

    @Resource
    private TunnelService tunnelService;

    @Resource
    private TunnelGateway tunnelGateway;

    @Resource
    private StatisticalReportsService statisticalReportsService;

    /**
     * 实时流量--隧道流量统计
     */
    @PostMapping("/trafficFlowMetric")
    @Operation(summary = "实时流量--隧道流量统计")
    public CommonResult<List<TrafficFlowMetricVO>> trafficFlowMetric(@RequestBody TunnelIdRequest request){
        List<TrafficFlowMetricDO> trafficFlowMetricDOList = tunnelService.getTrafficFlowMetricList(request.getTunnelId());
        List<TrafficFlowMetricVO> trafficFlowMetricVOList = TrafficFlowMetricVO.convert2VO(trafficFlowMetricDOList);
        TableStatsReqVO tableStatsReqVO = new TableStatsReqVO();
        Date now = DateUtil.date();
        String dateString = DateUtil.format(now, "yyyyMMdd");
        tableStatsReqVO.setEndStatisticsDay(dateString);
        tableStatsReqVO.setStartStatisticsDay(dateString);
        tableStatsReqVO.setTunnelId(request.getTunnelId());
        List<Map<String,Object>> carTypeFlowHour = statisticalReportsService.carTypeFlowHour(tableStatsReqVO);
        List<TrafficFlowMetricVO> trafficFlowMetricVOReturn = new ArrayList<>();
        for(Map<String, Object> map : carTypeFlowHour){
            Integer staticsHour = (Integer) map.get("staticsHour");

            List<ReportCarTypeFlowVO> dataList = (List<ReportCarTypeFlowVO>) map.get("dataMap");
            Integer countAll = 0;
            for(ReportCarTypeFlowVO vo:dataList){
                if("全部".equals(vo.getCatType())){
                    countAll = vo.getCount();
                }
            }
            String statisticsHourStr = "";
            if(staticsHour < 10){
                statisticsHourStr = dateString + "0" + staticsHour;
            }else{
                statisticsHourStr = dateString + staticsHour;
            }
            for(TrafficFlowMetricVO trafficFlowMetricVO : trafficFlowMetricVOList){
                String dateStr = DateUtil.format(trafficFlowMetricVO.getCreateTime(), "yyyyMMddHH");
                if(statisticsHourStr.equals(dateStr)){
                    trafficFlowMetricVO.setCarCount(countAll);
                    trafficFlowMetricVOReturn.add(trafficFlowMetricVO);
                    break;
                }
            }
        }
        return CommonResult.success(trafficFlowMetricVOReturn);
    }

    /**
     * 实时流量--隧道断面拥堵
     */
    @PostMapping("/sectionCongestion")
    @Operation(summary = "实时流量--隧道断面拥堵")
    public CommonResult<List<TunnelSectionCongestionStatusVO>> sectionCongestion(@RequestBody TunnelIdRequest request) {
        List<TunnelSectionCongestionStatusVO> sectionCongestionStatusVOS = tunnelGateway.getSectionCongestion(request.getTunnelId());
        return CommonResult.success(sectionCongestionStatusVOS);
    }

}
