package com.eitc.system.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.eitc.common.core.domain.entity.SysDictData;
import com.eitc.common.utils.spring.SpringUtils;
import com.eitc.system.service.ISysDictDataService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * SendSmsUtil
 *
 * @author mtx
 * @date: 2024/07/31 9:58
 */
@Component
public class SendSmsUtil {

    /**
     * 登录发送短信
     *
     * @param phoneNum 手机号
     * @param code     验证码
     * @author mtx
     * @date: 2024/7/31 11:24
     */
    public static void sendSmsToLogin(String phoneNum, String code) throws ClientException {
        String templateCode = "template-code";
        sendSms(phoneNum, code, templateCode);
    }

    /**
     * 登录发送短信
     *
     * @param phoneNum           手机号
     * @param code               验证码
     * @param templateCodeToDict 模板code
     * @author mtx
     * @date: 2024/7/31 11:24
     */
    public static void sendSms(String phoneNum, String code, String templateCodeToDict) throws ClientException {
        String accessKeyId = "access-key-id";
        String accessKeySecret = "access-key-secret";
        String signName = "sign-name";
        String templateCode = "template-code";

        ISysDictDataService dictDataService = SpringUtils.getBean(ISysDictDataService.class);
        List<SysDictData> ermSms = dictDataService.selectDictDataByType("erm_sms");
        for (SysDictData ermSm : ermSms) {
            if (Objects.equals(accessKeyId, ermSm.getDictLabel())) {
                accessKeyId = ermSm.getDictValue();
            }
            if (Objects.equals(accessKeySecret, ermSm.getDictLabel())) {
                accessKeySecret = ermSm.getDictValue();
            }
            if (Objects.equals(signName, ermSm.getDictLabel())) {
                signName = ermSm.getDictValue();
            }
            if (Objects.equals(templateCodeToDict, ermSm.getDictLabel())) {
                templateCode = ermSm.getDictValue();
            }
        }

        final String product = "Dysmsapi";
        final String endpoint = "dysmsapi.aliyuncs.com";
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-beijing", product, endpoint);
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求：
        SendSmsRequest request = new SendSmsRequest();
        request.setSysMethod(MethodType.POST);
        request.setPhoneNumbers(phoneNum);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setOutId(code);

        SendSmsResponse response = client.getAcsResponse(request);
        if (!"OK".equals(response.getCode())) {
            throw new ClientException(response.getMessage());
        }
    }

}
