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

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

import javax.servlet.http.HttpServletRequest;

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 org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.service.IAccountService;
import com.cloudcross.ssp.service.IAdReportService;

/**
 * 控制面板功能。
 * 
 * @author chenyou
 */
@Controller("advertiser.dashboardController")
@RequestMapping("/advertiser/main/dashboard")
public class DashboardController extends SimpleController {
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IAdReportService adReportService;
	
	/**
	 * 当首页登录的调用这里，如果登录的是一个广告主账号的话显示的就是一个广告主的余额，如果是一个代理商登录的话显示的就是代理商下所有广告主的余额的总和
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping
	public String index(Model model,HttpServletRequest request) {
		
		
		try {
			LocaleResolver localeResolver = RequestContextUtils
					.getLocaleResolver(request);
			if (localeResolver == null) {
				throw new IllegalStateException(
						"No LocaleResolver found: not in a DispatcherServlet request?");
			}
			Locale locale = localeResolver.resolveLocale(request);
			String language = locale.getLanguage();
			
		} catch (Exception ex) {
		}
		
		//得到登录账号
		Account account = getLoginUser();
		
		model.addAttribute("loginAccountName", account.getAccountName());
		Map<String,Object> paramMap = new HashMap<String, Object>();
		
		/**
		 * 判断一下登录的账号是代理商还是广告主的，如果是广告主则它的代理商id和广告主id不等于0，如果是代理商则代理商的id不等于0广告主id等于
		 * 0
		 */ 
		if (0 != account.getAdvertiserId() && 0 != account.getAgentId()) {
			
			paramMap.put("advertiserId", String.valueOf(account.getAdvertiserId()));
			
			model.addAttribute("type", "advertiser");
			model.addAttribute("advertiserId", account.getAdvertiserId());
			model.addAttribute("accountId", account.getId());
			model.addAttribute("compareMap", findCompareAjax(model,paramMap));
		}
		
		if(0 != account.getAgentId() && 0 == account.getAdvertiserId()) {
			paramMap.put("agentId", String.valueOf(account.getAgentId()));
			
			model.addAttribute("type", "agent");
			model.addAttribute("agentId", account.getAgentId());
			model.addAttribute("accountId", account.getId());
			model.addAttribute("compareMap", findCompareAjax(model,paramMap));
		}
		return index;
	}
	
	/**
	 * 当对于选定广告主之后查询账户余额和判断箭头等数据
	 * 这个方法的功能有两个一个是首页刚登陆之后要找该登陆账号的余额等信息，另一个就是当登陆账号是代理商时，下拉框选则了一个广告主，查询该广告主的余额
	 * @param model
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("findCompareAjax")
	protected @ResponseBody Map findCompareAjax(Model model,
			@RequestParam Map<String,Object> paramMap) {
		
		/**
		 * 因为当下拉框如果选则全部的时候传递过来的广告主id位-1，这时其实就是查询该代理商的账号余额
		 */
		if(null != paramMap && paramMap.containsKey("advertiserId") && "-1".equals((String)paramMap.get("advertiserId"))) {
			Account account = getLoginUser();
			paramMap.put("agentId", String.valueOf(account.getAgentId()));
		}
		
		Map compareMap = adReportService.findCompare(paramMap);
		
		return compareMap;
	}
	
	/**
	 * 返回图标的数据
	 * @param paramMap 这里面包含了要查询的广告主id，查询的选择的起始时间和截止时间
	 * 注意这里的startDate和endDate传递的前端往后端传递的格式为yyyy-MM-dd
	 * @return
	 */
	@RequestMapping("chart")
	public @ResponseBody List<HashMap<String, Object>>  getchart(@RequestParam Map<String,Object> paramMap) {
		Long advertiserId = null;
		String startDate = null;
		String endDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		
		/**
		 * 对传递过来的数据进行处理,如果传递过来的是一个空值或者空字符串就表示为默认值默认的为全部，值就是-1
		 * 时间的默认值为昨天
		 */
		if(null == paramMap.get("advertiserId") || "".equals(paramMap.get("advertiserId"))) {
			advertiserId = -1l;
		} else {
			advertiserId = Long.parseLong(String.valueOf(paramMap.get("advertiserId")));
		}
		
		if(null == paramMap.get("startDate") || "".equals(paramMap.get("startDate"))) {
			startDate = sdf.format(new Date((new Date())
					.getTime() - 24 * 60 * 60 * 1000));
		} else {
			startDate = String.valueOf(paramMap.get("startDate"));
		}
		
		
		if(null == paramMap.get("endDate") || "".equals(paramMap.get("endDate"))) {
			endDate = sdf.format(new Date((new Date())
					.getTime() - 24 * 60 * 60 * 1000));
		} else {
			endDate = String.valueOf(paramMap.get("endDate"));
		}
		
		return	adReportService.findChart(startDate,endDate,Long.valueOf(advertiserId));
		 
	}
	
	/**
	 * 首页列表模式显示数据
	 * @param model
	 * @param paramMap 这里面包含了要查询的广告主id，查询的选择的起始时间和截止时间
	 * @param page
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		Long advertiserId = null;
		String startDate = null;
		String endDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		/**
		 * 对传递过来的数据进行处理,如果传递过来的是一个空值或者空字符串就表示为默认值默认的为全部，值就是-1
		 * 时间的默认值为昨天
		 */
		if(null == paramMap.get("advertiserId") || "".equals(paramMap.get("advertiserId"))) {
			advertiserId = -1l;
		} else {
			advertiserId = Long.parseLong(String.valueOf(paramMap.get("advertiserId")));
		}
		
		if(null == paramMap.get("startDate") || "".equals(paramMap.get("startDate"))) {
			startDate = sdf.format(new Date((new Date())
					.getTime() - 24 * 60 * 60 * 1000));
		} else {
			startDate = String.valueOf(paramMap.get("startDate"));
		}
		
		
		if(null == paramMap.get("endDate") || "".equals(paramMap.get("endDate"))) {
			endDate = sdf.format(new Date((new Date())
					.getTime() - 24 * 60 * 60 * 1000));
		} else {
			endDate = String.valueOf(paramMap.get("endDate"));
		}
		
		int totalRow = 0;
		totalRow = adReportService.findCountForChart(startDate, endDate, advertiserId);
		
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);
		List<HashMap<String, Object>> reportList = adReportService.findChart(startDate, endDate, advertiserId, pager);
		model.addAttribute("pager", pager);
		model.addAttribute("reportList", reportList);
		return page("list");
	}
}