package com.chinacreator.video.restful.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.annotation.Log;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.dto.IncrDataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.restful.service.CallSendcoService;
import com.chinacreator.video.service.HandleService;
import com.chinacreator.video.util.RedisService;
import com.chinacreator.video.util.URLUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CallSendcoController {

    @Autowired
    private CallSendcoService callSendcoService;

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

    @PostMapping("/sendCapSmsCode")
    public Map<String, String> sendCapSmsCode(@RequestBody OrderInfo orderInfo) {
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "sendCapSmsCode");
    	logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        Map<String, String> respMap = new HashMap<>();
        long startTime = System.currentTimeMillis();
        try {
            orderInfo.setInvokeface("sendCapSmsCode");
            orderInfo.setUserid(URLDecoder.decode(orderInfo.getUserid(), "UTF-8"));
            callSendcoService.check(orderInfo);
            callSendcoService.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("times", System.currentTimeMillis()-startTime);
			logger.info(JSON.toJSONString(logmap));
			
            respMap.put("resultcode", resultcode);
            respMap.put("errorinfo", errorinfo);
        }
        return respMap;
    }

    @Log
    @PostMapping("/checkCapSmsCode")
    public DataOutPipe<?> checkCapSmsCode(@RequestBody OrderInfo orderInfo) throws Exception {
    	IncrDataOutPipe outpipe = new IncrDataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "checkCapSmsCode");
    	logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        Map<String, String> respMap = new HashMap<>();
        long startTime = System.currentTimeMillis();
        try {
            orderInfo.setUserid(URLDecoder.decode(orderInfo.getUserid(), "UTF-8"));
            orderInfo.setInvokeface("checkCapSmsCode");
            callSendcoService.check(orderInfo);
            callSendcoService.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());
            }
            
            throw e;
        } finally {
			logmap.put("resultcode",resultcode);
			if(logmap.get("errorinfo") == null){
				logmap.put("errorinfo",errorinfo);	
			}
			logmap.put("times", System.currentTimeMillis()-startTime);
			logger.info(JSON.toJSONString(logmap));
        	
            respMap.put("resultcode", resultcode);
            respMap.put("errorinfo", errorinfo);
        }
        
        return outpipe;
    }
}
