package com.chinacreator.video.restful.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import org.jboss.logging.Logger;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.bean.CapRevBean;
import com.chinacreator.video.restful.service.CapReverseService;

/**
 * CAP平台VAC反向通知数据入库
 * @author zhou.xu
 * @date 20190829
 */
@RestController
@RequestMapping(value = "/cap")
public class CapReverseController {
	
	private static Logger logger = Logger.getLogger(CapReverseController.class);
	
	private static Logger log = Logger.getLogger("capreverse");
	
	@Autowired
	CapReverseService service;
	
	/**
	 * POST方式办理业务
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/reverseOrdering", method = RequestMethod.POST, produces = "application/json")
    public String order(@RequestBody String params, HttpServletRequest request) throws Exception {
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	String inputParams = "";
    	String outputParams = "";
    	String respcode = "";
    	String respdesc = "";
    	String txid = "";	//服务流水号
    	String sequencdId = ""; //流水号，与请求流水号一致
    	try {
        	inputParams = params; //JSON.toJSONString(params); //输入参数
        	logmap.put("inputParams",inputParams);
        	CapRevBean crBean = new CapRevBean();
        	service.busiVali(crBean, inputParams);
        	txid = crBean.getTxid();
        	sequencdId = crBean.getSequenceId();
        	String result = service.operBusi(crBean); 
        	respcode = "0";
        	respdesc = "成功";
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof BussinessException) {
				respcode = ((BussinessException) e).getCode();
				respdesc = ((BussinessException) e).getMessage();
			}else {
				respcode = "8003";
				respdesc = "出现异常，"+e.getMessage();
			}
		} finally {			
			logmap.put("respcode",respcode);
			logmap.put("respdesc",respdesc);
			//获取输出参数
			outputParams = this.getOutputParams(txid, respcode, respdesc, sequencdId);
			logmap.put("outputParams",outputParams);
			log.info(JSON.toJSONString(logmap));	
			return outputParams;
		}
    }
    
    /**
     * 获取输出参数
     * @param txid
     * @param respcode
     * @param respdesc
     * @param sequencdId
     * @return
     */
    private String getOutputParams(String txid, String respcode, String respdesc, String sequencdId){
    	String outputParams = "";
    	JSONObject reqJson = new JSONObject();
		TreeMap<String, Object> bodyMap = new TreeMap<String, Object>();
		TreeMap<String, Object> roeMap = new TreeMap<String, Object>();
		TreeMap<String, Object> rspMap = new TreeMap<String, Object>();
		
		rspMap.put("RECORD_SEQUENCE_ID",sequencdId);
		if("0".equals(respcode)){
			respcode = "0000";
		}else{
			respcode = "8888";
		}
		rspMap.put("RSP_CODE",respcode);
		rspMap.put("RSP_DESC",respdesc);
		
		roeMap.put("RSP",rspMap);
		
		roeMap.put("STATUS","0000");
		roeMap.put("MSG","OK");
		roeMap.put("TXID",txid);
		
		//reqJson.put("REVERSE_ORDERING _RSP", roeMap);
		//reqJson.put("UNI_BSS_BODY", bodyMap);
		outputParams = JSON.toJSONString(roeMap);
    	return outputParams;
    }
}
