xiangpei
2025-02-18 450b41507e3e213cde2479204d9aa29a2fccbeb8
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/sync/deleteSync.java
@@ -1,10 +1,18 @@
package org.dromara.system.sync;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.oss.core.OssClient;
import org.dromara.common.oss.factory.OssFactory;
import org.dromara.system.domain.SysOss;
import org.dromara.system.domain.properties.Boundary;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.mapper.SysOssMapper;
import org.dromara.system.service.ISysOssService;
import org.dromara.system.uitil.HttpUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@@ -12,45 +20,50 @@
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
@Slf4j
@Component
public class deleteSync {
    private final SysOssMapper baseMapper;
    private final ISysOssService ossService;
    private final SysOssMapper ossMapper;
    private final Boundary boundary;
    public deleteSync(SysOssMapper baseMapper, Boundary boundary) {
        this.baseMapper = baseMapper;
    public deleteSync(ISysOssService ossService, SysOssMapper ossMapper, Boundary boundary) {
        this.ossService = ossService;
        this.ossMapper = ossMapper;
        this.boundary = boundary;
    }
//    @Scheduled(cron = "0 */10 * * * ?")
//    public void remove() throws IOException {
//        log.error("同步删除......");
//        List<SysOss> list = new LambdaQueryChainWrapper<>(baseMapper)
//            .le(SysOss::getCreateTime, LocalDateTime.now().minusDays(1L))
//            .list();
//        if (list.size() == 0)
//            return;
//
//        for (SysOss vo : list) {
//            OssClient storage = OssFactory.instance(vo.getService());
//            storage.delete(vo.getUrl());
//            HttpResponse httpResponse = HttpUtils.sendDeleteRequest(this.boundary.getVideo() + "/resource/synchronization/delete",
//                String.valueOf(vo.getOssId()));
//            if (httpResponse.getStatusLine().getStatusCode() == 200) {
//                baseMapper.deleteId(vo.getOssId());
//            }
//        }
//    }
    @Scheduled(cron = "0 */10 * * * ?")
    public void remove() throws IOException {
        System.out.println("同步删除......");
        List<Long> ids = baseMapper.getWillDeleted(LocalDateTime.now().minusDays(1));
        System.out.println("self delete......");
        List<Long> ids = ossMapper.getWillDeleted(LocalDateTime.now().minusDays(1));
        if (ids.size() == 0) {
            return;
        }
        for (Long ossId : ids) {
            HttpUtils.sendDeleteRequest(boundary.getVideo() + "/resource/synchronization/delete",
                String.valueOf(ossId));
        }
        ossService.deleteByIds(ids);
    }
    /**
     * 每小时删除30天之前的文件信息,确保上面的定时任务不会因为停电关机等原因导致文件未被删除
     *
     * @throws IOException
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void removeOssTable() throws IOException {
        System.out.println("每小时定时删除超过30天的数据库文件信息......");
        List<Long> ids = baseMapper.getWillDeleted(LocalDateTime.now().minusDays(30));
        if (ids.size() == 0) {
            return;
        }
        baseMapper.deleteBatchIds(ids);
    }
}