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

import java.util.List;
import java.util.Map;

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.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.framework.util.WServiceUtil;
import com.persagy.functions.weather.constant.Const;

@Service("Hour24Forecast")
@SuppressWarnings({"unchecked", "rawtypes"})
public class Hour24Forecast extends BaseBusinessService<Map> implements BusinessService {

	@Override
	public void handle(Map dto, List content) throws Exception {

		String cityName = (String) dto.get("cityName");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");

		if (StringUtils.isEmpty(projectId) && StringUtils.isEmpty(cityCode) && StringUtils.isEmpty(cityName)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}

		String location = ConfigUtil.getLocation(cityName, cityCode, projectId);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityNotFound);
		}
		
		try {
			Map<String, Object> dataMap = WServiceUtil.getHour24PredictWithLocation(location);
			if (!dataMap.isEmpty()) {
				List<Map<String, Object>> hourly = (List<Map<String, Object>>) dataMap.get("hourly");
				for(Map<String, Object> item : hourly) {
					item.put("time", DateUtils.date2Str(DateUtils.t8DateStr2Date(item.get("time")+""), DateUtils.SDF_SECOND));
				}
				content.add(dataMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
