package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.bean.JiyueOrderBean;
import com.chinacreator.video.restful.service.KcReverseService;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
public class kcReverseController {

    private static Logger log = Logger.getLogger("kcOrder");

    @Autowired
    private KcReverseService service;

    @RequestMapping(value = "/kcOrder", 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 = "";
        HashMap<String,String> outputParams = new HashMap<String,String>();
        String resultcode = "0000";
        String errorinfo = "ok";
        try {
            inputParams = params; //JSON.toJSONString(params); //输入参数
            logmap.put("inputParams",inputParams);
            JiyueOrderBean jiyueOrderBean = new JiyueOrderBean();
            service.busiVali(jiyueOrderBean, inputParams);
            service.operBusi(jiyueOrderBean);
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            }else {
                resultcode = "8003";
                errorinfo = "出现异常，"+e.getMessage();
            }
        }finally {
            logmap.put("resultCode",resultcode);
            logmap.put("errorinfo",errorinfo);
            outputParams.put("resultCode",resultcode);
            outputParams.put("errorinfo",errorinfo);
            log.info(JSON.toJSONString(logmap));
            return JSON.toJSONString(outputParams);
        }
    }
}
