@ -77,7 +77,7 @@ public class ExportExcelServiceImpl implements ExportExcelService {
@Autowired
@Autowired
private AuthenticationManager authenticationManager ;
private AuthenticationManager authenticationManager ;
/ / 每页查询的数据量
/ / 每页查询的数据量
private static final int PAGE_SIZE = 1 000;
private static final int PAGE_SIZE = 5 000;
@Autowired
@Autowired
private ExportMapper exportMapper ;
private ExportMapper exportMapper ;
@ -373,31 +373,55 @@ public class ExportExcelServiceImpl implements ExportExcelService {
/ / 8 . 检查文件是否存在且不为空
/ / 8 . 检查文件是否存在且不为空
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
/ / 9 . 上传到OSS
try {
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
File excelFile = new File ( tempFile . toURI ( ) ) ;
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
JsonNode uploadResult = objectMapper . readTree ( result ) ;
long code = uploadResult . path ( "code" ) . asLong ( ) ;
String fileUrl = "" ;
JsonNode dataNode = uploadResult . path ( "data" ) ;
if ( dataNode . isObject ( ) ) {
fileUrl = dataNode . path ( "url" ) . asText ( ) ;
} else if ( dataNode . isTextual ( ) ) {
fileUrl = dataNode . asText ( ) ;
/ / 定义最大重试次数
int maxRetries = 2 ;
int retryCount = 0 ;
boolean uploadSuccess = false ; / / 标记上传是否成功
Exception lastException = null ; / / 记录最后一次异常
while ( retryCount < maxRetries ) {
try {
/ / 9 . 上传到OSS
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
File excelFile = new File ( tempFile . toURI ( ) ) ;
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
JsonNode uploadResult = objectMapper . readTree ( result ) ;
long code = uploadResult . path ( "code" ) . asLong ( ) ;
String fileUrl = "" ;
JsonNode dataNode = uploadResult . path ( "data" ) ;
if ( dataNode . isObject ( ) ) {
fileUrl = dataNode . path ( "url" ) . asText ( ) ;
} else if ( dataNode . isTextual ( ) ) {
fileUrl = dataNode . asText ( ) ;
}
/ / 检查上传是否成功
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
uploadSuccess = true ; / / 标记上传成功
break ; / / 成功后退出循环
} else {
/ / 如果返回的状态码不是200或URL为空
lastException = new Exception ( "上传成功但URL为空或解析失败" ) ;
}
} catch ( Exception e ) {
/ / 捕获上传过程中的异常
lastException = e ;
}
}
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
retryCount + + ;
if ( ! uploadSuccess & & retryCount < maxRetries ) {
log . warn ( "第{}次上传失败,正在尝试重新上传..." , retryCount ) ;
}
}
} catch ( Exception e ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败" , e ) ;
}
/ / 如果经过多次尝试仍然失败
if ( ! uploadSuccess ) {
/ / 更新状态为失败 , 并记录最后一次异常信息
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( lastException . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败" , lastException ) ;
}
}
} else {
} else {
throw new Exception ( "导出的Excel文件不存在或为空" ) ;
throw new Exception ( "导出的Excel文件不存在或为空" ) ;