package com.bbd.sms;

import com.bbd.entity.AccessInfo;
import com.bbd.utils.HttpClientUtils;
import com.bbd.utils.PropertiesUtil;
import com.bbd.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;


/**
 * 发送短信
 *
 * @author zangtao
 * @create 2019 - 04 -11 15:35
 */
public class Sms {

    private static final Logger logger = LoggerFactory.getLogger(Sms.class);

    public static String sendSms(AccessInfo accessInfo,String mobile,String msg){
        //读取配置文件
        Properties properties = PropertiesUtil.getProperties();
        String smsUrl = properties.getProperty("smsUrl");
        String result;

        HttpClient httpClient = HttpClients.createDefault();

        try {

            HttpPost httpPost = new HttpPost(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
                   + "&mobile=" + mobile + "&msg=" + msg);
            HttpResponse response = httpClient.execute(httpPost);
            logger.info(response.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }

        //发送get请求
//        if (StringUtils.isEmpty(accessInfo.getAisign())){
//            // + "&Ai.ct=" + accessInfo.getAict()
//            result = HttpClientUtils.doGet(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
//                    + "&mobile=" + mobile + "&msg=" + msg );
//        }else{
//            result = HttpClientUtils.doGet(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
//                    + "&Ai.ct=" + accessInfo.getAict() + "&ai.sign=" + accessInfo.getAisign() + "&mobile=" + mobile + "&msg=" + msg );
//        }
        return null;
    }

    public static void main(String[] args) {
        AccessInfo accessInfo = new AccessInfo();
        accessInfo.setAicc("5");
        accessInfo.setAicp("123.56.146.7");
//        accessInfo.setAict("21");
        String res = Sms.sendSms(accessInfo,"13222650486","HelloWorld");
        System.out.println(res);
    }
}
