package com.jisco.ymtappoint;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

public class demo {
    public static void main(String[] args) {
//        getToken();

        test02();

    }
    public static String api2RebackStp2tgMES(){
        RestTemplate client = new RestTemplate();
//        String url="http://10.1.213.242:8080/jiscoqs-mes-dev/api/mes/MesIfYmt99Kw"; //质量一贯制测试机
        String url="http://10.1.213.213:8765/oauth/token?client_id=c1&client_secret=secret&grant_type=password&username=jk-jl&password=Jl123456";//质量一贯制生产机
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
       // headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
//        headers.add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJJZGVTdHVkaW8iLCJ1c2VyVHlwZSI6IjEiLCJhY2NvdW50IjoiMTYyNzkwMjk1NTU4MiJ9.4CnpLlzVPddVi8xXpjXKjzqQSTAwVtZFpszVO8hCE_s");//质量一贯制测试机
       // headers.add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJJZGVTdHVkaW8iLCJ1c2VyVHlwZSI6IjEiLCJhY2NvdW50IjoiMTYzNTczMjAzNTc1MyJ9.qRBHRfEaHnKKZ3_7dwn5ImMg12en54bfk3vAdLqfSaQ");//质量一贯制生产机

//        HttpEntity<String> request = new HttpEntity<>(list.toString(), headers);

//        ResponseEntity<String> entity = client.postForEntity(url, request, String.class);
        ResponseEntity<String> entity = client.postForEntity(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
//        if(jsonObject.get("code").toString().equals("200")){
//            return true;
//        }
        return jsonObject.get("access_token").toString();
//        else{
////            throw new CustomException("置入货位的结果回写碳钢MES失败！"+jsonObject.get("msg").toString());
//            throw new NullPointerException("获取异常！");
//        }
    }

    public static void test(){
        RestTemplate client = new RestTemplate();
        String ymtToken = "Bearer "+api2RebackStp2tgMES();
//        @Value("${lcdate}")
        String lcdate;
//        String url = "http://10.1.213.157:8778/appoint/getAppointToJL?lcdate=甘B-10505"; //质量一贯制测试机
        String url = "http://10.1.213.157:8778/appoint/getAppointToJL?lcdate=蒙M-19449"; //质量一贯制测试机
//        String lc = "甘B-10505";
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
//        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Authorization", ymtToken);//质量一贯制测试机

//        Map<String, Object> params = new HashMap<>();
//        List<JSONObject> list = new ArrayList<>();

//        params.put("lcdate", "甘B-10505");

//        params.put("name", "libai");
        // headers.add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJJZGVTdHVkaW8iLCJ1c2VyVHlwZSI6IjEiLCJhY2NvdW50IjoiMTYzNTczMjAzNTc1MyJ9.qRBHRfEaHnKKZ3_7dwn5ImMg12en54bfk3vAdLqfSaQ");//质量一贯制生产机
//         List<JSONObject> list = new List<JSONObject>;
        List<JSONObject> list = new ArrayList<>();
//        HttpEntity<String> request = new HttpEntity<>(list.toString(), headers);
        HttpEntity<String> request = new HttpEntity<>(list.toString(), headers);

//        ResponseEntity<String> entity = client.postForEntity(url, request, String.class);
        ResponseEntity<String> entity = client.postForEntity(url, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
        //system.out.println(jsonObject);
        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
//        //system.out.println(jsonObject.get("access_token"));
//        return jsonObject.get("access_token").toString();
//        if(jsonObject.get("code").toString().equals("200")){
//            return true;
//        }
//        else{
////            throw new CustomException("置入货位的结果回写碳钢MES失败！"+jsonObject.get("msg").toString());
//            throw new NullPointerException("获取异常！");
//        }
    }


   public static void test02(){
        RestTemplate client = new RestTemplate();
       String ymtToken = "Bearer "+api2RebackStp2tgMES();
//        String url="http://10.1.213.242:8080/jiscoqs-mes-dev/api/mes/MesIfYmt99Kw"; //质量一贯制测试机
//        String url="http://10.99.200.50:11480/jiscoqs-mes-dev/api/mes/MesIfYmt99Kw";//质量一贯制生产机
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
       String url = "http://10.1.213.157:8778/appoint/getAppointToJL?lcdate=蒙M-19449";
       MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Authorization", ymtToken);//质量一贯制生产机

        HttpEntity<String> request = new HttpEntity<>(headers);

        ResponseEntity<String> entity = client.postForEntity(url, request, String.class);
//        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
       JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
//       //system.out.println(jsonObject.get("data").toString());
//       return jsonObject.get("access_token").toString();
//        if(jsonObject.get("code").toString().equals("200")){
//            //system.out.println("ssssssss");
//        }

    }
}

