package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.system.domain.EpsInitialTrafficData;
import com.ruoyi.system.domain.InitialSwitchInfoDetails;
import com.ruoyi.system.service.EpsInitialTrafficDataService;
import com.ruoyi.system.service.IInitialSwitchInfoDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Slf4j
@RestController
@RequestMapping("calculateBandwidth")
public class CalculateController extends BaseController {

    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private EpsInitialTrafficDataService epsInitialTrafficDataService;
    @Autowired
    private IInitialSwitchInfoDetailsService initialSwitchInfoDetailsService;

    @GetMapping("/calculate95BandwidthDaily")
    public void calculate95BandwidthDaily(String day){
        // 获取昨天的日期范围（北京时间）
        LocalDate yesterday = LocalDate.parse(day);
        String dailyStartTime = yesterday.atStartOfDay().format(TIME_FORMAT);    // 00:00:00
        String dailyEndTime = yesterday.atTime(23, 59, 59).format(TIME_FORMAT);   // 23:59:59
        // 日
        String dayOrMonth = "1";
        // 95带宽值/日
        EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
        queryParam.setDayOrMonth(dayOrMonth);
        InitialSwitchInfoDetails initialSwitchInfoDetails = new InitialSwitchInfoDetails();
        initialSwitchInfoDetails.setDayOrMonth(dayOrMonth);
//        epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam, dailyStartTime, dailyEndTime, "1000");
//        epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam, dailyStartTime, dailyEndTime, "1024");
//        initialSwitchInfoDetailsService.calculateSwitch95BandwidthDaily(initialSwitchInfoDetails, dailyStartTime, dailyEndTime, "1000");
//        initialSwitchInfoDetailsService.calculateSwitch95BandwidthDaily(initialSwitchInfoDetails, dailyStartTime, dailyEndTime, "1024");
    }
    @GetMapping("/calculateMonthlyBandwidth")
    public void calculateMonthlyBandwidth(String day){
        // 获取上个月的日期范围
        LocalDate lastMonth = LocalDate.parse(day);
        LocalDate firstDayOfMonth = lastMonth.withDayOfMonth(1);
        LocalDate lastDayOfMonth = lastMonth.withDayOfMonth(lastMonth.lengthOfMonth());

        String monthlyStartTime = firstDayOfMonth.atStartOfDay().format(TIME_FORMAT);
        String monthlyEndTime = lastDayOfMonth.atTime(23, 59, 59).format(TIME_FORMAT);
        calculateServerMonthlyBandwidth(monthlyStartTime, monthlyEndTime, "1000");
        calculateServerMonthlyBandwidth(monthlyStartTime, monthlyEndTime, "1024");
        calculateSwitchMonthlyBandwidth(monthlyStartTime, monthlyEndTime, "1000");
        calculateSwitchMonthlyBandwidth(monthlyStartTime, monthlyEndTime, "1024");
    }
    /**
     * 计算服务器月95带宽值
     */
    private void calculateServerMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime, String calculationMode) {
        log.info("开始计算服务器月95带宽值...");
        try {
            String dayOrMonth = "2";
            EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
            queryParam.setDayOrMonth(dayOrMonth);
            epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam, monthlyStartTime, monthlyEndTime, calculationMode);
            log.info("服务器月95带宽值计算完成");
        } catch (Exception e) {
            log.error("计算服务器月95带宽值失败", e);
        }
    }

    /**
     * 计算交换机月95带宽值
     */
    private void calculateSwitchMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime, String calculationMode) {
        log.info("开始计算交换机月95带宽值...");
        try {
            String dayOrMonth = "2";
            InitialSwitchInfoDetails queryParam = new InitialSwitchInfoDetails();
            queryParam.setDayOrMonth(dayOrMonth);
            initialSwitchInfoDetailsService.calculateSwitch95BandwidthDaily(queryParam, monthlyStartTime, monthlyEndTime, calculationMode);
            log.info("交换机月95带宽值计算完成");
        } catch (Exception e) {
            log.error("计算交换机月95带宽值失败", e);
        }
    }
}
