package com.jkcredit.invoice.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: sunzhaoning
 * @create: 2019-06-03 15:22
 * @version: V1.0
 **/
@Component
@Slf4j
public class HttpUtil {

    @Autowired
    private RestTemplate restTemplate;

//    private static final String URL = "http://api.jkcredit.com:58080/gateway?api=credit.sec.data";
    private static final String URL = "http://192.168.1.28:58080/gateway?api=credit.sec.data";
    private static final String APPKEY= "JIAO_KE";
    private static final String SECRETKEY= "D78C393359BF128715C65D91C67051478A4DFC13";

    public String getCheckResult(Map<String, Object> params, String api) {
        String result=null;
        try {

            Map<String, Object> body = new HashMap<>(7);
            body.put("appKey", APPKEY);
            body.put("appSecret", SECRETKEY);
            body.put("data", params);
            body.put("api", api);

            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);

            HttpEntity<String> formEntity = new HttpEntity<>(JSONObject.toJSONString(body), headers);

            JSONObject jsonObject = restTemplate.postForObject(URL, formEntity, JSONObject.class);

            if(jsonObject != null){
                result= jsonObject.toJSONString();
            }else {
                result = null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        Map<String, Object> arrayData = new HashMap<>(5);
        arrayData.put("plateNumber", "苏G1815E");
        arrayData.put("plateColor", Integer.parseInt("1"));
//        System.out.println(getCheckResult(arrayData,"VEHICLE_REGISTER"));
    }
}
