优化代码

This commit is contained in:
chenxudong 2025-04-08 11:56:09 +08:00
parent c695c3b404
commit e11157ec41
8 changed files with 35 additions and 32 deletions

View File

@ -184,6 +184,7 @@ public class CommonService {
}
public String createFileCode(String parentId, String fileType, int version, String timeStr) {
Assert.isTrue(StrUtil.isNotEmpty(fileType), "文件类型不能为空");
if (fileType.equals(EleDataTypeEnum.FOLDER.desc) || StrUtil.isEmpty(fileType)) {
return parentId + "00" + version + timeStr;
}
@ -237,7 +238,7 @@ public class CommonService {
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code)
.eq(EdFileInfo::getParentId, parentId));
List<String> names = edFileInfos.stream().map(EdFileInfo::getFileName).collect(Collectors.toList());
List<String> names = edFileInfos.stream().map(EdFileInfo::getFileName).toList();
try {
String path = currentPath + MYSQL_FILE_PATH_SPLIT + folderId;
EdFileInfo fileInfo = new EdFileInfo();
@ -313,7 +314,7 @@ public class CommonService {
}
queryWrapper.in(EdFileInfo::getId, accessibleIds);
}
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList());
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).toList();
for (String id : prjIds) {
LambdaQueryWrapper<EdFileInfo> prjQueryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
@ -355,7 +356,7 @@ public class CommonService {
.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code)
.eq(EdFileInfo::getCreatedBy, UserThreadLocal.getUserId())
.eq(EdFileInfo::getParentId, PRJ_PARENT_ID);
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList());
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).toList();
for (String id : prjIds) {
LambdaQueryWrapper<EdFileInfo> prjQueryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
@ -400,7 +401,7 @@ public class CommonService {
queryWrapper.in(EdFileInfo::getId, accessibleIds);
}
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList());
List<String> prjIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).toList();
for (String id : prjIds) {
LambdaQueryWrapper<EdFileInfo> prjQueryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)

View File

@ -488,13 +488,13 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
List<EdFileInfo> importAllFiles = JSONUtil.toList(info, EdFileInfo.class);
// 找出层级文件夹
List<EdFileInfo> prjFolders = importAllFiles.stream().filter(e -> DataOwnEnum.isPrjCode(e.getDataOwn()))
.collect(Collectors.toList());
.toList();
// 找出用户创建的文件夹
List<EdFileInfo> userFolders = importAllFiles.stream().filter(e -> ObjUtil.equals(e.getDataType(), EleDataTypeEnum.FOLDER.code))
.filter(e -> DataOwnEnum.isFileCode(e.getDataOwn()))
.collect(Collectors.toList());
.toList();
// 找出所有文件
List<EdFileInfo> allFiles = importAllFiles.stream().filter(e -> ObjUtil.equals(e.getDataType(), EleDataTypeEnum.FILE.code)).collect(Collectors.toList());
List<EdFileInfo> allFiles = importAllFiles.stream().filter(e -> ObjUtil.equals(e.getDataType(), EleDataTypeEnum.FILE.code)).toList();
List<EdFileInfo> allObjs = new ArrayList<>(prjFolders);
// 处理用户创建的文件夹
@ -503,7 +503,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String parentId = edFileInfo.getParentId();
List<EdFileInfo> folders = queryChildFolders(parentId);
Map<String, EdFileInfo> foldersMap = folders.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
List<String> folderNames = folders.stream().map(EdFileInfo::getFileName).collect(Collectors.toList());
List<String> folderNames = folders.stream().map(EdFileInfo::getFileName).toList();
if (folderNames.contains(fileName)) { // 有同名文件夹
// 判断id是否相同
if (foldersMap.get(fileName).getId().equals(edFileInfo.getId())) { // id相同不做处理
@ -543,13 +543,13 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
// 导入的文件在线下已经被废除了
Optional<EdFileInfo> first = deepCopyV.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).findFirst();
if (!first.isPresent()) {
if (first.isEmpty()) {
continue;
}
EdFileInfo importEffectFile = first.get();
Optional<EdFileInfo> first1 = deepCopyDb.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).findFirst();
EdFileInfo dbEffectFile = null;
if (!first1.isPresent()) {
if (first1.isEmpty()) {
// 线下存在线上不存在被废除了此时根据更新时间判断如果线上的比线下的新则以线上的为准
EdFileInfo fileInfo = deepCopyDb.stream().max(Comparator.comparing(EdFileInfo::getUpdatedTime)).get();
if (fileInfo.getUpdatedTime().after(importEffectFile.getUpdatedTime())) {
@ -634,7 +634,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
private void update2FileSystem(List<EdFileInfo> needMove2FileSystemFiles, String prjDirPath, int dataOwnCode) {
Map<String, EdFileInfo> maps = needMove2FileSystemFiles.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
List<EdFileInfo> files = needMove2FileSystemFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).collect(Collectors.toList());
List<EdFileInfo> files = needMove2FileSystemFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).toList();
for (EdFileInfo edFileInfo : files) {
String filePath = edFileInfo.getFilePath();
StringBuilder sysFilePath = new StringBuilder();
@ -711,8 +711,8 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
resFiles.addAll(tmps.values());
resFiles.add(prjFileInfo);
String prjName = commonService.getPrjNameByDbPath(resFiles.get(0).getFilePath());
List<EdFileInfo> folders = resFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FOLDER.code)).collect(Collectors.toList());
List<EdFileInfo> files = resFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).collect(Collectors.toList());
List<EdFileInfo> folders = resFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FOLDER.code)).toList();
List<EdFileInfo> files = resFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).toList();
for (EdFileInfo edFileInfo : folders) {
String destFolderPath = userDownloadDataDir + File.separator + prjName + File.separator + commonService.getDbPath(edFileInfo.getFilePath()); // file
fileSystemService.createDirectory(destFolderPath);
@ -778,8 +778,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
@Transactional(rollbackFor = Exception.class)
public ElectromagneticResult<?> upload(String parentId, MultipartFile file, Integer strategy, int dataOwnCode) {
Assert.isTrue(FileRepeatEnum.contains(strategy), "解决重名文件参数错误");
String destPath = commonService.getDbPathById(parentId);
String fileName = file.getOriginalFilename();
String mainName = FileUtil.mainName(fileName);
String suffix = FileUtil.getSuffix(fileName);
Assert.isTrue(StrUtil.isNotEmpty(suffix), "文件类型不能为空");
String destPath = commonService.getDbPathById(parentId);
String strategyStr = FileRepeatEnum.getDesc(strategy);
Assert.isTrue(!file.isEmpty(), StrFormatter.format("文件 {} 为空,文件上传到 {} 失败,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr));
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), StrFormatter.format("文件 {} {},上传到 {} 失败,同名同后缀的处理方式为 {}", fileName, NAME_VALID_MSG, destPath, strategyStr));
@ -797,9 +800,6 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
}
long dirCount = this.baseMapper.selectCount(queryWrapper);
Assert.isTrue(dirCount == 0, "文件 {} 上传到 {} 失败,层级结构不允许上传文件,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr);
String mainName = FileUtil.mainName(fileName);
String suffix = FileUtil.getSuffix(fileName);
EdFileInfo newEdFileInfo = new EdFileInfo();
EdFileInfo finalEdFileInfo;
newEdFileInfo.newInit();
@ -1184,7 +1184,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code));
Integer maxFileVersion = Collections.max(parentFileInfos, Comparator.comparing(EdFileInfo::getFileVersion)).getFileVersion();
// 找到当前展示的版本
EdFileInfo effectFileInfo = parentFileInfos.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).collect(Collectors.toList()).get(0);
EdFileInfo effectFileInfo = parentFileInfos.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).toList().get(0);
String codePathByDbPath = commonService.getCodePathByDbPath(effectFileInfo.getFilePath());
String timeStr = EleCommonUtil.getNowTimeStr();
String fileCode = commonService.createFileCode(codePathByDbPath, suffix, FILE_START_VERSION, timeStr);

View File

@ -204,6 +204,10 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
public ElectromagneticResult<?> uploadFileAndRelation(String parentId, String id, MultipartFile file, String desc, int dataOwnCode) {
Assert.isTrue(EleCommonUtil.isFileNameValid(file.getOriginalFilename()), NAME_VALID_MSG);
String fileName = file.getOriginalFilename();
String mainName = FileUtil.mainName(fileName);
String suffix = FileUtil.getSuffix(fileName);
Assert.isTrue(StrUtil.isNotEmpty(suffix), "文件类型不能为空");
// 查找下一层看是否存在顶级定义相关文件如果存在则该层属于管理员层级定义的不允许上传文件
long dirCount = edFileInfoService.count(Wrappers.lambdaQuery(EdFileInfo.class)
.eq(EdFileInfo::getParentId, parentId)
@ -212,9 +216,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code));
Assert.isTrue(dirCount == 0, "层级目录不允许上传文件");
String fileName = file.getOriginalFilename();
String mainName = FileUtil.mainName(fileName);
String suffix = FileUtil.getSuffix(fileName);
EdFileInfo newEdFileInfo = new EdFileInfo();
newEdFileInfo.newInit();
try {

View File

@ -344,7 +344,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
.select(EdFileInfo::getId, EdFileInfo::getFilePath)
.eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code)
.likeRight(EdFileInfo::getFilePath, prjId);
List<String> paths = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getFilePath).collect(Collectors.toList());
List<String> paths = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getFilePath).toList();
List<String> fileSysPaths = new ArrayList<>();
paths.forEach(path -> {
String fileSysPath = commonService.getFileSysPath(path, dataOwnCode);
@ -415,11 +415,11 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
List<String> sysFilePaths = new ArrayList<>();
for (int i = 1; i <= prjFolderMaxLength; ++i) {
int layerIndex = i;
List<EdFileInfo> currentSourceLayerDirs = sourceEdFileInfos.stream().filter(e -> StrUtil.count(e.getFilePath(), MYSQL_FILE_PATH_SPLIT) == layerIndex).collect(Collectors.toList());
List<EdFileInfo> currentSourceLayerDirs = sourceEdFileInfos.stream().filter(e -> StrUtil.count(e.getFilePath(), MYSQL_FILE_PATH_SPLIT) == layerIndex).toList();
if (layerIndex == 1) {
targetEdFileInfos = commonService.selectAllPrjFolder(targetId, dataOwnCode);
EdFileInfo prjFileInfo = targetEdFileInfos.stream().filter(e -> e.getParentId().equals(PRJ_PARENT_ID)).findFirst().get();
List<EdFileInfo> targetChildLayerDirs = targetEdFileInfos.stream().filter(e -> StrUtil.count(e.getFilePath(), MYSQL_FILE_PATH_SPLIT) == 1).collect(Collectors.toList());
List<EdFileInfo> targetChildLayerDirs = targetEdFileInfos.stream().filter(e -> StrUtil.count(e.getFilePath(), MYSQL_FILE_PATH_SPLIT) == 1).toList();
int size = targetChildLayerDirs.size();
for (EdFileInfo edFileInfo : currentSourceLayerDirs) {
int maxFolderId = Integer.parseInt(this.baseMapper.maxPrjId());

View File

@ -31,7 +31,7 @@ public class FileFormatServiceImpl extends ServiceImpl<FileFormatMapper, FileFor
Assert.isTrue(suffixName.matches("^[a-zA-Z0-9]+$"), "文件格式不符合规范");
List<FileFormat> fileFormats = this.baseMapper.selectList(null);
List<String> names = fileFormats.stream().map(FileFormat::getSuffixName).collect(Collectors.toList());
List<String> names = fileFormats.stream().map(FileFormat::getSuffixName).toList();
// 有可能是被废除了现在恢复
if (names.contains(suffixName)) {

View File

@ -103,7 +103,7 @@ public class FileRecycleServiceImpl implements FileRecycleService {
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
.eq(EdFileInfo::getAllDeleted, true)
.eq(EdFileInfo::getPermanentDeleted, false);
List<String> ids = edFileInfoMapper.selectList(userPrjWrapper).stream().map(EdFileInfo::getFileCode).map(e -> e.substring(0, 6)).collect(Collectors.toList());
List<String> ids = edFileInfoMapper.selectList(userPrjWrapper).stream().map(EdFileInfo::getFileCode).map(e -> e.substring(0, 6)).toList();
List<String> accessibleTree = permissionService.getAccessibleTree();
accessibleTree.addAll(ids);
if (CollUtil.isNotEmpty(accessibleTree)) {

View File

@ -62,7 +62,7 @@ public class PermissionServiceImpl implements PermissionService {
queryWrapper1.select(RolePermission::getPermissionCode)
.eq(RolePermission::getFileId, id)
.in(RolePermission::getRoleId, roleIds);
List<String> permissionCodes = rolePermissionService.listObjs(queryWrapper1).stream().map(Object::toString).collect(Collectors.toList());
List<String> permissionCodes = rolePermissionService.listObjs(queryWrapper1).stream().map(Object::toString).toList();
Map<String, Boolean> res = transToMap(permissionCodes, includeView);
UserThreadLocal.setSuccessInfo("", "", "查看当前目录功能权限成功");
return res;
@ -88,7 +88,7 @@ public class PermissionServiceImpl implements PermissionService {
queryWrapper1.select(RolePermission::getFileId)
.in(RolePermission::getRoleId, roleIds)
.eq(RolePermission::getPermissionCode, FilePermission.VIEW);
List<String> ids = rolePermissionService.listObjs(queryWrapper1).stream().map(Object::toString).collect(Collectors.toList());
List<String> ids = rolePermissionService.listObjs(queryWrapper1).stream().map(Object::toString).toList();
Set<String> result = new HashSet<>();
// 把父亲节点加上
@ -109,7 +109,7 @@ public class PermissionServiceImpl implements PermissionService {
private List<String> getRoles(String userId) {
LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(UserRole::getRoleId).eq(UserRole::getUserId, userId);
return userRoleMapper.selectObjs(queryWrapper).stream().map(Object::toString).collect(Collectors.toList());
return userRoleMapper.selectObjs(queryWrapper).stream().map(Object::toString).toList();
}
/**
@ -125,7 +125,7 @@ public class PermissionServiceImpl implements PermissionService {
queryWrapper.select(RolePermission::getPermissionCode)
.eq(RolePermission::getFileId, fileId)
.eq(RolePermission::getRoleId, roleId);
return rolePermissionService.listObjs(queryWrapper).stream().map(Object::toString).collect(Collectors.toList());
return rolePermissionService.listObjs(queryWrapper).stream().map(Object::toString).toList();
}
/**
@ -199,7 +199,7 @@ public class PermissionServiceImpl implements PermissionService {
queryWrapper.eq(EdFileInfo::getDataStatus, EleDataStatusEnum.WAIT_DELETED.code)
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
.likeRight(EdFileInfo::getFilePath, prjId);
List<String> deletedIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList());
List<String> deletedIds = edFileInfoMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).toList();
LambdaQueryWrapper<RolePermission> deleteWrapper = new LambdaQueryWrapper<>();
if (!deletedIds.isEmpty()) {

View File

@ -251,7 +251,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Role::getRoleName);
List<String> roleNames = this.listObjs(queryWrapper).stream().map(Object::toString).collect(Collectors.toList());
List<String> roleNames = this.listObjs(queryWrapper).stream().map(Object::toString).toList();
List<HashMap<String, String>> result = new ArrayList<>();
for (String roleName : roleNames) {
HashMap<String, String> map = new HashMap<>();