package com.zhiwei.chendong;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.zw.entity.ExcelFile;

/**
 * 文件定期清除线程
 * 
 * @author admin
 *
 */
@Component
public class FileThread extends Thread {

	private static final Log log = LogFactory.getLog(FileThread.class);
	private static MongoForChenDong m = new MongoForChenDong();

	public FileThread() {
		this.start();
	}

	@Override
	public void run() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		while (true) {
			log.info("执行读取文本地址");
			Calendar c = Calendar.getInstance();
			if (c.get(Calendar.HOUR_OF_DAY) == 1) {
				c.add(Calendar.DATE, -7);
				List<String> removeList = new ArrayList<String>();
				List<ExcelFile> list = m.findFile();
				for (ExcelFile ex : list) {
					try {
						if (sdf.parse(ex.getDate()).before(c.getTime())) {
							removeList.add(ex.getId());
							ExcelReadAndWrite.delete(ex.getUrl());
						}
					} catch (ParseException e) {
						log.info("时间格式转换出错{FileThread.39}");
					}
				}
				for (String url : removeList) {
					m.delete(url);
				}
				try {
					Thread.sleep(1000 * 3600);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1000 * 3500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
