package com.ruoyi.system.controller;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.RemoteRocketMqService;
import com.ruoyi.system.domain.RmResourceRegistration;
import com.ruoyi.system.service.EpsInitialTrafficDataService;
import com.ruoyi.system.service.IEpsServerRevenueConfigService;
import com.ruoyi.system.service.IInitialSwitchInfoDetailsService;
import com.ruoyi.system.service.IRmResourceRegistrationService;
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.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 首页Controller
 * 
 * @author gyt
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/screen")
public class ScreenController extends BaseController
{
    @Autowired
    private IEpsServerRevenueConfigService epsServerRevenueConfigService;
    @Autowired
    private IRmResourceRegistrationService rmResourceRegistrationService;
    @Autowired
    private EpsInitialTrafficDataService epsInitialTrafficDataService;
    @Autowired
    private IInitialSwitchInfoDetailsService initialSwitchInfoDetailsService;
    @Autowired
    private RemoteRocketMqService remoteRocketMqService;

    /**
     * 统计当前在线服务器的流量相关的业务数
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/countBusinessByTraffic")
    public AjaxResult countBusinessByTraffic()
    {
        return success(epsServerRevenueConfigService.countBusinessByTraffic());
    }

    /**
     * 服务器在线率
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/getServerOnlineRate")
    public AjaxResult getServerOnlineRate()
    {
        return rmResourceRegistrationService.getServerOnlineRate();
    }
    /**
     * 交换机在线数量
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/countSwitchNum")
    public AjaxResult countSwitchNum()
    {
        RmResourceRegistration rmResourceRegistration = new RmResourceRegistration();
        rmResourceRegistration.setResourceType("2");
        // 查询资源列表（处理可能的null结果）
        List<RmResourceRegistration> resourceList = Optional.ofNullable(rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration))
                .orElse(Collections.emptyList());
        int onlineCount = (int) resourceList.stream()
                .filter(resource -> "1".equals(resource.getOnlineStatus()))
                .count();
        return AjaxResult.success()
                .put("total", resourceList.size())
                .put("onlineCount", onlineCount);
    }

    /**
     * 当日业务的在线设备数量统计TOP5
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/countDeviceNumTop5")
    public AjaxResult countDeviceNumTop5()
    {
        List<Map> maps = epsServerRevenueConfigService.countDeviceNumTop5();
        if(maps.isEmpty()){
            return success();
        }else{
            return success(maps);
        }
    }

    /**
     * 当前在线服务器发送带宽总流量
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/sumTrafficByServer")
    public AjaxResult sumTrafficByServer()
    {
        return success(epsInitialTrafficDataService.sumTrafficByServer());
    }
    /**
     * 当前在线交换机接收带宽总流量
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/sumTrafficBySwitch")
    public AjaxResult sumTrafficBySwitch()
    {
        return success(initialSwitchInfoDetailsService.sumTrafficBySwitch());
    }
    /**
     * 资源告警处理情况
     * @return
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/alarmProcess")
    public AjaxResult alarmProcess() {
        Map alarmList = remoteRocketMqService.alarmHandlingStatus(SecurityConstants.INNER).getData();
        return success(alarmList);
    }
}
