package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.CallSendLocalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * 走自有短信验证码接口进行能力平台的订购，用于腾讯
 * @author xu.zhou
 * @date 2022/03/16
 * 通过发自有短信获取验证码，生成模拟token
 */
@RestController
@RequestMapping(value = "/smsorder")
public class CallSendLocalController {

    @Autowired
    private CallSendLocalService callSendService;

    private Logger logger = LoggerFactory.getLogger("callSendco");

    @RequestMapping(value = "/sendcode", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> sendcode(@RequestBody OrderInfo orderInfo) {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "sendcode");
    	logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        long startime = System.currentTimeMillis();
        try {
        	orderInfo.setInvokeface("sendcode");
            callSendService.check(orderInfo);
            callSendService.process(orderInfo);
            Map<String, String> data = new HashMap<String, String>();
            data.put("token", orderInfo.getToken());
            outpipe.setData(data);
        } catch (Exception e) {
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = e.getMessage();
            } else {
                e.printStackTrace();
                resultcode = "8000";
                errorinfo = "系统异常,请稍后再试！";
                logger.error("出现异常，"+e.getMessage(),e);
                logmap.put("errorinfo","出现异常，"+e.getMessage());
            }
        } finally {
			logmap.put("resultcode",resultcode);
			if(logmap.get("errorinfo") == null){
				logmap.put("errorinfo",errorinfo);	
			}
			logmap.put("rspData", outpipe.getData());
			logmap.put("times", System.currentTimeMillis()-startime);
			logger.info(JSON.toJSONString(logmap));
			
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
        }
        return outpipe;
    }

    @RequestMapping(value = "/valicode", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> valicode(@RequestBody OrderInfo orderInfo) {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "valicode");
    	logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        long startime = System.currentTimeMillis();
        try {
        	orderInfo.setInvokeface("valicode");
            callSendService.check(orderInfo);
            callSendService.process(orderInfo);
        } catch (Exception e) {
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = e.getMessage();
            } else {
                e.printStackTrace();
                resultcode = "8000";
                errorinfo = "系统异常,请稍后再试！";
                logger.info("出现异常，"+e.getMessage(),e);
                logmap.put("errorinfo","出现异常，"+e.getMessage());
            }
        } finally {
			logmap.put("resultcode",resultcode);
			if(logmap.get("errorinfo") == null){
				logmap.put("errorinfo",errorinfo);	
			}
			logmap.put("rspData", outpipe.getData());
			logmap.put("times", System.currentTimeMillis()-startime);
			logger.info(JSON.toJSONString(logmap));
			
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
        }
        return outpipe;
    }
}
