package com.ruoyi.web.controller.monitor;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Server;
import com.ruoyi.jarvis.service.ILogisticsService;
import com.ruoyi.jarvis.service.IWxSendService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务器监控
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/server")
public class ServerController
{
    @Resource
    private ILogisticsService logisticsService;
    
    @Resource
    private IWxSendService wxSendService;
    
    @PreAuthorize("@ss.hasPermi('monitor:server:list')")
    @GetMapping()
    public AjaxResult getInfo() throws Exception
    {
        Server server = new Server();
        server.copyTo();
        return AjaxResult.success(server);
    }
    
    /**
     * 获取服务健康度检测
     */
    @PreAuthorize("@ss.hasPermi('monitor:server:list')")
    @GetMapping("/health")
    public AjaxResult getHealth() throws Exception
    {
        Map<String, Object> healthMap = new HashMap<>();
        
        // 物流服务健康检测
        try {
            ILogisticsService.HealthCheckResult logisticsHealth = logisticsService.checkHealth();
            Map<String, Object> logisticsMap = new HashMap<>();
            logisticsMap.put("healthy", logisticsHealth.isHealthy());
            logisticsMap.put("status", logisticsHealth.getStatus());
            logisticsMap.put("message", logisticsHealth.getMessage());
            logisticsMap.put("serviceUrl", logisticsHealth.getServiceUrl());
            healthMap.put("logistics", logisticsMap);
        } catch (Exception e) {
            Map<String, Object> logisticsMap = new HashMap<>();
            logisticsMap.put("healthy", false);
            logisticsMap.put("status", "异常");
            logisticsMap.put("message", "健康检测异常: " + e.getMessage());
            logisticsMap.put("serviceUrl", "");
            healthMap.put("logistics", logisticsMap);
        }
        
        // 微信推送服务健康检测
        try {
            IWxSendService.HealthCheckResult wxSendHealth = wxSendService.checkHealth();
            Map<String, Object> wxSendMap = new HashMap<>();
            wxSendMap.put("healthy", wxSendHealth.isHealthy());
            wxSendMap.put("status", wxSendHealth.getStatus());
            wxSendMap.put("message", wxSendHealth.getMessage());
            wxSendMap.put("serviceUrl", wxSendHealth.getServiceUrl());
            healthMap.put("wxSend", wxSendMap);
        } catch (Exception e) {
            Map<String, Object> wxSendMap = new HashMap<>();
            wxSendMap.put("healthy", false);
            wxSendMap.put("status", "异常");
            wxSendMap.put("message", "健康检测异常: " + e.getMessage());
            wxSendMap.put("serviceUrl", "");
            healthMap.put("wxSend", wxSendMap);
        }
        
        return AjaxResult.success(healthMap);
    }
}
