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.CollectionUtils;
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;

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

	@Override
	public void handle(Map dto, List content) throws Exception {
		List<String> cityCodes = (List<String>) dto.get("cityCodes");
		String beginTime = (String) dto.get("beginTime");
		
		String endTime = (String) dto.get("endTime");
		
		if(CollectionUtils.isEmpty(cityCodes)) {
			throw new Exception("cityCodes不能为空！");
		}

		if(StringUtils.isEmpty(beginTime)) {
			throw new Exception("beginTime不能为空！");
		}
		
		if(StringUtils.isEmpty(endTime)) {
			throw new Exception("endTime不能为空！");
		}
		
		Date startDate = DateUtils.str2Date(beginTime, "yyyy-MM-dd");
		Date endDate = DateUtils.str2Date(endTime, "yyyy-MM-dd");
	
		TypicalWeather queryObj = new TypicalWeather();
		queryObj.setSpecialOperation("cityCode", SpecialOperator.$in, cityCodes);
		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("cityCode", obj.getCityCode());
				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);
			}
		}
	}
}
