package com.chinacreator.video.restful.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;

import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.mapper.DictionaryDao;
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.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.restful.service.CshuyuService;
import com.chinacreator.video.restful.util.RequestUtil;

/**
 * 畅视互娱接口
 * xu.zhou
 * 20200330
 */
@RestController
public class CshuyuController {
	
	private static Logger logger = Logger.getLogger(CshuyuController.class);
	
	private static Logger log = Logger.getLogger("cshuyu");
	
	@Autowired
	private CshuyuService service;

	@Autowired
	private DictionaryDao dictionaryDao;
		
	/**
	 * POST方式办理业务
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/cshuyu", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> order(@RequestBody (required=false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "cshuyu");
    	logmap.put("reqData", orderInfo);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		orderInfo.setTimes(System.currentTimeMillis()+"");
    		service.busiCheck(orderInfo);
			String numberlimit = dictionaryDao.getValue("Numberlimit");
			if (numberlimit != null && numberlimit.indexOf(orderInfo.getUserid().substring(0, 7)) != -1) {
				throw new BussinessException("9010", "用户为黑名单用户");
			}
			orderInfo.setApptype(RequestUtil.getMobType(request));
			orderInfo.setOrderstatus("2");
    		service.busiOper(orderInfo);	
    		resultcode = "0";
        	errorinfo = "ok";
		} 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);	
			logmap.put("rspData", outpipe.getData());
			log.info(JSON.toJSONString(logmap));	
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }
}
