import com.dySweetFishPlugin.tool.lang.DateUtil
import org.apache.logging.log4j.Logger

import java.nio.file.Files
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.attribute.FileTime
import java.time.LocalDateTime

static def createRunnable(String appHome, Logger logger, long supplierCode) {
    return {
        logger.info("清理长期不访问文件开始...")

        //region 汇旺财支付二维码
        String directoryName1 = appHome + File.separator +
                "erpfiles" + File.separator +
                supplierCode + File.separator +
                "hsty"

        File directoryPath1 = new File(directoryName1)
        if (directoryPath1.exists()) {
            long deleteTime = DateUtil.localDateTimeToDate(LocalDateTime.now().minusDays(1)).getTime()
            directoryPath1.eachFile {
                //支付码给一天的时间
                BasicFileAttributes attr = Files.readAttributes(it.toPath(), BasicFileAttributes.class)
                FileTime lastAccessTime = attr.lastAccessTime()
                if (lastAccessTime.toMillis() < deleteTime) {
                    logger.info(it.absolutePath + "删除")
                    it.delete()
                }
            }
        }

        //endregion

        //region 卡联支付二维码
        String directoryName2 = appHome + File.separator +
                "erpfiles" + File.separator +
                supplierCode + File.separator +
                "kalian"

        File directoryPath2 = new File(directoryName2)
        if (directoryPath2.exists()) {
            long deleteTime = DateUtil.localDateTimeToDate(LocalDateTime.now().minusDays(1)).getTime()
            directoryPath2.eachFile {
                //支付码给一天的时间
                BasicFileAttributes attr = Files.readAttributes(it.toPath(), BasicFileAttributes.class)
                FileTime lastAccessTime = attr.lastAccessTime()
                if (lastAccessTime.toMillis() < deleteTime) {
                    logger.info(it.absolutePath + "删除")
                    it.delete()
                }
            }
        }
        //endregion

        //region 产品图片
        String directoryName3 = appHome + File.separator +
                "erpfiles" + File.separator +
                supplierCode + File.separator +
                "productImage"

        File directoryPath3 = new File(directoryName3)
        if (directoryPath3.exists()) {
            long deleteTime = DateUtil.localDateTimeToDate(LocalDateTime.now().minusDays(5)).getTime()
            directoryPath3.eachFile {
                BasicFileAttributes attr = Files.readAttributes(it.toPath(), BasicFileAttributes.class)
                FileTime lastAccessTime = attr.lastAccessTime()
                if (lastAccessTime.toMillis() < deleteTime) {
                    logger.info(it.absolutePath + "删除")
                    it.delete()
                }
            }
        }
        //endregion

        //region 内容详情图片
        String directoryName4 = appHome + File.separator +
                "erpfiles" + File.separator +
                supplierCode + File.separator +
                "sys_content_image"

        File directoryPath4 = new File(directoryName4)
        if (directoryPath4.exists()) {
            long deleteTime = DateUtil.localDateTimeToDate(LocalDateTime.now().minusDays(365)).getTime()
            directoryPath4.eachFile {
                BasicFileAttributes attr = Files.readAttributes(it.toPath(), BasicFileAttributes.class)
                FileTime lastAccessTime = attr.lastAccessTime()
                if (lastAccessTime.toMillis() < deleteTime) {
                    logger.info(it.absolutePath + "删除")
                    it.delete()
                }
            }
        }
        //endregion

        logger.info("清理长期不访问文件结束...")
    } as Runnable
}

//groovy最后一个表达式的值为返回
createRunnable(appHome, logger, supplierCode)
