package com.chinacreator.video.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URLEncoder;

@Component
public class SendSmsUtil {

    public static SendSmsUtil sendSmsUtil;


    @Autowired
    private  DictionaryDao dictionaryDao;


    @PostConstruct
    public void init(){
        System.out.println("PostConstruct接口的innit()执行");
        sendSmsUtil =this;
        sendSmsUtil.dictionaryDao = this.dictionaryDao;
    }



    public static String send(String userid,String content) throws Exception {

        if(sendSmsUtil.dictionaryDao==null){
            throw new BusinessException("500","dictionaryDao空指针");
        }

        String url = sendSmsUtil.dictionaryDao.getValue("newSmsSendUrl");
        if(url==null || "".equals(url)){
            throw new BusinessException("404","数据库中获取的url为null");
        }
        System.out.println("newSmsSendUrl对应的url是："+url);
        //新的url：newSmsSendUrl 对应：http://cctv2.mige.tv
//        String url = "http://111.206.133.54/smsbusi/sms/send";

        String smsid = "10655117";
        String pwd = "wo6bslq2";
        userid = DesUtil.encode(userid,pwd);
        System.out.println(userid);
        String timestamp = System.currentTimeMillis()/1000+"";
        String sign = SHAUtil.shaEncode(smsid+userid+timestamp+content+pwd).toLowerCase();
        userid = URLEncoder.encode(userid,"utf-8");
        content = URLEncoder.encode(content,"utf-8");
//        url = "http://111.206.133.54/smsbusi/sms/send?smsid="+smsid+"&userid="+userid+"&timestamp="+timestamp+"&sign="+sign+"&content="+content;
//        url = "http://cctv2.mige.tv/smsbusi/sms/send?smsid="+smsid+"&userid="+userid+"&timestamp="+timestamp+"&sign="+sign+"&content="+content;
        url = url+"?smsid="+smsid+"&userid="+userid+"&timestamp="+timestamp+"&sign="+sign+"&content="+content;

        System.out.println("生成的url是："+url);
        String result = "";
        try {
            result = URLUtil.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        if(StringUtils.isNotEmpty(result)){
            JSONObject obj = JSON.parseObject(result);
            String resultcode = obj.getString("resultcode");
            if ("0".equals(resultcode)){
                return resultcode;
            }else {
                throw new BusinessException(resultcode,"短信接口:"+obj.getString("errorinfo"));
            }
        }else {
            throw new BusinessException("8000","短信接口出现问题");
        }
    }



    public static void main(String args[]) throws Exception {
        SendSmsUtil s = new SendSmsUtil();
        s.send("15581368293","新网关地址测试");
//        try {
//            SendSmsUtil.send("18673197465","这是一条测试短信");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("123");
//        Object result = URLUtil.get("http://111.206.133.54/smsbusi/sms/send?smsid=10655117&userid=TxyXhnLm1vfHKMMyMtvycg%3D%3D&timestamp=1583916754&sign=10908a56107fa3479a7225f39d7b66ae0fef5a6a&content=这是一条测试短信");
//        System.out.println(result);


//        String str = DesUtil.decode("UJ7GEA99oV86kSyTD0GSag==","oaue7i6o");
//        System.out.println(str);
    }



}
