package com.sifang.modules.partner.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class PartnerApiUtils {

    private static Logger logger = LoggerFactory.getLogger(PartnerApiUtils.class);

    public static JSONObject clientPostToApi(String jsonStr, String url){
        logger.info(">>>>>>clientPostDingHao  url:{},jsonStr:{}",url,jsonStr);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20 * 1000);
        requestFactory.setReadTimeout(60 * 1000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        ResponseEntity<JSONObject> accessTokenJsonOb = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class);
        logger.info("clientPostDingHao -----------:{}"+accessTokenJsonOb);
        if (null != accessTokenJsonOb) {
            Gson gson=new Gson();
            JSONObject jsonObject= accessTokenJsonOb.getBody();
            logger.info("clientPostDingHao  url:{},responses:{}",url,gson.toJson(jsonObject));
            return jsonObject;
        }
        return null;
    }

    public static JSONArray doPostForPartnerApi(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getJSONArray("data");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static JSONObject doPostForHRUploadFile(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject;
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static JSONArray doPostForHRreleOrder(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                Integer codeData = jsonObject.getInteger("code");
                String msg = jsonObject.getString("msg");
                if("".equals(msg)){
                    return null;
                }else {
                    if(codeData == 200){
                        return jsonObject.getJSONArray("data");
                    }else{
                        throw new IllegalArgumentException(jsonObject.getString("msg"));
                    }
                }
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static JSONArray doPostForHRreleOrderUpload(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getJSONObject("data").getJSONArray("rows");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static JSONObject getresultDoPost(Map<String,Object> map, String url) {
        JSONArray jsonArray = doPostForPartnerApi(map, url);
        if (jsonArray != null && !jsonArray.isEmpty()) {
            return jsonArray.getJSONObject(0);
        }
        throw new IllegalArgumentException("请求失败！");
    }
    public static JSONObject getresultDoPost2(Map<String, Object> map, String url) {
        JSONObject jsonObject = doPostForHRUploadFile(map, url);
        if (jsonObject != null && !jsonObject.isEmpty()) {
            return jsonObject;
        }
        throw new IllegalArgumentException("请求失败！");
    }

    public static JSONObject getGfresultDoPost(Map<String,Object> map, String url) {
        JSONObject jsonObject = doPostForGfApi(map, url);
        if (jsonObject != null && !jsonObject.isEmpty()) {
            return jsonObject;
        }
        throw new IllegalArgumentException("请求失败！");
    }

    public static JSONObject doPostForGfApi(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getJSONObject("data");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static String doPostForGfApiResulToString(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getString("data");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static String doPostForGfAcceptReformed(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        map.put("capital","03"); //资方广发，03
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getString("data");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

    public static String doPostForGfReformed(Map<String,Object> map, String url) {
        map.put("partner","nenghui"); //合作方，固定为nenghui
        map.put("capital","03"); //资方广发，03
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        JSONObject jsonObject= PartnerApiUtils.clientPostToApi(jsonStr,url);
        if(jsonObject!=null){
            Integer code=jsonObject.getInteger("code");
            if(code==200){
                return jsonObject.getString("code");
            }else{
                throw new IllegalArgumentException(jsonObject.getString("msg"));
            }
        }else{
            throw new IllegalArgumentException("请求失败！");
        }
    }

}
