微调
This commit is contained in:
@@ -44,7 +44,9 @@ public enum DetectionResponseEnum {
|
|||||||
DEV_UN_CHECKED("A02053", "装置还未检测完成!"),
|
DEV_UN_CHECKED("A02053", "装置还未检测完成!"),
|
||||||
DEV_UN_REPORT("A02054", "装置报告未生成!"),
|
DEV_UN_REPORT("A02054", "装置报告未生成!"),
|
||||||
DEVICE_DIS_ERROR("A02055", "装置配置异常"),
|
DEVICE_DIS_ERROR("A02055", "装置配置异常"),
|
||||||
DEVICE_DELETE("A02056", "设备无法删除,已绑定计划!");
|
DEVICE_DELETE("A02056", "设备无法删除,已绑定计划!"),
|
||||||
|
CREATE_DIRECTORY_FAILED("A02057", "创建目录失败"),
|
||||||
|
DELETE_DIRECTORY_FAILED("A02058", "删除目录失败");
|
||||||
|
|
||||||
private final String code;
|
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.enums.PlanReportStateEnum;
|
||||||
import com.njcn.gather.plan.pojo.po.AdPlan;
|
import com.njcn.gather.plan.pojo.po.AdPlan;
|
||||||
import com.njcn.gather.plan.service.IAdPlanService;
|
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.mapper.PqReportMapper;
|
||||||
import com.njcn.gather.report.pojo.DevReportParam;
|
import com.njcn.gather.report.pojo.DevReportParam;
|
||||||
import com.njcn.gather.report.pojo.enums.ReportResponseEnum;
|
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.core.io.FileSystemResource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@@ -247,75 +247,89 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
|
|||||||
* @param isAdd
|
* @param isAdd
|
||||||
*/
|
*/
|
||||||
private void uploadFile(ReportParam reportParam, PqReport pqReport, boolean 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 baseFile = reportParam.getBaseFile();
|
||||||
MultipartFile detailFile = reportParam.getDetailFile();
|
MultipartFile detailFile = reportParam.getDetailFile();
|
||||||
if (ObjectUtil.isNotNull(baseFile) && ObjectUtil.isNotNull(detailFile) && !baseFile.isEmpty() && !detailFile.isEmpty()) {
|
String newDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator;
|
||||||
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);
|
|
||||||
|
|
||||||
if (!"docx".equals(baseSuffix) || !"docx".equals(detailSuffix)) {
|
if (isAdd) {
|
||||||
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty() && ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
|
||||||
}
|
|
||||||
if (baseFileName.equals(detailFileName)) {
|
|
||||||
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
pqReport.setBasePath(uploadDir + baseFileName + ".docx");
|
String baseOriginalFilename = baseFile.getOriginalFilename();
|
||||||
pqReport.setDetailPath(uploadDir + detailFileName + ".docx");
|
String detailOriginalFilename = detailFile.getOriginalFilename();
|
||||||
|
|
||||||
try {
|
if (!baseOriginalFilename.endsWith(".docx") || !detailOriginalFilename.endsWith(".docx")) {
|
||||||
this.createDirectory(uploadDir);
|
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||||
|
}
|
||||||
|
if (baseOriginalFilename.equals(detailOriginalFilename)) {
|
||||||
|
throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
// 保存文件
|
pqReport.setBasePath(newDir + baseOriginalFilename);
|
||||||
byte[] baseBytes = baseFile.getBytes();
|
pqReport.setDetailPath(newDir + detailOriginalFilename);
|
||||||
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pqReport.getBasePath()));
|
|
||||||
bufferedOutputStream.write(baseBytes);
|
|
||||||
bufferedOutputStream.flush();
|
|
||||||
bufferedOutputStream.close();
|
|
||||||
|
|
||||||
byte[] detailBytes = detailFile.getBytes();
|
this.createDirectory(newDir);
|
||||||
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(pqReport.getDetailPath()));
|
this.clearDirectory(newDir);
|
||||||
bufferedOutputStream.write(detailBytes);
|
this.uploadFile(baseFile, pqReport.getBasePath());
|
||||||
bufferedOutputStream.flush();
|
this.uploadFile(detailFile, pqReport.getDetailPath());
|
||||||
|
} else {
|
||||||
bufferedOutputStream.close();
|
throw new BusinessException(ReportResponseEnum.FILE_NOT_NULL);
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (isAdd) {
|
PqReport oldPqReport = this.getById(pqReport.getId());
|
||||||
throw new BusinessException(ReportResponseEnum.FILE_NOT_NULL);
|
String oldDir = oldPqReport.getBasePath().substring(0, oldPqReport.getBasePath().lastIndexOf(File.separator) + 1);
|
||||||
} 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 (!oldDir.equals(newDir)) {
|
if (ObjectUtil.isNotNull(baseFile) && !baseFile.isEmpty()) {
|
||||||
// 文件夹重命名
|
baseFileOriginalFilename = baseFile.getOriginalFilename();
|
||||||
String oldBasePathStr = oldPqReport.getBasePath();
|
if (!baseFileOriginalFilename.endsWith(".docx")) {
|
||||||
String baseName = oldBasePathStr.substring(oldBasePathStr.lastIndexOf(File.separator) + 1);
|
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||||
Path oldBasePath = Paths.get(oldBasePathStr);
|
}
|
||||||
Path newBasePath = Paths.get(newDir + baseName);
|
}
|
||||||
pqReport.setBasePath(newDir + baseName);
|
|
||||||
|
|
||||||
String oldDetailPathStr = oldPqReport.getDetailPath();
|
String detailFileOriginalFilename = "";
|
||||||
String detailName = oldDetailPathStr.substring(oldDetailPathStr.lastIndexOf(File.separator) + 1);
|
if (ObjectUtil.isNotNull(detailFile) && !detailFile.isEmpty()) {
|
||||||
Path oldDetailPath = Paths.get(oldDetailPathStr);
|
detailFileOriginalFilename = detailFile.getOriginalFilename();
|
||||||
Path newDetailPath = Paths.get(newDir + detailName);
|
if (!detailFileOriginalFilename.endsWith(".docx")) {
|
||||||
pqReport.setDetailPath(newDir + detailName);
|
throw new BusinessException(ReportResponseEnum.FILE_SUFFIX_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if (!"".equals(baseFileOriginalFilename) && !"".equals(detailFileOriginalFilename)) {
|
||||||
this.createDirectory(newDir);
|
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(oldBasePath, newBasePath);
|
||||||
Files.move(oldDetailPath, newDetailPath);
|
Files.move(oldDetailPath, newDetailPath);
|
||||||
if (!oldPqReport.getName().equals(reportParam.getName()) && !this.existSameName(pqReport.getId(), oldPqReport.getName())) {
|
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 {
|
} else {
|
||||||
Paths.get(oldDir).toFile().delete();
|
Paths.get(oldDir).toFile().delete();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} else {
|
||||||
throw new BusinessException(ReportResponseEnum.FILE_RENAME_FAILED);
|
// 文件夹重命名
|
||||||
|
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 id
|
||||||
* @param name
|
* @param name
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean existSameName(String id, String name) {
|
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
|
* @param dirPath
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void createDirectory(String dirPath) throws IOException {
|
private void createDirectory(String dirPath) {
|
||||||
// 创建上传目录(如果不存在)
|
// 创建上传目录(如果不存在)
|
||||||
Path uploadPath = Paths.get(dirPath);
|
Path uploadPath = Paths.get(dirPath);
|
||||||
if (!Files.exists(uploadPath)) {
|
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)) {
|
if (ArrayUtil.isNotEmpty(files)) {
|
||||||
for (int i = 0; i < files.length; i++) {
|
for (int i = 0; i < files.length; i++) {
|
||||||
files[i].delete();
|
files[i].delete();
|
||||||
@@ -399,13 +467,17 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
|
|||||||
*
|
*
|
||||||
* @param dirPath
|
* @param dirPath
|
||||||
*/
|
*/
|
||||||
private void recursionDeleteDirectory(String dirPath) throws IOException {
|
private void recursionDeleteDirectory(String dirPath) {
|
||||||
Path path = Paths.get(dirPath);
|
Path path = Paths.get(dirPath);
|
||||||
if (Files.exists(path)) {
|
if (Files.exists(path)) {
|
||||||
Files.walk(path)
|
try {
|
||||||
.sorted(Comparator.reverseOrder())
|
Files.walk(path)
|
||||||
.map(Path::toFile)
|
.sorted(Comparator.reverseOrder())
|
||||||
.forEach(File::delete);
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new BusinessException(DetectionResponseEnum.DELETE_DIRECTORY_FAILED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user