/** 
 * @Title: ssa.java 
 * @Package com.zhiweidata.bjfzxWebapp.timerTask 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午10:51:10 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.timerTask;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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.listener.MediaBaiDuSerach;
import com.zhiweidata.bjfzxWebapp.mongoDao.MediaAndWechatEventDAO;
import com.zhiweidata.bjfzxWebapp.service.KeysWordSerivce;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/** 
 * @ClassName: ssa 
 * @Description: TODO(网媒爬虫定时器) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午10:51:10  
 */
public class MediaBaiduSearchTimerTask extends TimerTask
{
	private static Logger log = LoggerFactory.getLogger(MediaBaiduSearchTimerTask.class);
	private MediaBaiDuSerach mediaBaiduSeach;
	private MediaAndWechatEventDAO mediaDAO;
	private KeysWordSerivce keysWordSerivce;
	
	private static int crawlerCount = 0;
    private static Long startTime = System.currentTimeMillis(); //此次run的开始时间 
    private static Long ex_startTime = System.currentTimeMillis()-1*24*60*60*1000; //上次run的开始时间,假定第一次运行的上一次时间是一天前
    
    /**
     * @class MediaBaiduSearchTimerTask
     * 百度网媒爬虫timerTask
     * @param mediaBaiduSeach 网媒爬虫
     * @param mediaDAO 网媒Dao
     * @param keywordDao 关键字dao
     */
    public MediaBaiduSearchTimerTask(MediaBaiDuSerach mediaBaiduSeach, MediaAndWechatEventDAO mediaDAO,
    		KeysWordSerivce keysWordSerivce) {
		super();
		this.mediaBaiduSeach = mediaBaiduSeach;
		this.mediaDAO = mediaDAO;
		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)
		{
			log.debug("网媒关键字【{}】采集",keyword.getKeyword());
			try
			{	
				List<MediaAndWechatEvent> medias = new ArrayList<MediaAndWechatEvent>();
				//百度时间参数 以秒计算
				//传入的keyword作为关键字所属区分
				medias = mediaBaiduSeach.mediaKeywordSearchByBaiDu(ex_startTime/1000, 
						startTime/1000, keyword,keywords);
				
				show.put(keyword.getKeyword(), medias.size());
				
				mediaDAO.add(medias);
			}
			catch (UnsupportedEncodingException e)
			{
				log.error("关键字【{}】获取出错：【{}】",keyword.getKeyword(),e.getMessage());
			}
			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);
    }
}