package com.cloudcross.ssp.web.back.main;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.model.RptZone;
import com.cloudcross.ssp.service.IRptZoneService;
import com.cloudcross.ssp.base.web.SimpleController;

/**
 * 控制面板功能。
 * 首页控制
 * @author chenyou
 */
@Controller("back.dashboardController")
@RequestMapping("/back/main/dashboard")
public class DashboardController extends SimpleController {
	
	@Autowired
	protected IRptZoneService rptZoneService;
	
	@RequestMapping
	public String index(Model model) {
		/**
		 * 后端首页输出的是所有媒体的总的pv，uv，广告曝光数，广告点击数，ecpm和收入
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		//今天的时间字符串
		String today = sdf.format(new Date());
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("date", today);
		
		RptZone todayRptZone = rptZoneService.findByDate(paramMap);
		
		//判断查询到的是不是空值,如果是则把曝光数、点击数、收入等等数据设为0
		if(null == todayRptZone) {
			todayRptZone = new RptZone();
			todayRptZone.setDate(new Date());
			todayRptZone.setClick(0l);
			todayRptZone.setImpression(0l);
			todayRptZone.setIncome(0l);
			todayRptZone.setPv(0l);
			todayRptZone.setUv(0l);
		}
		
		model.addAttribute("todayRptZone", todayRptZone);
		
		return index;
	}
	
	
	@RequestMapping("/list")
	public @ResponseBody List<RptZone> provide(
			) {
		/**
		 * 得到所有媒体的近七天的总的pv，uv，广告曝光数，广告点击数，ecpm和收入
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		//存储近七天的时间
		List<String> dateList = new ArrayList<String>();
		dateList.add(sdf.format(new Date((new Date()).getTime() - 24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 2*24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 3*24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 4*24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 5*24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 6*24*60*60*1000)));
		dateList.add(sdf.format(new Date((new Date()).getTime() - 7*24*60*60*1000)));
		List<RptZone> rptZoneList = new ArrayList<RptZone>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		/**
		 * 查询近七天该运营商的所有广告位的曝光数、点击数、收入等等
		 * 如果出现哪一天的曝光数、点击数、收入等等数据库表里没有则查询出来的对象就会出现空值，那这时先用mark标记记录下来，然后再让这一天的这些数据设为0
		 */
		boolean mark = false;
		for(int i = 6; i >= 0; i--) {
			paramMap.put("date", dateList.get(i));
			RptZone rptZone = rptZoneService.findByDate(paramMap);
			if(null == rptZone) {
				mark = true;
			}
			
			if(mark) {
				rptZone = new RptZone();
				rptZone.setDate((new Date((new Date()).getTime() - (i + 1)*24*60*60*1000)));
				rptZone.setClick(0l);
				rptZone.setImpression(0l);
				rptZone.setIncome(0l);
				rptZone.setPv(0l);
				rptZone.setUv(0l);
				mark = false;
			}
		
			rptZoneList.add(rptZone);
		}
		return rptZoneList;
	}

	
}