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;
import com.chinacreator.video.service.QueenCardService;

/**
 * 女王卡接口
 * @author xu.zhou
 * @date 20200221
 */
@RestController
public class QueenCardControl {
	
	private static final  Logger logger = LoggerFactory.getLogger(QueenCardControl.class);
	
	private static final  Logger log = LoggerFactory.getLogger("qrybusi");
	
	@Autowired
	private QueenCardService service;
	
	@RequestMapping("/qryQcAuth")
	public DataOutPipe<?> hand(OrderInfo orderInfo) throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		logmap.put("interface", "qryQcAuth");
		try {
			logmap.put("reqData", orderInfo);
			service.checkBusi(orderInfo);//业务验证
			//业务处理，返回女王卡产品编码，用于前台区分女王卡类型
			String productid = service.busiProcess(orderInfo);
			HashMap<String, String> data = new HashMap<String, String>();
			data.put("productid", productid);
			outpipe.setData(data);
			resultcode = "0";
			errorinfo = "成功";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
				throw e;
			}else {
				e.printStackTrace();
				resultcode = "80003";
				errorinfo = "出现异常，"+e.getMessage();
				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;
	}
	
	@RequestMapping("/qryQcRes")
	public DataOutPipe<?> qryOrderRes(OrderInfo orderInfo) throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		logmap.put("interface", "qryQcRes");
		try {
			logmap.put("reqData", orderInfo);
			service.checkBusiRes(orderInfo);//业务验证
			List<HashMap> orderList = service.qryOrderRes(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;
	}

}
