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

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("CommonCityHourWeatherService")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class CommonCityHourWeatherService 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("startDate");
		String endDate = (String) dto.get("endDate");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");
		String cityName = (String) dto.get("cityName");
		Boolean minRespone = (Boolean) dto.get("minRespone");
		minRespone = Boolean.valueOf(minRespone == null ? false : minRespone.booleanValue());
		if ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode)) && (StringUtils.isEmpty(cityName))) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		String location = ConfigUtil.getHourCity(cityName, cityCode, projectId);
		if (location == null) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
		Date dateStart = null;
		Date dateEnd = null;
		if(startDate.contains("-") && startDate.length() == 19) {
			try {
				dateStart = DateUtils.str2Date(startDate, "yyyy-MM-dd HH:mm:ss");
				dateEnd = DateUtils.str2Date(endDate, "yyyy-MM-dd HH:mm:ss");
			} catch (Exception localException) {
			}
		}else if(startDate.length() == 14){
			try {
				dateStart = DateUtils.str2Date(startDate, "yyyyMMddHHmmss");
				dateEnd = DateUtils.str2Date(endDate, "yyyyMMddHHmmss");
			} catch (Exception localException) {
			}
		}
		if ((dateStart == null) || (dateEnd == null)) {
			throw new Exception(Const.ErrorTag.paramFormat);
		}
		List<HourRecordData> rList = this.hourDataService.query(location, dateStart, dateEnd);
		if (rList != null) {
			for (HourRecordData record : rList) {
				Map<String, String> item = getResultItem(record, minRespone);
				content.add(item);
			}
		}
	}

	private Map<String, String> getResultItem(HourRecordData record, Boolean minRespone) throws Exception {
		Map<String, String> item = new HashMap();
		item.put("receivetime", DateUtils.date2Str(record.getHourTime(), "yyyy-MM-dd HH:mm:ss"));
		item.put("temperature", record.getTemperature());
		item.put("humidity", record.getHumidity());
		item.put("windSpeed", record.getWind_speed());
		item.put("text", record.getText());
		item.put("quality", record.getQuality());
		if (!minRespone) {
			item.put("feelsLike", record.getFeels_like());
			item.put("locationId", record.getCityId());
			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());
		}
		return item;
	}
}
