package shop.alien.lawyer.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import shop.alien.entity.store.StoreVerificationCode;
import shop.alien.mapper.StoreVerificationCodeMapper;
import shop.alien.util.common.RandomCreateUtil;
import shop.alien.lawyer.config.BaseRedisService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 阿里云验证码配置
 *
 * @author ssk
 * @version 1.0
 * @date 2024/12/12 10:17
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AliSms {
    private final BaseRedisService baseRedisService;

    private final StoreVerificationCodeMapper storeVerificationCodeMapper;

    @Value("${ali.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${ali.sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${ali.sms.endPoint}")
    private String endPoint;

    @Value("${ali.sms.signName}")
    private String signName;

    @Value("${ali.sms.templateCode}")
    private String templateCode;

    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @return 验证码
     */
    public Integer sendSms(String phone) {
        log.info("AliSmsConfig.sendSms?phone={}", phone);
        try {
            // -----------------测试用手机号--------------------------------------------------------------------------------------------
            List<String> phoneList = Arrays.asList("19999990001", "19999990002", "19999990003", "19999990004", "19999990005", "19999990006", "19999990007", "19999990008", "19999990009", "19999990010",
                    "16666660001", "16666660002", "16666660003", "16666660004", "16666660005", "16666660006", "16666660007", "16666660008", "16666660009", "16666660010",
                    "17777770001","17777770002","17777770003","17777770004","17777770005","17777770006","17777770007","17777770008","17777770009","17777770010");
            if (phoneList.contains(phone)) {
                // 验证码发送成功，将验证码保存到redis中 设置60秒过期
                baseRedisService.setString("verification_lawyer_"+phone,"123456",Long.valueOf(300));
                return 123456;
            }
            // -----------------测试用手机号--------------------------------------------------------------------------------------------

            Config config = new Config()
                    .setEndpoint(endPoint)
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            Integer code = RandomCreateUtil.getRandomNum(100000, 999999);

            // 构建发送请求
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    // 设置签名
                    .setSignName(signName)
                    // 设置模板
                    .setTemplateCode(templateCode)
                    // 设置手机号为参数传入的值
                    .setPhoneNumbers(phone)
                    // 设置模板参数为传入的验证码
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            // 运行时选择，可以设置不同的属性来配置运行时环境的参数。
            RuntimeOptions runtime = new RuntimeOptions();
            Client client = new Client(config);
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            if (!"OK".equals(sendSmsResponse.getBody().getCode())) {
                return null;
            }
            // 验证码发送成功，将验证码保存到redis中 设置60秒过期
            baseRedisService.setString("verification_lawyer_"+phone,code.toString(),Long.valueOf(300));
            // 永久存储验证码
            StoreVerificationCode entity = new StoreVerificationCode();
            entity.setAppType(2);
            entity.setBusinessType(7);
            entity.setPhone(phone);
            entity.setCode(String.valueOf(code));
            Date now = new Date();
            entity.setCreatedTime(now);
            entity.setUpdatedTime(now);
            entity.setDeleteFlag(0);
            storeVerificationCodeMapper.insert(entity);
            return code;
        } catch (Exception e) {
            log.error("AliSmsConfig.sendSms ERROR Msg={}", e.getMessage());
            return null;
        }
    }

}
