/** 
 * @Title: ESSearchTimer.java 
 * @Package com.zhiweidata.bjfzxWebapp.timerTask 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月7日 下午4:49:42 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.timerTask;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiweidata.bjfzxWebapp.entity.Keyword;
import com.zhiweidata.bjfzxWebapp.entity.MediaAndWechatEvent;
import com.zhiweidata.bjfzxWebapp.entity.WeiboBusiness;
import com.zhiweidata.bjfzxWebapp.mongoDao.MediaAndWechatEventDAO;
import com.zhiweidata.bjfzxWebapp.mongoDao.WeiboBusinessDao;
import com.zhiweidata.bjfzxWebapp.service.ESThreeDataService;
import com.zhiweidata.bjfzxWebapp.service.KeysWordSerivce;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/** 
 * @ClassName: ESSearchTimer 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author xuyimeng 
 * @date 2018年2月7日 下午4:49:42  
 */
public class ESSearchTimer extends TimerTask{
	private static Logger log = LoggerFactory.getLogger(ESSearchTimer.class);
	private KeysWordSerivce keysWordSerivce;
	private ESThreeDataService esThreeDataService;
	private MediaAndWechatEventDAO mediaDAO;
	private WeiboBusinessDao weiboDao;
	
	private static Long startTime = System.currentTimeMillis(); //此次run的开始时间 
	private static Long ex_startTime = System.currentTimeMillis()-2*24*60*60*1000; //上次run的开始时间,假定第一次运行的上一次时间是一天前
	private static Long crawlerCount = 0L;
	
	public ESSearchTimer(KeysWordSerivce keysWordSerivce, ESThreeDataService esThreeDataService,
			MediaAndWechatEventDAO mediaDAO, WeiboBusinessDao weiboDao) {
		super();
		this.keysWordSerivce = keysWordSerivce;
		this.esThreeDataService = esThreeDataService;
		this.mediaDAO = mediaDAO;
		this.weiboDao = weiboDao;
	}

	@Override
	public void run() {
		log.info("ES库采集本轮开始采集：【{}】",TimeUtil.getCurrentDateStr());
		Map<String, Object> show = new HashMap<String, Object>();
		long s_ = System.currentTimeMillis();
		startTime = s_;
		List<Keyword> keywords = keysWordSerivce.findNoShieldList();
		for(Keyword keyword : keywords)
		{
			if (log.isDebugEnabled())
	        {
	            log.debug("ES关键字【{}】采集",keyword.getKeyword());
	        }
			try
			{	
				//传入的keyword作为关键字所属区分
				List<WeiboBusiness> weibolist = 
						esThreeDataService.findWeibo(new Date(ex_startTime), new Date(startTime), keyword);
				show.put(keyword.getKeyword(), weibolist.size());
				log.info("es查询到关键词【{}】微博数据量为【{}】",keyword.getKeyword(), weibolist.size());
				
				weiboDao.add(weibolist);
				weibolist = null;
				List<MediaAndWechatEvent> eventList = 
						esThreeDataService.findMediaAndWechat(new Date(ex_startTime), new Date(startTime), keyword);
				log.info("es查询到关键词【{}】网媒微博数据量为【{}】",keyword.getKeyword(), eventList.size());
				mediaDAO.add(eventList);
				
			}
			catch (Exception e)
			{
				log.error("ES关键字【{}】获取出错：【{}】",keyword.getKeyword(),e.getMessage());
			}			
		}
		
		long e_ = System.currentTimeMillis();
		log.info("ES采集本轮采集结束【{}】，用时【{}】秒",TimeUtil.formatTime(new Date(e_)),(e_ - s_) / 1000);        
        
		ex_startTime = s_;
		crawlerCount++;
		showDetai(show);

	}

	private void showDetai(Map<String, Object> show)
    {
        String showStr = "\n--------ES接口采集结果--------\n";
        for (String key : show.keySet())
        {
            showStr += key + "\t" + show.get(key).toString() + "个\n";
        }
        showStr +="--------ES接口采集结果--------";
        log.info(showStr);
    }
}
