package com.ruoyi.monitor.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.monitor.domain.SystemMonitor;
import com.ruoyi.monitor.service.ISystemMonitorService;

/**
 * 系统监控Controller
 * 
 * @author ruoyi
 * @date 2025-12-17
 */
@RestController
@RequestMapping("/monitor/system")
public class SystemMonitorController extends BaseController
{
    @Autowired
    private ISystemMonitorService systemMonitorService;

    /**
     * 查询系统监控列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(SystemMonitor systemMonitor)
    {
        startPage();
        List<SystemMonitor> list = systemMonitorService.selectSystemMonitorList(systemMonitor);
        return getDataTable(list);
    }

    /**
     * 导出系统监控列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:export')")
    @Log(title = "系统监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemMonitor systemMonitor)
    {
        List<SystemMonitor> list = systemMonitorService.selectSystemMonitorList(systemMonitor);
        ExcelUtil<SystemMonitor> util = new ExcelUtil<SystemMonitor>(SystemMonitor.class);
        util.exportExcel(response, list, "系统监控数据");
    }

    /**
     * 获取系统监控详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping(value = "/{monitorId}")
    public AjaxResult getInfo(@PathVariable("monitorId") Long monitorId)
    {
        return success(systemMonitorService.selectSystemMonitorByMonitorId(monitorId));
    }

    /**
     * 新增系统监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:add')")
    @Log(title = "系统监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SystemMonitor systemMonitor)
    {
        return toAjax(systemMonitorService.insertSystemMonitor(systemMonitor));
    }

    /**
     * 修改系统监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:edit')")
    @Log(title = "系统监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemMonitor systemMonitor)
    {
        return toAjax(systemMonitorService.updateSystemMonitor(systemMonitor));
    }

    /**
     * 删除系统监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:remove')")
    @Log(title = "系统监控", businessType = BusinessType.DELETE)
	@DeleteMapping("/{monitorIds}")
    public AjaxResult remove(@PathVariable Long[] monitorIds)
    {
        return toAjax(systemMonitorService.deleteSystemMonitorByMonitorIds(monitorIds));
    }

    /**
     * 获取系统健康状态
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/health")
    public AjaxResult getSystemHealth()
    {
        Map<String, Object> healthStatus = systemMonitorService.getSystemHealthStatus();
        return success(healthStatus);
    }

    /**
     * 收集当前系统监控数据
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:collect')")
    @Log(title = "系统监控数据收集", businessType = BusinessType.OTHER)
    @PostMapping("/collect")
    public AjaxResult collectMetrics()
    {
        SystemMonitor metrics = systemMonitorService.collectSystemMetrics();
        systemMonitorService.insertSystemMonitor(metrics);
        return success(metrics);
    }

    /**
     * 查询最新的系统监控数据
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/latest")
    public AjaxResult getLatestMetrics(@RequestParam(defaultValue = "10") int limit)
    {
        List<SystemMonitor> latestMetrics = systemMonitorService.selectLatestSystemMonitor(limit);
        return success(latestMetrics);
    }

    /**
     * 查询系统监控统计数据
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics(@RequestParam Map<String, Object> params)
    {
        List<Map<String, Object>> statistics = systemMonitorService.selectSystemMonitorStatistics(params);
        return success(statistics);
    }

    /**
     * 查询指定时间范围内的监控数据
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/timerange")
    public AjaxResult getByTimeRange(@RequestParam Map<String, Object> params)
    {
        List<SystemMonitor> monitors = systemMonitorService.selectSystemMonitorByTimeRange(params);
        return success(monitors);
    }

    /**
     * 查询系统资源使用率趋势
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/trend")
    public AjaxResult getResourceTrend(@RequestParam Map<String, Object> params)
    {
        List<Map<String, Object>> trends = systemMonitorService.selectResourceUsageTrend(params);
        return success(trends);
    }

    /**
     * 检查系统资源告警
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/alerts")
    public AjaxResult checkAlerts()
    {
        List<Map<String, Object>> alerts = systemMonitorService.checkSystemAlerts();
        return success(alerts);
    }

    /**
     * 生成系统监控报告
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:query')")
    @GetMapping("/report")
    public AjaxResult generateReport(@RequestParam Map<String, Object> params)
    {
        Map<String, Object> report = systemMonitorService.generateMonitorReport(params);
        return success(report);
    }

    /**
     * 启动系统监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:control')")
    @Log(title = "启动系统监控", businessType = BusinessType.OTHER)
    @PostMapping("/start")
    public AjaxResult startMonitoring()
    {
        systemMonitorService.startSystemMonitoring();
        return success("系统监控已启动");
    }

    /**
     * 停止系统监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:control')")
    @Log(title = "停止系统监控", businessType = BusinessType.OTHER)
    @PostMapping("/stop")
    public AjaxResult stopMonitoring()
    {
        systemMonitorService.stopSystemMonitoring();
        return success("系统监控已停止");
    }

    /**
     * 清理过期的监控数据
     */
    @PreAuthorize("@ss.hasPermi('monitor:system:remove')")
    @Log(title = "清理监控数据", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean/{retentionDays}")
    public AjaxResult cleanExpiredData(@PathVariable("retentionDays") int retentionDays)
    {
        int deletedCount = systemMonitorService.cleanExpiredMonitorData(retentionDays);
        return success("成功清理 " + deletedCount + " 条过期监控数据");
    }
}