package com.ruoyi.hywz.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

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

public class IappMsgUtil {

    private static final String url = "http://25.212.128.19:30003/api/sms/send";
    private static final String spCode = "100001";
    private static final String loginName = "zhout20";
    private static final String password = "313393dca0234fa75bf0903709eafaf0";
    private static IappMsgUtil iappMsgUtil = null;

    public static IappMsgUtil getInstance(){
        if(iappMsgUtil==null){
            iappMsgUtil = new IappMsgUtil();
        }
        return iappMsgUtil;
    }

    public void sendApp(String userNumber,String messageContent){
        System.out.println(">>>>>>>>>>>>>send APP>>>>>>>>>>>.");
//        messageContent = "您好！"+messageContent+"。如已处理，请忽略。";
        String contentType = "application/json;charset=UTF-8";
        String requestId = System.currentTimeMillis()+"123456";
        Map map4Sign = new HashMap();
        int scenid = 100;
        //公共部分
        map4Sign.put("spCode", spCode);
        map4Sign.put("loginName", loginName);
        map4Sign.put("apiKey", password);
        map4Sign.put("requestId", requestId);
        Map<String,Object> map4RequestBody = new HashMap<>();
        map4Sign.put("appUserId", userNumber);
        map4Sign.put("sceneId", scenid);
        map4Sign.put("type","3");
        map4Sign.put("messageContent",messageContent);

        map4RequestBody.put("appUserId", userNumber);
        map4RequestBody.put("sceneId", scenid);
        map4RequestBody.put("type","3");
        map4RequestBody.put("messageContent",messageContent);

        String sign = EncryptUtil.getSign(map4Sign);
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("Content-Type", contentType);
        postMethod.setRequestHeader("x-sp-code", spCode);
        postMethod.setRequestHeader("x-app-key", loginName);
        postMethod.setRequestHeader("x-request-id", requestId);
        postMethod.setRequestHeader("x-sign", sign);
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
        String responseStr = null;
        long start = System.currentTimeMillis();
        String requestBody = JSON.toJSONString(map4RequestBody);
        try {
            RequestEntity se = new StringRequestEntity(requestBody, contentType, "UTF-8");
            postMethod.setRequestEntity(se);

            MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
            HttpConnectionManagerParams params = manager.getParams();
            params.setDefaultMaxConnectionsPerHost(100);
            params.setMaxTotalConnections(100);
            params.setConnectionTimeout(3000);
            params.setSoTimeout(3000);
            int httpStatus = new HttpClient(manager).executeMethod(postMethod);
            if (httpStatus != 200) {
                throw new HttpException("请求异常：url:" + url + " ,requestBody:" + requestBody + " ,status:" + httpStatus);
            }
            responseStr = postMethod.getResponseBodyAsString();
            System.out.println(responseStr);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();
        }
    }
}
