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.mapper.AqyDrawDao;
import com.chinacreator.video.restful.service.MuorderService;
import com.chinacreator.video.restful.service.Tw5gSyncService;
import com.chinacreator.video.restful.util.RequestUtil;
import com.chinacreator.video.util.RedisService;
import net.sf.json.JSONObject;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 拓维
 * zhengrong.yan
 * 20201110
 */
@RestController
public class Tw5GSyncController {
	
	private static Logger logger = Logger.getLogger(String.valueOf(Tw5GSyncController.class));
	
	private static Logger log = Logger.getLogger("tw5gsync");

	private static Logger log2 = Logger.getLogger("testredis");
	
	@Autowired
	private Tw5gSyncService service;

	@Autowired
	RedisService redisService;

	@Autowired
	AqyDrawDao aqyDrawDao;

	/**
	 * POST方式办理业务
	 * @param
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/tw5gsync", 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", "tw5gsync");
    	logmap.put("reqData", orderInfo);
    	String resultcode = "";
    	String errorinfo = "";
//		System.out.println(orderInfo);
    	try {
    		orderInfo.setTimes(System.currentTimeMillis()+"");
    		service.busiCheck(orderInfo);
			orderInfo.setApptype(RequestUtil.getMobType(request));
    		service.busiOper(orderInfo);	
    		resultcode = "0";
        	errorinfo = "";
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				resultcode = "8000";
				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;
		}
    }
}
