package com.ruoyi.send;

import java.nio.charset.Charset;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;


public class HttpUtil {
	private static String SEND_URL = "https://bizdata.tianxiapai.com/api/sendmessage";
	private static String RECEIVE_URL = "https://bizdata.tianxiapai.com/api/receivemessage";
	private static String CONFIRM_URL = "https://bizdata.tianxiapai.com/api/confirm";
	private static String YW = "ywsys";
	private static String YWS = "003f9eb399f522a3db54bca3287b3c6505dcc1e99a93c523b5220f447a250d0c";
	private static String CW = "cwsys";
	private static String CWS = "c22bc79d9d7e856b095414f2864fa0e760af2a7d9d7dfee8f215feff962df49c";

	public static String post(String url, String postBody) throws Exception {
		System.out.println("========body: " + postBody);
		System.out.println("========url: " + url);
		String result;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		// 设置超时时间
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(2000)
				// 报案接口超时时间建议设置为40秒
				.setSocketTimeout(40000).build();
		httpPost.setConfig(requestConfig);
		// 构造消息头，这个参数很重要，如果去掉，则返回的数据不能被json工具解析
		httpPost.setHeader("Accept", "application/json;charset=utf-8");

		// 构建消息实体
		StringEntity entity = new StringEntity(postBody, Charset.forName("UTF-8"));
		entity.setContentEncoding("UTF-8");
		// 发送Json格式的数据请求
		entity.setContentType("application/json");
		httpPost.setEntity(entity);

		CloseableHttpResponse response = httpClient.execute(httpPost);

		result = EntityUtils.toString(response.getEntity(), "utf-8");

		System.out.println("========result: " + result);
		System.out.println("========statusLine: " + response.getStatusLine());
		response.close();
		httpClient.close();
		return result;
	}

	public static void main(String[] args) throws Exception {
		System.out.println("开票申请开始！！！！");
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> message = new HashMap<String, Object>();
		data.put("Message", message);
		message.put("Envelope", getEnvelope());
		message.put("Doc", getDoc());
		Map<String, Object> postBody = getBody(YW, YWS, data);
		post(SEND_URL, JSONObject.toJSONString(postBody));

		System.out.println("接收消息开始！！！！");
		data = new HashMap<String, Object>();
		data.put("MaxSize", 10);
		postBody = getBody(CW, CWS, data);
		String r = post(RECEIVE_URL, JSONObject.toJSONString(postBody));

		System.out.println("确认消息开始！！！！");
		JSONObject jo = JSONObject.parseObject(r);
		List<Map<String, Object>> dlist = new ArrayList<Map<String, Object>>();
		for (Object o : jo.getJSONArray("Data")) {
			data = new HashMap<String, Object>();
			dlist.add(data);
			data.put("MessageID", ((JSONObject) o).getString("MessageID"));
		}
		postBody = getBody(CW, CWS, dlist);
		post(CONFIRM_URL, JSONObject.toJSONString(postBody));

	}

	private static Map<String, Object> getBody(String AccountCode, String AccessSecret, Object data) {
		Map<String, Object> postBody = new HashMap<String, Object>();
		postBody.put("Version", "1.0");
		postBody.put("AccountCode", AccountCode);
		postBody.put("AccessSecret", AccessSecret);
		postBody.put("Data", data);
		return postBody;
	}

	private static Map<String, Object> getEnvelope() {
		Map<String, Object> envelope = new HashMap<String, Object>();
		envelope.put("SenderNodeCode", "YW");
		envelope.put("SenderAccount", "ywsys");
		envelope.put("ReceiverNodeCodes", "CW");
		envelope.put("SendTime", Instant.now().toString());
		envelope.put("MessageVersion", "1.0");
		envelope.put("Signature", "Signature");
		return envelope;
	}

	private static Map<String, Object> getDoc() {

		Map<String, Object> doc = new HashMap<String, Object>();
		Map<String, Object> header = new HashMap<String, Object>();
		Map<String, Object> content = new HashMap<String, Object>();
		doc.put("Header", header);
		doc.put("Content", content);
		header.put("DocId", "test" + new Date());
		header.put("DocNo", "test" + new Date());
		header.put("DocTitle", "开票申请TEST");
		header.put("SenderDept", "业务部");
		header.put("Sender", "张三");
		header.put("DestDepts", "财务部");
		header.put("DestReceivers", "李四");
		header.put("DocTypeId", "b51dbf12-9443-11ec-a0ab-005056b4b3b1");
		header.put("DocTypeName", "开票申请");
		header.put("Creator", "张三");
		header.put("CreateTime", Instant.now().toString());
		header.put("RelDocId", "");
		header.put("RelType", "");
		header.put("SLA", "5天内完成");
		content.put("Data", "Data1");
		content.put("Otherdata", "Otherdata2");
		content.put("Remark", "");
		content.put("Attachments", null);
		return doc;
	}

}
