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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.construct.RequestProcessor;
import com.persagy.framework.dto.DataQueryParam;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.HourRecordService;

/**
 * 小时历史数据
 */
@Service("historyHour")
public class QueryHistoryHour implements RequestProcessor {
	@Resource
	private HourRecordService hourDataService;

	@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(31)) {
			List<HourRecordData> list = hourDataService.query(location, param.timeSpan);
			for(HourRecordData record : list) {
				resultList.add(getResultItem(record));
			}
		}else {
			throw new Exception(Const.ErrorTag.timeToLong);
		}
		
		return resultList;
	}
	
	private Map<String, Object> getResultItem(HourRecordData record) throws Exception {
		Map<String, Object> item = new HashMap<>();
		item.put("time", record.getHourTime().getTime());
		item.put("temperature", record.getTemperature());
		item.put("text", record.getText());
		item.put("windSpeed", record.getWind_speed());
		item.put("humidity", record.getHumidity());
		item.put("quality", record.getQuality());
		item.put("feelsLike", record.getFeels_like());
		item.put("code", record.getCode());
		item.put("windDirection", record.getWind_direction());
		item.put("windDirectionDegree", record.getWind_direction_degree());
		item.put("windScale", record.getWind_scale());
		item.put("pressure", record.getPressure());
		item.put("visibility", record.getVisibility());
		item.put("clouds", record.getClouds());
		item.put("pm25", record.getPm25());
		item.put("pm10", record.getPm10());
		item.put("so2", record.getSo2());
		item.put("no2", record.getNo2());
		item.put("co", record.getCo());
		item.put("o3", record.getO3());
		item.put("aqi", record.getAqi());
		item.put("primaryPollutant", record.getPrimary_pollutant());
		return item;
	}
}