package com.sysu.admin.support.sms.aliyun;

import com.alibaba.fastjson.JSON;
import com.github.zhangquanli.aliyun.sms.AliyunSms;
import com.github.zhangquanli.aliyun.sms.request.QuerySendDetailsRequest;
import com.github.zhangquanli.aliyun.sms.request.SendSmsRequest;
import com.github.zhangquanli.aliyun.sms.response.QuerySendDetailsResponse;
import com.github.zhangquanli.aliyun.sms.response.SendSmsResponse;
import com.google.common.collect.Maps;
import com.sysu.admin.utils.TextUtil;
import com.xiesx.fastboot.core.exception.RunExc;
import com.xiesx.fastboot.core.exception.RunException;
import com.xiesx.fastboot.core.token.JwtHelper;
import com.xiesx.fastboot.core.token.cfg.TokenCfg;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @title SmsSenderHelper
 * @description
 * @author XIE
 * @date 2020年4月26日下午6:44:33
 */
@Component
@Slf4j
public class AliSmsSenderHelper {

    @Autowired
    AliyunSms mAliyunSms;

    /**
     * @param bizId: 短信对象的对应的bizId
     * @param phoneNumber: 手机号
     * @param pageSize: 分页大小
     * @param currentPage: 当前页码
     * @author XIE
     * @Description: 查询发送短信的内容
     */
    public QuerySendDetailsResponse querySms(String bizId, String phoneNumber, Long pageSize, Long currentPage) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        QuerySendDetailsRequest.Builder builder = QuerySendDetailsRequest.builder();
        builder.bizId(bizId);
        builder.phoneNumber(phoneNumber);
        builder.pageSize(pageSize);
        builder.currentPage(currentPage);
        builder.sendDate(ft.format(new Date()));
        return mAliyunSms.querySendDetails(builder.build());
    }

    /**
     * @param phoneNumbers: 手机号
     * @param templateCode: 阿里云短信模板code
     * @author XIE
     * @Description: 对接阿里云短信服务实现短信发送 发送验证码类的短信时，每个号码每分钟最多发送一次，每个小时最多发送5次。其它类短信频控请参考阿里云
     * @return
     */
    public SendSmsResponse sendSms(String phoneNumbers, String templateCode, String code) {
        Map<String, String> map = Maps.newHashMap();
        map.put("code", code);
        SendSmsRequest.Builder builder = SendSmsRequest.builder();
        builder.phoneNumbers(phoneNumbers);
        builder.signName("智聪教育");
        builder.templateCode(templateCode);
        builder.templateParam(JSON.toJSONString(map));
        // builder.smsUpExtendCode(null);
        // builder.outId(null);
        return mAliyunSms.sendSms(builder.build());
    }


    public static String getMessage(String msg){
        String code = TextUtil.rightSubstring(":",msg);
        if(code != null){
            if(code.equals("1")){
                return "每分钟只能发送一条短信";
            }else if(code.equals("5")){
                return "每小时钟只能发送5条短信";
            }else if(code.equals("10")){
                return "每天只能发送10条短信";
            }
        }
        return msg;
    }

    public String checkToken(String token){
        try {
            // 获取token
            Claims claims = JwtHelper.parser(token);
            // 设置request
            String mobile = claims.getOrDefault(TokenCfg.USERNAME,"").toString();
            return mobile;
        } catch (Exception e) {
            log.error("jwt token error", e);
            if (e instanceof ExpiredJwtException) {
                throw new RunException(RunExc.TOKEN, "请重新登录");
            } else {
                throw new RunException(RunExc.TOKEN);
            }
        }
    }

    public static void main(String[] args) {
        AliSmsSenderHelper a =new AliSmsSenderHelper();
        a.sendSms("13797066447","SMS_188515112", "1234");
    }
}
