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

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.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.DayPredictRecord;
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;
import com.persagy.functions.weather.dto.DayPredictDTO;
import com.persagy.functions.weather.service.DayHisRecordService;

@Service("WeatherForecast")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class WeatherForecastService extends BaseBusinessService<Map> implements BusinessService {
	@Resource
	private DayHisRecordService dayHisRecordService;

	@Override
	public void handle(Map dto, List content) throws Exception {
		String name = (String)dto.get("name");
	    String projectId = (String)dto.get("projectId");
	    String cityCode = (String)dto.get("cityCode");
	    if ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode)) && (StringUtils.isEmpty(name))) {
	      throw new Exception(Const.ErrorTag.paramMis);
	    }
	    String location = ConfigUtil.getLocation(name, cityCode, projectId);
	    
	    List<DayPredictRecord> list = dayHisRecordService.queryPredictData(location, 15);
	    if(null != list && !list.isEmpty()) {
	    	for(DayPredictRecord item : list) {
	    		Map<String, Object> resultMap = new HashMap<>();
    			resultMap.put("date", DateUtils.date2Str(item.getDayTime(), "yyyy-MM-dd"));
    			resultMap.put("textDay", item.getText_day());
    			resultMap.put("codeDay", item.getCode_day());
    			resultMap.put("textNight", item.getText_night());
    			resultMap.put("codeNight", item.getCode_night());
    			resultMap.put("precip", item.getPrecip());
    			resultMap.put("windDirection", item.getWind_direction());
    			resultMap.put("windDirectionDegree", item.getWind_direction_degree());
    			resultMap.put("windScale", item.getWind_scale());
    			resultMap.put("windMaxSpeed", item.getWind_speed());
    			resultMap.put("windMinSpeed", item.getWind_speed());
    			resultMap.put("minTemperature", item.getLow());
    			resultMap.put("maxTemperature", item.getHigh());
    			content.add(resultMap);
	    	}
	    }else {
	    	List<DayPredictDTO> predicDataList = WServiceUtil.getDayPredictObj(location, 0, 15);
	    	
	    	if (!CollectionUtils.isEmpty(predicDataList)) {
	    		for (DayPredictDTO item : predicDataList) {
	    			Map<String, Object> resultMap = new HashMap<>();
	    			resultMap.put("date", item.getDate());
	    			resultMap.put("textDay", item.getText_day());
	    			resultMap.put("codeDay", item.getCode_day());
	    			resultMap.put("textNight", item.getText_night());
	    			resultMap.put("codeNight", item.getCode_night());
	    			resultMap.put("precip", item.getPrecip());
	    			resultMap.put("windDirection", item.getWind_direction());
	    			resultMap.put("windDirectionDegree", item.getWind_direction_degree());
	    			resultMap.put("windScale", item.getWind_scale());
	    			resultMap.put("windMaxSpeed", item.getWind_speed());
	    			resultMap.put("windMinSpeed", item.getWind_speed());
	    			resultMap.put("minTemperature", item.getLow());
	    			resultMap.put("maxTemperature", item.getHigh());
	    			content.add(resultMap);
	    		}
	    		
	    		dayHisRecordService.coverPredictRecordUseDTO(location, predicDataList);
	    	}
	    }
	}
}