package com.usoft.credit.service;

import com.usoft.credit.config.PyConfig;
import com.usoft.credit.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询入口
 * created by shicr on 2018/6/6
 **/
@Service
public class ErpQueryService {

    

    @Autowired
    private PyConfig pyConfig;


    public String requestApi(String host, String path,String query) {

        // https双向认证使用,配合PySSLContextUtil中的DefaultSSLContext
//        System.setProperty("javax.net.debug", "all");
//        System.setProperty("javax.net.ssl.keyStore", PyConfig.KEYSTORE_FILE);
//        System.setProperty("javax.net.ssl.keyStorePassword", PyConfig.KEYSTORE_PASSWORD);
//        System.setProperty("javax.net.ssl.trustStore", PyConfig.TRUSTSTORE_FILE);
//        System.setProperty("javax.net.ssl.trustStorePassword", PyConfig.TRUSTSTORE_PASSWORD);

        Map<String, String> headers = new HashMap<String, String>();
        Map<String, String> querys = null;
        Map<String, String> bodys = new HashMap<String, String>();
        //默认请求条件是JSON格式.如果请求条件是xml，需要指定格式。
        //bodys.put("format","xml")
        bodys.put("userID", pyConfig.getUserId());
        bodys.put("password", pyConfig.getPassword());
        bodys.put("queryCondition", query);
        HttpResponse response = null;
        try {
            response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = null;
        try {
            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }



}