/** 
 * @Title: sasds.java 
 * @Package com.zhiweidata.bjfzxWebapp.timerTask 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午11:06:43 
 * @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.WeiboBusiness;
import com.zhiweidata.bjfzxWebapp.listener.WeiboEvetSearchByBusiness;
import com.zhiweidata.bjfzxWebapp.mongoDao.WeiboBusinessDao;
import com.zhiweidata.bjfzxWebapp.service.KeysWordSerivce;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/** 
 * @ClassName: WeiboEventSearchByBusinessTimerTask 
 * @Description: TODO(微博商业接口定时器) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午11:06:43  
 */
public class WeiboEventSearchByBusinessTimerTask extends TimerTask
{
	
	private static Logger log = LoggerFactory.getLogger(WeiboEventSearchByBusinessTimerTask.class);
	private WeiboEvetSearchByBusiness weiboSeach;
	private WeiboBusinessDao weiboDao;
	private KeysWordSerivce keysWordSerivce;
	
	private static int crawlerCount = 0;
    private static Long startTime = System.currentTimeMillis(); //此次run的开始时间 
    private static Long ex_startTime = System.currentTimeMillis()-2*24*60*60*1000; //上次run的开始时间,假定第一次运行的上一次时间是一天前
    
    /**
     * @class WeiboEventSearchByBusinessTimerTask
     * 微博爬虫timerTask
     * @param weiboSeach 微博爬虫
     * @param weiboDao 微博结果dao weiboBusiness
     * @param keywordDao 关键字Dao
     */
	public WeiboEventSearchByBusinessTimerTask(WeiboEvetSearchByBusiness weiboSeach, WeiboBusinessDao weiboDao,
			KeysWordSerivce keysWordSerivce) {
		super();
		this.weiboSeach = weiboSeach;
		this.weiboDao = weiboDao;
		this.keysWordSerivce = keysWordSerivce;
	}


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

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

}

