package com.chinacreator.video.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.bean.TrafficOrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.service.TrafficOrderService;
import com.chinacreator.video.util.RedisService;
import com.chinacreator.video.validate.BusiValidate;
import com.chinacreator.video.validate.IpValidate;

/**
 * 湖北2元畅视超级包页面计数接口
 * @author Administrator
 *
 */
@RestController
public class HbTcPreOrderCntControl {
	
	private static final  Logger logger = LoggerFactory.getLogger(HbTcPreOrderCntControl.class);
	
	@Autowired
	RedisService redisService;
	
	@Autowired
	private IpValidate ipValidate;
	
	@Autowired
	private DictionaryDao dictionaryDao;
		
	@RequestMapping("/hbtcpreCnt")
	public DataOutPipe<?> hand() throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		String totalcnt = "";
		Map data = new HashMap();
		try {
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setChannel("hbcs");
			//打印IP
			printHeader();
			/**			
			ipValidate.volidate(orderInfo);
			
	    	String randnum = dictionaryDao.getValue("hbtcpreOrderrand");
	    	if(randnum == null) randnum = "4";
	    	totalcnt = redisService.incr("HBTCPRECNT", Long.parseLong(randnum))+"";
	    	**/
			resultcode = "0";
			errorinfo = "ok";
		}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);
			data.put("totalcnt", totalcnt);
			outpipe.setData(data);
			logmap.put("resultcode", resultcode);
			logmap.put("errorinfo", errorinfo);
			logger.info(JSON.toJSONString(logmap));
		}
		return outpipe;
	}
	
	/**
	 * 输出IP地址信息
	 * @param request
	 */
	private static void printHeader(){
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		logger.info("=====================header信息=======================");
		logger.info("remoteaddr=>"+request.getRemoteAddr());
		Enumeration er = request.getHeaderNames();//获取请求头的所有name值
		while(er.hasMoreElements()){
			String name	=(String) er.nextElement();
			String value = request.getHeader(name);
			logger.info("name=>"+name+", value=>"+value);
		}
	}
}
