package com.zcxk.rmcp.web.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcxk.rmcp.api.vo.XxlJobInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author heyumin
 * @date 2021-07-30 9:44
 */
@Slf4j
@Component
public class XxlJobUtil {
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.group.id}")
    private Integer groupId;

    private RestTemplate restTemplate = new RestTemplate();


    private static final String UPDATE_URL = "/jobinfo/updateJob";
    private static final String REMOVE_URL = "/jobinfo/removeJob";
    private static final String REMOVE_ALL_URL = "/jobinfo/removeAllJob";
    private static final String PAUSE_URL = "/jobinfo/pauseJob";
    private static final String START_URL = "/jobinfo/startJob";
    private static final String ADD_START_URL = "/jobinfo/addAndStart";
    //private static final String GET_GROUP_ID = "/jobgroup/getGroupId";
    //private static final String ADD_URL = "/jobinfo/addJob";

    public String add(XxlJobInfo jobInfo){
        // 查询对应groupId:
        jobInfo.setJobGroup(groupId);
        String json2 = JSON.toJSONString(jobInfo);
        return doPost(adminAddresses + ADD_START_URL, json2);
    }

    public String update( XxlJobInfo jobInfo){
       jobInfo.setJobGroup(groupId);
       String json = JSON.toJSONString(jobInfo);

        return doPost(adminAddresses + UPDATE_URL, json);
    }

    public String remove(int id){
        Map<String,Object> param = new HashMap<>(8);
        param.put("id", id);
        String json = JSON.toJSONString(param);
        return doPost(adminAddresses + REMOVE_URL, json);
    }
    public String removeAll(List<Integer> ids){

        String json = JSON.toJSONString(ids);
        return doPost(adminAddresses + REMOVE_ALL_URL, json);
    }
    public String pause(int id){
        Map<String,Object> param = new HashMap<>(8);
        param.put("id", id);
        String json = JSON.toJSONString(param);
        return doPost(adminAddresses + PAUSE_URL, json);
    }

    public String start(int id){
        Map<String,Object> param = new HashMap<>(8);
        param.put("id", id);
        String json = JSON.toJSONString(param);
        return doPost(adminAddresses + START_URL, json);
    }



    public String doPost(String url, String json){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(json ,headers);
        log.info(entity.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        return stringResponseEntity.getBody();

    }


}
