forked from toolchaintechnologycenter/spdm-backend
feat:报告模板库
This commit is contained in:
@@ -2,6 +2,7 @@ package com.sdm.data.controller;
|
||||
|
||||
import com.sdm.common.annotation.IgnoreAuth;
|
||||
import com.sdm.common.common.SdmResponse;
|
||||
import com.sdm.common.entity.enums.DirTypeEnum;
|
||||
import com.sdm.common.entity.req.data.*;
|
||||
import com.sdm.common.entity.req.system.LaunchApproveReq;
|
||||
import com.sdm.common.entity.resp.PageDataResp;
|
||||
@@ -370,14 +371,29 @@ public class DataFileController implements IDataFeignClient {
|
||||
@PostMapping(value = "/uploadScriptFile")
|
||||
@Operation(summary = "上传脚本文件", description = "上传脚本文件")
|
||||
public SdmResponse uploadScriptFile(@RequestParam("script") MultipartFile script) {
|
||||
return IDataFileService.uploadScriptFile(script);
|
||||
return IDataFileService.uploadScriptAndReportFile(script, DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
}
|
||||
|
||||
// 根基脚本文件ID更新当前脚本文件
|
||||
@PostMapping(value = "/updateScriptFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "更新脚本文件", description = "更新脚本文件")
|
||||
public SdmResponse updateScriptFile(UpdateScriptReq req) {
|
||||
return IDataFileService.updateScriptFile(req);
|
||||
public SdmResponse updateScriptFile(UpdateScriptAndReportReq req) {
|
||||
req.setDirName(DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
return IDataFileService.updateScriptAndReportFile(req);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/uploadReportTemplateFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "上传报告模板文件", description = "上传报告模板文件")
|
||||
public SdmResponse<Long> uploadReportTemplateFile(UploadFilesReq req) {
|
||||
return IDataFileService.uploadScriptAndReportFile(req.getFile(), DirTypeEnum.REPORT_TEMPLATE_DIR.getDirName());
|
||||
}
|
||||
|
||||
// 根基脚本文件ID更新当前脚本文件
|
||||
@PostMapping(value = "/updateReportTemplateFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@Operation(summary = "更新报告模板文件", description = "更新报告模板文件")
|
||||
public SdmResponse<Long> updateReportTemplateFile(UpdateScriptAndReportReq req) {
|
||||
req.setDirName(DirTypeEnum.REPORT_TEMPLATE_DIR.getDirName());
|
||||
return IDataFileService.updateScriptAndReportFile(req);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.sdm.data.model.req;
|
||||
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import com.sdm.common.entity.enums.FileBizTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Schema(description = "更新脚本文件请求参数")
|
||||
public class UpdateScriptReq {
|
||||
@Schema(description = "需要更新的脚本文件ID")
|
||||
private Long scriptFileId;
|
||||
|
||||
@Schema(description = "脚本文件名称")
|
||||
private String fileName;
|
||||
|
||||
@Schema(description = "需要更新的脚本文件")
|
||||
private MultipartFile updateFile;
|
||||
}
|
||||
@@ -209,15 +209,15 @@ public interface IDataFileService {
|
||||
|
||||
/**
|
||||
* 上传脚本文件
|
||||
* @param scriptFile 脚本
|
||||
* @param file 文件
|
||||
* @return 上传文件id
|
||||
*/
|
||||
default SdmResponse<Long> uploadScriptFile(MultipartFile scriptFile){
|
||||
default SdmResponse<Long> uploadScriptAndReportFile(MultipartFile file, String dirName){
|
||||
return null;
|
||||
}
|
||||
|
||||
// 根基脚本文件ID更新当前脚本文件
|
||||
default SdmResponse updateScriptFile(UpdateScriptReq req) {
|
||||
default SdmResponse<Long> updateScriptAndReportFile(UpdateScriptAndReportReq req) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -2045,25 +2045,25 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SdmResponse<Long> uploadScriptFile(MultipartFile scriptFile) {
|
||||
// 先创建 script 目录
|
||||
String dirMinioObjectKey = getDirMinioObjectKey(DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
public SdmResponse<Long> uploadScriptAndReportFile(MultipartFile file, String dirName) {
|
||||
// 先创建根目录
|
||||
String dirMinioObjectKey = getDirMinioObjectKey(dirName);
|
||||
Optional<FileMetadataInfo> fileMetadataInfoByObjectKey = getFileMetadataInfoByObjectKey(dirMinioObjectKey, ThreadLocalContext.getTenantId());
|
||||
// 检查目录是否已存在
|
||||
if (!fileMetadataInfoByObjectKey.isPresent()) {
|
||||
return SdmResponse.failed("script 目录不存在,等待initSystemDirectory 初始化完成");
|
||||
return SdmResponse.failed(dirMinioObjectKey + " 目录不存在,等待initSystemDirectory 初始化完成");
|
||||
}
|
||||
|
||||
// 检查文件是否为空
|
||||
if (scriptFile == null || scriptFile.isEmpty()) {
|
||||
return SdmResponse.failed("请选择要上传的脚本文件");
|
||||
if (file == null || file.isEmpty()) {
|
||||
return SdmResponse.failed("请选择要上传的文件");
|
||||
}
|
||||
|
||||
// 获取 script 目录的 id
|
||||
FileMetadataInfo scriptDirMetadataInfo = fileMetadataInfoByObjectKey.get();
|
||||
Long parScriptDirId = scriptDirMetadataInfo.getId();
|
||||
// 获取上级目录(也是根目录)的 id
|
||||
FileMetadataInfo dirMetadataInfo = fileMetadataInfoByObjectKey.get();
|
||||
Long parDirId = dirMetadataInfo.getId();
|
||||
|
||||
String originalFilename = scriptFile.getOriginalFilename();
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
|
||||
|
||||
// 生成带时间戳的文件名以避免冲突
|
||||
@@ -2071,43 +2071,42 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
|
||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||
String newFilename = filenameWithoutSuffix + "_" + timestamp + "." + suffix;
|
||||
|
||||
// 再上传脚本文件
|
||||
String scriptFileMinioObjectKey = getFileMinioObjectKey(scriptDirMetadataInfo.getObjectKey() + newFilename);
|
||||
// 再上传文件
|
||||
String fileMinioObjectKey = getFileMinioObjectKey(dirMetadataInfo.getObjectKey() + newFilename);
|
||||
|
||||
try {
|
||||
minioService.uploadFile(scriptFile, scriptFileMinioObjectKey, null,scriptDirMetadataInfo.getBucketName());
|
||||
minioService.uploadFile(file, fileMinioObjectKey, null,dirMetadataInfo.getBucketName());
|
||||
|
||||
// 创建目录元数据并保存到数据库
|
||||
FileMetadataInfo fileInfo = createFileMetadata(scriptFileMinioObjectKey, originalFilename, null,
|
||||
null, null, null, parScriptDirId, scriptFile.getSize());
|
||||
FileMetadataInfo fileInfo = createFileMetadata(fileMinioObjectKey, originalFilename, null,
|
||||
null, null, null, parDirId, file.getSize());
|
||||
fileMetadataInfoService.save(fileInfo);
|
||||
return SdmResponse.success(fileInfo.getId());
|
||||
} catch (Exception e) {
|
||||
minioService.deleteFile(scriptFileMinioObjectKey, scriptDirMetadataInfo.getBucketName() );
|
||||
log.error("上传传脚本文件", e);
|
||||
throw new RuntimeException("上传传脚本文件: " + e.getMessage(), e);
|
||||
minioService.deleteFile(fileMinioObjectKey, dirMetadataInfo.getBucketName());
|
||||
log.error("上传文件失败", e);
|
||||
throw new RuntimeException("上传文件失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SdmResponse updateScriptFile(UpdateScriptReq req) {
|
||||
public SdmResponse<Long> updateScriptAndReportFile(UpdateScriptAndReportReq req) {
|
||||
// 先创建 script 目录
|
||||
String dirMinioObjectKey = getDirMinioObjectKey(DirTypeEnum.SCRIPT_DIR.getDirName());
|
||||
Optional<FileMetadataInfo> scriptDirInfo = getFileMetadataInfoByObjectKey(dirMinioObjectKey,ThreadLocalContext.getTenantId() );
|
||||
String dirMinioObjectKey = getDirMinioObjectKey(req.getDirName());
|
||||
Optional<FileMetadataInfo> dirInfo = getFileMetadataInfoByObjectKey(dirMinioObjectKey,ThreadLocalContext.getTenantId() );
|
||||
// 检查目录是否已存在
|
||||
if (!scriptDirInfo.isPresent()) {
|
||||
return SdmResponse.failed("script 目录不存在,等待initSystemDirectory 初始化完成");
|
||||
if (!dirInfo.isPresent()) {
|
||||
return SdmResponse.failed(req.getDirName() + " 目录不存在,等待initSystemDirectory 初始化完成");
|
||||
}
|
||||
|
||||
// 检查文件是否为空
|
||||
MultipartFile updateFile = req.getUpdateFile();
|
||||
if (ObjectUtils.isEmpty(updateFile)) {
|
||||
return SdmResponse.failed("请选择要更新的脚本文件");
|
||||
return SdmResponse.failed("请选择要更新的文件");
|
||||
}
|
||||
|
||||
// 获取 script 目录的 id
|
||||
FileMetadataInfo scriptDirMetadataInfo = scriptDirInfo.get();
|
||||
Long parScriptDirId = scriptDirMetadataInfo.getId();
|
||||
FileMetadataInfo dirMetadataInfo = dirInfo.get();
|
||||
|
||||
String originalFilename = req.getFileName();
|
||||
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
|
||||
@@ -2118,23 +2117,23 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
|
||||
String newFilename = filenameWithoutSuffix + "_" + timestamp + "." + suffix;
|
||||
|
||||
// 再上传脚本文件
|
||||
String scriptFileMinioObjectKey = getFileMinioObjectKey(scriptDirMetadataInfo.getObjectKey() + newFilename);
|
||||
String fileMinioObjectKey = getFileMinioObjectKey(dirMetadataInfo.getObjectKey() + newFilename);
|
||||
|
||||
try {
|
||||
minioService.uploadFile(updateFile, scriptFileMinioObjectKey, null,scriptDirMetadataInfo.getBucketName());
|
||||
minioService.uploadFile(updateFile, fileMinioObjectKey, null,dirMetadataInfo.getBucketName());
|
||||
|
||||
// 创建目录元数据并保存到数据库
|
||||
fileMetadataInfoService.lambdaUpdate()
|
||||
.set(FileMetadataInfo::getObjectKey,scriptFileMinioObjectKey)
|
||||
.set(FileMetadataInfo::getObjectKey,fileMinioObjectKey)
|
||||
.set(FileMetadataInfo::getOriginalName,originalFilename)
|
||||
.set(FileMetadataInfo::getFileSize,updateFile.getSize())
|
||||
.eq(FileMetadataInfo::getId,req.getScriptFileId())
|
||||
.eq(FileMetadataInfo::getId,req.getUpdateFileId())
|
||||
.update();
|
||||
return SdmResponse.success(req.getScriptFileId());
|
||||
return SdmResponse.success(req.getUpdateFileId());
|
||||
} catch (Exception e) {
|
||||
minioService.deleteFile(scriptFileMinioObjectKey, scriptDirMetadataInfo.getBucketName());
|
||||
log.error("更新传脚本文件", e);
|
||||
throw new RuntimeException("更新传脚本文件: " + e.getMessage(), e);
|
||||
minioService.deleteFile(fileMinioObjectKey, dirMetadataInfo.getBucketName());
|
||||
log.error("更新文件失败", e);
|
||||
throw new RuntimeException("更新文件失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user