Prechádzať zdrojové kódy

上传分块,新增事务注解

tsurumure 3 mesiacov pred
rodič
commit
f1f9dfcd74

+ 11 - 9
src/main/java/com/backendsys/modules/upload/service/impl/SysFileCategoryServiceImpl.java

@@ -85,6 +85,7 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
     public Map<String, Object> updateFileCategoryBatch(List<SysFileCategory> sysFileCategoryList) {
 
         // 手动校验参数
+        if (sysFileCategoryList.size() > 20) throw new CustException("批量编辑数量不大于: 20");
         sysFileCategoryList.stream().forEach(item -> {
             if (item.getCategory_name() != null && item.getCategory_name().isEmpty()) throw new CustException("文件分类名称不能为空");
             if (item.getCategory_name() != null && item.getCategory_name().length() > 50) throw new CustException("文件分类名称长度不超过 50 字符");
@@ -100,19 +101,20 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
         entity.setUser_id(user_id);
         List<Map<String, Object>> originList = sysFileCategoryDao.selectUploadFileCategoryList(entity);
 
-        // 提取两个列表中的id到Set集合
+        // 提取列表中的id集合作对比
         Set<Long> insertIdSet = new HashSet<>();
         Set<Long> originIdSet = originList.stream().map(item -> Convert.toLong(item.get("id"))).collect(Collectors.toSet());
         Set<Long> updateIdSet = sysFileCategoryList.stream().map(SysFileCategory::getId).collect(Collectors.toSet());
         Set<Long> updateIdSetNoNull = updateIdSet.stream().filter(id -> id != null && !id.toString().isEmpty()).collect(Collectors.toSet());
-        Collection<Long> extraIds = CollUtil.subtract(originIdSet, updateIdSet);
+        Collection<Long> deleteIds = CollUtil.subtract(originIdSet, updateIdSet);
+
         Collection<Long> missingIds = CollUtil.subtract(updateIdSet, originIdSet);
         Collection<Long> missingIdsNoNull = missingIds.stream().filter(id -> id != null && !id.toString().isEmpty()).collect(Collectors.toSet());
 
 //        System.out.println("------------------------------------");
 //        System.out.println("originIdSet = " + originIdSet);
 //        System.out.println("updateIdSet = " + updateIdSet);
-//        System.out.println("extraIds = " + extraIds);
+//        System.out.println("deleteIds = " + deleteIds);
 //        System.out.println("missingIds = " + missingIds);
 //        System.out.println("missingIdsNoNull = " + missingIdsNoNull);
 //        System.out.println("------------------------------------");
@@ -156,12 +158,12 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
         }
 
         // 如果有缺少的ID,则是删除
-        if (!extraIds.isEmpty()) {
+        if (!deleteIds.isEmpty()) {
             // 判断每个分类下已绑定的文件是否为空,不空的分类不允许删除
             for (Map<String, Object> item : originList) {
                 Long id = Convert.toLong(item.get("id"));
                 Long category_file_count = Convert.toLong(item.get("category_file_count"));
-                if (extraIds.contains(id)) {
+                if (deleteIds.contains(id)) {
                     if (category_file_count == null || category_file_count != 0) {
                         throw new CustException("id: " + id + ", 需要清除文件后才可删除");
                     }
@@ -169,9 +171,9 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
             }
 
             // [DB] 批量删除
-            sysFileCategoryDao.deleteBatchIds(extraIds);
-            String extraIdsStr = extraIds.stream().map(String::valueOf).collect(Collectors.joining(", "));
-            message += "删除了 Id: " + extraIdsStr + ", ";
+            sysFileCategoryDao.deleteBatchIds(deleteIds);
+            String deleteIdsStr = deleteIds.stream().map(String::valueOf).collect(Collectors.joining(", "));
+            message += "删除了 Id: " + deleteIdsStr + ", ";
         }
 
 
@@ -185,7 +187,7 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
         Map<String, Object> response = new LinkedHashMap<>();
         response.put("message", message);
         response.put("insert_ids", insertIdSet);
-        response.put("delete_ids", extraIds);
+        response.put("delete_ids", deleteIds);
         response.put("update_ids", updateIdSetNoNull);
         return response;
     }

+ 2 - 0
src/main/java/com/backendsys/modules/upload/service/impl/SysFileMultipartServiceImpl.java

@@ -23,6 +23,7 @@ import com.volcengine.tos.model.object.UploadPartV2Output;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
@@ -142,6 +143,7 @@ public class SysFileMultipartServiceImpl implements SysFileMultipartService {
     // 2.上传分块
     // - 抖音云单个分块大小不能小于4MB
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Map<String, Object> multipartUpload(MultipartFile multipartFile, String upload_id, Integer upload_chunk_index) {
 
         if (multipartFile.isEmpty()) throw new CustException("file 上传文件不能为空");