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

import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.utils.freemarker.FreemarkerTemplateProcessor;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.OperatorReport;
import com.cloudcross.ssp.model.RptZoneHour;
import com.cloudcross.ssp.service.IOperatorReportService;
import com.cloudcross.ssp.service.IOperatorService;
import com.cloudcross.ssp.service.IRptZoneHourService;
import com.cloudcross.ssp.service.IZoneService;

/**
 * 统计报表页面。
 * 
 * @author chenyou
 */
@Controller("back.operatorReportController")
@RequestMapping("/back/main/ad/operator/operator-report")
public class OperatorReportController extends SimpleController {

	@Autowired
	private IOperatorReportService operatorReportService;
	@Autowired
	private FreemarkerTemplateProcessor templateProcessor;
	@Autowired
	private IRptZoneHourService rptZoneHourService;
	@Autowired
	private IZoneService zoneService;
	@Autowired
	private IOperatorService operatorService;

	/**
	 * 媒体数据报表控制器
	 * 前端要传递三个参数给后端，分别是媒体的id(变量名selectedOperatorId)，广告位的id(变量名selectedZoneId
	 * )和日期dateRangePicker，如果没有传递则为默认的值，媒体的默认值为全部的媒体，这时operatorId设为
	 * null就行，广告位id和媒体类似，时间默认的是昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping
	public String index(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		// 媒体id
		Long operatorId = null;
		// 广告位id
		Long zoneId = null;

		/**
		 * 下面是对传递过来的三个参数进行处理，如果三个参数都为空值则三个参数都设为默认值
		 * 注：如果广告位id和媒体id为-1值的情况则表名选择的是全部的媒体和全部的广告位
		 */
		if (null != paramMap) {
			if (null == paramMap.get("selectedZoneId")
					|| "".equals(paramMap.get("selectedZoneId"))) {
				zoneId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedZoneId"))) {
					zoneId = null;
				} else {
					zoneId = Long.parseLong((String) paramMap
							.get("selectedZoneId"));
				}
			}

			if (null == paramMap.get("selectedOperatorId")
					|| "".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedOperatorId"))) {
					operatorId = null;
				} else {
					operatorId = Long.parseLong((String) paramMap
							.get("selectedOperatorId"));
				}
			}

			if (null == paramMap.get("dateRangePicker")
					|| "".equals(paramMap.get("dateRangePicker"))) {
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			} else {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			}
		} else {
			operatorId = null;
			zoneId = null;
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}

		paramMap.put("zoneId", zoneId);
		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		int totalRow = operatorReportService
				.findCountForDeliverEffectAnalysis(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<OperatorReport> reportList = operatorReportService
				.findDeliverEffectAnalysis(paramMap, pager);

		OperatorReport sumReport = null;

		// 根据媒体id和广告位id和时间查询总的pv，uv，广告曝光数等等
		sumReport = operatorReportService.findSum(paramMap);

		// 如果没有符合条件的数据则全部置0
		if (null == sumReport) {
			OperatorReport de = new OperatorReport();
			de.setPv(0);
			de.setUv(0);
			de.setImpression(0);
			de.setClick(0);
			de.setIncome(0);
			sumReport = de;
		}

		/**
		 * 这里判断一下传递过来的是一个时间段还是某一天的时间，如果是某一天的时间，则数据要显示为一天24个时的p小v，uv，曝光数等等
		 */
		if (startDate.equals(endDate)) {
			// 先找到这一天改运营商每一个小时所有广告位的pv，uv，曝光数等等
			List<RptZoneHour> rptZoneHourList = null;
			rptZoneHourList = rptZoneHourService.findEveryHourDataBack(paramMap);

			// 如果查询到的集合大小为0则表名没有符合条件的数据则给24个小时加0
			if (rptZoneHourList.size() == 0) {
				rptZoneHourList = new ArrayList<RptZoneHour>();
				for (int i = 0; i < 24; i++) {
					RptZoneHour rptZoneHour = new RptZoneHour();
					rptZoneHour.setHour(i);
					rptZoneHour.setPv(0l);
					rptZoneHour.setUv(0l);
					rptZoneHour.setImpression(0l);
					rptZoneHour.setClick(0l);
					rptZoneHour.setIncome(0l);

					rptZoneHourList.add(rptZoneHour);
				}
			} else {
				for (int i = 0; i < 24; i++) {
					if (!(isInList(i, rptZoneHourList))) {
						RptZoneHour rptZoneHour = new RptZoneHour();
						rptZoneHour.setHour(i);
						rptZoneHour.setPv(0l);
						rptZoneHour.setUv(0l);
						rptZoneHour.setImpression(0l);
						rptZoneHour.setClick(0l);
						rptZoneHour.setIncome(0l);

						rptZoneHourList.add(rptZoneHour);
					}
				}
				
				/**
				 * 对得到的rptZoneHourList进行排序，按hour的大小从小到大排序
				 */
				List<RptZoneHour> tempList = new ArrayList<RptZoneHour>();
				for(int i = 0; i < 24; i++) {
					for(RptZoneHour rptZoneHour : rptZoneHourList) {
						if(rptZoneHour.getHour() == i) {
							tempList.add(rptZoneHour);
							break;
						}
					}
				}
				
				rptZoneHourList = tempList;
				
			}

			model.addAttribute("rptZoneHourList", rptZoneHourList);
		}

		if (null == zoneId) {
			paramMap.put("selectedZoneId", "-1");
		} else {
			paramMap.put("selectedZoneId", paramMap.get("zoneId"));
		}
		
		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		
		model.addAttribute("listSize", reportList.size());
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		model.addAttribute("sumReport", sumReport);

		return index;
	}
	
	/**
	 * 返回一个OperatorReport的集合，用来在媒体数据报表列表页的图表显示
	 * 前端要传递三个参数给后端，分别是媒体的id(变量名selectedOperatorId)，广告位的id(变量名selectedZoneId
	 * )和日期dateRangePicker，如果没有传递则为默认的值，媒体的默认值为全部的媒体，这时operatorId设为
	 * null就行，广告位id和媒体类似，时间默认的是昨天
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/listChartData")
	public @ResponseBody List<OperatorReport> listChartData(@RequestParam HashMap<String, Object> paramMap) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		// 媒体id
		Long operatorId = null;
		// 广告位id
		Long zoneId = null;

		/**
		 * 下面是对传递过来的三个参数进行处理，如果三个参数都为空值则三个参数都设为默认值
		 * 注：如果广告位id和媒体id为-1值的情况则表名选择的是全部的媒体和全部的广告位
		 */
		if (null != paramMap) {
			if (null == paramMap.get("selectedZoneId")
					|| "".equals(paramMap.get("selectedZoneId"))) {
				zoneId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedZoneId"))) {
					zoneId = null;
				} else {
					zoneId = Long.parseLong((String) paramMap
							.get("selectedZoneId"));
				}
			}

			if (null == paramMap.get("selectedOperatorId")
					|| "".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedOperatorId"))) {
					operatorId = null;
				} else {
					operatorId = Long.parseLong((String) paramMap
							.get("selectedOperatorId"));
				}
			}

			if (null == paramMap.get("dateRangePicker")
					|| "".equals(paramMap.get("dateRangePicker"))) {
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			} else {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			}
		} else {
			operatorId = null;
			zoneId = null;
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}

		paramMap.put("zoneId", zoneId);
		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		
		List<OperatorReport> reportList = operatorReportService
				.findDeliverEffectAnalysis(paramMap);
		
		return reportList;
	}
	
	/**
	 * 热点报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/apmac")
	public String apmac(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		
		int totalRow = operatorReportService.countByApmac(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<OperatorReport> reportList = operatorReportService.findByApmac(
				paramMap, pager);
		
		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		// 加了一个listsize
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		// 到此为止
		return page("apmac");
	}

	/**
	 * 尺寸报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/size")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		int totalRow = operatorReportService.countBySize(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<OperatorReport> reportList = operatorReportService.findBySize(
				paramMap, pager);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		// 加了一个listsize
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		// 到此为止
		return page("size");
	}

	/**
	 * 位置报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/position")
	public String position(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		int totalRow = operatorReportService.countByPosition(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<OperatorReport> reportList = operatorReportService.findByPosition(
				paramMap, pager);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);

		// 加了一个listsize
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		// 到此为止
		return page("position");
	}

	/**
	 * 系统报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/system")
	public String system(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = operatorReportService.countBySystem(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<OperatorReport> reportList = operatorReportService.findBySystem(
				paramMap, pager);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}

		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		// 加了一个listsize
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		// 到此为止
		return page("system");
	}

	/**
	 * 类型报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/type")
	public String type(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		List<OperatorReport> reportList = operatorReportService
				.findByDevice(paramMap);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("reportList", reportList);
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		return page("type");
	}

	/**
	 * 地域报表控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/areal")
	public String areal(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = operatorReportService.countByAllAreal(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<OperatorReport> reportList = operatorReportService.findByAllAreal(
				paramMap, pager);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);

		return page("areal");
	}

	/**
	 * 查询给定省的下面地级市的pv，uv，流量占比等等信息
	 * 所以这里要传递三个参数，分别是媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)还有一个
	 * 省的变量cn
	 * (这个变量不能为默认值必须要传的参数)，其他两个如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/city")
	public @ResponseBody List<OperatorReport> arealCtiy(Model model,
			@RequestParam HashMap<String, Object> paramMap) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;
		
		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		List<OperatorReport> reportList = operatorReportService
				.findByAreal(paramMap);
		
		return reportList;
	}

	/**
	 * 场景控制器 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param model
	 * @param paramMap
	 * @param page
	 * @return
	 */
	@RequestMapping("/place")
	public String place(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;

		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}

		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = operatorReportService.countByPlace(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<OperatorReport> reportList = operatorReportService.findByPlace(
				paramMap, pager);

		if (null == operatorId) {
			paramMap.put("selectedOperatorId", "-1");
		} else {
			paramMap.put("selectedOperatorId", paramMap.get("operatorId"));
		}
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);

		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		return page("place");
	}

	/**
	 * 媒体数据报表导出excel
	 * 前端要传递三个参数给后端，分别是媒体的id(变量名selectedOperatorId)，广告位的id(变量名selectedZoneId
	 * )和日期dateRangePicker，如果没有传递则为默认的值，媒体的默认值为全部的媒体，这时operatorId设为
	 * null就行，广告位id和媒体类似，时间默认的是昨天
	 * 
	 * @param request
	 * @param response
	 * @param paramMap
	 */
	@RequestMapping("/exportDataReportExcel")
	public void exportDataReportExcel(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, Object> paramMap) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		// 媒体id
		Long operatorId = null;
		// 广告位id
		Long zoneId = null;

		/**
		 * 下面是对传递过来的三个参数进行处理，如果三个参数都为空值则三个参数都设为默认值
		 * 注：如果广告位id和媒体id为-1值的情况则表名选择的是全部的媒体和全部的广告位
		 */
		if (null != paramMap) {
			if (null == paramMap.get("selectedZoneId")
					|| "".equals(paramMap.get("selectedZoneId"))) {
				zoneId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedZoneId"))) {
					zoneId = null;
				} else {
					zoneId = Long.parseLong((String) paramMap
							.get("selectedZoneId"));
				}
			}

			if (null == paramMap.get("selectedOperatorId")
					|| "".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = null;
			} else {
				// 如果要查询的是所有广告位则让广告位id为空
				if (-1 == Long.parseLong((String) paramMap
						.get("selectedOperatorId"))) {
					operatorId = null;
				} else {
					operatorId = Long.parseLong((String) paramMap
							.get("selectedOperatorId"));
				}
			}

			if (null == paramMap.get("dateRangePicker")
					|| "".equals(paramMap.get("dateRangePicker"))) {
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			} else {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			}
		} else {
			operatorId = null;
			zoneId = null;
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}

		paramMap.put("zoneId", zoneId);
		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		List<OperatorReport> reportList = operatorReportService
				.findDeliverEffectAnalysis(paramMap);

		paramMap.put("dataList", reportList);

		// 如果得到了广告位id的话且广告位id不是默认的全部值，则将广告位名称添加上否则添加全部
		if (paramMap.containsKey("selectedZoneId")
				&& !"".equals(paramMap.get("selectedZoneId"))
				&& -1 != Long
						.parseLong((String) paramMap.get("selectedZoneId"))) {
			paramMap.put(
					"zoneName",
					zoneService.findById(
							Long.parseLong((String) paramMap
									.get("selectedZoneId"))).getName());
		} else {
			paramMap.put("zoneName", "全部广告位");
		}

		// 如果得到了媒体id的话且媒体id不是默认的全部值，则将媒体名称添加上否则添加全部
		if (paramMap.containsKey("selectedOperatorId")
				&& !"".equals(paramMap.get("selectedOperatorId"))
				&& -1 != Long.parseLong((String) paramMap
						.get("selectedOperatorId"))) {
			paramMap.put(
					"operatorName",
					operatorService.findById(
							Long.parseLong((String) paramMap
									.get("selectedOperatorId")))
							.getCompanyName());
		} else {
			paramMap.put("operatorName", "全部媒体");
		}
		response.reset();
		// Content-Type:application/vnd.ms-excel;charset=utf8或者text/xml;charset=utf8
		response.setContentType("application/vnd.ms-excel;charset=utf8");
		// 设置excel文件名称
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "report" + df2.format(new Date());
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		// 需要对excel的列和行的总数进行指定
		int column = 11;
		paramMap.put("dataSize", reportList.size());
		paramMap.put("column", column);
		String excelTemplate = templateProcessor.processTemplate(
				"excel/dataReportBack.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelTemplate);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 媒体端其他报表导出excel功能
	 * 前端要传递两个参数，分别为媒体id(变量名为selectedOperatorId)和日期(dateRangePicker)
	 * 如果没传则为默认值，媒体id的默认值为全部的媒体，这时媒体id设为空就行，日期默认为昨天
	 * 
	 * @param request
	 * @param response
	 * @param paramMap
	 *            如果是尺寸报表就必须传递一个size的参数，值是size（这个值可以随便）如果是位置报表传递一个position，
	 *            值是position
	 *            其他的类似，依次是系统（system）,类型(device),地域(location),场景(place),热点(apmac)
	 */
	@RequestMapping("/exportOtherReportExcel")
	public void exportOtherReportExcel(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, Object> paramMap) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		Long operatorId = null;
		
		if (!paramMap.containsKey("dateRangePicker")) {
			// 说明要按默认时间
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		} else {
			
			if (paramMap.get("dateRangePicker") != null
					&& !"".equals(paramMap.get("dateRangePicker"))) {
				String[] choosedDate = ((String) paramMap
						.get("dateRangePicker")).split("to");
				startDate = choosedDate[0].trim();
				endDate = choosedDate[1].trim();
			} else {// 显示昨天的数据
				startDate = endDate = sdf.format(new Date((new Date())
						.getTime() - 24 * 60 * 60 * 1000));
			}
		}

		if (!paramMap.containsKey("selectedOperatorId")) {
			// 说明要按默认值
			operatorId = null;
		} else {
			// 当选择媒体id为-1时说明要选择的是全部媒体
			if (paramMap.get("selectedOperatorId") != null
					&& !"".equals(paramMap.get("selectedOperatorId"))
					&& !"-1".equals(paramMap.get("selectedOperatorId"))) {
				operatorId = Long.parseLong((String) paramMap
						.get("selectedOperatorId"));
			} else {
				operatorId = null;
			}
		}
		// 如果得到了媒体id的话且媒体id不是默认的全部值，则将媒体名称添加上否则添加全部
		if (paramMap.containsKey("selectedOperatorId")
				&& !"".equals(paramMap.get("selectedOperatorId"))
				&& -1 != Long.parseLong((String) paramMap
						.get("selectedOperatorId"))) {
			paramMap.put(
					"operatorName",
					operatorService.findById(
							Long.parseLong((String) paramMap
									.get("selectedOperatorId")))
							.getCompanyName());
		} else {
			paramMap.put("operatorName", "全部媒体");
		}
		paramMap.put("operatorId", operatorId);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);

		List<OperatorReport> reportList = null;
		/**
		 * 判断一下是哪个报表
		 */
		if (paramMap.containsKey("size")) {
			reportList = operatorReportService.findBySize(paramMap);
		}

		if (paramMap.containsKey("position")) {
			reportList = operatorReportService.findByPosition(paramMap);
		}

		if (paramMap.containsKey("system")) {
			reportList = operatorReportService.findBySystem(paramMap);
		}

		if (paramMap.containsKey("device")) {
			reportList = operatorReportService.findByDevice(paramMap);
		}

		if (paramMap.containsKey("location")) {
			reportList = new ArrayList<OperatorReport>();
			/**
			 * 地域报表导出excel时要对数据的格式进行排列一下
			 * 如：先出现江苏省的总的pv,uv,流量占比等等信息，然后接着下面出现江苏省下的每个城市的pv,uv,流量占比等等信息
			 * 一下实现思路：
			 * 首先找到所有的省的总的pv，uv，流量占比等等信息，然后对找到的集合进行循环，找到每一个省下面的市的pv,uv,流量占比等等信息
			 */
			List<OperatorReport> cnList = operatorReportService
					.findAllArea(paramMap);

			for (int i = 0; i < cnList.size(); i++) {
				cnList.get(i).setCnMark(1);
				reportList.add(cnList.get(i));

				paramMap.put("cn", cnList.get(i).getCn());
				List<OperatorReport> cityList = operatorReportService
						.findByAreal(paramMap);

				for (int j = 0; j < cityList.size(); j++) {
					reportList.add(cityList.get(j));
				}
			}

		}

		if (paramMap.containsKey("place")) {
			reportList = operatorReportService.findByPlace(paramMap);
		}
		
		if (paramMap.containsKey("apmac")) {
			reportList = operatorReportService.findByApmac(paramMap);
		}

		paramMap.put("dataList", reportList);
		response.reset();
		// Content-Type:application/vnd.ms-excel;charset=utf8或者text/xml;charset=utf8
		response.setContentType("application/vnd.ms-excel;charset=utf8");
		// 设置excel文件名称
		SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "report" + df2.format(new Date());
		response.setHeader("Content-Disposition", "attachment;filename="
				+ fileName + ".xls");
		// 需要对excel的列的总数进行指定
		int column = 12;
		paramMap.put("dataSize", reportList.size());
		paramMap.put("column", column);
		paramMap.put("startDate", paramMap.get("startDate"));
		paramMap.put("endDate", paramMap.get("endDate"));

		String excelTemplate = templateProcessor.processTemplate(
				"excel/otherReportBack.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelTemplate);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 判断给定的某个小时是不是在给定的小时集合里面如果在，则返回true，否则返回false
	 */
	private boolean isInList(int i, List<RptZoneHour> list) {
		for (int j = 0; j < list.size(); j++) {
			if (i == list.get(j).getHour()) {
				return true;
			}
		}
		return false;
	}
}
