package com.chinacreator.video.restful.controller;


import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.bean.SmsBean;
import com.chinacreator.video.util.URLUtil;
import com.chinacreator.video.validate.IpValidate;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.HashMap;


/**
 * 短信推送，由科创发起，铂源转发
 */
@RestController
public class FlowAlertSmsController {


    @Autowired
    private IpValidate ipValidate;

    private static Logger logger = Logger.getLogger("xsSendSMS");


    @RequestMapping("/xsSendSMS")
    public String xsSendSMS(SmsBean bean) throws Exception {
        String resp = "";
        HashMap<String,String> logmap = new HashMap<>();
        logmap.put("请求参数",JSON.toJSONString(bean));
        try {
            ipCheck();
            String usermob = URLEncoder.encode(bean.getUserid(),"utf-8");
            String contentid = URLEncoder.encode(bean.getContentid(),"utf-8");
            String vars = URLEncoder.encode(bean.getVars(),"utf-8");
            String url =bean.getUrl()+ "?userid="+usermob+"&smsid="+bean.getSmsid()+"&timestamp="+bean.getTimestamp()+"&sign="+bean.getSign()+"&contentid="+contentid+"&vars="+vars;
            logmap.put("请求url",url);
            resp =  URLUtil.get(url);
        }finally {
            logmap.put("返回结果",resp);
            logger.info(JSON.toJSONString(logmap));
        }
        return resp;
    }

    private void ipCheck() throws Exception {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setInvokeface("xsSendSMS");
        orderInfo.setChannel("xsSendSMS");
        orderInfo.setSpid("xsSendSMS");
        orderInfo.setCpid("xsSendSMS");
        ipValidate.volidate(orderInfo,"1");
    }
}
