package com.huimv.admin.common.utils;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
public class SendSMS {


    static String httpUrl = "http://121.41.41.182:8001/sms";
    static String userNmae = "6085";
    static String passWord = "huimu@6085";

    //短信内容不能出现空格，换行，百分号等特殊字符.相同两条短信之间内容不能相同
    public static void main(String[] args) {
        /**
         *发送给报警器
         *//*
		String content = "003C0043004C0045004100520030003000310039003E";
		String mobile = "18767100452";//多个用英文逗号隔开 如18767100452,1388888888
		content = UnicodeTool.decodeUnicode(UnicodeTool.addUnicodeU(content.toString()));*/

        /**
         * 发送给用户
         */
        ArrayList<String> phoneList = new ArrayList<>();
        phoneList.add("15869188386");
        String content ="测试结果";
        sendSMS(phoneList,content);

    }

    public static void sendSMS( ArrayList<String> mobiles, String content) {
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        // 设置请求参数
        Map params = new HashMap();
//        ArrayList<String> phoneList = new ArrayList<>();
//        phoneList.add("13140429955");
//        String content ="【慧牧科技】测试结果";
        content =   "【慧牧科技】"+content;
        long timestamp = System.currentTimeMillis();
        params.put("userName", userNmae);
        params.put("content", content);
        params.put("phoneList", mobiles);
        params.put("timestamp", timestamp);
        MD5 md5 = SecureUtil.md5();
        String md5PassWord = md5.digestHex(passWord);
        String sign = md5.digestHex(userNmae+content+timestamp+md5PassWord);

        params.put("sign", sign);

        System.out.println(params.toString());
        // 创建HttpEntity对象，封装请求头和请求参数
        HttpEntity requestEntity = new HttpEntity<>(params, headers);
        // 发送POST请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(httpUrl+"/api/sendMessage", requestEntity, String.class);

        // 获取响应结果
        String responseBody = responseEntity.getBody();
        System.out.println(responseBody);

    }
//    public static void sendSMS(String[] mobiles, String content) {
//        for (String mobile : mobiles) {
//            String httpArg = "accesskey=5493&secretkey=7e6f0b6e9d12d2c750cbf1932a4b9cc53e36509b&mobile=" + mobile + "&content=" + content + "通知【慧牧科技】";
//            System.out.println(httpArg);
//            String jsonResult = request(httpUrl, httpArg);
//            System.out.println(jsonResult);//返回码
//        }
//    }


    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

