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

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.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("DayForecast")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DayForecast extends BaseBusinessService<Map> implements BusinessService {
	@Resource
	private DayHisRecordService dayHisRecordService;
	
	@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");
	    String startDay = (String)dto.get("startDay");
	    String endDay =  (String)dto.get("endDay");
	    if ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode)) && (StringUtils.isEmpty(cityName))) {
	      throw new Exception(Const.ErrorTag.paramMis);
	    }
	    
	    String location = ConfigUtil.getLocationStrict(cityName, cityCode, projectId);
	    if(null == location) {
	    	throw new Exception(Const.ErrorTag.cityNotFound);
	    }
	    
	    Date today = DateUtils.getShortDate(new Date(), 3);
	    Date day15 = DateUtils.getDayOff(today, 15);
	    
	    Date dateStart = today, dateEnd = day15;
	    if(null != startDay) {
	    	dateStart = DateUtils.str2Date(startDay, DateUtils.SDF_DAY);
	    }else {
	    	startDay = DateUtils.date2Str(today, DateUtils.SDF_DAY);
	    }
	    
	    if(null != endDay) {
	    	dateEnd = DateUtils.str2Date(endDay, DateUtils.SDF_DAY);
	    }else {
	    	endDay = DateUtils.date2Str(day15, DateUtils.SDF_DAY);
	    }
	    
	    List<DayPredictRecord> list = dayHisRecordService.queryPredictData(location, today, day15);
	    if(null != list && list.size() == 15) {
	    	Date temp = null;
	    	for(DayPredictRecord item : list) {
	    		temp = item.getDayTime();
	    		if(temp == null || temp.before(dateStart) || !temp.before(dateEnd)) {
	    			continue;
	    		}
	    		Map<String, Object> resultMap = new HashMap<>();
    			resultMap.put("date", DateUtils.date2Str(temp, DateUtils.SDF_DAY));
    			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("humidity", item.getHumidity());
    			resultMap.put("rainfall", item.getRainfall());
    			resultMap.put("windDirection", item.getWind_direction());
    			resultMap.put("windDirectionDegree", item.getWind_direction_degree());
    			resultMap.put("windScale", item.getWind_scale());
    			resultMap.put("windSpeed", 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)) {
	    		String dayTmp = null;
	    		for (DayPredictDTO item : predicDataList) {
	    			dayTmp = item.getDate();
	    			if(dayTmp == null || dayTmp.compareTo(startDay) < 0 || dayTmp.compareTo(endDay) >=0) {
	    				continue;
	    			}
	    			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("humidity", item.getHumidity());
	    			resultMap.put("rainfall", item.getRainfall());
	    			resultMap.put("windDirection", item.getWind_direction());
	    			resultMap.put("windDirectionDegree", item.getWind_direction_degree());
	    			resultMap.put("windScale", item.getWind_scale());
	    			resultMap.put("windSpeed", item.getWind_speed());
	    			resultMap.put("minTemperature", item.getLow());
	    			resultMap.put("maxTemperature", item.getHigh());
	    			content.add(resultMap);
	    		}
	    		
	    		dayHisRecordService.coverPredictRecordUseDTO(location, predicDataList);
	    	}
	    }
	}
}
