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

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

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.DayStaticMoreRecord;
import com.persagy.ems.pojo.wz.DayStaticRecord;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.constant.ConstStatisticType;
import com.persagy.functions.weather.constant.ConstTime;
import com.persagy.functions.weather.service.DayStaticService;

@Service("DayStaticData")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DayStaticData extends BaseBusinessService<Map> implements BusinessService {
	private static final Logger log = Logger.getLogger(DayStaticData.class);
	@Resource
	private DayStaticService dayStaticService;
	
	@Override
	public void handle(Map dto, List content) throws Exception {
		String cityName = (String) dto.get("cityName");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");
		String dataType = (String) dto.get("dataType");

		if (StringUtils.isEmpty(projectId) && StringUtils.isEmpty(cityCode) && StringUtils.isEmpty(cityName)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		ConstStatisticType dtype = null;
		if(null == dataType) {
			dtype = ConstStatisticType.Max;
		}else {
			dtype = ConstStatisticType.getItem(dataType);
		}
		if(null == dtype) {
			throw new Exception(Const.ErrorTag.paramUnrecognized);
		}
		
		String startTime = (String) dto.get("startTime");
		String endTime = (String) dto.get("endTime");
		Date timeFrom = null, timeTo = null;
		try {
			timeFrom = DateUtils.str2Date(startTime, DateUtils.SDF_SECOND);
			timeTo = DateUtils.str2Date(endTime, DateUtils.SDF_SECOND);
		}catch (Exception e) {
		}
		
		long tspan = timeTo.getTime() - timeFrom.getTime();
		if(null == timeFrom || null == timeTo) {
			throw new Exception(Const.ErrorTag.paramFormat);
		}else if(tspan > (5 * ConstTime.ms_year)) {
			throw new Exception(Const.ErrorTag.timeToLong);
		}
		
		String location = ConfigUtil.getHourCity(cityName, cityCode, projectId);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
		
		Long t1 = System.currentTimeMillis();
		
		Map<Date, DayStaticRecord> maxMap = new HashMap<>();
		if(dtype == ConstStatisticType.Max || dtype == ConstStatisticType.All) {
			List<DayStaticRecord> listMax = dayStaticService.query(location, timeFrom, timeTo);
			if(null != listMax) {
				for(DayStaticRecord obj : listMax) {
					maxMap.put(obj.getDayTime(), obj);
				}
			}
		}
		
		Long t2 = System.currentTimeMillis();
		
		Map<Date, DayStaticMoreRecord> avgMap = new HashMap<>();
		if(dtype == ConstStatisticType.Avg || dtype == ConstStatisticType.All) {
			List<DayStaticMoreRecord> listAvg = dayStaticService.queryMore(location, timeFrom, timeTo);
			if(null != listAvg) {
				for(DayStaticMoreRecord obj : listAvg) {
					avgMap.put(obj.getDayTime(), obj);
				}
			}
		}
		
		Long t3 = System.currentTimeMillis();
		
		Date timeKey = timeFrom;
		while(timeKey.before(timeTo)) {
			DayStaticRecord maxObj = maxMap.get(timeKey);
			DayStaticMoreRecord avgObj = avgMap.get(timeKey);
			
			if(null == maxObj && avgObj == null) {
			}else {
				Map<String, Object> item = new HashMap<>();
				content.add(item);
				item.put("dayTime", timeKey);
				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);
		}
		
		Long t4 = System.currentTimeMillis();
		if((t4-t1)/1000.0 > 5.0) {
			JSONObject jobj = new JSONObject();
			jobj.put("queryMax", (t2-t1)/1000.0);
			jobj.put("queryAvg", (t3-t2)/1000.0);
			jobj.put("buildResult", (t4-t3)/1000.0);
			log.info("DayStaticData TimeCost=" + jobj.toString());
		}
	}
}
