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.CbErrInfoQryService;
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.List;
import java.util.Map;

/**
 * CB订购失败错误信息查询
 * @author xu.zhou
 * @date 2022/04/11
 */
@RestController
public class CbErrInfoQryController {

    @Autowired
    private CbErrInfoQryService service;

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

    @RequestMapping(value = "/qrycberrinfo", 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", "qrycberrinfo");
    	logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        long startime = System.currentTimeMillis();
        try {
        	orderInfo.setInvokeface("qrycberrinfo");
        	service.check(orderInfo);
        	List<Map<String, String>> data = service.process(orderInfo);
            outpipe.setData(JSON.toJSON(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;
    }
}
