SceneOssDataTask.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.fdkankan.openApi.task;
  2. /**
  3. * @author Xiewj
  4. * @date 2022年11月21日11:25:30
  5. */
  6. import cn.hutool.core.date.DateUnit;
  7. import cn.hutool.core.date.DateUtil;
  8. import cn.hutool.core.util.StrUtil;
  9. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  10. import com.fdkankan.openApi.entity.system.SceneDataDownloadEntity;
  11. import com.fdkankan.openApi.service.system.SceneDataDownloadService;
  12. import com.fdkankan.openApi.util.DateUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import org.springframework.stereotype.Component;
  17. import java.io.IOException;
  18. import java.util.List;
  19. /**
  20. * 清理场景下载OSS任务
  21. *
  22. * @author
  23. */
  24. @Component("SceneOssDataTask")
  25. @Slf4j
  26. public class SceneOssDataTask {
  27. @Autowired
  28. SceneDataDownloadService sceneDataDownloadService;
  29. @Autowired
  30. private FYunFileServiceInterface fYunFileService;
  31. @Scheduled(cron = "0 0 1 * * ?")
  32. public void handel() {
  33. // 每天凌晨1点执行的任务逻辑
  34. log.info("开始执行清理下载任务");
  35. int count=0;
  36. List<SceneDataDownloadEntity> sceneDataDownloadEntityList = sceneDataDownloadService.findByOssDeleteIsNull();
  37. int totalCount=sceneDataDownloadEntityList.size();
  38. for (SceneDataDownloadEntity sceneDataDownloadEntity : sceneDataDownloadEntityList) {
  39. long betweenHour = DateUtils.getDatePoorUnit(DateUtil.date(), sceneDataDownloadEntity.getCreateTime(), DateUnit.HOUR);
  40. if (StrUtil.isNotEmpty(sceneDataDownloadEntity.getOssKey())&&
  41. StrUtil.isNotEmpty(sceneDataDownloadEntity.getBucket())&&
  42. betweenHour>(7*24)
  43. ){
  44. String ossKey=sceneDataDownloadEntity.getOssKey();
  45. String bucket=sceneDataDownloadEntity.getBucket();
  46. if (fYunFileService.fileExist(bucket,ossKey)) {
  47. log.info("ossKey删除的bucket--{},地址是{}",bucket,ossKey);
  48. try {
  49. fYunFileService.deleteFile(bucket,ossKey);
  50. } catch (IOException e) {
  51. throw new RuntimeException(e);
  52. }
  53. sceneDataDownloadEntity.setOssDelete(DateUtil.date());
  54. boolean b = sceneDataDownloadService.updateById(sceneDataDownloadEntity);
  55. log.info("数据库修改状态为::{}",b);
  56. }
  57. }
  58. }
  59. log.info("执行清理下载任务结束,共计::{},实际处理::{}",totalCount,count);
  60. }
  61. }