package com.persagy.functions.weather.thread;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.ConstStatisticType;
import com.persagy.functions.weather.service.DayStaticService;
import com.persagy.functions.weather.service.HourRecordService;

public class HourToDayAvgDataComputeThread implements Runnable {
	private static final Logger LOGGER = Logger.getLogger(HourToDayAvgDataComputeThread.class);
	private HourRecordService hourDataService;
	private DayStaticService dayStaticService;
	private String cityId;
	private Date fromDay;
	private Date toDay;
	
	public HourToDayAvgDataComputeThread(HourRecordService hourDataService,
			DayStaticService dayStaticService, String cityId, Date fromDay, Date toDay) {
		super();
		this.hourDataService = hourDataService;
		this.dayStaticService = dayStaticService;
		this.cityId = cityId;
		this.fromDay = fromDay;
		this.toDay = toDay;
	}

	@Override
	public void run() {
		Set<String> hourCites = new HashSet<>();
		
		int skip = 0;
		if("ALL".equals(cityId)) {
			hourCites = ConfigUtil.getHourCitySet();
		}else if(cityId.startsWith("ALL")){
			hourCites = ConfigUtil.getHourCitySet();
			String skipStr = cityId.split("_")[1];
			skip = Integer.parseInt(skipStr);
		}else {
			hourCites.add(cityId);
		}
		
		int cindex = 1;
		for(String cid : hourCites) {
			
			if(cindex < skip) {
				continue;
			}
			
			Date sdate = fromDay;
			while(sdate.before(toDay)) {
				Date tdate = DateUtils.getDayOff(sdate, 50);
				if(tdate.after(toDay)) {
					tdate = toDay;
				}
				
				List<HourRecordData> hourDataList = null;
				try {
					hourDataList = hourDataService.query(cid, sdate, tdate);
				} catch (Exception e) {
					LOGGER.info("hourDataToDayAvgData hourDataService Error, cCity=" + cid + ", msg=" + e.getMessage());
					e.printStackTrace();
				}
				if(null != hourDataList && !hourDataList.isEmpty()) {
					Map<Date, List<HourRecordData>> dayDate_list = new HashMap<>();
					for(HourRecordData obj : hourDataList) {
						Date dayDate = null;
						try {
							dayDate = DateUtils.getShortDate(obj.getHourTime(), 3);
						} catch (Exception e) {
							e.printStackTrace();
						}
						if(dayDate_list.containsKey(dayDate)) {
							dayDate_list.get(dayDate).add(obj);
						}else {
							List<HourRecordData> list = new ArrayList<>();
							list.add(obj);
							dayDate_list.put(dayDate, list);
						}
					}
					
					
					try {
						dayStaticService.restaticHourToDay(cid, dayDate_list, ConstStatisticType.Avg);
						LOGGER.info("hourDataToDayAvgData, cityNow:" + cindex + "/" + hourCites.size() + ", time:" + DateUtils.date2Str(sdate, "yyyy-MM-dd"));
					} catch (Exception e) {
						LOGGER.info("hourDataToDayAvgData Error, cCity=" + cid + ", msg=" + e.getMessage()); 
						e.printStackTrace();
					}
				}
				
				sdate = tdate;
				
			}
			
			cindex ++;
		}
	}
}
