package com.usky.system.controller.web;


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.profile.DefaultProfile;
import com.google.gson.Gson;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.StringUtils;
import com.usky.common.redis.core.RedisHelper;
import com.usky.system.domain.SysMobileTenantConfig;
import com.usky.system.domain.SysUser;
import com.usky.system.mapper.SysUserMapper;
import com.usky.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import com.usky.system.service.SysMobileTenantConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.rmi.ServerException;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 移动端_租户配置表 前端控制器
 * </p>
 *
 * @author JCB
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/SendSms")
public class SendSmsController {

    @Autowired
    private RedisHelper redisHelper;

    @Autowired
    private ISysUserService sysUserService;

    @Value("${sms.ali.region-id}")
    private String regionId;

    @Value("${sms.ali.access-key-id}")
    private String accessKeyId;

    @Value("${sms.ali.access-key-secret}")
    private String accessKeySecret;

    @GetMapping("/noteSending")
    public ApiResult<Void> noteSending(@RequestParam(value = "tenantId" ,required = false) Integer tenantId,@RequestParam(value = "phone") String phone) {
        if((tenantId != null) && !StringUtils.isBlank(phone)){
            sysUserService.getAppUserInfo(null, tenantId, phone,null);
        }

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        String code = String.valueOf(new Random().nextInt(1000000));
        redisHelper.set(phone,code,300,TimeUnit.SECONDS);//key为手机号，value为验证码存储
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);//接收短信的手机号码
        request.setSignName("永天智慧云");//短信签名名称
        request.setTemplateCode("SMS_164095840");//短信模板CODE
        request.setTemplateParam("{\"code\":\""+code+"\"}");//短信模板变量对应的实际值
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return ApiResult.success();
    }




}

