package com.nms.server.thread.detectDataHandler;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.LinkedBlockingDeque;

import org.apache.log4j.Logger;

import com.nms.server.bean.DetectInfo;
import com.nms.server.common.Common;

public class DataInsertManagerThread implements Runnable{
	private static final Logger logger = Logger.getLogger(DataInsertManagerThread.class);
	
	@Override
	@SuppressWarnings("rawtypes")
	public void run() {
		Thread.currentThread().setName("DataInsertManagerThread");
		try {
			//遍历 监测数据等待入库 table
			Hashtable<String, LinkedBlockingDeque> hb = Common.DETECT_QUEUE;
			Set<Entry<String, LinkedBlockingDeque>> es = hb.entrySet();
			Iterator<Entry<String, LinkedBlockingDeque>> ite = es.iterator();
			while(ite.hasNext() && ! Common.isStop() && ! Thread.currentThread().isInterrupted() ){
				Entry<String, LinkedBlockingDeque> next = ite.next();
				String key = next.getKey();
				if(DetectInfo.INFO_KEY.equalsIgnoreCase(key)){
					//info 表的数据此线程不做解析
					continue;
				}
				LinkedBlockingDeque<Map<String,String>> data = next.getValue();
				List<Map<String,String>> list = new LinkedList<>();
				int total = data.drainTo(list);
				if(total > 0){
					Common.dataResoveService.submit(new DetectDetailInsertThread(key, list));
					logger.info(key +"->" + total);
				}else{
					logger.info(key + " 没有监测详细数据");
				}
			}
		} catch (Exception e) {
			logger.error("DataInsertManagerThread error",e);
		}
	}

}
