From 1ca5584d7e3200a9af65a099bd26d3593e2ba702 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期四, 07 五月 2026 14:36:08 +0800
Subject: [PATCH] 迁移pro

---
 src/main/java/com/ruoyi/basic/utils/FileUtil.java |  262 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 248 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/utils/FileUtil.java b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
index 4092f30..c57468a 100644
--- a/src/main/java/com/ruoyi/basic/utils/FileUtil.java
+++ b/src/main/java/com/ruoyi/basic/utils/FileUtil.java
@@ -1,7 +1,10 @@
 package com.ruoyi.basic.utils;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.basic.dto.StorageAttachmentDTO;
 import com.ruoyi.basic.dto.StorageAttachmentVO;
 import com.ruoyi.basic.dto.StorageBlobDTO;
 import com.ruoyi.basic.dto.StorageBlobVO;
@@ -52,9 +55,6 @@
      * @param storageBlobDTOS 鏂囦欢淇℃伅
      */
     public void saveStorageAttachment(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, List<StorageBlobDTO> storageBlobDTOS) {
-        if (CollectionUtils.isEmpty(storageBlobDTOS)) {
-            throw new RuntimeException("鏂囦欢淇℃伅涓嶈兘涓虹┖");
-        }
         if (application == null) {
             throw new RuntimeException("鏂囦欢鐢ㄩ�斾笉鑳戒负绌�");
         }
@@ -66,10 +66,54 @@
         }
         // 鍒犻櫎鏃ч檮浠朵俊鎭�
         deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageBlobDTOS)) {
+            return;
+        }
         List<StorageAttachment> storageAttachments = new ArrayList<>();
         for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) {
             StorageAttachment storageAttachment = new StorageAttachment();
             storageAttachment.setApplication(application.getType());
+            storageAttachment.setRecordType(recordType.getType());
+            storageAttachment.setRecordId(recordId);
+            storageAttachment.setStorageBlobId(storageBlobDTO.getId());
+            storageAttachment.setDeleted(0L);
+            storageAttachments.add(storageAttachment);
+        }
+        storageAttachmentMapper.insert(storageAttachments);
+    }
+
+    /**
+     * 淇濆瓨闄勪欢淇℃伅
+     *
+     * @param recordType      鍏宠仈璁板綍绫诲瀷
+     * @param recordId        鍏宠仈璁板綍id
+     * @param storageBlobDTOS 鏂囦欢淇℃伅
+     */
+    public void saveStorageAttachmentByRecordTypeAndRecordId(String application,RecordTypeEnum recordType, Long recordId, List<StorageBlobDTO> storageBlobDTOS) {
+        if (recordType == null) {
+            throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
+        }
+        if (recordId == null) {
+            throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+        }
+
+        // 鍒犻櫎鏃ч檮浠朵俊鎭�
+        if (application == null) {
+            for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) {
+                deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.getByType(storageBlobDTO.getApplication()), recordType, recordId);
+            }
+        } else {
+            deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.getByType(application), recordType, recordId);
+        }
+
+        if (CollectionUtils.isEmpty(storageBlobDTOS)) {
+            deleteStorageAttachmentsByRecordTypeAndRecordId(recordType, recordId);
+        }
+
+        List<StorageAttachment> storageAttachments = new ArrayList<>();
+        for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) {
+            StorageAttachment storageAttachment = new StorageAttachment();
+            storageAttachment.setApplication(Objects.requireNonNullElseGet(application, () -> ApplicationTypeEnum.getByType(storageBlobDTO.getApplication()).getType()));
             storageAttachment.setRecordType(recordType.getType());
             storageAttachment.setRecordId(recordId);
             storageAttachment.setStorageBlobId(storageBlobDTO.getId());
@@ -128,6 +172,29 @@
     }
 
     /**
+     * 閫氳繃鍏宠仈璁板綍绫诲瀷銆佸叧鑱旇褰昳d鍒犻櫎鏂囦欢淇℃伅
+     *
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordId    鍏宠仈璁板綍id
+     */
+    public void deleteStorageBlobsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) {
+        if (recordId == null || recordId <= 0) {
+            throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+        }
+        if (recordType == null) {
+            throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
+        }
+        List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
+                .eq(StorageAttachment::getRecordType, recordType.getType())
+                .eq(StorageAttachment::getRecordId, recordId));
+        if (CollectionUtils.isNotEmpty(storageAttachments)) {
+            List<Long> storageAttachmentIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId)
+                    .collect(Collectors.toList());
+            deleteStorageBlobsByStorageAttachmentIds(storageAttachmentIds);
+        }
+    }
+
+    /**
      * 鍒犻櫎鏂囦欢鍏宠仈淇℃伅
      *
      * @param storageAttachmentIds 鏂囦欢鍏宠仈id
@@ -159,6 +226,25 @@
                 .eq(StorageAttachment::getRecordType, recordType.getType())
                 .eq(StorageAttachment::getRecordId, recordId)
                 .eq(StorageAttachment::getApplication, application.getType()));
+    }
+
+    /**
+     * 鍒犻櫎鏂囦欢鍏宠仈淇℃伅
+     *
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordId    鍏宠仈璁板綍id
+     */
+    public void deleteStorageAttachmentsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) {
+        if (recordId == null || recordId <= 0) {
+            throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+        }
+        if (recordType == null) {
+            throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
+        }
+        deleteStorageBlobsByRecordTypeAndRecordId(recordType, recordId);
+        storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>()
+                .eq(StorageAttachment::getRecordType, recordType.getType())
+                .eq(StorageAttachment::getRecordId, recordId));
     }
 
     /**
@@ -198,6 +284,25 @@
     }
 
     /**
+     * 閫氳繃璁板綍绫诲瀷鑾峰彇鏂囦欢淇℃伅 attachment锛堝垎椤碉級
+     *
+     * @param storageAttachmentDTO 鍏宠仈璁板綍淇℃伅
+     */
+    public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(StorageAttachmentDTO storageAttachmentDTO) {
+        LambdaQueryWrapper<StorageAttachment> queryWrapper = new LambdaQueryWrapper<StorageAttachment>()
+                .eq(StorageAttachment::getRecordType, storageAttachmentDTO.getRecordType())
+                .eq(StorageAttachment::getRecordId, storageAttachmentDTO.getRecordId());
+        if (storageAttachmentDTO.getApplication() != null) {
+            queryWrapper.eq(StorageAttachment::getApplication, storageAttachmentDTO.getApplication());
+        }
+        List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(queryWrapper);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return null;
+        }
+        return getStorageBlobVOsByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()));
+    }
+
+    /**
      * 閫氳繃鏂囦欢鐢ㄩ�斻�佸叧鑱旇褰曠被鍨嬨�佸叧鑱旇褰昳d鑾峰彇鏂囦欢鍏宠仈淇℃伅 attachment
      *
      * @param application 鏂囦欢鐢ㄩ��
@@ -229,6 +334,9 @@
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
         }
+        Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream()
+                .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId));
+
         List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
         List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
         List<StorageBlobVO> storageBlobDTOS = new ArrayList<>();
@@ -237,9 +345,28 @@
             BeanUtils.copyProperties(storageBlob, storageBlobVO);
             storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO));
             storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO));
+            storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId()));
             storageBlobDTOS.add(storageBlobVO);
         }
         return storageBlobDTOS;
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鐢ㄩ�斻�佸叧鑱旇褰曠被鍨嬨�佸叧鑱旇褰昳d鑾峰彇鏂囦欢鍏宠仈淇℃伅 attachment
+     *
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordId    鍏宠仈璁板綍id
+     */
+    public List<StorageAttachment> getStorageAttachmentsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) {
+        if (recordId == null || recordId <= 0) {
+            throw new RuntimeException("鍏宠仈璁板綍id涓嶈兘涓虹┖");
+        }
+        if (recordType == null) {
+            throw new RuntimeException("鍏宠仈璁板綍绫诲瀷涓嶈兘涓虹┖");
+        }
+        return storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>()
+                .eq(StorageAttachment::getRecordType, recordType.getType())
+                .eq(StorageAttachment::getRecordId, recordId));
     }
 
     /**
@@ -251,6 +378,35 @@
      */
     public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
         List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return null;
+        }
+        // 鏋勫缓 storageBlobId -> storageAttachmentId 鐨勬槧灏�
+        Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream()
+                .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId));
+
+        List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
+        List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
+        List<StorageBlobVO> storageBlobDTOS = new ArrayList<>();
+        for (StorageBlob storageBlob : storageBlobs) {
+            StorageBlobVO storageBlobVO = new StorageBlobVO();
+            BeanUtils.copyProperties(storageBlob, storageBlobVO);
+            storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO));
+            storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO));
+            storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId()));
+            storageBlobDTOS.add(storageBlobVO);
+        }
+        return storageBlobDTOS;
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鐢ㄩ�斻�佸叧鑱旇褰曠被鍨嬨�佸叧鑱旇褰昳d鑾峰彇鏂囦欢淇℃伅 blob
+     *
+     * @param recordType  鍏宠仈璁板綍绫诲瀷
+     * @param recordId    鍏宠仈璁板綍id
+     */
+    public List<StorageBlobVO> getStorageBlobVOsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByRecordTypeAndRecordId(recordType, recordId);
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
         }
@@ -280,6 +436,10 @@
         if (CollectionUtils.isEmpty(storageAttachments)) {
             return null;
         }
+        // 鏋勫缓 storageBlobId -> storageAttachmentId 鐨勬槧灏�
+        Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream()
+                .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId));
+
         List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList());
         List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds);
         List<StorageBlobVO> storageBlobDTOS = new ArrayList<>();
@@ -288,6 +448,7 @@
             BeanUtils.copyProperties(storageBlob, storageBlobVO);
             storageBlobVO.setPreviewURL(buildSignedUrl(storageBlobVO, "/preview/", expired));
             storageBlobVO.setDownloadURL(buildSignedUrl(storageBlobVO, "/download/", expired));
+            storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId()));
             storageBlobDTOS.add(storageBlobVO);
         }
         return storageBlobDTOS;
@@ -481,6 +642,68 @@
         return res;
     }
 
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢棰勮鍦板潃
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     */
+    public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()));
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢棰勮鍦板潃瀛樺湪杩囨湡鏃堕棿
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     * @param expired     杩囨湡鏃堕棿锛堝垎閽燂級
+     */
+    public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired);
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢涓嬭浇鍦板潃
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     */
+    public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()));
+    }
+
+    /**
+     * 閫氳繃鏂囦欢鍏宠仈id鑾峰彇鏂囦欢涓嬭浇鍦板潃瀛樺湪杩囨湡鏃堕棿
+     *
+     * @param application 搴旂敤
+     * @param recordType  璁板綍绫诲瀷
+     * @param recordId    璁板綍id
+     * @param expired     杩囨湡鏃堕棿锛堝垎閽燂級
+     */
+    public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) {
+        List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId);
+        if (CollectionUtils.isEmpty(storageAttachments)) {
+            return new ArrayList<>();
+        }
+        return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired);
+    }
+
     public String buildSignedPreviewUrl(StorageBlobVO storageBlob) {
         return buildSignedUrl(storageBlob, "/preview/", properties.getExpired());
     }
@@ -503,8 +726,28 @@
         if (storageBlob == null || !StringUtils.hasText(storageBlob.getUidFilename())) {
             throw new IllegalArgumentException("鏂囦欢淇℃伅涓嶅畬鏁�");
         }
+        String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/');
+        String prefix = properties.getUrlPrefix().startsWith("/") ? properties.getUrlPrefix() : "/" + properties.getUrlPrefix();
+        String normalizedActionPath = StringUtils.hasText(actionPath) ? actionPath : "/preview/";
+        if (!normalizedActionPath.startsWith("/")) {
+            normalizedActionPath = "/" + normalizedActionPath;
+        }
+        if (!normalizedActionPath.endsWith("/")) {
+            normalizedActionPath = normalizedActionPath + "/";
+        }
+        String baseUrl = domain + prefix + normalizedActionPath + storageBlob.getUidFilename();
+
+        // -1 琛ㄧず姘镐箙鏈夋晥锛屼笉鐢熸垚 token锛屾敼涓� publicKey 缁勫悎鏍¢獙
+        if (expired != null && BigDecimal.valueOf(-1L).compareTo(expired) == 0) {
+            if (!StringUtils.hasText(storageBlob.getResourceKey())) {
+                throw new IllegalArgumentException("鍏紑閾炬帴缂哄皯publicKey");
+            }
+            return baseUrl + "?publicKey=" + storageBlob.getResourceKey();
+        }
+
         long now = System.currentTimeMillis();
-        long expiredMillis = expired.multiply(new BigDecimal("60000")).longValue();
+        BigDecimal expiredValue = expired == null ? new BigDecimal("120") : expired;
+        long expiredMillis = expiredValue.multiply(new BigDecimal("60000")).longValue();
         if (expiredMillis <= 0L) {
             expiredMillis = 2L * 60L * 60L * 1000L;
         }
@@ -521,16 +764,7 @@
                 .signWith(key)            // 閲嶇偣锛氫紶鍏ヤ笂闈㈢敓鎴愮殑 key 瀵硅薄锛岃�屼笉鏄� String
                 .compact();
         cacheTokenUsage(token, expiredMillis);
-        String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/');
-        String prefix = properties.getUrlPrefix().startsWith("/") ? properties.getUrlPrefix() : "/" + properties.getUrlPrefix();
-        String normalizedActionPath = StringUtils.hasText(actionPath) ? actionPath : "/preview/";
-        if (!normalizedActionPath.startsWith("/")) {
-            normalizedActionPath = "/" + normalizedActionPath;
-        }
-        if (!normalizedActionPath.endsWith("/")) {
-            normalizedActionPath = normalizedActionPath + "/";
-        }
-        return domain + prefix + normalizedActionPath + storageBlob.getUidFilename() + "?token=" + token;
+        return baseUrl + "?token=" + token;
     }
 
     private void cacheTokenUsage(String token, long expiredMillis) {

--
Gitblit v1.9.3