使用switch预计代替多个if-else的demo

This commit is contained in:
chenxudong 2025-04-11 10:22:31 +08:00
parent 0712a1d912
commit 692af9163c
3 changed files with 40 additions and 25 deletions

View File

@ -198,12 +198,12 @@ public class CommonService {
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code)
.likeRight(EdFileInfo::getFilePath, id);
if (DataOwnEnum.isSysCode(dataOwnCode)) {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.SYS_PRJ.code);
} else if (DataOwnEnum.isUserCode(dataOwnCode)) {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code);
} else {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.REPO_PRJ.code);
DataOwnEnum obj = DataOwnEnum.getEnumByCode(dataOwnCode);
switch (Objects.requireNonNull(obj)) {
case USER_FILE, USER_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code);
case SYS_FILE, SYS_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.SYS_PRJ.code);
case REPO_PRJ, REPO_FILE -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.REPO_PRJ.code);
default -> ElectromagneticResultUtil.fail("-1", "参数错误");
}
return edFileInfoMapper.selectList(queryWrapper);
}
@ -435,6 +435,7 @@ public class CommonService {
public ElectromagneticResult<?> deleteFolder(String id, int dataOwnCode) {
// 如果文件夹下存在文件包括文件夹和已经逻辑删除的文件则不允许删除后面管理员选择会有物理删除文件夹和文件的功能此时MySQL和文件系统则会进行物理删除该文件
EdFileInfo srcFileInfo = edFileInfoMapper.selectById(id);
Assert.isTrue(srcFileInfo.getDataType().equals(EleDataTypeEnum.FOLDER.code), "禁止删除目录");
String srcFilePath = getFileSysPath(srcFileInfo.getFilePath(), dataOwnCode);
String uuid = IdUtil.fastSimpleUUID();
try {
@ -502,7 +503,7 @@ public class CommonService {
List<String> paths = StrUtil.split(dbPath, MYSQL_FILE_PATH_SPLIT);
List<String> reversePaths = CollUtil.reverse(paths);
for (String path : reversePaths) {
if (path.length() == 6) {
if (path.length() == elePropertyConfig.getPrjFolderMaxLength()) {
return path;
}
}
@ -519,13 +520,14 @@ public class CommonService {
Set<String> res = new HashSet<>();
List<String> prjInfo;
if (DataOwnEnum.isSysCode(dataOwnCode)) {
prjInfo = querySysPrjTree(PrjQuerySource.SYS_DB.value, accessibleIds, new FileProjectVO()).getOrDefault(PrjQuerySource.SYS_DB.value, new ArrayList<>());
} else if (DataOwnEnum.isUserCode(dataOwnCode)) {
prjInfo = queryUserPrjTree(PrjQuerySource.USER_DB.value, new FileProjectVO()).getOrDefault(PrjQuerySource.USER_DB.value, new ArrayList<>());
} else {
prjInfo = queryRepoPrjTree(PrjQuerySource.REPO_DB.value, accessibleIds, new FileProjectVO()).getOrDefault(PrjQuerySource.REPO_DB.value, new ArrayList<>());
List<String> prjInfo = List.of();
DataOwnEnum obj = DataOwnEnum.getEnumByCode(dataOwnCode);
switch (Objects.requireNonNull(obj)) {
case USER_FILE, USER_PRJ -> prjInfo = queryUserPrjTree(PrjQuerySource.USER_DB.value, new FileProjectVO()).getOrDefault(PrjQuerySource.USER_DB.value, new ArrayList<>());
case SYS_FILE, SYS_PRJ -> prjInfo = querySysPrjTree(PrjQuerySource.SYS_DB.value, accessibleIds, new FileProjectVO()).getOrDefault(PrjQuerySource.SYS_DB.value, new ArrayList<>());
case REPO_PRJ, REPO_FILE -> prjInfo = queryRepoPrjTree(PrjQuerySource.REPO_DB.value, accessibleIds, new FileProjectVO()).getOrDefault(PrjQuerySource.REPO_DB.value, new ArrayList<>());
default -> ElectromagneticResultUtil.fail("-1", "参数错误");
}
for (String info : prjInfo) {

View File

@ -60,6 +60,8 @@ import java.util.*;
import java.util.stream.Collectors;
import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.*;
import static com.electromagnetic.industry.software.common.enums.DataOwnEnum.USER_FILE;
import static com.electromagnetic.industry.software.common.enums.FileRepeatEnum.IGNORE;
@Service
public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo> implements EdFileInfoService {
@ -99,7 +101,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
if (DataOwnEnum.isSysCode(dataOwnCode)) {
String parentId = pars.getParentId();
List<String> accessibleTree = permissionService.getAccessibleTree();
if ((dataOwnCode != DataOwnEnum.USER_FILE.code) && (!accessibleTree.contains(parentId) && parentId.length() == elePropertyConfig.getPrjFolderMaxLength())) {
if ((dataOwnCode != USER_FILE.code) && (!accessibleTree.contains(parentId) && parentId.length() == elePropertyConfig.getPrjFolderMaxLength())) {
throw new PermissionDeniedException();
}
}
@ -792,13 +794,14 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.eq(EdFileInfo::getParentId, parentId)
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FOLDER.code);
if (DataOwnEnum.isUserCode(dataOwnCode)) {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code);
} else if (DataOwnEnum.isSysCode(dataOwnCode)) {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.SYS_PRJ.code);
} else {
queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.REPO_PRJ.code);
DataOwnEnum obj = DataOwnEnum.getEnumByCode(dataOwnCode);
switch (Objects.requireNonNull(obj)) {
case USER_FILE, USER_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.USER_PRJ.code);
case SYS_FILE, SYS_PRJ -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.SYS_PRJ.code);
case REPO_PRJ, REPO_FILE -> queryWrapper.eq(EdFileInfo::getDataOwn, DataOwnEnum.REPO_PRJ.code);
default -> ElectromagneticResultUtil.fail("-1", "参数错误");
}
long dirCount = this.baseMapper.selectCount(queryWrapper);
Assert.isTrue(dirCount == 0, "文件 {} 上传到 {} 失败,层级结构不允许上传文件,同名同后缀的处理方式为 {}", fileName, destPath, strategyStr);
EdFileInfo newEdFileInfo = new EdFileInfo();
@ -942,7 +945,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
throw new BizException(info);
}
if (strategy == FileRepeatEnum.IGNORE.code) {
if (strategy == IGNORE.code) {
return srcFileInfo;
} else if (strategy == FileRepeatEnum.REVERSION.code) {
// 做版本更新
@ -1060,7 +1063,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FILE.code)
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code);
if (dataOwnCode == DataOwnEnum.USER_FILE.code) {
if (dataOwnCode == USER_FILE.code) {
lambdaQuery.eq(EdFileInfo::getCreatedBy, UserThreadLocal.getUserId());
} else {
List<String> accessibleIds = permissionService.getAccessibleTree();
@ -1116,7 +1119,8 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
log.info(info);
throw new BizException(info);
}
if (strategy == FileRepeatEnum.IGNORE.code) {
if (strategy == IGNORE.code) {
return destFolderInfo;
}
if (strategy == FileRepeatEnum.REVERSION.code) {
@ -1499,7 +1503,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
e.setIsFavorite(isFavorite(UserThreadLocal.getUserId(), e.getId()) ? 1 : 0);
if (String.valueOf(DataOwnEnum.REPO_FILE.code).equals(e.getDataOwn())) {
e.setIsPersonal(2);
} else if (String.valueOf(DataOwnEnum.USER_FILE.code).equals(e.getDataOwn()) && Objects.equals(e.getCreatedBy(), UserThreadLocal.getUserId())) {
} else if (String.valueOf(USER_FILE.code).equals(e.getDataOwn()) && Objects.equals(e.getCreatedBy(), UserThreadLocal.getUserId())) {
e.setIsPersonal(1);
} else {
e.setIsPersonal(0);

View File

@ -60,4 +60,13 @@ public enum DataOwnEnum {
}
return "";
}
public static DataOwnEnum getEnumByCode(int code) {
for (DataOwnEnum e : DataOwnEnum.values()) {
if (e.code == code) {
return e;
}
}
return null;
}
}