package com.persagy.functions.weather.thread;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.persagy.ems.pojo.wz.DayStaticMoreRecord;
import com.persagy.ems.pojo.wz.DayStaticRecord;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.DayStaticService;

public class DayStaticThread implements Runnable {
	private static final Logger log = Logger.getLogger(DayStaticThread.class);
	private static boolean firstRun = true;
	private DayStaticService dayStaticService;
	private static Set<String> dayStaticCitySet = new HashSet<String>();
	private static Set<String> doneSet = new HashSet<String>();
	private static final String tname = "hourToDay-static";
	
	public DayStaticThread(DayStaticService dayStaticService) {
		this.dayStaticService = dayStaticService;
	}

	private void updateStaticCity() {
		Set<String> hourCitySet = new HashSet<>(ConfigUtil.getHourCitySet());
		if (!hourCitySet.isEmpty()) {
			int addNum = 0;
			for (String cityId : hourCitySet) {
				if (!dayStaticCitySet.contains(cityId)) {
					addNum++;
				}
				dayStaticCitySet.add(cityId);
			}
			if (addNum > 0) {
				log.info("day static city add " + addNum);
			}
		}
	}

	@Override
	public void run() {

		log.info(tname + " 线程启动...");
		while (true) {
			updateStaticCity();

			Set<String> haveStaticCity = new HashSet<>();
			Date todayStart = null, yesterDay = null;
			try {
				todayStart = DateUtils.getShortDate(new Date(), 3);
				yesterDay = DateUtils.getDayOff(todayStart, -1);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			if (firstRun) {
				List<DayStaticRecord> list = null;
				List<DayStaticMoreRecord> listMore = null;
				try {
					list = this.dayStaticService.query(null, yesterDay, todayStart);
					listMore = this.dayStaticService.queryMore(null, yesterDay, todayStart);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (list != null) {
					for (DayStaticRecord obj : list) {
						haveStaticCity.add(obj.getCityId());
					}
				}
				if (listMore != null) {
					for (DayStaticMoreRecord obj : listMore) {
						haveStaticCity.add(obj.getCityId());
					}
				}
			}

			int updateNum = 0;
			long startTime = System.currentTimeMillis();

			for (String cityId : dayStaticCitySet) {
				if (!doneSet.contains(cityId)) {
					boolean success = false;
					try {
						if (haveStaticCity.contains(cityId)) {
							this.dayStaticService.restaticHourToDay(cityId, yesterDay);
							updateNum++;
						} else {
							this.dayStaticService.staticHourToDay(cityId, yesterDay);
						}
						success = true;
					} catch (Exception localException1) {
					}
					if (success) {
						doneSet.add(cityId);
					}
				}
			}

			long endTime = System.currentTimeMillis();

			log.info(tname + " executed. total-success-update:" + dayStaticCitySet.size() + "-" + doneSet.size()
					+ "-" + updateNum + ", timeCost: " + (endTime - startTime) / 1000L + "s");

			long sleepSeconds = Const.AboutTime.min15Minlliseconds;
			Date tomorrowStart = DateUtils.getDayOff(todayStart, 1);

			if (doneSet.size() == dayStaticCitySet.size()) {
				doneSet.clear();
				Date tomorrowStartTime = DateUtils.getMinuteOff(tomorrowStart, 45);
				sleepSeconds = tomorrowStartTime.getTime() - new Date().getTime();
				sleepSeconds = sleepSeconds < Const.AboutTime.minMinlliseconds ? Const.AboutTime.minMinlliseconds : sleepSeconds;
			} else {
				Date nowTime = new Date();
				long timeSpan = tomorrowStart.getTime() - nowTime.getTime();
				if (timeSpan < Const.AboutTime.min15Minlliseconds) {
					sleepSeconds = timeSpan;
					doneSet.clear();
				} else {
					sleepSeconds = Const.AboutTime.min15Minlliseconds;
				}
			}
			log.error(tname + " sleep " + sleepSeconds / Const.AboutTime.minMinlliseconds + "min...");
			try {
				Thread.sleep(sleepSeconds);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
