兼容+功能

This commit is contained in:
ran.shang
2026-01-05 16:35:46 +08:00
parent e99b717e48
commit ea6d25ca97
103 changed files with 1563 additions and 581 deletions

View File

@@ -4,11 +4,14 @@ import com.senmesh.common.constant.CommonConstants;
import com.senmesh.common.exception.util.Assert;
import com.senmesh.common.util.ListUtil;
import com.senmesh.common.util.UserContextHolder;
import com.senmesh.component.file.service.FileService;
import com.senmesh.sfile.constant.FileStatusEnum;
import com.senmesh.sfile.constant.FileTypeEnum;
import com.senmesh.sfile.constant.RootTypeEnum;
import com.senmesh.sfile.convert.UserFileConvert;
import com.senmesh.sfile.dao.UserFileMapper;
import com.senmesh.sfile.dto.ExpandUserFileCreateDTO;
import com.senmesh.sfile.dto.ExpandUserFileDTO;
import com.senmesh.sfile.dto.UserFileCreateDTO;
import com.senmesh.sfile.dto.UserFileDTO;
import com.senmesh.sfile.dto.UserFileModifyDTO;
@@ -16,73 +19,84 @@ import com.senmesh.sfile.dto.UserFileModifyStatusDTO;
import com.senmesh.sfile.dto.UserFileQueryDTO;
import com.senmesh.sfile.entity.UserFile;
import com.senmesh.sfile.service.UserFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 用户文件ServiceImpl
*/
@Slf4j
@Service
public class UserFileServiceImpl implements UserFileService {
@Autowired
private UserFileMapper userFileMapper;
@Autowired
private FileService fileService;
@Override
@Transactional
public Long create(UserFileCreateDTO createDTO) {
public UserFileDTO create(UserFileCreateDTO createDTO) {
Long parentId = createDTO.getParentId();
String parentPath = createDTO.getParentPath();
Long userId = createDTO.getUserId();
String rootType = createDTO.getRootType();
// 1. 校验文件类型时fileCode不能为空
// 1. 校验 userId 和 rootType 不能为空
Assert.notNull(userId, "用户ID不能为空");
Assert.notBlank(rootType, "根类型不能为空");
// 2. 校验文件类型时fileCode不能为空
if (FileTypeEnum.isFile(createDTO.getFileType())) {
Assert.notBlank(createDTO.getFileCode(), "文件编码不能为空");
}
// 2. 校验父节点(如果不是根目录)
if (parentId != null && parentId != 0L) {
// 如果没有提供parentPath,需要查询父节点
if (StringUtils.isBlank(parentPath)) {
UserFileDTO parentFile = this.findOne(UserFileQueryDTO.builder()
.id(parentId)
.userId(userId)
.rootType(rootType)
.build());
Assert.isTrue(FileTypeEnum.isFolder(parentFile.getFileType()), "父节点必须是文件夹");
// 获取父路径用于后续拼接
parentPath = parentFile.getPath();
}
// 如果提供了parentPath,信任调用方,不做校验
// 3. 处理 parentPath(性能优化:如果提供了 parentPath,直接使用,避免查询)
if (StringUtils.isBlank(parentPath) && parentId != null && parentId != 0L) {
// 如果没有提供 parentPath,但提供了 parentId,需要查询父节点获取 parentPath
UserFileDTO parentFile = this.findOne(UserFileQueryDTO.builder()
.id(parentId)
.userId(userId)
.rootType(rootType)
.build());
Assert.isTrue(FileTypeEnum.isFolder(parentFile.getFileType()), "父节点必须是文件夹");
// 获取父路径用于后续拼接
parentPath = parentFile.getPath();
}
// 如果提供了 parentPath,直接信任调用方,不做校验
// 3. 插入数据获取ID
// 4. 插入数据获取ID
UserFile userFile = UserFileConvert.INSTANCE.toEntity(createDTO);
userFileMapper.saveEntity(userFile);
Long newId = userFile.getId();
// 4. 构建path
// 5. 构建path
String path = StringUtils.isNotBlank(parentPath)
? parentPath + CommonConstants.COMMA + newId
: String.valueOf(newId);
// 5. 更新path
// 6. 更新path
userFile.setPath(path);
userFileMapper.updateEntitySelective(userFile);
return newId;
// 7. 转换为DTO返回
return UserFileConvert.INSTANCE.toDto(userFile);
}
@Override
@Transactional
public Long create(ExpandUserFileCreateDTO expandCreateDTO) {
public ExpandUserFileDTO create(ExpandUserFileCreateDTO expandCreateDTO) {
Long userFileId = expandCreateDTO.getUserFileId();
Long userId = expandCreateDTO.getUserId();
String rootType = expandCreateDTO.getRootType();
@@ -94,26 +108,7 @@ public class UserFileServiceImpl implements UserFileService {
.rootType(RootTypeEnum.NORMAL.getType())
.build());
// 2. 创建文件夹,文件名 = 原文件名 + 当前时间
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String folderName = normalFile.getFileName() + "_" + timestamp;
UserFileCreateDTO folderCreateDTO = UserFileCreateDTO.builder()
.userId(userId)
.rootType(rootType)
.fileType(FileTypeEnum.FOLDER.getType())
.fileName(folderName)
.parentId(0L)
.endTime(expandCreateDTO.getEndTime())
.accessCode(expandCreateDTO.getAccessCode())
.build();
Long folderId = this.create(folderCreateDTO);
String folderPath = String.valueOf(folderId);
// 3. 查询需要复制的文件列表
// 查询条件:path LIKE 'xxx%' AND fileType = '1'
// 如果是文件:会查到自己
// 如果是文件夹:会查到其下所有文件类型的子文件
UserFileQueryDTO queryDTO = UserFileQueryDTO.builder()
.userId(userId)
.rootType(RootTypeEnum.NORMAL.getType())
@@ -122,23 +117,98 @@ public class UserFileServiceImpl implements UserFileService {
.build();
queryDTO.disablePage();
List<UserFileDTO> filesToCopy = this.list(queryDTO);
Assert.notEmpty(filesToCopy, "未查询到需要处理的文件");
// 2. 创建文件夹,文件名 = 原文件名 + 当前时间
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String folderName = normalFile.getFileName() + "_" + timestamp;
UserFileCreateDTO folderCreateDTO = UserFileCreateDTO.builder()
.userId(userId)
.rootType(rootType)
.fileStatus(expandCreateDTO.getFileStatus())
.fileType(FileTypeEnum.FOLDER.getType())
.fileName(folderName)
.parentId(0L)
.endTime(expandCreateDTO.getEndTime())
.accessCode(expandCreateDTO.getAccessCode())
.build();
UserFileDTO folderDTO = this.create(folderCreateDTO);
Long folderId = folderDTO.getId();
String folderPath = String.valueOf(folderId);
// 4. 在文件夹下创建文件对象
List<UserFileDTO> createdFiles = new java.util.ArrayList<>();
for (UserFileDTO fileDTO : filesToCopy) {
UserFileCreateDTO fileCreateDTO = UserFileCreateDTO.builder()
.userId(userId)
.rootType(rootType)
.fileStatus(expandCreateDTO.getFileStatus())
.fileType(FileTypeEnum.FILE.getType())
.fileName(fileDTO.getFileName())
.fileCode(fileDTO.getFileCode())
.parentId(folderId)
.parentPath(folderPath)
.build();
this.create(fileCreateDTO);
UserFileDTO createdFile = this.create(fileCreateDTO);
createdFiles.add(createdFile);
}
return folderId;
// 6. 返回结果
return ExpandUserFileDTO.builder()
.folder(folderDTO)
.files(createdFiles)
.build();
}
@Override
@Transactional
public ExpandUserFileDTO create(UserFileDTO folder, List<UserFileDTO> files) {
Long userId = folder.getUserId();
String rootType = folder.getRootType();
// 3. 创建文件夹
UserFileCreateDTO folderCreateDTO = UserFileCreateDTO.builder()
.userId(userId)
.rootType(rootType)
.fileType(FileTypeEnum.FOLDER.getType())
.fileName(folder.getFileName())
.parentId(0L)
.fileStatus(FileStatusEnum.NORMAL.getStatus())
.build();
UserFileDTO folderDTO = this.create(folderCreateDTO);
Long folderId = folderDTO.getId();
String folderPath = String.valueOf(folderId);
log.info("创建文件夹成功: folderId={}, 文件名={}", folderId, folder.getFileName());
// 4. 创建文件列表
List<UserFileDTO> createdFiles = new ArrayList<>();
for (UserFileDTO fileInfo : files) {
// 创建文件记录
UserFileCreateDTO fileCreateDTO = UserFileCreateDTO.builder()
.userId(userId)
.rootType(rootType)
.fileType(FileTypeEnum.FILE.getType())
.fileName(fileInfo.getFileName())
.fileCode(fileInfo.getFileCode())
.parentId(folderId)
.parentPath(folderPath)
.fileStatus(FileStatusEnum.NORMAL.getStatus())
.build();
UserFileDTO createdFile = this.create(fileCreateDTO);
createdFiles.add(createdFile);
}
// 5. 返回结果
return ExpandUserFileDTO.builder()
.folder(folderDTO)
.files(createdFiles)
.build();
}
@Override
@Transactional