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

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 org.springframework.util.StringUtils;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.HourRecordData;
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.HourRecordService;

@Service("HourHistoryData")
@SuppressWarnings({"unchecked", "rawtypes"})
public class HourHistoryData extends BaseBusinessService<Map> implements BusinessService {
	public static final String sdf_sec = "yyyy-MM-dd HH:mm:ss";

	@Resource
	private HourRecordService hourDataService;

	@Override
	public void handle(Map dto, List content) throws Exception {
		String startDate = (String) dto.get("startTime");
		String endDate = (String) dto.get("endTime");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");
		String cityName = (String) dto.get("cityName");
		Boolean minResponse = (Boolean) dto.get("minResponse");

		minResponse = minResponse == null ? true : minResponse;
		if ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode)) && (StringUtils.isEmpty(cityName))) {
			throw new Exception(Const.ErrorTag.paramMis);
		}

		Date dateStart = null;
		Date dateEnd = null;
		try {
			dateStart = DateUtils.str2Date(startDate, sdf_sec);
			dateEnd = DateUtils.str2Date(endDate, sdf_sec);
		} catch (Exception Exception) {
		}
		if ((dateStart == null) || (dateEnd == null)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		if (DateUtils.getDayOff(dateStart, 31).before(dateEnd)) {
			throw new Exception(Const.ErrorTag.timeToLong);
		}
		
		String location = ConfigUtil.getHourCity(cityName, cityCode, projectId);
		if (location == null) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
		
		List<HourRecordData> rList = this.hourDataService.query(location, dateStart, dateEnd);
		
		if (rList != null) {
			for (HourRecordData record : rList) {
				Map<String, String> item = getResultItem(record, minResponse.booleanValue());
				content.add(item);
			}
		}
	}

	private Map<String, String> getResultItem(HourRecordData record, boolean minResponse) throws Exception {
		Map<String, String> item = new HashMap();
		item.put("hourTime", DateUtils.date2Str(record.getHourTime(), "yyyy-MM-dd HH:mm:ss"));
		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());
		if (!minResponse) {
			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("dewPoint", record.getDew_point());
			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;
	}
}
