fix: 优化附件逻辑
This commit is contained in:
parent
1239b58fee
commit
101e55a9d2
@ -424,15 +424,11 @@ public class FishDraftDataController {
|
|||||||
|
|
||||||
@PostMapping("/deleteAttachment")
|
@PostMapping("/deleteAttachment")
|
||||||
@Operation(summary = "删除附件")
|
@Operation(summary = "删除附件")
|
||||||
public ResponseResult deleteAttachment(@RequestParam String id,
|
public ResponseResult deleteAttachment(@RequestParam String id) {
|
||||||
@RequestHeader("token") String token) {
|
|
||||||
if (id == null || id.isEmpty()) {
|
if (id == null || id.isEmpty()) {
|
||||||
return ResponseResult.error("附件ID不能为空");
|
return ResponseResult.error("附件ID不能为空");
|
||||||
}
|
}
|
||||||
if (token == null || token.isEmpty()) {
|
boolean result = attachmentUploadService.deleteFile(id);
|
||||||
return ResponseResult.error("token不能为空");
|
|
||||||
}
|
|
||||||
boolean result = attachmentUploadService.deleteFile(id, token);
|
|
||||||
if (result) {
|
if (result) {
|
||||||
return ResponseResult.success("删除成功");
|
return ResponseResult.success("删除成功");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package com.yfd.platform.data.service;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -27,6 +28,10 @@ import java.util.stream.Collectors;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class AttachmentUploadService {
|
public class AttachmentUploadService {
|
||||||
|
|
||||||
|
@Value("${attachment.token}")
|
||||||
|
private String token;
|
||||||
|
|
||||||
// 定义一个固定的线程池用于文件上传(建议根据服务器性能调整核心线程数)
|
// 定义一个固定的线程池用于文件上传(建议根据服务器性能调整核心线程数)
|
||||||
private static final ExecutorService UPLOAD_EXECUTOR = new ThreadPoolExecutor(
|
private static final ExecutorService UPLOAD_EXECUTOR = new ThreadPoolExecutor(
|
||||||
5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
|
5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
|
||||||
@ -261,9 +266,9 @@ public class AttachmentUploadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String DELETE_URL = "https://211.99.26.225:12125/delete";
|
private static final String DELETE_URL = "https://211.99.26.225:12125/FileDelete";
|
||||||
|
|
||||||
public boolean deleteFile(String attachmentId, String token) {
|
public boolean deleteFile(String attachmentId) {
|
||||||
if (attachmentId == null || attachmentId.isEmpty()) {
|
if (attachmentId == null || attachmentId.isEmpty()) {
|
||||||
log.warn("附件ID为空");
|
log.warn("附件ID为空");
|
||||||
return false;
|
return false;
|
||||||
@ -285,7 +290,7 @@ public class AttachmentUploadService {
|
|||||||
.sslContext(sc)
|
.sslContext(sc)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
String formData = "id=" + attachmentId;
|
String formData = "fileId=" + attachmentId;
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(DELETE_URL))
|
.uri(URI.create(DELETE_URL))
|
||||||
|
|||||||
@ -1,10 +1,15 @@
|
|||||||
package com.yfd.platform.data.service.impl;
|
package com.yfd.platform.data.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.yfd.platform.data.domain.FishImportResult;
|
||||||
import com.yfd.platform.data.domain.ImportTask;
|
import com.yfd.platform.data.domain.ImportTask;
|
||||||
import com.yfd.platform.data.mapper.ImportTaskMapper;
|
import com.yfd.platform.data.mapper.ImportTaskMapper;
|
||||||
|
import com.yfd.platform.data.service.AttachmentUploadService;
|
||||||
import com.yfd.platform.data.service.IImportTaskService;
|
import com.yfd.platform.data.service.IImportTaskService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -25,6 +30,12 @@ public class ImportTaskServiceImpl extends ServiceImpl<ImportTaskMapper, ImportT
|
|||||||
@Resource
|
@Resource
|
||||||
private ImportTaskMapper importTaskMapper;
|
private ImportTaskMapper importTaskMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
@Resource
|
||||||
|
AttachmentUploadService attachmentUploadService;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<ImportTask> queryPageList(Page<ImportTask> page, String bizType, String status, String uploadUserId) {
|
public Page<ImportTask> queryPageList(Page<ImportTask> page, String bizType, String status, String uploadUserId) {
|
||||||
return this.page(page, this.lambdaQuery()
|
return this.page(page, this.lambdaQuery()
|
||||||
@ -125,7 +136,21 @@ public class ImportTaskServiceImpl extends ServiceImpl<ImportTaskMapper, ImportT
|
|||||||
}
|
}
|
||||||
importTask.setStatus("CONFIRMED");
|
importTask.setStatus("CONFIRMED");
|
||||||
importTask.setUpdatedAt(new Date());
|
importTask.setUpdatedAt(new Date());
|
||||||
return this.updateById(importTask);
|
boolean b = this.updateById(importTask);
|
||||||
|
// 删除本地临时目录数据
|
||||||
|
String resultJson = importTask.getResultJson();
|
||||||
|
if (resultJson != null && !resultJson.isEmpty()) {
|
||||||
|
try {
|
||||||
|
FishImportResult importResult = objectMapper.readValue(resultJson, FishImportResult.class);
|
||||||
|
String tempDir = importResult.getTempDir();
|
||||||
|
// del 方法会递归删除目录及其所有内容
|
||||||
|
FileUtil.del(tempDir);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// ignore parse error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -157,6 +182,45 @@ public class ImportTaskServiceImpl extends ServiceImpl<ImportTaskMapper, ImportT
|
|||||||
if ("CONFIRMED".equals(currentStatus) || "FAILED".equals(currentStatus) || "CANCELLED".equals(currentStatus)) {
|
if ("CONFIRMED".equals(currentStatus) || "FAILED".equals(currentStatus) || "CANCELLED".equals(currentStatus)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (importTask.getResultJson() != null && !importTask.getResultJson().isEmpty()) {
|
||||||
|
try {
|
||||||
|
FishImportResult importResult = objectMapper.readValue(importTask.getResultJson(), FishImportResult.class);
|
||||||
|
for (FishImportResult.FishImportRow successRow : importResult.getSuccessRows()) {
|
||||||
|
if (successRow.getData()==null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String vdpth = successRow.getData().getVdpth();
|
||||||
|
if (StringUtils.hasText(vdpth)) {
|
||||||
|
List<String> fileIds = StrUtil.split(vdpth, ",");
|
||||||
|
fileIds.forEach(fileId -> attachmentUploadService.deleteFile(fileId));
|
||||||
|
}
|
||||||
|
String picpth = successRow.getData().getPicpth();
|
||||||
|
if (StringUtils.hasText(picpth)) {
|
||||||
|
List<String> fileIds = StrUtil.split(vdpth, ",");
|
||||||
|
fileIds.forEach(fileId -> attachmentUploadService.deleteFile(fileId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (FishImportResult.FishImportRow failedRow : importResult.getFailedRows()) {
|
||||||
|
if (failedRow.getData()==null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String vdpth = failedRow.getData().getVdpth();
|
||||||
|
if (StringUtils.hasText(vdpth)) {
|
||||||
|
List<String> fileIds = StrUtil.split(vdpth, ",");
|
||||||
|
fileIds.forEach(fileId -> attachmentUploadService.deleteFile(fileId));
|
||||||
|
}
|
||||||
|
String picpth = failedRow.getData().getPicpth();
|
||||||
|
if (StringUtils.hasText(picpth)) {
|
||||||
|
List<String> fileIds = StrUtil.split(vdpth, ",");
|
||||||
|
fileIds.forEach(fileId -> attachmentUploadService.deleteFile(fileId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// ignore parse error
|
||||||
|
}
|
||||||
|
}
|
||||||
importTask.setStatus("CANCELLED");
|
importTask.setStatus("CANCELLED");
|
||||||
importTask.setErrorMsg("用户取消: " + operatorId);
|
importTask.setErrorMsg("用户取消: " + operatorId);
|
||||||
importTask.setUpdatedAt(new Date());
|
importTask.setUpdatedAt(new Date());
|
||||||
|
|||||||
@ -39,3 +39,6 @@ springdoc:
|
|||||||
enabled: true
|
enabled: true
|
||||||
path: /swagger-ui.html
|
path: /swagger-ui.html
|
||||||
packages-to-scan: com.yfd.platform
|
packages-to-scan: com.yfd.platform
|
||||||
|
|
||||||
|
attachment:
|
||||||
|
token: ${ATTACHMENT_TOKEN:qgcBkod25ngBa4wu8BtfCPYsJ7lQGVDoexH}
|
||||||
Loading…
Reference in New Issue
Block a user