|
@@ -1,5 +1,7 @@
|
|
|
package com.backendsys.modules.upload.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import com.backendsys.exception.CustException;
|
|
|
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
import com.backendsys.modules.upload.dao.SysFileCategoryDao;
|
|
@@ -7,16 +9,13 @@ import com.backendsys.modules.upload.dao.SysFileDao;
|
|
|
import com.backendsys.modules.upload.entity.SysFile;
|
|
|
import com.backendsys.modules.upload.entity.SysFileCategory;
|
|
|
import com.backendsys.modules.upload.service.SysFileCategoryService;
|
|
|
-import com.backendsys.utils.response.PageEntity;
|
|
|
-import com.backendsys.utils.response.PageInfoResult;
|
|
|
-import com.backendsys.utils.v2.PageUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -85,19 +84,110 @@ public class SysFileCategoryServiceImpl implements SysFileCategoryService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Map<String, Object> updateFileCategoryBatch(List<SysFileCategory> sysFileCategoryList) {
|
|
|
|
|
|
- // 是先删除当前用户ID下的全部记录,然后批量插入新数据
|
|
|
+ // 手动校验参数
|
|
|
+ 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 字符");
|
|
|
+ if (item.getSort() != null && (item.getSort() < 1 || item.getSort() > 9999)) throw new CustException("排序必须在 1 到 9999 之间");
|
|
|
+ if (item.getStatus() != null && item.getStatus() != 1 && item.getStatus() != -1) throw new CustException("状态取值有误,范围应是(-1禁用, 1启用)");
|
|
|
+ });
|
|
|
+
|
|
|
+ // 获得当前用户ID
|
|
|
Long user_id = SecurityUtil.getUserId();
|
|
|
- sysFileCategoryDao.delete(new LambdaQueryWrapper<SysFileCategory>().eq(SysFileCategory::getUser_id, user_id));
|
|
|
|
|
|
- sysFileCategoryList = sysFileCategoryList.stream().map(item -> {
|
|
|
- item.setUser_id(user_id);
|
|
|
- return item;
|
|
|
- }).collect(Collectors.toList());
|
|
|
+ // 获得原文件分类列表
|
|
|
+ SysFileCategory entity = new SysFileCategory();
|
|
|
+ entity.setUser_id(user_id);
|
|
|
+ List<Map<String, Object>> originList = sysFileCategoryDao.selectUploadFileCategoryList(entity);
|
|
|
+
|
|
|
+ // 提取两个列表中的id到Set集合
|
|
|
+ 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> 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("missingIds = " + missingIds);
|
|
|
+// System.out.println("missingIdsNoNull = " + missingIdsNoNull);
|
|
|
+// System.out.println("------------------------------------");
|
|
|
+
|
|
|
+ String message = "";
|
|
|
+
|
|
|
+ if (!missingIds.isEmpty()) {
|
|
|
+
|
|
|
+ // 如果存在null,即是无id记录,则是新增
|
|
|
+ List<SysFileCategory> insertListEntity = new ArrayList<>();
|
|
|
+ for (SysFileCategory item : sysFileCategoryList) {
|
|
|
+ Long id = item.getId();
|
|
|
+ if (id == null) {
|
|
|
+ item.setUser_id(user_id);
|
|
|
+ insertListEntity.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // [DB] 批量新增
|
|
|
+ if (!insertListEntity.isEmpty()) {
|
|
|
+ System.out.println("insertListEntity: " + insertListEntity);
|
|
|
+ sysFileCategoryDao.insertBatch(insertListEntity);
|
|
|
+ StringBuilder insertIdSetStr = new StringBuilder();
|
|
|
+ insertListEntity.stream().forEach(it -> {
|
|
|
+ if (it.getId() != null) {
|
|
|
+ insertIdSet.add(it.getId());
|
|
|
+ if (insertIdSetStr.length() > 0) {
|
|
|
+ insertIdSetStr.append(", ");
|
|
|
+ }
|
|
|
+ insertIdSetStr.append(Convert.toStr(it.getId()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ message += "新增了 Id: " + insertIdSetStr + ", ";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有多出的ID,则抛出异常 (过滤空值)
|
|
|
+ if (!missingIdsNoNull.isEmpty()) {
|
|
|
+ String missingIdsStr = missingIdsNoNull.stream().map(String::valueOf).collect(Collectors.joining(", "));
|
|
|
+ throw new CustException("id: " + missingIdsStr + ", 不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有缺少的ID,则是删除
|
|
|
+ if (!extraIds.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 (category_file_count == null || category_file_count != 0) {
|
|
|
+ throw new CustException("id: " + id + ", 需要清除文件后才可删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // [DB] 批量删除
|
|
|
+ sysFileCategoryDao.deleteBatchIds(extraIds);
|
|
|
+ String extraIdsStr = extraIds.stream().map(String::valueOf).collect(Collectors.joining(", "));
|
|
|
+ message += "删除了 Id: " + extraIdsStr + ", ";
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- sysFileCategoryDao.insertBatch(sysFileCategoryList);
|
|
|
+ // [DB] 批量更新
|
|
|
+ System.out.println("updateByIdBatch: " + sysFileCategoryList);
|
|
|
+ sysFileCategoryDao.updateByIdBatch(sysFileCategoryList);
|
|
|
+ String updateIdSetStr = updateIdSetNoNull.stream().map(String::valueOf).collect(Collectors.joining(", "));
|
|
|
+ message += "更新了 Id: " + updateIdSetStr;
|
|
|
|
|
|
- List<Long> ids = sysFileCategoryList.stream().map(item -> item.getId()).collect(Collectors.toList());
|
|
|
- return Map.of("ids", ids);
|
|
|
+ // 返回值
|
|
|
+ Map<String, Object> response = new LinkedHashMap<>();
|
|
|
+ response.put("message", message);
|
|
|
+ response.put("insert_ids", insertIdSet);
|
|
|
+ response.put("delete_ids", extraIds);
|
|
|
+ response.put("update_ids", updateIdSetNoNull);
|
|
|
+ return response;
|
|
|
}
|
|
|
|
|
|
/**
|