package com.aiotagro.common.core.utils;

import com.aiotagro.common.core.web.domain.AjaxResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

/**
 * 短信相关工具类
 */
public class SmsUtils {

    private static final Logger logger = LoggerFactory.getLogger(SmsUtils.class);

    // 短信应用SDK AppID
    private static final int APPID = 1400915506;
    // 短信应用SDK AppKey
    private static final String APPKEY = "dce6f4a7653b7a3506f77461446deed5";
    // 国家码
    private static final String NATIONCODE = "86";



    //签名
    @Value("${sms.sign}")
    private static final String SIGN = "武汉爱农云联科技";

    /**
     * 短信验证码发送
     * @param params
     * @param cellphone
     * @return
     */
    public static boolean sendSms(String[] params, String cellphone,Integer templateCode) {
        try {
            SmsSingleSender sender = new SmsSingleSender(APPID, APPKEY);
            SmsSingleSenderResult result = sender.sendWithParam(NATIONCODE, cellphone,
                    templateCode, params, SIGN, "", "");

            return result.result == 0;
        } catch (Exception e) {
            logger.error("短信发送失败", e);
        }
        return false;
    }

    /**
     * 短信验证码发送
     * @param tempCode
     * @param cellphone
     * @return
     */
    public static AjaxResult sendSms(String cellphone, Integer tempCode) {
        try {
            SmsSingleSender sender = new SmsSingleSender(APPID, APPKEY);
            SmsSingleSenderResult result = sender.sendWithParam(NATIONCODE, cellphone,
                    tempCode, new String[]{}, SIGN, "", "");
            return result.result == 0 ? AjaxResult.success("发送成功") : AjaxResult.error(result.errMsg);
        } catch (Exception e) {
            logger.error("短信发送失败", e);
            return AjaxResult.error("系统繁忙，发送失败");
        }
    }

}
