Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -42,6 +42,8 @@ import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -452,7 +454,17 @@ public class AdPlanController extends BaseController {
|
||||
fileTypeError.setMessage("请上传zip文件");
|
||||
throw new BusinessException(fileTypeError);
|
||||
}
|
||||
asyncPlanHandler.importAndMergePlanCheckData(file, getUserId(), planId);
|
||||
// 创建临时文件
|
||||
File tempFile = cn.hutool.core.io.FileUtil.createTempFile();
|
||||
// 将MultipartFile内容写入临时文件
|
||||
try {
|
||||
file.transferTo(tempFile);
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(CommonResponseEnum.FAIL, "文件保存失败");
|
||||
}
|
||||
// 获取文件路径
|
||||
String filePath = tempFile.getAbsolutePath();
|
||||
asyncPlanHandler.importAndMergePlanCheckData(filePath, getUserId(), planId);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
|
||||
|
||||
}
|
||||
|
||||
@@ -37,10 +37,8 @@ import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
@@ -65,6 +63,8 @@ public class AsyncPlanHandler {
|
||||
private final IAdPariService adPairService;
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
@Value("${report.reportDir}")
|
||||
private String reportPath;
|
||||
@Value("${data.homeDir}")
|
||||
@@ -235,35 +235,28 @@ public class AsyncPlanHandler {
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
@Async
|
||||
public void importAndMergePlanCheckData(MultipartFile file, String uid, String planId) {
|
||||
public void importAndMergePlanCheckData(String zipFilePath, String uid, String planId) {
|
||||
importAndMergePlanCheckDataLogic(zipFilePath, uid, planId);
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void importAndMergePlanCheckDataLogic(String zipFilePath, String uid, String planId) {
|
||||
NonWebAutoFillValueHandler.setCurrentUserId(uid);
|
||||
LocalDateTime startTime = LocalDateTime.now();
|
||||
AtomicInteger progress = new AtomicInteger(0);
|
||||
AtomicInteger currentProgress = new AtomicInteger(0);
|
||||
AtomicInteger dataCount = new AtomicInteger(0);
|
||||
try {
|
||||
sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "开始保存文件,请耐心等待..."));
|
||||
// 创建临时目录用于解压文件
|
||||
File tempDir = FileUtil.mkdir(FileUtil.getTmpDirPath() + "import_plan_check_data_" + System.currentTimeMillis() + "/");
|
||||
|
||||
// 将上传的zip文件保存到临时目录
|
||||
File zipFile = FileUtil.file(tempDir, file.getOriginalFilename());
|
||||
try {
|
||||
// 直接获取文件字节,避免依赖Tomcat临时文件
|
||||
byte[] fileBytes = file.getBytes();
|
||||
FileUtil.writeBytes(fileBytes, zipFile);
|
||||
} catch (IOException e) {
|
||||
log.error("保存上传文件失败", e);
|
||||
throw e;
|
||||
}
|
||||
progress.addAndGet(1);
|
||||
sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "开始解压文件,请耐心等待..."));
|
||||
|
||||
// 解压zip文件
|
||||
File unzipDir = FileUtil.mkdir(FileUtil.file(tempDir, "unzip"));
|
||||
ZipUtil.unzip(zipFile.getAbsolutePath(), unzipDir.getAbsolutePath());
|
||||
ZipUtil.unzip(zipFilePath, unzipDir.getAbsolutePath());
|
||||
|
||||
// 查找解压目录中的json文件
|
||||
File[] files = unzipDir.listFiles();
|
||||
@@ -464,14 +457,14 @@ public class AsyncPlanHandler {
|
||||
sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "数据合并完成"));
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
log.error("导入数据失败", e);
|
||||
sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.FAIL.getCode(), progress.get() + currentProgress.get(), "导入失败"));
|
||||
} finally {
|
||||
NonWebAutoFillValueHandler.clearCurrentUserId();
|
||||
}
|
||||
|
||||
FileUtil.del(zipFilePath);
|
||||
sseClient.closeSse(uid);
|
||||
|
||||
}
|
||||
|
||||
// 构建分页查询SQL
|
||||
|
||||
Reference in New Issue
Block a user