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

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.enumeration.SpecialOperator;
import com.persagy.core.mvc.service.CoreService;
import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.TypicalWeather;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;

@Service("GetTypicalWeather")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class QueryTypicalWeather extends BaseBusinessService<Map> implements BusinessService {
	@Resource(name = "coreService")
	private CoreService coreService;

	@Override
	public void handle(Map dto, List content) throws Exception {
		String cityCode = (String) dto.get("cityCode");
		String time = (String) dto.get("time");
		if ((StringUtils.isEmpty(cityCode)) || (StringUtils.isEmpty(time))) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		Date startDate = DateUtils.str2Date(time, "yyyy-MM");
		Date endDate = DateUtils.getMonthOff(startDate, 1);

		TypicalWeather queryObj = new TypicalWeather();
		queryObj.setCityCode(cityCode);
		queryObj.setSpecialOperation("date", SpecialOperator.$gte, startDate);
		queryObj.setSpecialOperation("date", SpecialOperator.$lt, endDate);
		List<TypicalWeather> list = this.coreService.query(queryObj);
		if (list != null) {
			for (TypicalWeather obj : list) {
				Map<String, Object> item = new HashMap<>();
				item.put("date", obj.getDate());
				item.put("hdd", obj.getHdd());
				item.put("cdd", obj.getCdd());
				item.put("maxTemp", obj.getMaxTemp());
				item.put("minTemp", obj.getMinTemp());
				item.put("avgTemp", obj.getAvgTemp());
				item.put("maxWindSpeed", obj.getMaxWindSpeed());
				item.put("minWindSpeed", obj.getMinWindSpeed());
				item.put("avgWindSpeed", obj.getAvgWindSpeed());
				item.put("maxHumidity", obj.getMaxHumidity());
				item.put("minHumidity", obj.getMinHumidity());
				item.put("avgHumidity", obj.getAvgHumidity());
				item.put("maxPressure", obj.getMaxPressure());
				item.put("minPressure", obj.getMinPressure());
				item.put("avgPressure", obj.getAvgPressure());
				item.put("maxVisibility", obj.getMaxVidibility());
				item.put("minVisibility", obj.getMinVidibility());
				item.put("avgVisibility", obj.getAvgVidibility());
				content.add(item);
			}
		}
	}
}
