package com.wgb.system.service.component;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SmsUtil {

	//产品名称:云通信短信API产品,开发者无需替换
    private   static final String PRODUCT = "Dysmsapi";
    //产品域名,开发者无需替换
    private   static final String DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String SERVICE_NAME="乌龟邦维修大王";
    private static final String ACCESS_KEY_ID = "LTAImujR6wLxzXde";
    private static final String ACCESS_KEY_SECRET = "ymNpHA9vR5O3o9EYwDMW573dWsxGot";
    private static final String SERVICE_PHONE="(020)28939026";

    public static SendSmsResponse sendSmsCode(String phoneNumber,String code) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phoneNumber);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(SERVICE_NAME);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("SMS_115700019");
        //获取随机4位数
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{ \"code\":\""+code+"\"}");

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }

    public static SendSmsResponse sendSmsInform(String phoneNumber) throws ClientException {
    	//可自助调整超时时间
    	System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    	System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        IAcsClient acsClient=initAcsClient();
    	SendSmsRequest request = new SendSmsRequest();
    	request.setPhoneNumbers(phoneNumber);
    	request.setSignName(SERVICE_NAME);
    	request.setTemplateCode("SMS_116590217");
    	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	return sendSmsResponse;
    }
    /**
     *  邀请短信
     * @param phoneNumber
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSmsInvite(String phoneNumber) throws ClientException {
        IAcsClient acsClient=initAcsClient();
    	SendSmsRequest request = new SendSmsRequest();
    	request.setPhoneNumbers(phoneNumber);
    	request.setSignName("乌龟邦维修大王");
    	request.setTemplateCode("SMS_116590220");
    	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	
    	return sendSmsResponse;
    }
    /**
     *  入驻短信
     * @param phoneNumber
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSmsInIdentifying(String phoneNumber,String code) throws ClientException {
        IAcsClient acsClient=initAcsClient();
    	SendSmsRequest request = new SendSmsRequest();
    	request.setPhoneNumbers(phoneNumber);
    	request.setSignName("乌龟邦维修大王");
    	request.setTemplateCode("SMS_116561601");
    	request.setTemplateParam("{ \"code\":\""+code+"\"}");
    	
    	//hint 此处可能会抛出异常，注意catch
    	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	
    	return sendSmsResponse;
    }
    /**
     *  下单验证短信
     * @param phoneNumber
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSmsOrder(String phoneNumber,String code) throws ClientException {
        IAcsClient acsClient=initAcsClient();
    	SendSmsRequest request = new SendSmsRequest();
    	request.setPhoneNumbers(phoneNumber);
    	request.setSignName("乌龟邦维修大王");
    	request.setTemplateCode("SMS_117290903");
    	request.setTemplateParam("{ \"code\":\""+code+"\"}");
    	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	
    	return sendSmsResponse;
    }
    /**
     *  绑定验证短信
     * @param phoneNumber
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSmsBound(String phoneNumber,String code) throws ClientException {
        IAcsClient acsClient=initAcsClient();
    	SendSmsRequest request = new SendSmsRequest();
    	request.setPhoneNumbers(phoneNumber);
    	request.setSignName("乌龟邦维修大王");
    	request.setTemplateCode("SMS_117527430");
    	request.setTemplateParam("{ \"code\":\""+code+"\"}");
    	SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	
    	return sendSmsResponse;
    }



    public static  QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {
        IAcsClient acsClient=initAcsClient();
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        request.setPhoneNumber("15000000000");
        request.setBizId(bizId);
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        request.setPageSize(10L);
        request.setCurrentPage(1L);
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
        return querySendDetailsResponse;
    }

    /**
     *维修订单派单
     * @param userCellphone             接收短信手机号
     * @param name                      维修师傅名称
     * @param customerName              客户名称
     * @param businessUserCellphone     客户手机号
     * @param address                   维修地址
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse pushBusinessSMS(String userCellphone,String name,String customerName,String businessUserCellphone,String address) throws ClientException {
        IAcsClient acsClient=initAcsClient();
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(userCellphone);
        request.setSignName(SERVICE_NAME);
        request.setTemplateCode("SMS_163525700");
        //name-其他；customerName-其他；userCellphone-电话号码；address-其他；servicePhone-其他；
        request.setTemplateParam("{\"name\":\""+name+"\",\"customerName\":\""+customerName+"\",\"userCellphone\":\""+businessUserCellphone+"\",\"address\":\""+address+"\",\"servicePhone\":\""+SERVICE_PHONE+"\"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }


    public static IAcsClient initAcsClient() throws ClientException {
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        return new DefaultAcsClient(profile);
    }


    public static void main(String[] args)throws Exception {

        //发短信
        /*SendSmsResponse response = sendSmsInIdentifying("17673352133","1234");
        sendSmsOrder("17673352133","1234");
        sendSmsBound("17673352133","1234");
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());
        Thread.sleep(3000L);
*/
        //发维修订单派单
        //String userCellphone,String name,String customerName,String businessUserCellphone,String address
        SendSmsResponse sendSmsResponse = pushBusinessSMS("17673352133","张三","李四","17673352134","广东省广州市天河区XXX街道XXX号");

        //查明细
//        if(response.getCode() != null && response.getCode().equals("OK")) {
//            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
//            System.out.println("短信明细查询接口返回数据----------------");
//            System.out.println("Code=" + querySendDetailsResponse.getCode());
//            System.out.println("Message=" + querySendDetailsResponse.getMessage());
//            int i = 0;
//            for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
//            {
//                System.out.println("SmsSendDetailDTO["+i+"]:");
//                System.out.println("Content=" + smsSendDetailDTO.getContent());
//                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
//                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
//                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
//                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
//                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
//                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
//                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
//            }
//            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
//            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
//        }

    }
}
