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;

@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();
	}
	
	public void run(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		while(true){
			System.out.println("执行");
			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();
			}
//			try {
//				Thread.sleep(1000*60*30);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
		}
	}
}
