This commit is contained in:
caozehui
2025-03-31 11:25:06 +08:00
parent 09972e0764
commit af58306d96
2 changed files with 146 additions and 72 deletions

View File

@@ -44,7 +44,9 @@ public enum DetectionResponseEnum {
DEV_UN_CHECKED("A02053", "装置还未检测完成!"),
DEV_UN_REPORT("A02054", "装置报告未生成!"),
DEVICE_DIS_ERROR("A02055", "装置配置异常"),
DEVICE_DELETE("A02056", "设备无法删除,已绑定计划!");
DEVICE_DELETE("A02056", "设备无法删除,已绑定计划!"),
CREATE_DIRECTORY_FAILED("A02057", "创建目录失败"),
DELETE_DIRECTORY_FAILED("A02058", "删除目录失败");
private final String code;

View File

@@ -24,6 +24,7 @@ import com.njcn.gather.err.service.IPqErrSysDtlsService;
import com.njcn.gather.plan.pojo.enums.PlanReportStateEnum;
import com.njcn.gather.plan.pojo.po.AdPlan;
import com.njcn.gather.plan.service.IAdPlanService;
import com.njcn.gather.pojo.enums.DetectionResponseEnum;
import com.njcn.gather.report.mapper.PqReportMapper;
import com.njcn.gather.report.pojo.DevReportParam;
import com.njcn.gather.report.pojo.enums.ReportResponseEnum;
@@ -67,7 +68,6 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
@@ -247,59 +247,71 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
* @param isAdd
*/
private void uploadFile(ReportParam reportParam, PqReport pqReport, boolean isAdd) {
String uploadDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator;
//校验文件后缀
MultipartFile baseFile = reportParam.getBaseFile();
MultipartFile detailFile = reportParam.getDetailFile();
if (ObjectUtil.isNotNull(baseFile) && ObjectUtil.isNotNull(detailFile) && !baseFile.isEmpty() && !detailFile.isEmpty()) {
this.deleteFile(Collections.singletonList(pqReport.getId()));
String baseOriginalFilename = baseFile.getOriginalFilename();
String baseFileName = baseOriginalFilename.substring(0, baseOriginalFilename.lastIndexOf("."));
String baseSuffix = baseOriginalFilename.substring(baseOriginalFilename.lastIndexOf(".") + 1);
String detailOriginalFilename = detailFile.getOriginalFilename();
String detailFileName = detailOriginalFilename.substring(0, detailOriginalFilename.lastIndexOf("."));
String detailSuffix = detailOriginalFilename.substring(detailOriginalFilename.lastIndexOf(".") + 1);
String newDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator;
if (!"docx".equals(baseSuffix) || !"docx".equals(detailSuffix)) {
if (isAdd) {
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty() && ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
String baseOriginalFilename = baseFile.getOriginalFilename();
String detailOriginalFilename = detailFile.getOriginalFilename();
if (!baseOriginalFilename.endsWith(".docx") || !detailOriginalFilename.endsWith(".docx")) {
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
}
if (baseFileName.equals(detailFileName)) {
if (baseOriginalFilename.equals(detailOriginalFilename)) {
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
}
pqReport.setBasePath(uploadDir + baseFileName + ".docx");
pqReport.setDetailPath(uploadDir + detailFileName + ".docx");
pqReport.setBasePath(newDir + baseOriginalFilename);
pqReport.setDetailPath(newDir + detailOriginalFilename);
try {
this.createDirectory(uploadDir);
// 保存文件
byte[] baseBytes = baseFile.getBytes();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pqReport.getBasePath()));
bufferedOutputStream.write(baseBytes);
bufferedOutputStream.flush();
bufferedOutputStream.close();
byte[] detailBytes = detailFile.getBytes();
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pqReport.getDetailPath()));
bufferedOutputStream.write(detailBytes);
bufferedOutputStream.flush();
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED);
this.createDirectory(newDir);
this.clearDirectory(newDir);
this.uploadFile(baseFile, pqReport.getBasePath());
this.uploadFile(detailFile, pqReport.getDetailPath());
} else {
throw new BusinessException(ReportResponseEnum.FILE_NOT_NULL);
}
} else {
if (isAdd) {
throw new BusinessException(ReportResponseEnum.FILE_NOT_NULL);
} else {
PqReport oldPqReport = this.getById(pqReport.getId());
String newDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator;
String oldDir = oldPqReport.getBasePath().substring(0, oldPqReport.getBasePath().lastIndexOf(File.separator) + 1);
String baseFileOriginalFilename = "";
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty()) {
baseFileOriginalFilename = baseFile.getOriginalFilename();
if (!baseFileOriginalFilename.endsWith(".docx")) {
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
}
}
String detailFileOriginalFilename = "";
if (ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
detailFileOriginalFilename = detailFile.getOriginalFilename();
if (!detailFileOriginalFilename.endsWith(".docx")) {
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
}
}
if (!"".equals(baseFileOriginalFilename) && !"".equals(detailFileOriginalFilename)) {
if (baseFileOriginalFilename.equals(detailFileOriginalFilename)) {
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
}
}
if (!"".equals(baseFileOriginalFilename)) {
if (baseFileOriginalFilename.equals(oldPqReport.getDetailPath().substring(oldPqReport.getDetailPath().lastIndexOf(File.separator) + 1))) {
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
}
}
if (!"".equals(detailFileOriginalFilename)) {
if (detailFileOriginalFilename.equals(oldPqReport.getBasePath().substring(oldPqReport.getBasePath().lastIndexOf(File.separator) + 1))) {
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
}
}
//若修改了文件名称、版本号,则需要重命名文件
this.createDirectory(newDir);
if (!oldDir.equals(newDir)) {
// 文件夹重命名
String oldBasePathStr = oldPqReport.getBasePath();
@@ -315,7 +327,9 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
pqReport.setDetailPath(newDir + detailName);
try {
this.createDirectory(newDir);
// windows下文件夹名称不区分大小写
if (!oldDir.equalsIgnoreCase(newDir)) {
this.clearDirectory(newDir);
Files.move(oldBasePath, newBasePath);
Files.move(oldDetailPath, newDetailPath);
if (!oldPqReport.getName().equals(reportParam.getName()) && !this.existSameName(pqReport.getId(), oldPqReport.getName())) {
@@ -323,23 +337,66 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
} else {
Paths.get(oldDir).toFile().delete();
}
} else {
// 文件夹重命名
Paths.get(oldDir).toFile().renameTo(Paths.get(newDir).toFile());
}
} catch (IOException e) {
throw new BusinessException(ReportResponseEnum.FILE_RENAME_FAILED);
}
}
if (!"".equals(baseFileOriginalFilename)) {
pqReport.setBasePath(newDir + baseFileOriginalFilename);
Paths.get(oldPqReport.getBasePath()).toFile().delete();
Paths.get(newDir + oldPqReport.getBasePath().substring(oldPqReport.getBasePath().lastIndexOf(File.separator) + 1)).toFile().delete();
this.uploadFile(baseFile, pqReport.getBasePath());
}
if (!"".equals(detailFileOriginalFilename)) {
pqReport.setDetailPath(newDir + detailFileOriginalFilename);
Paths.get(oldPqReport.getDetailPath()).toFile().delete();
Paths.get(newDir + oldPqReport.getDetailPath().substring(oldPqReport.getDetailPath().lastIndexOf(File.separator) + 1)).toFile().delete();
this.uploadFile(detailFile, pqReport.getDetailPath());
}
}
}
private void uploadFile(MultipartFile file, String filePath) {
if (!ObjectUtil.isNull(file) && !file.isEmpty()) {
try {
// 如果文件存在,则先删除
Path path = Paths.get(filePath);
File file1 = path.toFile();
if (file1.exists()) {
file1.delete();
}
// 保存文件
byte[] bytes = file.getBytes();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedOutputStream.close();
} catch (IOException e) {
throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED);
}
}
}
/**
* 返回是否存在同名的报告
* 返回是否存在同名(但是不同版本号)的报告
*
* @param id
* @param name
* @return
*/
public boolean existSameName(String id, String name) {
return this.lambdaQuery().eq(PqReport::getName, name).ne(PqReport::getId, id).count() > 0;
return this.lambdaQuery().eq(PqReport::getName, name)
.ne(PqReport::getId, id)
.eq(PqReport::getState, DataStateEnum.ENABLE.getCode())
.count() > 0;
}
/**
@@ -348,14 +405,25 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
* @param dirPath
* @throws IOException
*/
private void createDirectory(String dirPath) throws IOException {
private void createDirectory(String dirPath) {
// 创建上传目录(如果不存在)
Path uploadPath = Paths.get(dirPath);
if (!Files.exists(uploadPath)) {
try {
Files.createDirectories(uploadPath);
} catch (IOException e) {
throw new BusinessException(DetectionResponseEnum.CREATE_DIRECTORY_FAILED);
}
//清空目录下的文件
File[] files = uploadPath.toFile().listFiles();
}
}
/**
* 清空目录下的文件
*
* @param dirPath
*/
private void clearDirectory(String dirPath) {
File[] files = Paths.get(dirPath).toFile().listFiles();
if (ArrayUtil.isNotEmpty(files)) {
for (int i = 0; i < files.length; i++) {
files[i].delete();
@@ -399,13 +467,17 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
*
* @param dirPath
*/
private void recursionDeleteDirectory(String dirPath) throws IOException {
private void recursionDeleteDirectory(String dirPath) {
Path path = Paths.get(dirPath);
if (Files.exists(path)) {
try {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
throw new BusinessException(DetectionResponseEnum.DELETE_DIRECTORY_FAILED);
}
}
}