微调
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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,75 +247,89 @@ 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)) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||
}
|
||||
if (baseFileName.equals(detailFileName)) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
|
||||
}
|
||||
if (isAdd) {
|
||||
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty() && ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
|
||||
|
||||
pqReport.setBasePath(uploadDir + baseFileName + ".docx");
|
||||
pqReport.setDetailPath(uploadDir + detailFileName + ".docx");
|
||||
String baseOriginalFilename = baseFile.getOriginalFilename();
|
||||
String detailOriginalFilename = detailFile.getOriginalFilename();
|
||||
|
||||
try {
|
||||
this.createDirectory(uploadDir);
|
||||
if (!baseOriginalFilename.endsWith(".docx") || !detailOriginalFilename.endsWith(".docx")) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||
}
|
||||
if (baseOriginalFilename.equals(detailOriginalFilename)) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
byte[] baseBytes = baseFile.getBytes();
|
||||
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pqReport.getBasePath()));
|
||||
bufferedOutputStream.write(baseBytes);
|
||||
bufferedOutputStream.flush();
|
||||
bufferedOutputStream.close();
|
||||
pqReport.setBasePath(newDir + baseOriginalFilename);
|
||||
pqReport.setDetailPath(newDir + detailOriginalFilename);
|
||||
|
||||
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);
|
||||
PqReport oldPqReport = this.getById(pqReport.getId());
|
||||
String oldDir = oldPqReport.getBasePath().substring(0, oldPqReport.getBasePath().lastIndexOf(File.separator) + 1);
|
||||
|
||||
//若修改了文件名称、版本号,则需要重命名文件
|
||||
if (!oldDir.equals(newDir)) {
|
||||
// 文件夹重命名
|
||||
String oldBasePathStr = oldPqReport.getBasePath();
|
||||
String baseName = oldBasePathStr.substring(oldBasePathStr.lastIndexOf(File.separator) + 1);
|
||||
Path oldBasePath = Paths.get(oldBasePathStr);
|
||||
Path newBasePath = Paths.get(newDir + baseName);
|
||||
pqReport.setBasePath(newDir + baseName);
|
||||
String baseFileOriginalFilename = "";
|
||||
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty()) {
|
||||
baseFileOriginalFilename = baseFile.getOriginalFilename();
|
||||
if (!baseFileOriginalFilename.endsWith(".docx")) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
String oldDetailPathStr = oldPqReport.getDetailPath();
|
||||
String detailName = oldDetailPathStr.substring(oldDetailPathStr.lastIndexOf(File.separator) + 1);
|
||||
Path oldDetailPath = Paths.get(oldDetailPathStr);
|
||||
Path newDetailPath = Paths.get(newDir + detailName);
|
||||
pqReport.setDetailPath(newDir + detailName);
|
||||
String detailFileOriginalFilename = "";
|
||||
if (ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
|
||||
detailFileOriginalFilename = detailFile.getOriginalFilename();
|
||||
if (!detailFileOriginalFilename.endsWith(".docx")) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
this.createDirectory(newDir);
|
||||
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();
|
||||
String baseName = oldBasePathStr.substring(oldBasePathStr.lastIndexOf(File.separator) + 1);
|
||||
Path oldBasePath = Paths.get(oldBasePathStr);
|
||||
Path newBasePath = Paths.get(newDir + baseName);
|
||||
pqReport.setBasePath(newDir + baseName);
|
||||
|
||||
String oldDetailPathStr = oldPqReport.getDetailPath();
|
||||
String detailName = oldDetailPathStr.substring(oldDetailPathStr.lastIndexOf(File.separator) + 1);
|
||||
Path oldDetailPath = Paths.get(oldDetailPathStr);
|
||||
Path newDetailPath = Paths.get(newDir + detailName);
|
||||
pqReport.setDetailPath(newDir + detailName);
|
||||
|
||||
try {
|
||||
// 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();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(ReportResponseEnum.FILE_RENAME_FAILED);
|
||||
} 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)) {
|
||||
Files.createDirectories(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)) {
|
||||
Files.walk(path)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
try {
|
||||
Files.walk(path)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(DetectionResponseEnum.DELETE_DIRECTORY_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user