package com.cloudcross.ssp.web.operator.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.Account;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.model.RptZone;
import com.cloudcross.ssp.service.IRptZoneService;

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

	
}