package com.yc.ship.module.trade.service.order.handler;

import cn.hutool.json.JSONUtil;
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.http.HttpClientConfig;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


/**
 * 三峡短信模块
 * @author lishiqiang
 */
@Slf4j
public class MoblieMessageAliyunUtil {

    /**
     * 产品名称:云通信短信API产品,开发者无需替换
     */

    private static final String product = "Dysmsapi";

    /**
     * 产品域名,开发者无需替换
     */

    private static final String domain = "dysmsapi.aliyuncs.com";


    /**
     * 阿里云短信发送
     *
     * @param mobile
     * @param templateParam "{\"code\":\"" + 123456 + "\"}
     * @param templateCode
     * @return
     * @throws ClientException
     */
    public static SendSmsResponse sendSms(String accessKeyId,String accessKeySecret,String signName,String mobile, String templateParam, String templateCode) {
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        // 初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        SendSmsResponse sendSmsResponse;
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            HttpClientConfig httpClientConfig = profile.getHttpClientConfig();
            httpClientConfig.setProtocolType(ProtocolType.HTTPS);
            profile.setHttpClientConfig(httpClientConfig);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            // 组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            // 必填:待发送手机号
            request.setPhoneNumbers(mobile);
            // 必填:短信签名-可在短信控制台中找到
            request.setSignName(signName);
            request.setProtocol(ProtocolType.HTTPS);
            // 必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(templateCode);
            // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            System.out.println(templateParam);
            request.setTemplateParam(templateParam);
            // 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
            // request.setSmsUpExtendCode("90997");
            // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
            request.setOutId("yourOutId");
            // hint 此处可能会抛出异常，注意catch
            sendSmsResponse = acsClient.getAcsResponse(request);
            log.error("短信接口返回的数据-------request="+JSONUtil.parseObj(request)+";--------sendSmsResponse=" +JSONUtil.parseObj(sendSmsResponse));
            if (StringUtils.equals("OK", sendSmsResponse.getCode())) {
                try {
//                    querySendDetails(sendSmsResponse.getBizId(), mobile, templateParam);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(mobile+"10短信接口返回的数据----------------getMessage=" + e.getMessage());
                    return null;
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
        return sendSmsResponse;
    }

    public static void main(String[] args) throws Exception{
        //您预定的${useDate}“长江游轮”即将开启.请您于${startTime}起前往 ${startAddr}登船,身份证即为船票,出行时请携带所有出行人的身份证原件,${endTime}将停止检票.请勿携带宠物、管制刀具等违禁物品登船,祝您旅途愉快!
        String ss = "{\"boatName\":\"太阳神号游轮\",\"routeName\":\"航线\",\"startTime\":\"2026年05月01日\",\"jzTime\":\"2026年04月26日\"}";
//        String ss = "{\"boatName\":\"1\",\"orderNo\":\"2\",\"routeName\":\"3\",\"startTime\":\"2026\",\"jzTime\":\"2026\"}";
//        String ss = "{\"boatName\":\"1\",\"routeName\":\"3\"}";
//        SendSmsResponse s = sendSms("LTAI5t9eyWeREnjqs3ozEP22","hdl5UOYVFGCXz9ZNwy8w1mEt8z8Lmz","长江行游轮","18628286025", ss, "SMS_501695562");
//        SendSmsResponse s = sendSms("LTAI5t68WFsQmRcgLUcpydhb","EzZp3TQa7Ah96htShNqb7m44PEGk8v","长江行游轮","18628286025", ss, "SMS_501695562");
        SendSmsResponse s = sendSms("LTAI5t68WFsQmRcgLUcpydhb","EzZp3TQa7Ah96htShNqb7m44PEGk8v","长江行游轮","18628286025", ss, "SMS_501700557");
        System.out.println(s.getMessage());

//        String input = "这是一个【date】示例【startTime】字符【content】串";
//        Pattern pattern = Pattern.compile("\\【(.*?)\\】");
//        Matcher matcher = pattern.matcher(input);
//        List<String> list = new ArrayList<>();
//        int count = 0;
//        while  (matcher.find()) {
//            System.out.println("Match number " + count);
//            String result = input.substring(matcher.start()+1, matcher.end()-1); // 获取第一个匹配的括号内的内容
//            list.add(result);
//            count++;
//        }
//        System.out.println(list);

    }


}