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

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
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 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.DayStaticRecord;
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.service.DayStaticService;

/**
 * Description: 查询城市某一时间段的日天气统计信息
 */
@Service("GetCityWeather")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GetCityWeather extends BaseBusinessService<Map> implements BusinessService {
	
	private static final String SDF_SECOND = "yyyy-MM-dd HH:mm:ss";
	
	@Resource
	private DayStaticService dayStaticService;

    @Override
    public void handle(Map dto, List content) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(SDF_SECOND);

		String cityCode = (String) dto.get("cityCode");
		String cityName = (String) dto.get("cityName");
		
		if (StringUtils.isEmpty(cityCode) && StringUtils.isEmpty(cityName)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		String location = ConfigUtil.getHourCity(cityName, cityCode, null);
		if(null == location) {
			throw new Exception(Const.ErrorTag.cityUnsupport);
		}
		
		String timeFrom = (String)dto.get("timeFrom");
	    String timeTo = (String)dto.get("timeTo");
	    Date startDate = null;
	    if (StringUtils.isEmpty(timeFrom)) {
	      startDate = dateFormat.parse("2011-01-01 00:00:00");
	    } else {
	      startDate = dateFormat.parse(timeFrom);
	    }
        
        // 默认查询未来一个月的日期
        Date endDate;
        if (StringUtils.isEmpty(timeTo)) {
            endDate = DateUtils.getDayOff(DateUtils.getStartTimeOfDay(new Date()), 30);
        } else {
            endDate = dateFormat.parse(timeTo);
        }
        List<String> weatherTypeList = (List<String>) dto.get("weatherType");
        if(CollectionUtils.isEmpty(weatherTypeList)) {
        	weatherTypeList = new ArrayList<>();
        	weatherTypeList.add("maxTemp");
        	weatherTypeList.add("minTemp");
        }
        
        List<DayStaticRecord> resultList = new ArrayList<>();
        List<DayStaticRecord> histList = dayStaticService.query(location, startDate, endDate);
        if(null != histList) {
        	resultList.addAll(histList);
        }
        
        Date today = DateUtils.getShortDate(new Date(), 3);
        if(today.before(endDate)) {
        	Long daySpan = (endDate.getTime() - today.getTime()) / (24 * 60 * 60 * 1000) + 1;
        	if(daySpan > 15) {
        		daySpan = 15L;
        	}
        	List<Map<String, Object>> preDataList = WServiceUtil.getDayPredict(location, 0, daySpan.intValue());
        	for(Map<String, Object> map : preDataList) {
        		DayStaticRecord preDayStatic = new DayStaticRecord();
        		Date dateTime = DateUtils.str2Date(map.get("date")+"", DateUtils.SDF_DAY);
        		if(dateTime.before(endDate)) {
        			preDayStatic.setDayTime(DateUtils.str2Date(map.get("date")+"", DateUtils.SDF_DAY));
        			preDayStatic.setMaxTemp(Double.parseDouble(map.get("high")+""));
        			preDayStatic.setMinTemp(Double.parseDouble(map.get("low")+""));
        			resultList.add(preDayStatic);
        		}
        	}
        }
        
        if(null != histList) {
        	for (DayStaticRecord dayStatic : resultList) {
        		Map<String, Object> resultMap = new HashMap<>();
        		resultMap.put("weatherDate", dayStatic.getDayTime());
        		for (String weatherType : weatherTypeList) {
        			Field field = dayStatic.getClass().getDeclaredField(weatherType);
        			// 设置对象的访问权限，保证对private的属性的访问
        			field.setAccessible(true);
        			resultMap.put(weatherType, field.get(dayStatic));
        		}
        		content.add(resultMap);
        	}
        }
    }
}

