package com.persagy.functions.weather.thread;

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

import org.apache.log4j.Logger;

import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.core.mvc.service.CoreService;
import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;

public class HourRepairThread implements Runnable {
	private static final Logger LOGGER = Logger.getLogger(HourRepairThread.class);
	private CoreService coreService;
	private String cityId;
	private Date fromDay;
	private Date toDay;
	
	public HourRepairThread(CoreService coreService, String cityId, Date fromDay, Date toDay) {
		super();
		this.coreService = coreService;
		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;
				HourRecordData queryObj = new HourRecordData();
				String splitTag = ConfigUtil.getComputeStr(cid);
				queryObj.setBuildingForContainer(splitTag);
				queryObj.setCityId(cid);
				queryObj.setSpecialOperation("hourTime", SpecialOperator.$gte, sdate);
				queryObj.setSpecialOperation("hourTime", SpecialOperator.$lt, tdate);
				try {
					hourDataList = coreService.query(queryObj);
				} catch (Exception e) {
					LOGGER.info("hourDataRepair->hourData query Error, City=" + cid + ", msg=" + e.getMessage());
					e.printStackTrace();
				}
				if(null != hourDataList && !hourDataList.isEmpty()) {
					List<HourRecordData> saveDataList = new ArrayList<>();
					for(HourRecordData obj : hourDataList) {
						String visib = obj.getVisibility();
						if(null != visib && visib.trim().length() > 0) {
							Double vis = Double.parseDouble(visib);
							if(vis.compareTo(35.0) > 0) {
								visib = (vis / 1000) + "";
								visib = visib.length() > 10 ? visib.substring(0, 6) : visib;
								obj.setVisibility(visib);
							}
						}
						obj.setBuildingForContainer(splitTag);
						saveDataList.add(obj);
					}
					
					try {
						coreService.remove(queryObj);
						coreService.save(saveDataList);
						LOGGER.info("hourDataRepair, cityNow:" + cindex + "/" + hourCites.size() + ", time:" + DateUtils.date2Str(sdate, "yyyy-MM-dd"));
					} catch (Exception e) {
						LOGGER.info("hourDataRepair Error, City=" + cid + ", msg=" + e.getMessage()); 
						e.printStackTrace();
					}
				}
				
				sdate = tdate;
				
			}
			
			cindex ++;
		}
	}
}
