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

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.Account;
import com.cloudcross.ssp.model.DeliverAnalysis;
import com.cloudcross.ssp.model.RptZoneHour;
import com.cloudcross.ssp.service.IAdReportService;
import com.cloudcross.ssp.service.IRptZoneHourService;
import com.cloudcross.ssp.service.IZoneService;


@Controller("operator.advertiserReportController")
@RequestMapping("/operator/main/ad/report/advertiser-report")
public class AdvertiserReportController extends SimpleController {

	@Autowired
	private IAdReportService adReportService;
	@Autowired
	private FreemarkerTemplateProcessor templateProcessor;
	@Autowired
	private IRptZoneHourService rptZoneHourService;
	@Autowired
	private IZoneService zoneService;

	// 总体报表控制器
	@RequestMapping
	public String index(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		/**
		 * 前端要传递一个广告位id和时间段默认的时间段是昨天，传递过来的参数为dateRangePicker，格式为
		 * "2015-09-06 to 2015-09-13"，广告位id是全部 首先通过登录账号得到该媒体的id
		 * 注：往前端传递的广告曝光率和点击率已经乘过100了，前端只要加个“%”就可以
		 */
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		String zoneName = null;

		// 广告位id
		Long zoneId = null;
		/**
		 * 下面是对传递过来的两个参数进行处理，如果两个参数都为空值则两个参数都设为默认值，如果有一个参数有值另一个参数为空值，则
		 * 把空值的参数设置为默认值 广告位id的默认值为空即“null”,时间的默认值为昨天
		 */
		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"));
				zoneName = zoneService.findById(zoneId).getName();
			}
		}

		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();
		}

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

		int totalRow = adReportService.findCountForDeliverEffectAnalysisOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<DeliverAnalysis> reportList = adReportService
				.findDeliverEffectAnalysisOperator(paramMap, pager);

		DeliverAnalysis sumReport = null;

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

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

		/**
		 * 这里判断一下传递过来的是一个时间段还是某一天的时间，如果是某一天的时间，则数据要显示为一天24个小时的pv，uv，曝光数等等
		 */
		if (startDate.equals(endDate)) {
			// 先找到这一天改运营商每一个小时所有广告位的pv，uv，曝光数等等
			List<RptZoneHour> rptZoneHourList = null;
			rptZoneHourList = rptZoneHourService.findEveryHourData(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 == paramMap.get("zoneId")) {
			paramMap.put("selectedZoneId", "-1");
			paramMap.put("zoneName", "全部广告位");
		} else {
			paramMap.put("selectedZoneId", paramMap.get("zoneId"));
			paramMap.put("zoneName", zoneName);
		}
		
		model.addAttribute("listSize", reportList.size());
		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		model.addAttribute("sumReport", sumReport);

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

		// 广告位id
		Long zoneId = null;
		/**
		 * 下面是对传递过来的两个参数进行处理，如果两个参数都为空值则两个参数都设为默认值，如果有一个参数有值另一个参数为空值，则
		 * 把空值的参数设置为默认值 广告位id的默认值为空即“null”,时间的默认值为昨天
		 */
		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("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();
		}

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

		
		List<DeliverAnalysis> reportList = adReportService
				.findDeliverEffectAnalysisOperator(paramMap);
		
		
		return reportList;
	}

	// 尺寸报表
	@RequestMapping("/size")
	public String size(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		
		if (paramMap.containsKey("dateRangePicker")) {
			if (paramMap.get("dateRangePicker") != ""
					&& paramMap.get("dateRangePicker") != null) {
				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));
			}
		} else {
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		
		int totalRow = adReportService.countBySizeOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<DeliverAnalysis> reportList = adReportService.findBySizeOperator(paramMap,
				pager);

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

	// 位置报表控制器
	@RequestMapping("/position")
	public String position(Model model,
			@RequestParam HashMap<String, Object> paramMap) {
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = null;
		if (paramMap.containsKey("dateRangePicker")) {
			if (paramMap.get("dateRangePicker") != ""
					&& paramMap.get("dateRangePicker") != null) {
				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));
			}
		} else {
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		List<DeliverAnalysis> reportList = adReportService.findByPositionOperator(
				paramMap);

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

	// 系统报表控制器
	@RequestMapping("/system")
	public String system(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = adReportService.countBySystemOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<DeliverAnalysis> reportList = adReportService.findBySystemOperator(
				paramMap, pager);

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

	
	// 热点报表控制器
	@RequestMapping("/apmac")
	public String apmac(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		
		/**
		 * 前端要传递一个时间段默认的时间段是昨天，传递过来的参数为dateRangePicker，格式为
		 * "2015-09-06 to 2015-09-13" 注：往前端传递的广告曝光率和点击率已经乘过100了，前端只要加个“%”就可以
		 */
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}

		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = adReportService.countByApmacOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<DeliverAnalysis> reportList = adReportService
				.findByApmacOperator(paramMap, pager);
		model.addAllAttributes(paramMap);
		model.addAttribute("reportList", reportList);
		model.addAttribute("pager", pager);
		if (null != reportList) {
			model.addAttribute("listSize", reportList.size());
		}
		
		return page("apmac");
		
	}
	
	
	
	// 类型报表控制器
	@RequestMapping("/type")
	public String type(Model model,
			@RequestParam HashMap<String, Object> paramMap) {
		/**
		 * 前端要传递一个时间段默认的时间段是昨天，传递过来的参数为dateRangePicker，格式为
		 * "2015-09-06 to 2015-09-13" 注：往前端传递的广告曝光率和点击率已经乘过100了，前端只要加个“%”就可以
		 */
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}

		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		List<DeliverAnalysis> reportList = adReportService
				.findByDeviceOperator(paramMap);

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

	// 地域报表控制器
	@RequestMapping("/areal")
	public String areal(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = adReportService.countByAllArealOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<DeliverAnalysis> reportList = adReportService.findByAllArealOperator(
				paramMap, pager);

		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);

		return page("areal");
	}

	/**
	 * 查寻某省下各个市的pv，uv等信息
	 * 前端要传递的变量名有：省，它的变量名为cn，时间段dateRangePicker，如果时间为空或者为空字符串则默认为昨天
	 * @param model
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/arealCity")
	public @ResponseBody List<DeliverAnalysis> arealCtiy(Model model,
			@RequestParam HashMap<String, Object> paramMap) {
		
		paramMap.put("cn", "广东");
		
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		
		System.out.println("chenyou:" + paramMap);
		List<DeliverAnalysis> reportList = adReportService.findByArealCityOperator(paramMap);

		return reportList;
	}

	// 场景报表控制器
	@RequestMapping("/place")
	public String place(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {

		/**
		 * 前端要传递一个时间段默认的时间段是昨天，传递过来的参数为dateRangePicker，格式为
		 * "2015-09-06 to 2015-09-13" 注：往前端传递的广告曝光率和点击率已经乘过100了，前端只要加个“%”就可以
		 */
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}

		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		int totalRow = adReportService.countByPlaceOperator(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		List<DeliverAnalysis> reportList = adReportService.findByPlaceOperator(
				paramMap, pager);

		model.addAllAttributes(paramMap);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);

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

	@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 zoneId = null;
		/**
		 * 下面是对传递过来的两个参数进行处理，如果两个参数都为空值则两个参数都设为默认值，如果有一个参数有值另一个参数为空值，则
		 * 把空值的参数设置为默认值 广告位id的默认值为空即“null”,时间的默认值为昨天
		 */
		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("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 {
			zoneId = null;
			startDate = endDate = sdf.format(new Date((new Date()).getTime()
					- 24 * 60 * 60 * 1000));
		}
		
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		paramMap.put("zoneId", zoneId);
		
		List<DeliverAnalysis> reportList = adReportService
				.findDeliverEffectAnalysisOperator(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 = 11;
		paramMap.put("dataSize", reportList.size());
		paramMap.put("column", column);

		// 如果得到了广告位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", "全部");
		}
		String excelTemplate = templateProcessor.processTemplate(
				"excel/dataReportOperator.ftl", paramMap);
		try {
			PrintWriter out = response.getWriter();
			out.write(excelTemplate);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 这里搜索数据的条件都还没有都是默认的
	 * 
	 * @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) {

		/**
		 * 前端要传递一个时间段默认的时间段是昨天，传递过来的参数为dateRangePicker，格式为
		 * "2015-09-06 to 2015-09-13" 注：往前端传递的广告曝光率和点击率已经乘过100了，前端只要加个“%”就可以
		 */
		Account account = getLoginUser();
		paramMap.put("operatorId", account.getOperatorId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = null;
		String endDate = 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));
			}
		}
		
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		
		
		List<DeliverAnalysis> reportList = null;
		/**
		 * 判断一下是哪个报表
		 */
		if (paramMap.containsKey("size")) {
			reportList = adReportService.findBySizeOperator(paramMap);
		}

		if (paramMap.containsKey("position")) {
			reportList = adReportService.findByPositionOperator(paramMap);
		}

		if (paramMap.containsKey("system")) {
			reportList = adReportService.findBySystemOperator(paramMap);
		}

		if (paramMap.containsKey("device")) {
			reportList = adReportService.findByDeviceOperator(paramMap);
		}

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

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

		}

		if (paramMap.containsKey("place")) {
			reportList = adReportService.findByPlaceOperator(paramMap);
		}
		
		if (paramMap.containsKey("apmac")) {
			reportList = adReportService.findByApmacOperator(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/otherReportOperator.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;
	}

}
