package com.persagy.functions.weather.business.v2021;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.ems.pojo.wz.DayStaticMoreRecord;
import com.persagy.ems.pojo.wz.DayStaticRecord;
import com.persagy.framework.construct.RequestProcessor;
import com.persagy.framework.dto.DataQueryParam;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.DayStaticService;

/**
 * 日统计数据
 */
@Service("statisticDay")
public class QueryStatisticDay implements RequestProcessor {
	@Resource
	private DayStaticService dayStaticService;

	@Override
	public Object handle(DataQueryParam param) throws Exception {
		List<Map<String, Object>> resultList = new ArrayList<>();
		
		String location = ConfigUtil.getHourCity(param.cityName, param.cityCode, param.projectId);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
		
		if(param.timeSpanCheck(366 * 3)) {
			Date startDay = param.timeSpan.timeLeft;
			Date endDay = param.timeSpan.timeRight;
			if(param.timeSpan.optLeft == SpecialOperator.$gt) {
				startDay = DateUtils.getDayOff(startDay, 1);
			}
			if(param.timeSpan.optRight == SpecialOperator.$lte) {
				endDay = DateUtils.getDayOff(endDay, 1);
			}
			
			Map<Date, DayStaticRecord> maxMap = new HashMap<>();
			List<DayStaticRecord> listMax = dayStaticService.query(location, startDay, endDay);
			if(null != listMax) {
				for(DayStaticRecord obj : listMax) {
					maxMap.put(obj.getDayTime(), obj);
				}
			}
			
			Map<Date, DayStaticMoreRecord> avgMap = new HashMap<>();
			List<DayStaticMoreRecord> listAvg = dayStaticService.queryMore(location, startDay, endDay);
			if(null != listAvg) {
				for(DayStaticMoreRecord obj : listAvg) {
					avgMap.put(obj.getDayTime(), obj);
				}
			}
			
			Date timeKey = startDay;
			while(timeKey.before(endDay)) {
				DayStaticRecord maxObj = maxMap.get(timeKey);
				DayStaticMoreRecord avgObj = avgMap.get(timeKey);
				
				Map<String, Object> item = new HashMap<>();
				resultList.add(item);
				item.put("time", timeKey.getTime());
				if(null != maxObj) {
					item.put("maxHumidity", maxObj.getMaxHumidity());
					item.put("maxPressure", maxObj.getMaxPressure());
					item.put("maxTemp", maxObj.getMaxTemp());
					item.put("maxVisibility", maxObj.getMaxVisibility());
					item.put("maxWindSpeed", maxObj.getMaxWindSpeed());
					item.put("minHumidity", maxObj.getMinHumidity());
					item.put("minPressure", maxObj.getMinPressure());
					item.put("minTemp", maxObj.getMinTemp());
					item.put("minVisibility", maxObj.getMinVisibility());
					item.put("minWindSpeed", maxObj.getMinWindSpeed());
				}
				if(avgObj != null) {
					item.put("avgHumidity", avgObj.getAvgHumidity());
					item.put("avgPressure", avgObj.getAvgPressure());
					item.put("avgTemp", avgObj.getAvgTemp());
					item.put("avgVisibility", avgObj.getAvgVisibility());
					item.put("avgWindSpeed", avgObj.getAvgWindSpeed());
				}
				
				timeKey = DateUtils.getDayOff(timeKey, 1);
			}
		}else {
			throw new Exception(Const.ErrorTag.timeToLong);
		}
		
		return resultList;
	}
	
}