package com.chinacreator.video.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
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.service.QryBusiService;

/**
 * 统一查询接口
 * @author xu.zhou
 * @date 20190628
 */
@RestController
public class QryBusiControl {
	
	private static final  Logger logger = LoggerFactory.getLogger(QryBusiControl.class);
	
	private static final  Logger log = LoggerFactory.getLogger("qrybusi");
	
	@Autowired
	private QryBusiService service;
	
	@RequestMapping("/qrybusi")
	public DataOutPipe<?> hand(OrderInfo orderInfo) throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		orderInfo.setInvokeface("qrybusi");
		try {
			logmap.put("reqData", orderInfo);
			service.checkBusi(orderInfo);//业务验证
			service.valiVisitLimit(orderInfo);//访问次数验证
			List<Map> orderList = service.getOrderInfo(orderInfo);
			outpipe.setData(orderList);
			resultcode = "0";
			errorinfo = "成功";
		} catch (Exception e) {
			e.printStackTrace();
			resultcode = "80003";
			errorinfo = "出现异常，"+e.getMessage();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
				throw e;
			}else {
				throw new BussinessException(resultcode, errorinfo);
			}
		}finally {
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			logmap.put("rspData", outpipe.getData());
			logmap.put("resultcode", resultcode);
			logmap.put("errorinfo", errorinfo);
			log.info(JSON.toJSONString(logmap)); //写日志文件
		}
		return outpipe;
	}

}
