package com.ruoyi.quartz.task;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.Optional;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void validPost(String url, String name){
        HttpRequest get = HttpUtil.createPost(url);
        sendContent(name, get);
    }
    public void validGet(String url, String name){
        HttpRequest get = HttpUtil.createGet(url);
        sendContent(name, get);
    }

    private void sendContent(String name, HttpRequest get) {
        Boolean success = Optional.ofNullable(get.execute())
                .filter(x -> x.getStatus() == 200)
                .filter(x -> JSONUtil.isJson(x.body()))
                .map(x -> JSONObject.parseObject(x.body()))
                .map(x -> x.getString("code"))
                .map(x -> "200".equals(x))
                .orElse(false);
        if (!success){
            DingTalkUtil.sendMsg("服务【%s】崩了，请尽快查看 ！！ ",name);
            System.out.println(name + "服务崩了");
        }else {
            System.out.println("一切正常");
        }

    }

    // 测试服务器，qz_monitor     SELECT * FROM `sys_job`   任务

    public static void main(String[] args) {
        new RyTask().validGet("http://jiandingapi.5taogame.com/api/guest/sendTestMessage","快鉴定");
    }
}
