123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package com.fdkankan.openApi.task;
- /**
- * @author Xiewj
- * @date 2022年11月21日11:25:30
- */
- import cn.hutool.core.date.DateUnit;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.openApi.entity.system.SceneDataDownloadEntity;
- import com.fdkankan.openApi.service.system.SceneDataDownloadService;
- import com.fdkankan.openApi.util.DateUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
- import java.util.List;
- /**
- * 清理场景下载OSS任务
- *
- * @author
- */
- @Component("SceneOssDataTask")
- @Slf4j
- public class SceneOssDataTask {
- @Autowired
- SceneDataDownloadService sceneDataDownloadService;
- @Autowired
- private FYunFileServiceInterface fYunFileService;
- @Scheduled(cron = "0 0 1 * * ?")
- public void handel() {
- // 每天凌晨1点执行的任务逻辑
- log.info("开始执行清理下载任务");
- int count=0;
- List<SceneDataDownloadEntity> sceneDataDownloadEntityList = sceneDataDownloadService.findByOssDeleteIsNull();
- int totalCount=sceneDataDownloadEntityList.size();
- for (SceneDataDownloadEntity sceneDataDownloadEntity : sceneDataDownloadEntityList) {
- long betweenHour = DateUtils.getDatePoorUnit(DateUtil.date(), sceneDataDownloadEntity.getCreateTime(), DateUnit.HOUR);
- if (StrUtil.isNotEmpty(sceneDataDownloadEntity.getOssKey())&&
- StrUtil.isNotEmpty(sceneDataDownloadEntity.getBucket())&&
- betweenHour>(7*24)
- ){
- String ossKey=sceneDataDownloadEntity.getOssKey();
- String bucket=sceneDataDownloadEntity.getBucket();
- if (fYunFileService.fileExist(bucket,ossKey)) {
- log.info("ossKey删除的bucket--{},地址是{}",bucket,ossKey);
- try {
- fYunFileService.deleteFile(bucket,ossKey);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- sceneDataDownloadEntity.setOssDelete(DateUtil.date());
- boolean b = sceneDataDownloadService.updateById(sceneDataDownloadEntity);
- log.info("数据库修改状态为::{}",b);
- }
- }
- }
- log.info("执行清理下载任务结束,共计::{},实际处理::{}",totalCount,count);
- }
- }
|