From 1b4492e81265083f8666544e26b440e8594bf3e0 Mon Sep 17 00:00:00 2001 From: chenxudong Date: Wed, 18 Dec 2024 16:03:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manage/common/GlobalExceptionHandler.java | 32 +++ .../manage/service/FileSystemService.java | 4 +- .../serviceimpl/EdFileInfoServiceImpl.java | 259 ++++++++++-------- .../serviceimpl/FileSystemServiceImpl.java | 5 + 4 files changed, 188 insertions(+), 112 deletions(-) create mode 100644 electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java new file mode 100644 index 0000000..35aa853 --- /dev/null +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java @@ -0,0 +1,32 @@ +package com.electromagnetic.industry.software.manage.common; + +import com.electromagnetic.industry.software.common.exception.BizException; +import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; +import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@Slf4j +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(RuntimeException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ResponseBody + public ElectromagneticResult runTimeError(Throwable e) { + log.error(e.getMessage(), e); + return ElectromagneticResultUtil.fail("-1", e.getMessage()); + } + + @ExceptionHandler(BizException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ResponseBody + public ElectromagneticResult bizError(BizException e) { + log.error(e.getMessage(), e); + return ElectromagneticResultUtil.fail("-1", e.getMsg()); + } +} \ No newline at end of file diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/FileSystemService.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/FileSystemService.java index 9b2286d..41cd016 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/FileSystemService.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/FileSystemService.java @@ -12,7 +12,9 @@ public interface FileSystemService { void save(InputStream inputStream, String destination); - void renameFile(String sourcePath, String oldName, String newName); + void renameFile(String sourcePath, String newName); + + void renameFile(String sourcePath, String sourceName, String newName); boolean checkFolderExist(String newPath); } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java index 6644afc..a65f93f 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java @@ -1,7 +1,6 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNodeConfig; @@ -17,6 +16,7 @@ import com.electromagnetic.industry.software.common.enums.EffectFlagEnum; import com.electromagnetic.industry.software.common.enums.EleDataSaveStatusEnum; import com.electromagnetic.industry.software.common.enums.EleDataStatusEnum; import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum; +import com.electromagnetic.industry.software.common.exception.BizException; import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; import com.electromagnetic.industry.software.common.util.*; import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper; @@ -121,7 +121,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl{}", e.getMessage()); log.error(info, e); - return ElectromagneticResultUtil.fail("-1", e.getMessage()); + throw new BizException(-1, info); } return ElectromagneticResultUtil.success(true); } @@ -151,7 +151,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl{},{}", newPrjName, e.getMessage()); log.error(info, e); - throw new RuntimeException(e); + throw new BizException(-1, info); } return ElectromagneticResultUtil.success(true); } @@ -163,17 +163,23 @@ public class EdFileInfoServiceImpl extends ServiceImpl delete(String prjId) { - - List ids = new ArrayList<>(); - ids.add(prjId); - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId) - .likeRight(EdFileInfo::getFilePath, prjId + MYSQL_FILE_PATH_SPLIT); - List edFileInfos = this.baseMapper.selectList(queryWrapper); - edFileInfos.forEach(e -> ids.add(e.getId())); - Wrappers.lambdaUpdate(EdFileInfo.class).set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code).in(EdFileInfo::getId, ids); - return ElectromagneticResultUtil.success(true); + try { + List ids = new ArrayList<>(); + ids.add(prjId); + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) + .select(EdFileInfo::getId) + .likeRight(EdFileInfo::getFilePath, prjId + MYSQL_FILE_PATH_SPLIT); + List edFileInfos = this.baseMapper.selectList(queryWrapper); + edFileInfos.forEach(e -> ids.add(e.getId())); + Wrappers.lambdaUpdate(EdFileInfo.class).set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code).in(EdFileInfo::getId, ids); + return ElectromagneticResultUtil.success(true); + } catch (Exception e) { + String info = "删除项目失败"; + log.error(info, e); + throw new BizException(-1, info); + } } /** @@ -219,32 +225,38 @@ public class EdFileInfoServiceImpl extends ServiceImpl queryAllPrjInfo() { - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId) - .eq(EdFileInfo::getParentId, 0) - .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code); - List ids = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList()); + try { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) + .select(EdFileInfo::getId) + .eq(EdFileInfo::getParentId, 0) + .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code); + List ids = this.baseMapper.selectList(queryWrapper).stream().map(EdFileInfo::getId).collect(Collectors.toList()); - List projectVOS = new ArrayList<>(); + List projectVOS = new ArrayList<>(); - for (String id : ids) { - LambdaQueryWrapper queryWrapper1 = Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getParentId, EdFileInfo::getSort) - .like(EdFileInfo::getFilePath, MYSQL_FILE_PATH_SPLIT + id + MYSQL_FILE_PATH_SPLIT); - List edFileInfos = this.baseMapper.selectList(queryWrapper1); - // 转换为树 - TreeNodeConfig config = new TreeNodeConfig(); - config.setIdKey(EdFileInfo.Fields.id); - config.setParentIdKey(EdFileInfo.Fields.parentId); - config.setWeightKey(EdFileInfo.Fields.sort); + for (String id : ids) { + LambdaQueryWrapper queryWrapper1 = Wrappers.lambdaQuery(EdFileInfo.class) + .select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getParentId, EdFileInfo::getSort) + .like(EdFileInfo::getFilePath, MYSQL_FILE_PATH_SPLIT + id + MYSQL_FILE_PATH_SPLIT); + List edFileInfos = this.baseMapper.selectList(queryWrapper1); + // 转换为树 + TreeNodeConfig config = new TreeNodeConfig(); + config.setIdKey(EdFileInfo.Fields.id); + config.setParentIdKey(EdFileInfo.Fields.parentId); + config.setWeightKey(EdFileInfo.Fields.sort); - List> trees = TreeUtil.build(edFileInfos, "0", config, ((obj, treeNode) -> { - treeNode.putExtra(EdFileInfo.Fields.id, obj.getId()); - treeNode.putExtra(EdFileInfo.Fields.parentId, obj.getParentId()); - treeNode.putExtra(EdFileInfo.Fields.sort, obj.getSort()); - treeNode.putExtra(EdFileInfo.Fields.fileName, obj.getFileName()); - })); + List> trees = TreeUtil.build(edFileInfos, "0", config, ((obj, treeNode) -> { + treeNode.putExtra(EdFileInfo.Fields.id, obj.getId()); + treeNode.putExtra(EdFileInfo.Fields.parentId, obj.getParentId()); + treeNode.putExtra(EdFileInfo.Fields.sort, obj.getSort()); + treeNode.putExtra(EdFileInfo.Fields.fileName, obj.getFileName()); + })); - String jsonStr = JSONUtil.toJsonStr(trees); - ProjectVO projectVO = JSONUtil.toList(jsonStr, ProjectVO.class).get(0); - projectVOS.add(projectVO); + String jsonStr = JSONUtil.toJsonStr(trees); + ProjectVO projectVO = JSONUtil.toList(jsonStr, ProjectVO.class).get(0); + projectVOS.add(projectVO); + } + return ElectromagneticResultUtil.success(projectVOS); + } catch (Exception e) { + String info = "查询项目失败"; + log.error(info, e); + throw new BizException(-1, info); } - return ElectromagneticResultUtil.success(projectVOS); } /** @@ -297,14 +315,19 @@ public class EdFileInfoServiceImpl extends ServiceImpl folderResort(List folderResortDTOList) { - - for (FolderResortDTO folderResortDTO : folderResortDTOList) { - LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) - .set(EdFileInfo::getSort, folderResortDTO.getSort()) - .eq(EdFileInfo::getId, folderResortDTO.getId()); - this.update(updateWrapper); + try { + for (FolderResortDTO folderResortDTO : folderResortDTOList) { + LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) + .set(EdFileInfo::getSort, folderResortDTO.getSort()) + .eq(EdFileInfo::getId, folderResortDTO.getId()); + this.update(updateWrapper); + } + return ElectromagneticResultUtil.success(true); + } catch (Exception e) { + String info = "子集重排序异常"; + log.error(info, e); + throw new BizException(-1, info); } - return ElectromagneticResultUtil.success(true); } /** @@ -314,12 +337,19 @@ public class EdFileInfoServiceImpl extends ServiceImpl publish(String prjId) { - LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) - .set(EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code) - .likeRight(EdFileInfo::getFilePath, prjId); - this.update(updateWrapper); - return ElectromagneticResultUtil.success(true); + try { + LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) + .set(EdFileInfo::getDataStatus, EleDataStatusEnum.PUBLISHED.code) + .likeRight(EdFileInfo::getFilePath, prjId); + this.update(updateWrapper); + return ElectromagneticResultUtil.success(true); + } catch (Exception e) { + String info = "项目发布异常"; + log.error(info, e); + throw new BizException(-1, info); + } } /** @@ -330,41 +360,48 @@ public class EdFileInfoServiceImpl extends ServiceImpl deleteFolder(String fileId) { - // TODO是否需要判断文件夹是否为空 - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId, EdFileInfo::getFilePath) - .like(EdFileInfo::getFilePath, MYSQL_FILE_PATH_SPLIT + fileId + MYSQL_FILE_PATH_SPLIT) - .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code); - List edFileInfos = this.baseMapper.selectList(queryWrapper); - List ids = edFileInfos.stream().map(EdFileInfo::getId).collect(Collectors.toList()); - ids.add(fileId); - LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) - .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code) - .set(EdFileInfo::getSort, -1) - .in(EdFileInfo::getId, ids); - this.baseMapper.update(null, updateWrapper); - String[] tmpFileIds = edFileInfos.get(0).getFilePath().split(MYSQL_FILE_PATH_SPLIT); - String parentId = tmpFileIds[0]; - for (String tmpPathId : tmpFileIds) { - parentId = this.baseMapper.maxPrjId(); - if (fileId.equals(tmpPathId)) { - break; + try { + // TODO是否需要判断文件夹是否为空 + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) + .select(EdFileInfo::getId, EdFileInfo::getFilePath) + .like(EdFileInfo::getFilePath, MYSQL_FILE_PATH_SPLIT + fileId + MYSQL_FILE_PATH_SPLIT) + .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code); + List edFileInfos = this.baseMapper.selectList(queryWrapper); + List ids = edFileInfos.stream().map(EdFileInfo::getId).collect(Collectors.toList()); + ids.add(fileId); + LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class) + .set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code) + .set(EdFileInfo::getSort, -1) + .in(EdFileInfo::getId, ids); + this.baseMapper.update(null, updateWrapper); + + String[] tmpFileIds = edFileInfos.get(0).getFilePath().split(MYSQL_FILE_PATH_SPLIT); + String parentId = tmpFileIds[0]; + for (String tmpPathId : tmpFileIds) { + parentId = this.baseMapper.maxPrjId(); + if (fileId.equals(tmpPathId)) { + break; + } } + // 同层级的resort + List edFileInfos1 = this.baseMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class) + .select(EdFileInfo::getId, EdFileInfo::getSort) + .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code) + .eq(EdFileInfo::getParentId, parentId) + .orderByAsc(EdFileInfo::getSort)); + for (int i = 1; i <= edFileInfos1.size(); i++) { + String id = edFileInfos1.get(i).getId(); + this.baseMapper.update(null, Wrappers.lambdaUpdate(EdFileInfo.class) + .set(EdFileInfo::getSort, i) + .eq(EdFileInfo::getId, id)); + } + return ElectromagneticResultUtil.success(true); + } catch (Exception e) { + String info = "删除子集异常"; + log.error(info, e); + throw new BizException(-1, info); } - // 同层级的resort - List edFileInfos1 = this.baseMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class) - .select(EdFileInfo::getId, EdFileInfo::getSort) - .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code) - .eq(EdFileInfo::getParentId, parentId) - .orderByAsc(EdFileInfo::getSort)); - for (int i = 1; i <= edFileInfos1.size(); i++) { - String id = edFileInfos1.get(i).getId(); - this.baseMapper.update(null, Wrappers.lambdaUpdate(EdFileInfo.class) - .set(EdFileInfo::getSort, i) - .eq(EdFileInfo::getId, id)); - } - return ElectromagneticResultUtil.success(true); } /** @@ -424,7 +461,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl