package com.cim.idm.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Description TODO
 * @Author li
 * @Date 2024/4/25 11:59
 */
@Component
public class WCSHttpUtil {
    private final static Log logger = LogFactory.getLog(WCSHttpUtil.class);

    public static String httpPost(String requestUrl, JSONObject parm) {
        StringBuffer buffer = null;
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            // conn.setRequestProperty("access_token", access_token);
            conn.connect();
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // body参数在这里put到JSONObject中
            // JSONObject parm = new JSONObject();
            writer.write(parm.toString());
            writer.flush();
            // 读取服务器端返回的内容
            java.io.InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

    // 获取apptoken--关务使用
//    public static String httpPostapptoken(String requestUrl, String requestMethod) {
//        logger.info("************************开始****************************" );
//        StringBuffer buffer = null;
//        String result = "";
//        String app_token = "";
//        try {
//            URL url = new URL(requestUrl);
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            conn.setRequestMethod(requestMethod);
//            conn.setDoOutput(true);
//            conn.setDoInput(true);
//            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//            conn.connect();
//            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
//            // body参数在这里put到JSONObject中
//            JSONObject parm = new JSONObject();
//            parm.put("usercode", "WMS");
//            parm.put("userpwd", "e10adc3949ba59abbe56e057f20f883e");
//            writer.write(parm.toString());
//            writer.flush();
//            // 读取服务器端返回的内容
//            java.io.InputStream is = conn.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is, "utf-8");
//            BufferedReader br = new BufferedReader(isr);
//
//            buffer = new StringBuffer();
//            String line = null;
//            while ((line = br.readLine()) != null) {
//                result += line;
//                buffer.append(line);
//            }
//            org.json.JSONObject buffers = new org.json.JSONObject(result);
//            Object token = ((org.json.JSONObject) buffers.get("data")).get("token");
//            app_token = (String) token;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        logger.info("************************结束****************************" + app_token);
//        return app_token;
//    }

    // 设置token---关务使用
    public static String httpPost(String requestUrl, JSONObject parm, String token) {
        StringBuffer buffer = null;
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("X-Auth-Token", token);
            conn.connect();
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            writer.write(parm.toString());
            writer.flush();
            // 读取服务器端返回的内容
            java.io.InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
}