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.ems.pojo.wz.DisasterWarning;
import com.persagy.framework.util.BeanUtils;
import com.persagy.framework.util.DateUtils;
import com.persagy.framework.util.WServiceUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.dto.AlarmDTO;
import com.persagy.functions.weather.service.DisasterWarningService;

/**
 * 灾害预警数据记录
 */
public class DisasterWarningThread implements Runnable {
	private static final Logger log = Logger.getLogger(DisasterWarningThread.class);
	
	private DisasterWarningService service;
	
	private static final String tname = "disasterPredict";
	
	public DisasterWarningThread(DisasterWarningService service) {
		this.service = service;
	}
	

	@Override
	public void run() {

		log.info(tname+" 线程启动...");
		while(true) {
			Date dateNow = new Date();
			Date date48Before = DateUtils.getDayOff(dateNow, -2);
			Set<String> idTypeTime = new HashSet<>();
			try {
				List<DisasterWarning> todayList = this.service.query(null, null, null, date48Before, dateNow);
				if (todayList != null) {
					for (DisasterWarning item : todayList) {
						idTypeTime.add(item.getCityId() + item.getType() + item.getLevel() + item.getPubTime().getTime()
								+ item.getTitle());
					}
				}
			}catch (Exception e) {
				log.error("query today disasterWarning record error:"+e.getMessage());
			}
			
			List<AlarmDTO> alarmList = WServiceUtil.getAlarmObjs();
			List<DisasterWarning> saveList = new ArrayList<>();
			
			int repeatNum = 0;
			
			String cityId = null;
			String type = null;
			String level = null;
		    String title = null;
			Date pubTime = null;
			String description = "";
			for(AlarmDTO dto : alarmList) {
				cityId = dto.getId();
		        type = dto.getType();
		        level = dto.getLevel();
		        title = dto.getTitle();
				description = dto.getDescription();
				description = (description != null && description.length() > 1000) ? description.substring(0, 1000) : description;
				try {
					pubTime = DateUtils.t8DateStr2Date(dto.getPub_date());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				if(null == pubTime) {
					continue;
				}
				
				if(pubTime.before(date48Before) || idTypeTime.contains(cityId + type + level + pubTime.getTime() + title)) {
					repeatNum++;
				}else {
					DisasterWarning saveItem = new DisasterWarning();
					saveItem.setCityId(cityId);
					saveItem.setPubTime(pubTime);
					saveItem.setInsertTime(dateNow);
					saveItem.setDescription(description);
					dto.setPub_date(null);
					dto.setId(null);
					dto.setDescription(null);
					try {
						BeanUtils.copyBean(dto, saveItem);
						saveList.add(saveItem);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			
			long sleepSeconds = Const.AboutTime.min15Minlliseconds;
			if(!saveList.isEmpty()) {
				try {
					service.save(saveList);
					log.info("disasterWarning record saved total-success-repeat:" + alarmList.size() + "-" + saveList.size() + "-" + repeatNum + ". sleep 15min.");
				}catch (Exception e) {
					sleepSeconds = Const.AboutTime.minMinlliseconds;
					log.error("disasterWarning record batchSave error:" + e.getMessage() + ". sleep 1min");
				}
			}
			
			log.error(tname+" 线程睡眠15min...");
			try {
				Thread.sleep(sleepSeconds);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
