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

import java.util.ArrayList;
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 com.persagy.ems.pojo.wz.DayPredictRecord;
import com.persagy.framework.construct.RequestProcessor;
import com.persagy.framework.dto.DataQueryParam;
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.constant.Const.ErrorTag;
import com.persagy.functions.weather.dto.DayPredictDTO;
import com.persagy.functions.weather.service.DayHisRecordService;

/**
 * 15日(含今日)预报
 */
@Service("predictDay")
public class QueryPredictDay implements RequestProcessor {
	@Resource
	private DayHisRecordService dayHisRecordService;

	@Override
	public Object handle(DataQueryParam param) throws Exception {
		List<Map<String, Object>> resultList = new ArrayList<>();
		
		String location = ConfigUtil.getLocationStrict(param.cityName, param.cityCode, param.projectId);
	    if(null == location) {
	    	throw new Exception(Const.ErrorTag.cityNotFound);
	    }
	    
	    Date dateStart = DateUtils.getShortDate(new Date(), 3);
	    Date dateEnd = DateUtils.getDayOff(dateStart, 15);
		
	    List<DayPredictRecord> list = dayHisRecordService.queryPredictData(location, dateStart, dateEnd);
	    if(null != list && list.size() == 15) {
	    	Date temp = null;
	    	for(DayPredictRecord item : list) {
	    		temp = item.getDayTime();
	    		Map<String, Object> resultMap = new HashMap<>();
    			resultMap.put("time", temp.getTime());
    			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("temperatureLow", item.getLow());
    			resultMap.put("temperatureHigh", item.getHigh());
    			resultList.add(resultMap);
	    	}
	    }else {
	    	List<DayPredictDTO> predicDataList = WServiceUtil.getDayPredictObj(location, 0, 15);
	    	
	    	if (!predicDataList.isEmpty()) {
	    		String dayTmp = null;
	    		for (DayPredictDTO item : predicDataList) {
	    			dayTmp = item.getDate();
	    			
	    			Map<String, Object> resultMap = new HashMap<>();
	    			resultMap.put("time", DateUtils.str2Date(dayTmp, DateUtils.SDF_DAY).getTime());
	    			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("temperatureLow", item.getLow());
	    			resultMap.put("temperatureHigh", item.getHigh());
	    			resultList.add(resultMap);
	    		}
	    		
	    		try {
	    			dayHisRecordService.coverPredictRecordUseDTO(location, predicDataList);
	    		}catch (Exception e) {
					e.printStackTrace();
				}
	    	}else {
	    		throw new Exception(ErrorTag.service3rd);
	    	}
	    }
		return resultList;
	}
}