package com.yeejoin.amos.boot.module.ugp.api.quartz;

import com.yeejoin.amos.feign.systemctl.Systemctl;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Stream;

@Component
@EnableScheduling
public class CylinderSchedulerJob {

    /**
     * 每天3点-删除图片
     */
//    @Scheduled(cron = "${cylinder-early-warning-cron:0 0 19 * * ?}")
//    public void clean() {
//        dayReport(new File("/home/yeejoin/ugp/minio-data/upload/common/ugp/qrcode"));
//    }


//    public static void dayReport(File file) {
////        file.isAbsolute();
//        if (!ValidationUtil.isEmpty(file.listFiles())) {
//            for (File f : file.listFiles()) {
//                //实现递归
//                if (f.isDirectory()) {
//                    dayReport(f);
//                }
//                //清理文件
//                f.delete();
//                System.out.println(f.getName() + "已清理!!!");
//            }
//        }
//    }
    @Scheduled(cron = "${cylinder-early-warning-cron:0 0 12 * * ?}")
    public void cleanFile() throws IOException {
        deleteFile("/home/yeejoin/ugp/minio-data/upload/common/ugp/ceshi");
    }

    public static void deleteFile(String destDirPath) throws IOException {
        System.out.println("开始删除文件 destDirPath:{}");
        Process p;
        InputStreamReader inputStreamReader = null;
        BufferedReader br = null;
        String co = "rm -rf " + destDirPath;
        try {
            p = Runtime.getRuntime().exec(co);
            inputStreamReader = new InputStreamReader(p.getInputStream());
            br = new BufferedReader(inputStreamReader);
            System.out.println("执行删除任务完毕 删除命令:{} 时间:{}");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            br.close();
        }
    }
//    public static void main(String[] args) {
//
//        dayReport(new File(new File("/home/yeejoin/ugp/minio-data/upload/common/ugp/qrcode").getAbsolutePath()));
//    }
}

