增加异常处理相关逻辑。

This commit is contained in:
chenxudong 2024-12-18 16:03:05 +08:00
parent e76449212e
commit 1b4492e812
4 changed files with 188 additions and 112 deletions

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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<EdFileInfoMapper, EdFileI
} catch (Exception e) {
String info = StrFormatter.format("文件创建失败,具体为--->{}", 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<EdFileInfoMapper, EdFileI
} catch (Exception e) {
String info = StrFormatter.format("修改工程名异常--->{}{}", newPrjName, e.getMessage());
log.error(info, e);
throw new RuntimeException(e);
throw new BizException(-1, info);
}
return ElectromagneticResultUtil.success(true);
}
@ -163,8 +163,9 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public ElectromagneticResult<?> delete(String prjId) {
try {
List<String> ids = new ArrayList<>();
ids.add(prjId);
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
@ -174,6 +175,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
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,6 +225,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
return ElectromagneticResultUtil.fail("-1", info);
}
try {
int id = Integer.parseInt(this.baseMapper.maxPrjId());
Date now = new Date();
String currentUserId = UserThreadLocal.getUserId();
@ -245,6 +252,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String targetFilePath = getPath(paths) + File.separator + folderName;
fileSystemService.createDirectory(targetFilePath);
return ElectromagneticResultUtil.success(true);
} catch (Exception e) {
String info = "添加子集失败";
log.error(info, e);
throw new BizException(-1, info);
}
}
/**
@ -255,6 +267,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
@Override
public ElectromagneticResult<?> queryAllPrjInfo() {
try {
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
.select(EdFileInfo::getId)
.eq(EdFileInfo::getParentId, 0)
@ -286,6 +299,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
projectVOS.add(projectVO);
}
return ElectromagneticResultUtil.success(projectVOS);
} catch (Exception e) {
String info = "查询项目失败";
log.error(info, e);
throw new BizException(-1, info);
}
}
/**
@ -297,7 +315,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
@Override
@Transactional(rollbackFor = Exception.class)
public ElectromagneticResult<?> folderResort(List<FolderResortDTO> folderResortDTOList) {
try {
for (FolderResortDTO folderResortDTO : folderResortDTOList) {
LambdaUpdateWrapper<EdFileInfo> updateWrapper = Wrappers.lambdaUpdate(EdFileInfo.class)
.set(EdFileInfo::getSort, folderResortDTO.getSort())
@ -305,6 +323,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
this.update(updateWrapper);
}
return ElectromagneticResultUtil.success(true);
} catch (Exception e) {
String info = "子集重排序异常";
log.error(info, e);
throw new BizException(-1, info);
}
}
/**
@ -314,12 +337,19 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public ElectromagneticResult<?> publish(String prjId) {
try {
LambdaUpdateWrapper<EdFileInfo> 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,6 +360,8 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
*/
@Override
public ElectromagneticResult<?> deleteFolder(String fileId) {
try {
// TODO是否需要判断文件夹是否为空
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
.select(EdFileInfo::getId, EdFileInfo::getFilePath)
@ -365,6 +397,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.eq(EdFileInfo::getId, id));
}
return ElectromagneticResultUtil.success(true);
} catch (Exception e) {
String info = "删除子集异常";
log.error(info, e);
throw new BizException(-1, info);
}
}
/**
@ -424,7 +461,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.setUpdatedBy(currentUserId);
this.save(targetFile);
String targetSysFilePath = getSysFilePathByDbPath(targetFile.getFilePath()) + File.separator + sourceFileName;
FileUtil.mkdir(targetSysFilePath);
fileSystemService.createDirectory(targetSysFilePath);
targetEdFileInfos = this.baseMapper.selectAllAdminFolder(targetId);
}
}
@ -452,7 +489,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String sysFilePath = getSysFilePathByDbPath(fileInfo.getFilePath());
this.baseMapper.update(null, Wrappers.lambdaUpdate(EdFileInfo.class)
.set(EdFileInfo::getFileName, newFolderName));
FileUtil.rename(new File(sysFilePath), newFolderName, true);
fileSystemService.renameFile(sysFilePath, newFolderName);
return ElectromagneticResultUtil.success(true);
} catch(Exception e) {
String info = StrFormatter.format("修改子集名称为{}失败", newFolderName);

View File

@ -27,6 +27,11 @@ public class FileSystemServiceImpl implements FileSystemService {
public void save(InputStream inputStream, String destination) {
}
@Override
public void renameFile(String sourcePath, String newName) {
FileUtil.rename(new File(sourcePath), newName, true);
}
@Override
public void renameFile(String sourcePath, String oldName, String newName) {
File sourceFile = new File(sourcePath + File.separator + oldName);