package com.cm.manager.sms.utils;

import com.cm.common.exception.base.SystemException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DefaultSmsUtil
 * @Description: 默认短信工具
 * @Author: WangGeng
 * @Date: 2020/3/10 1:53 下午
 * @Version: 1.0
 **/
public class DefaultSmsUtil {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultSmsUtil.class);

    public static void sendSms(String url, String[] paramArray) {
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class, paramArray);
        JSONObject jsonObject = XML.toJSONObject(result);
        if (StringUtils.equals("Success", jsonObject.getJSONObject("returnsms").getString("returnstatus"))) {
            LOG.info("验证码发送成功");
        } else {
            LOG.error("验证码发送失败，原因：{}。", jsonObject.toString());
            throw new SystemException(jsonObject.toString());
        }
    }

}
