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.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.dto.TempStatisticsDTO;
import com.persagy.functions.weather.service.WeatherStatisticsService;

/**
 * Description: 查询历史天气统计信息
 */
@Service("GetTempMonthStatistics")
@SuppressWarnings({ "rawtypes","unchecked" })
public class GetTempMonthStatistics extends BaseBusinessService<Map> implements BusinessService {
	
	@Resource
	private WeatherStatisticsService weatherStatisticsService;

	@Override
    public void handle(Map dto, List content) throws Exception {
    	String cityCode = (String) dto.get("cityCode");
    	String startDateStr = (String) dto.get("startDate");
    	String endDateStr = (String)dto.get("endDate");
    	if(StringUtils.isEmpty(cityCode) || StringUtils.isEmpty(startDateStr) || StringUtils.isEmpty(endDateStr)) {
    		throw new Exception(Const.ErrorTag.paramMis);
    	}
    	String location = ConfigUtil.getHourCity(null, cityCode, null);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
    	Date startDate = DateUtils.str2Date(startDateStr, DateUtils.SDF_SECOND);
    	Date endDate = DateUtils.str2Date(endDateStr, DateUtils.SDF_SECOND);
    	TempStatisticsDTO statistics = weatherStatisticsService.getTempMonthStatisticsDTO(location, startDate, endDate);
    	Map<String,Object> resultMap = new HashMap<>();
    	resultMap.put("maxTemp", statistics.getMaxTemp());
    	resultMap.put("minTemp", statistics.getMinTemp());
    	resultMap.put("avgTemp", statistics.getAvgTemp());
    	resultMap.put("monthTempMap", statistics.getMonthTempMap());
    	content.add(resultMap);
    }
}

