package com.nms.server.thread.file;

import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.util.FileUtils;

/**
 * XML解析线程
 * 
 * @author ZGGG3
 * 
 */
public class DetectFileReaderThread implements Runnable {
	Logger logger = Logger.getLogger(DetectFileReaderThread.class);
	private String dir;
	private String name; // 自定义Thread Name

	public DetectFileReaderThread(String name, String url) {
		this.dir = url;
		this.name = name;
	}


	public void run() {
		
		// 为当前线程命名，用与日志友好输出。
		Thread.currentThread().setName(name);
		
		try {
			logger.info("文件夹路径 :>" + dir);

			File driFile = new File(dir);
			
			// 获取目录下的.csv文件集合
			File[] files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".csv")); // 按文件名称 升序排序
			
			// 解析文件数为0时，终止该计划任务
			if(files==null || files.length==0){
				Common.cancelRunnableAtOnce(dir);
				return;
			}
			
			logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 "  + files.length + "个文件，解析开始");
			long l = Calendar.getInstance().getTimeInMillis();
			if (files != null && files.length > 0) {
				for (File file : files) {
					
					if(file.length()>0){
						try {
							FileInputStream fis = new FileInputStream(file);
							
							int fileLength = (int) file.length();		//大小
							int len = 0;
							byte[] buff = new byte[fileLength];
							if(fileLength>0){
								len = fis.read(buff);
							}
							if(buff.length==0){continue;}
							Common.addDeteData(buff);
							fis.close();
						} catch (Exception e) {
							FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName()));
							logger.error("Monitoring data file reading errors："+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e);
						}
					}
					file.delete();
				}
			}
			logger.debug(driFile.getAbsolutePath()+" 解析完成耗时：>"+(Calendar.getInstance().getTimeInMillis()-l));
			
			//清理文件
			files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".tp")); // 按文件名称 升序排序
			if(files!=null && files.length>0){
				for(File file : files){
					file.delete();
				}
			}
			logger.debug("临时文件清理完成");
		} catch (Exception e) {
			logger.error("",e);
		}
	}
/*
	public static void main(String[] args) {
		CommonDao dao = null;
		try {
			BoneCPPool.initPool();
			dao = new CommonDao();
			new InitServerThread().initCommonInfo(dao);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally{
			dao.close();
		}
		
		
		
		long l = Calendar.getInstance().getTimeInMillis();
		Future<?> future = Common.service.submit(new DetectFileReaderThread("解析 监测数据文件","D:\\NMS\\nmsdata\\dc_zip\\resove\\detect"));
		try {
			future.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Calendar.getInstance().getTimeInMillis()-l);
		future = Common.service.submit(new DetecDataResoveThread("解析 监测数据文件",Common.getDeteDataList()));
		try {
			future.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Common.chengeDeteDataFlag();
		future = Common.service.submit(new DetecDataResoveThread("解析 监测数据文件",Common.getDeteDataList()));
		try {
			future.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
}
