package com.cyksj.xxljob;

/**
 * @ClassName XxlJobUtil
 * @Description TODO
 * @Author Huang yuanzhi
 * @Date 2023/6/12 16:11
 * @Version 1.0.0
 **/

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.XxlJobResult;

import java.io.IOException;
import java.net.HttpCookie;
import java.util.List;

/**
 * @Author: JCccc
 * @Date: 2022-6-22 9:51
 * @Description:
 */
public class XxlJobUtil {

	private final static String BASE_URL = "http://app7.yicanggongyi.com:9099/xxl-job-admin";

	private static String cookie = "";


//    /**
//     * 查询现有的任务（可以关注这个整个调用链，可以自己模仿着写其他的拓展接口）
//     * @return
//     * @throws HttpException
//     * @throws IOException
//     */
//    public static JSONObject pageList(Map<String,Object> param) throws Exception {
//        String path = "/jobinfo/pageList" + "?jobGroup=5&triggerStatus=-1";
//        String targetUrl = BASE_URL + path;
//        System.out.println(cookie);
//        HttpResponse execute = HttpUtil.createPost(targetUrl)
//                .header("Content-Type","application/json")
//                .header("Cookie",getCookie())
//                .execute();
//        return Jsons.parseObject(execute.body(),JSONObject.class);
//    }


	/**
	 * 新增任务
	 *
	 * @param jobParam
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public static XxlJobResult addJob(String jobParam) throws Exception {
		String path = "/jobinfo/add";
		String targetUrl = BASE_URL + path + jobParam;

		HttpResponse execute = HttpUtil.createPost(targetUrl)
				.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				.header("Cookie", getCookie())
				.execute();
		return Jsons.parseObject(execute.body(), XxlJobResult.class);
	}

	public static void main(String[] args) throws Exception {
		// updateJob(XxlJobInfo.getUpdateInstance(58,XxlJobInfo.AUTHOR.HUANG_YUAN_ZHI,"修改job","0/4 * * * * ?",5,"LikeJob","").toString());
		JSONObject jsonObject = deleteJob("67");
		System.out.println(jsonObject);
	}

	/**
	 * 编辑任务
	 *
	 * @param query
	 * @return
	 * @throws Exception
	 */
	public static JSONObject updateJob(String query) throws Exception {
		String path = "/jobinfo/update";
		String targetUrl = BASE_URL + path + query;

		HttpResponse execute = HttpUtil.createPost(targetUrl)
				.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				.header("Cookie", getCookie())
				.execute();
		return Jsons.parseObject(execute.body(), JSONObject.class);
	}

	/**
	 * 删除任务
	 *
	 * @param id
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public static JSONObject deleteJob(String id) throws Exception {
		String path = "/jobinfo/remove?id=" + id;
		String url = BASE_URL + path;
		HttpResponse execute = HttpUtil.createGet(url).header("Cookie", getCookie()).execute();
		return Jsons.parseObject(execute.body(), JSONObject.class);
	}

	/**
	 * 立即执行某一定时任务
	 * @param jobParam
	 */
	public static XxlJobResult executeMarkDynamicRuleTagJob(String jobParam) throws Exception {
		String path = "/jobinfo/trigger";
		String targetUrl = BASE_URL + path + jobParam;

		HttpResponse execute = HttpUtil.createPost(targetUrl)
				.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				.header("Cookie", getCookie())
				.execute();
		return Jsons.parseObject(execute.body(), XxlJobResult.class);
	}

	public static XxlJobResult stopJob(String jobId) throws Exception {
		String path = "/jobinfo/stop?id=" + jobId;
		String targetUrl = BASE_URL + path;

		HttpResponse execute = HttpUtil.createPost(targetUrl)
				.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				.header("Cookie", getCookie())
				.execute();
		return Jsons.parseObject(execute.body(), XxlJobResult.class);
	}


	/**
	 * 获取登录cookie
	 *
	 * @return
	 */
	public static String getCookie() {
		String path = "/login";
		String targetUrl = BASE_URL + path;
		HttpResponse execute = HttpUtil.createPost(targetUrl)
				.form("userName", "admin")
				.form("password", "123456")
				.execute();
		if (execute.getStatus() == 200) {
			List<HttpCookie> cookies = execute.getCookies();
			StringBuffer tmpcookies = new StringBuffer();
			for (HttpCookie c : cookies) {
				tmpcookies.append(c.getName()).append("=").append(c.getValue() + ";");
			}
			cookie = tmpcookies.toString();
		} else {
			try {
				cookie = "";
			} catch (Exception e) {
				cookie = "";
			}
		}
		return cookie;
	}

}