package com.cloudcross.ssp.service.impl;

import java.text.DecimalFormat;
import java.text.NumberFormat;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Advertiser;
import com.cloudcross.ssp.model.DeliverAnalysis;
import com.cloudcross.ssp.service.IAccountService;
import com.cloudcross.ssp.service.IAdReportService;
import com.cloudcross.ssp.service.IAdvBalanceService;
import com.cloudcross.ssp.service.IAdvertiserService;


@Service
public class AdReportService implements IAdReportService {
	@Autowired
	private GenericIBatisDao iBatisDao;
	@Autowired
	private IAdvertiserService advertiserService;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IAdvBalanceService advBalanceService;
	
	@Override
	public DeliverAnalysis findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<DeliverAnalysis> findByParams(Map<String, Object> paramMap,
			Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean add(DeliverAnalysis t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean edit(DeliverAnalysis t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int findCountForDeliverEffectAnalysisAdvertiser(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForDeliverEffectAnalysisAdvertiser", paramMap);
	}

	@Override
	public int findCountForDeliverEffectAnalysis2(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int findCountForLocationAdvertiser(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForLocationAdvertiser", paramMap);
	}

	@Override
	public int findCountForOs(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForOs", paramMap);
	}

	@Override
	public int findCountForScene(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForScene", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalReportAdvertiser(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByTotalReportAdvertiser", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalReport2(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByTotalReport2", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByPlaceName(Map<String, Object> paramMap,
			Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByPlaceName", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByLocationAdvertiser(Map<String, Object> paramMap,
			Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByLocationAdvertiser", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByOs(Map<String, Object> paramMap,
			Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByOs", paramMap);
	}

	@Override
	public int findCountForBanner(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForBanner", paramMap);
	}


	@Override
	public List<DeliverAnalysis> findBannerByConditions(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findBannerList", paramMap);
	}


	@Override
	public List<DeliverAnalysis> exportBannerListList(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.exportBannerList", paramMap);
	}
	@Override
	public Map<String, Object> findCompare(Map<String, Object> paramMap) {
		Long advertiserId = null;
		Long agentId = null;
		
		/**
		 * 判断下是广告主，还是代理商
		 */
		if (!paramMap.containsKey("advertiserId")) {
			advertiserId = null;
		} else {
			if (paramMap.get("advertiserId") != null
					&& !"".equals(paramMap.get("advertiserId"))) {
				advertiserId = Long.parseLong((String) paramMap
						.get("advertiserId"));
			} else {
				advertiserId = null;
			}
		}
		
		
		if (!paramMap.containsKey("agentId")) {
			agentId = null;
		} else {
			if (paramMap.get("agentId") != null
					&& !"".equals(paramMap.get("agentId"))) {
				agentId = Long.parseLong(String.valueOf(paramMap.get("agentId")));
			} else {
				agentId = null;
			}
		}
		
		//查询用户当前余额
		//格式化
		DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(Locale.CHINESE);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		df.applyPattern("0.00");
		Map<String,Object> compareMap = new HashMap<String,Object>();
		/**
		 * 先判断传递进来的是一个广告主id，还是代理商id
		 * 如果广告主id不为空，则为广告主账号，如果是代理商id不为空则为代理商账号
		 */
		if(null != advertiserId) {
			Map<String, Object> map = null;
			paramMap.put("advertiserId", advertiserId);
			Advertiser advertiser = advertiserService.findById(advertiserId);
			
			/**
			 * 下面是查找该广告主下的昨天和前天的曝光数和点击数
			 */
			paramMap.put("date", sdf.format(new Date().getTime() - 24 * 60 * 60 * 1000));
			Long yesterdayImpressionSum = null;
			Long yesterdayClickSum = null;
			
			map = findTotalImpressionAndClickByAdvertiserId(paramMap);
			
			/**
			 * 对于查询的结果进行判断，如果没找到的结果是空的，则两个值都设为0
			 * 如果找到其中的一个值为空就把这个值设为0
			 */
			if(null == map) {
				yesterdayClickSum = new Long(0);
				yesterdayImpressionSum = new Long(0);
			} else {
				yesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
				yesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
			}
			
			compareMap.put("totalImpression", yesterdayImpressionSum);
			compareMap.put("totalClick", yesterdayClickSum);
			
			paramMap.put("date", sdf.format(new Date().getTime() - 2 * 24 * 60 * 60 * 1000));
			Long beforeYesterdayImpressionSum = null;
			Long beforeYesterdayClickSum = null;
			map = findTotalImpressionAndClickByAdvertiserId(paramMap);
			
			/**
			 * 对于查询的结果进行判断，如果m找到的结果是空的，则两个值都设为0
			 * 如果找到其中的一个值为空就把这个值设为0
			 */
			if(null == map) {
				beforeYesterdayClickSum = new Long(0);
				beforeYesterdayImpressionSum = new Long(0);
			} else {
				beforeYesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
				beforeYesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
			}
			
			/**
			 * 通过比对昨天和前天的数据大小判断箭头
			 */
			if((yesterdayImpressionSum - beforeYesterdayImpressionSum) > 0) {
				compareMap.put("impressionStatus", "up");
			} else if((yesterdayImpressionSum == beforeYesterdayImpressionSum)) {
				compareMap.put("impressionStatus", "equal");
			} else {
				compareMap.put("impressionStatus", "down");
			}
			
			if((yesterdayClickSum - beforeYesterdayClickSum) > 0) {
				compareMap.put("clickStatus", "up");
			} else if((yesterdayClickSum == beforeYesterdayClickSum)) {
				compareMap.put("clickStatus", "equal");
			} else {
				compareMap.put("clickStatus", "down");
			}
			
			
			compareMap.put("balance",df.format(advertiser.getBalance()));
		}
		
		if(null != agentId) {
			paramMap.put("agentId", agentId);
			
			//得到代理商账号的余额
			Double balance = advBalanceService.findBalanceByAgentId(agentId);
			
			//如果找到的余额为空则余额为0
			if(null == balance) {
				balance = new Double(0);
			}
			
			Map<String, Object> map = null;
			/**
			 * 下面是查找该代理商下的所有广告主的昨天和前天的曝光数和点击数
			 */
			paramMap.put("date", sdf.format(new Date().getTime() - 24 * 60 * 60 * 1000));
			
			Long yesterdayImpressionSum = null;
			Long yesterdayClickSum = null;
			map = findTotalImpressionAndClickByAgentId(paramMap);
			
			/**
			 * 对于查询的结果进行判断，如果没找到的结果是空的，则两个值都设为0
			 * 如果找到其中的一个值为空就把这个值设为0
			 */
			if(null == map) {
				yesterdayClickSum = new Long(0);
				yesterdayImpressionSum = new Long(0);
			} else {
				yesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
				yesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
			}
			
			compareMap.put("totalImpression", yesterdayImpressionSum);
			compareMap.put("totalClick", yesterdayClickSum);
			
			paramMap.put("date", sdf.format(new Date().getTime() - 2 * 24 * 60 * 60 * 1000));
			Long beforeYesterdayImpressionSum = null;
			Long beforeYesterdayClickSum = null;
			map = findTotalImpressionAndClickByAgentId(paramMap);
			
			/**
			 * 对于查询的结果进行判断，如果m找到的结果是空的，则两个值都设为0
			 * 如果找到其中的一个值为空就把这个值设为0
			 */
			if(null == map) {
				beforeYesterdayClickSum = new Long(0);
				beforeYesterdayImpressionSum = new Long(0);
			} else {
				beforeYesterdayClickSum = Long.parseLong(map.get("totalClick").toString());
				beforeYesterdayImpressionSum = Long.parseLong(map.get("totalImpression").toString());
			}
			
			
			/**
			 * 通过比对昨天和前天的数据大小判断箭头
			 */
			if((yesterdayImpressionSum - beforeYesterdayImpressionSum) > 0) {
				compareMap.put("impressionStatus", "up");
			} else if((yesterdayImpressionSum == beforeYesterdayImpressionSum)) {
				compareMap.put("impressionStatus", "equal");
			} else {
				compareMap.put("impressionStatus", "down");
			}
			
			if((yesterdayClickSum - beforeYesterdayClickSum) > 0) {
				compareMap.put("clickStatus", "up");
			} else if((yesterdayClickSum == beforeYesterdayClickSum)) {
				compareMap.put("clickStatus", "equal");
			} else {
				compareMap.put("clickStatus", "down");
			}
			
			
			compareMap.put("balance",df.format(balance));
		}
		
		return compareMap;
	}

	@Override
	public List<HashMap<String, Object>> findChart(String startDate,
			String endDate, Long advertiserId) {
		/**
    	 * 这里查询分为是一段时间和某一天时间
    	 */
    	if(startDate.equals(endDate)){
    		return iBatisDao.getList("adReportSqlMapper.findByAdvertiser1", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("advertiserId", advertiserId).map());
    	} else {
    		 return iBatisDao.getList("adReportSqlMapper.findByAdvertiser", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("endDate", endDate).add("advertiserId", advertiserId).map());      
    	}
		
	}

	@Override
	public int findCountForChart(String startDate, String endDate,
			Long advertiserId) {
		/**
    	 * 这里查询分为如果是一段时间或者是一天时间
    	 */
    	if(startDate.equals(endDate)){
    		return iBatisDao.get("adReportSqlMapper.findCountForChart1", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("advertiserId", advertiserId).map());
    	} else {
    		 return iBatisDao.get("adReportSqlMapper.findCountForChart", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("endDate", endDate).add("advertiserId", advertiserId).map());      
    	}
	}

	@Override
	public List<HashMap<String, Object>> findChart(String startDate,
			String endDate, Long advertiserId, Pager pager) {
		/**
    	 * 这里查询分为一段时间和某一天时间
    	 */
    	if(startDate.equals(endDate)){
    		return iBatisDao.getList("adReportSqlMapper.findByAdvertiser2", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("advertiserId", advertiserId).add("pager", pager).map());
    	} else {
    		 return iBatisDao.getList("adReportSqlMapper.findByAdvertiser3", MapBuilder.create(HashMap.class).add("startDate", startDate)
                     .add("endDate", endDate).add("advertiserId", advertiserId).add("pager", pager).map());      
    	}
	}

	@Override
	public HashMap<String, Object> findTotalImpressionAndClickByAdvertiserId(
			Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findTotalImpressionAndClickByAdvertiserId", paramMap);
	}

	@Override
	public HashMap<String, Object> findTotalImpressionAndClickByAgentId(
			Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findTotalImpressionAndClickByAgentId", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalForExcelAdvertiser(Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByTotalForExcelAdvertiser", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByPlaceName(Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByPlaceNameForExcel", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByLocation(Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByLocationForExcel", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByOs(Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByOsForExcel", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findDeliverEffectAnalysisOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findDeliverEffectAnalysis", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findDeliverEffectAnalysisOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findDeliverEffectAnalysis1", paramMap);

	}

	@Override
	public int countBySizeOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.countBySize", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findBySizeOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findBySize", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findBySizeOperator(Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findBySize1", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByPositionOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByPosition", paramMap);
	}

	@Override
	public int countBySystemOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.countBySystem", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findBySystemOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findBySystem", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findBySystemOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findBySystem1", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByDeviceOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByDevice", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByArealCityOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByArealCity", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByArealOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByAreal", paramMap);
	}

	@Override
	public int countByPlaceOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.countByPlace", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByPlaceOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByPlace", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByPlaceOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByPlace1", paramMap);
	}

	@Override
	public int countByApmacOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.countByApmac", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByApmacOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByApmac", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByApmacOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByApmac1", paramMap);
	}

	@Override
	public int countByAllArealOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.countByAllAreal", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByAllArealOperator(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByAllAreal", paramMap);
	}

	@Override
	public DeliverAnalysis findSumOperator(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findSum", paramMap);	}

	@Override
	public List<DeliverAnalysis> findAllAreaOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findAllArea", paramMap);
	}

	@Override
	public int findCountForDeliverEffectAnalysisOperator(
			Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForDeliverEffectAnalysisOperator", paramMap);
	}

	@Override
	public int findCountForDeliverEffectAnalysisBack(
			Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForDeliverEffectAnalysisBack", paramMap);
	}

	@Override
	public int findCountForLocationBack(Map<String, Object> paramMap) {
		return iBatisDao.get("adReportSqlMapper.findCountForLocationBack", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalReportBack(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByTotalReportBack", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByLocationBack(
			Map<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		return iBatisDao.getList("adReportSqlMapper.findByLocationBack", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalForExcelBack(
			Map<String, Object> paramMap) {
		return iBatisDao.getList("adReportSqlMapper.findByTotalForExcelBack", paramMap);
	}

	@Override
	public List<DeliverAnalysis> findByTotalForExcelOperator(
			Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
