package com.persagy.functions.weather.business.v2021;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.construct.RequestProcessor;
import com.persagy.framework.dto.DataQueryParam;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.WServiceUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.HourRecordService;

/**
 * 实时数据
 */
@Service("present")
public class QueryPresent implements RequestProcessor {
	@Resource
	private HourRecordService hourDataService;

	@Override
	public Object handle(DataQueryParam param) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		String location = ConfigUtil.getHourCity(param.cityName, param.cityCode, param.projectId);
		
		if(null != location) {
			HourRecordData obj = hourDataService.getPresent(location);
			if(null != obj) {
				resultMap.put("no2", obj.getNo2());
				resultMap.put("pm25", obj.getPm25());
				resultMap.put("o3", obj.getO3());
				resultMap.put("so2", obj.getSo2());
				resultMap.put("aqi", obj.getAqi());
				resultMap.put("pm10", obj.getPm10());
				resultMap.put("primaryPollutant", obj.getPrimary_pollutant());
				resultMap.put("co", obj.getCo());
				resultMap.put("quality", obj.getQuality());
				
				resultMap.put("code", obj.getCode());
				resultMap.put("visibility", obj.getVisibility());
				resultMap.put("windDirection", obj.getWind_direction());
				resultMap.put("pressure", obj.getPressure());
				resultMap.put("clouds", obj.getClouds());
				resultMap.put("feelsLike", obj.getFeels_like());
				resultMap.put("windScale", obj.getWind_scale());
				resultMap.put("temperature", obj.getTemperature());
				resultMap.put("humidity", obj.getHumidity());
				resultMap.put("windDirectionDegree", obj.getWind_direction_degree());
				resultMap.put("windSpeed", obj.getWind_speed());
				resultMap.put("text", obj.getText());
			}
		}
		
		if(resultMap.isEmpty()) {
			// 若查不到最新的小时数据，则直接访问第三方实时天气数据API
			
			location = ConfigUtil.getLocation(param.cityName, param.cityCode, param.projectId);
			if(null != location) {
				try {
					Map<String, Object> dataMap = WServiceUtil.getNowAir(location);
					if (!dataMap.isEmpty()) {
						resultMap.put("no2", dataMap.get("no2"));
						resultMap.put("pm25", dataMap.get("pm25"));
						resultMap.put("o3", dataMap.get("o3"));
						resultMap.put("so2", dataMap.get("so2"));
						resultMap.put("aqi", dataMap.get("aqi"));
						resultMap.put("pm10", dataMap.get("pm10"));
						resultMap.put("primaryPollutant", dataMap.get("primary_pollutant"));
						resultMap.put("co", dataMap.get("co"));
						resultMap.put("quality", dataMap.get("quality"));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				try {
					Map<String, Object> dataMap = WServiceUtil.getNowWeather(location);
					if (!dataMap.isEmpty()) {
						resultMap.put("text", dataMap.get("text"));
						resultMap.put("code", dataMap.get("code"));
						resultMap.put("temperature", dataMap.get("temperature"));
						resultMap.put("feelsLike", dataMap.get("feels_like"));
						resultMap.put("pressure", dataMap.get("pressure"));
						resultMap.put("humidity", dataMap.get("humidity"));
						resultMap.put("visibility", dataMap.get("visibility"));
						resultMap.put("windDirection", dataMap.get("wind_direction"));
						resultMap.put("windDirectionDegree", dataMap.get("wind_direction_degree"));
						resultMap.put("windSpeed", dataMap.get("wind_speed"));
						resultMap.put("windScale", dataMap.get("wind_scale"));
						resultMap.put("clouds", dataMap.get("clouds"));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else {
				throw new Exception(Const.ErrorTag.cityNotFound);
			}
		}
		
		return resultMap;
	}

}