package com.liang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.common.utils.*;
import com.liang.entity.serverinfo.ServerInfo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @date 2023-05-21-14:32
 * @description 服务器监控
 **/
@Api(tags = "服务器监控")
@Controller
@RequestMapping("/serverinfo")
public class ServerInfoController {
    final static Logger logger = LoggerFactory.getLogger(ServerInfoController.class);

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 页面初始化
     *
     * @return
     */
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "serverinfo/index";
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult list() {
        String mac = MacUtils.getMac();
        Object object = redisUtil.get(SystemConst.SYS_SERVER_INFO + ":" + mac);
        if (null != object) {
            logger.info("SysServerInfoController - list - 从redis获取缓存成功");
            return BaseResult.success(JsonTool.parseMap(object.toString()));
        }
        ServerInfo serverInfo = new ServerInfo();
        try {
            serverInfo.copyTo();
            // 放在redis里面，120秒过期时间
            Map<String, Object> infoMap = new HashMap<>();
            infoMap.put("data", serverInfo);
            infoMap.put("updateTime", DateTool.getCurrentTime("yyyy-MM-dd HH:mm:ss"));
            redisUtil.set(SystemConst.SYS_SERVER_INFO + ":" + mac, JsonTool.toJsonString(infoMap), 120);
            return BaseResult.success(infoMap);
        } catch (Exception e) {
            logger.error("SysServerInfoController - list - Exception = ", e);
        }
        return BaseResult.failure("获取服务器信息失败！");
    }

}
