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.DayHisRecord;
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.DayHisRecordService;

/**
 * 日历史数据
 */
@Service("historyDay")
public class QueryHistoryDay implements RequestProcessor {
	@Resource
	private DayHisRecordService dayHisRecordService;

	@Override
	public Object handle(DataQueryParam param) throws Exception {
		List<Map<String, Object>> resultList = new ArrayList<>();
		
		String location = ConfigUtil.getLocation(param.cityName, param.cityCode, param.projectId);
		
		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);
			}
			
			List<DayHisRecord> list = dayHisRecordService.query(location, startDay, endDay);
			for(DayHisRecord obj : list) {
				Map<String, Object> item = new HashMap<>();
				item.put("codeDay", obj.getCode_day());
				item.put("codeNight", obj.getCode_night());
				item.put("time", obj.getDayTime().getTime());
				item.put("temperatureHigh", obj.getHigh());
				item.put("humidity", obj.getHumidity());
				item.put("temperatureLow", obj.getLow());
				item.put("textDay", obj.getText_day());
				item.put("textNight", obj.getText_night());
				item.put("windDirection", obj.getWind_direction());
				item.put("windDirectionDegree", obj.getWind_direction_degree());
				item.put("windScale", obj.getWind_scale());
				item.put("windSpeed", obj.getWind_speed());
				item.put("rainfall", obj.getRainfall());
				item.put("precip", obj.getPrecip());
				resultList.add(item);
			}
		}else {
			throw new Exception(Const.ErrorTag.timeToLong);
		}
		
		return resultList;
	}
	
}