@ -5,10 +5,15 @@ import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter ;
import com.alibaba.excel.write.metadata.WriteSheet ;
import com.example.demo.Util.ExcelUploadUtil ;
import com.example.demo.controller.ConsumeController ;
import com.example.demo.controller.GoldDetailController ;
import com.example.demo.controller.RechargeController ;
import com.example.demo.controller.RefundController ;
import com.example.demo.domain.entity.Export ;
import com.example.demo.domain.export.Goldmingxi ;
import com.example.demo.domain.vo.* ;
import com.example.demo.mapper.ExportMapper ;
import com.example.demo.service.ExportExcelService ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.ObjectMapper ;
@ -17,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.example.demo.service.AiEmotionService ;
import com.github.pagehelper.PageInfo ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -25,7 +31,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.* ;
import java.util.List ;
import java.util.Map ;
@Service
@ -40,9 +45,17 @@ public class ExportExcelServiceImpl implements ExportExcelService {
/ / 注入GoldDetailController
@Autowired
private GoldDetailController goldDetailController ;
@Autowired
private RechargeController rechargeController ;
@Autowired
private RefundController refundController ;
@Autowired
private ConsumeController consumeController ;
/ / 每页查询的数据量
private static final int PAGE_SIZE = 1000 ;
@Autowired
private ExportMapper exportMapper ;
@Transactional
@ -92,20 +105,31 @@ public class ExportExcelServiceImpl implements ExportExcelService {
Result pageResult = goldDetailController . getGoldDetail ( page ) ;
Integer code = pageResult . getCode ( ) ;
Object data = pageResult . getData ( ) ;
if ( code = = 200 ) {
Map < String , Object > rawData = ( Map < String , Object > ) data ;
Long total = ( Long ) rawData . get ( "total" ) ;
List < Map < String , Object > > list = ( List < Map < String , Object > > ) rawData . get ( "list" ) ;
/ / 检查是否还有数据
/ / 判断 data 是否是 PageInfo 类型
if ( ! ( data instanceof PageInfo < ? > ) ) {
log . error ( "返回数据类型错误,期望 PageInfo,实际为:{}" , data . getClass ( ) ) ;
hasMore = false ;
continue ;
}
@SuppressWarnings ( "unchecked" )
PageInfo < GoldDetail > pageInfo = ( PageInfo < GoldDetail > ) data ;
Long total = ( long ) pageInfo . getTotal ( ) ; / / 转换为 long
List < GoldDetail > list = pageInfo . getList ( ) ;
if ( list = = null | | list . isEmpty ( ) ) {
hasMore = false ;
} else {
/ / 写入数据 ( 注意 : finish ( ) 应在所有数据写入后调用 )
/ / 写入 Excel 数据
excelWriter . write ( list , writeSheet ) ;
page . setPageNum ( page . getPageNum ( ) + 1 ) ;
totalCount + = list . size ( ) ;
log . info ( "导出进度 recordId: {}, 已处理: {}条" , recordId , totalCount ) ;
/ / 检查是否还有更多数据
hasMore = totalCount < total ;
}
} else {
@ -136,16 +160,35 @@ public class ExportExcelServiceImpl implements ExportExcelService {
/ / 3 . 执行上传
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
/ / 1 . 解析JSON任务
/ / JsonNode uploadResult = objectMapper . readTree ( result ) ;
/ / System . out . println ( uploadResult + "11111111111111111111111" ) ;
/ / long code = uploadResult . path ( "code" ) . asLong ( ) ;
/ / String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
/ / url = url . replace ( "\"" , "" ) ;
/ / if ( code = = 1 ) {
/ / / / 3 . 验证导出记录decodecode
/ / aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
/ / } else {
/ / / / 更新失败
/ / aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
/ / }
JsonNode uploadResult = objectMapper . readTree ( result ) ;
long code = uploadResult . path ( "code" ) . asLong ( ) ;
String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
url = url . replace ( "\"" , "" ) ;
if ( code = = 1 ) {
/ / 3 . 验证导出记录decodecode
aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
String fileUrl = "" ;
JsonNode dataNode = uploadResult . path ( "data" ) ;
if ( dataNode . isObject ( ) ) {
fileUrl = dataNode . path ( "url" ) . asText ( ) ;
} else if ( dataNode . isTextual ( ) ) {
fileUrl = dataNode . asText ( ) ; / / 如果 data 是直接字符串 URL
}
log . info ( "解析到的URL: {}" , fileUrl ) ;
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
/ / 更新失败
aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
}
} catch ( Exception e ) {
/ / 更新失败
@ -215,6 +258,812 @@ public class ExportExcelServiceImpl implements ExportExcelService {
}
@Transactional
@Override
public Exception handleExcel ( String message ) throws Exception {
System . out . println ( "明细导出excel数据开始执行:" + message ) ;
long startTime = System . currentTimeMillis ( ) ;
Long recordId = null ;
String fileName = null ;
File tempFile = null ;
OutputStream outputStream = null ;
ExcelWriter excelWriter = null ;
try {
/ / 1 . 解析JSON任务
JsonNode rootNode = objectMapper . readTree ( message ) ;
/ / 2 . 获取基本参数
recordId = rootNode . path ( "recordId" ) . asLong ( ) ;
JsonNode requestDataNode = rootNode . path ( "requestData" ) ;
/ / 3 . 验证导出记录
AiEmotionExportRecordVO record = validateExportRecord ( recordId ) ;
if ( record = = null ) return null ;
/ / 4 . 更新状态为处理中
aiEmotionService . updateStatus ( recordId , 1 , "" , "" , 0 ) ;
/ / 5 . 准备Excel文件
fileName = record . getFileName ( ) ;
/ / 初始化临时文件 ( 保存到本地临时目录 )
tempFile = File . createTempFile ( "export_" , ".xlsx" ) ;
outputStream = new FileOutputStream ( tempFile ) ; / / 使用文件输出流
/ / 从JSON中提取单个值
String text = requestDataNode . has ( "text" ) ? requestDataNode . get ( "text" ) . asText ( ) : null ;
Integer sort = requestDataNode . has ( "sort" ) ? requestDataNode . get ( "sort" ) . asInt ( ) : null ;
String field = requestDataNode . has ( "field" ) ? requestDataNode . get ( "field" ) . asText ( ) : null ;
String deptId = requestDataNode . has ( "deptId" ) ? requestDataNode . get ( "deptId" ) . asText ( ) : null ;
try {
/ / 6 . 初始化Excel写入器 ( 指向本地文件流 )
excelWriter = initExcelWriter ( outputStream , "user" ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( "Sheet1" ) . build ( ) ;
/ / 7 . 分页查询并写入数据
Page page = new Page ( ) ;
page . setPageNum ( 1 ) ;
page . setPageSize ( 1000 ) ;
Integer totalCount = 0 ;
boolean hasMore = true ;
while ( hasMore ) {
Result pageResult = goldDetailController . getGold ( page ) ;
Integer code = pageResult . getCode ( ) ;
Object data = pageResult . getData ( ) ;
if ( code = = 200 ) {
/ / 判断 data 是否是 PageInfo 类型
if ( ! ( data instanceof PageInfo < ? > ) ) {
log . error ( "返回数据类型错误,期望 PageInfo,实际为:{}" , data . getClass ( ) ) ;
hasMore = false ;
continue ;
}
@SuppressWarnings ( "unchecked" )
PageInfo < GoldDetail > pageInfo = ( PageInfo < GoldDetail > ) data ;
Long total = ( long ) pageInfo . getTotal ( ) ; / / 转换为 long
List < GoldDetail > list = pageInfo . getList ( ) ;
if ( list = = null | | list . isEmpty ( ) ) {
hasMore = false ;
} else {
/ / 写入 Excel 数据
excelWriter . write ( list , writeSheet ) ;
page . setPageNum ( page . getPageNum ( ) + 1 ) ;
totalCount + = list . size ( ) ;
log . info ( "导出进度 recordId: {}, 已处理: {}条" , recordId , totalCount ) ;
hasMore = totalCount < total ;
}
} else {
hasMore = false ;
log . error ( "获取数据失败,状态码: {}" , code ) ;
}
}
/ / 7 . 完成Excel写入 ( 所有数据写入后关闭写入器 )
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . flush ( ) ; / / 确保所有数据写入
outputStream . close ( ) ; / / 关闭文件流
}
/ / 检查文件是否存在且不为空
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
/ / 8 . 上传到OSS ( 读取本地临时文件 )
/ / 获取接口的基础 URL
String uploadUrl = "http://39.101.133.168:8828/hljw/api/aws/upload" ;
try {
/ / 1 . 创建上传工具实例
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
/ / 2 . 准备要上传的文件
File excelFile = new File ( tempFile . toURI ( ) ) ;
try {
/ / 3 . 执行上传
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
/ / 1 . 解析JSON任务
/ / JsonNode uploadResult = objectMapper . readTree ( result ) ;
/ / System . out . println ( uploadResult + "11111111111111111111111" ) ;
/ / long code = uploadResult . path ( "code" ) . asLong ( ) ;
/ / String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
/ / url = url . replace ( "\"" , "" ) ;
/ / if ( code = = 1 ) {
/ / / / 3 . 验证导出记录decodecode
/ / aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
/ / } else {
/ / / / 更新失败
/ / aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
/ / }
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 ( ) ; / / 如果 data 是直接字符串 URL
}
log . info ( "解析到的URL: {}" , fileUrl ) ;
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
}
} catch ( Exception e ) {
/ / 更新失败
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败1" , e ) ;
}
} catch ( Exception e ) {
log . error ( "上传文件失败 recordId: {}, 文件名: {}" , recordId , fileName , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "文件上传云端失败2" , e ) ;
}
} else {
throw new Exception ( "导出的Excel文件不存在或为空" ) ;
}
} catch ( Exception e ) {
System . out . println ( "导出异常" + e . getMessage ( ) ) ;
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "导出异常" , e ) ;
} finally {
/ / 确保资源被关闭
try {
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . close ( ) ;
}
} catch ( Exception e ) {
log . error ( "关闭资源失败" , e ) ;
throw new Exception ( "excel文件关闭资源失败" , e ) ;
}
}
} catch ( Exception e ) {
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
System . out . println ( "<导出失败>" + e . getMessage ( ) ) ;
throw new Exception ( "导出任务处理失败" , e ) ;
} finally {
/ / 清理临时文件
if ( tempFile ! = null & & tempFile . exists ( ) ) {
try {
if ( tempFile . delete ( ) ) {
log . info ( "临时文件已删除: {}" , tempFile . getAbsolutePath ( ) ) ;
} else {
log . warn ( "无法删除临时文件: {}" , tempFile . getAbsolutePath ( ) ) ;
}
} catch ( Exception e ) {
log . error ( "删除临时文件失败" , e . getMessage ( ) ) ;
throw new Exception ( "删除临时文件失败" , e ) ;
}
}
long endTime = System . currentTimeMillis ( ) ;
log . info ( "导出任务完成,耗时: {}毫秒" , ( endTime - startTime ) ) ;
}
return null ;
}
@Transactional
@Override
public Exception rechargeExcel ( String message ) throws Exception {
System . out . println ( "明细导出excel数据开始执行:" + message ) ;
long startTime = System . currentTimeMillis ( ) ;
Long recordId = null ;
String fileName = null ;
File tempFile = null ;
OutputStream outputStream = null ;
ExcelWriter excelWriter = null ;
try {
/ / 1 . 解析JSON任务
JsonNode rootNode = objectMapper . readTree ( message ) ;
/ / 2 . 获取基本参数
recordId = rootNode . path ( "recordId" ) . asLong ( ) ;
JsonNode requestDataNode = rootNode . path ( "requestData" ) ;
/ / 3 . 验证导出记录
AiEmotionExportRecordVO record = validateExportRecord ( recordId ) ;
if ( record = = null ) return null ;
/ / 4 . 更新状态为处理中
aiEmotionService . updateStatus ( recordId , 1 , "" , "" , 0 ) ;
/ / 5 . 准备Excel文件
fileName = record . getFileName ( ) ;
/ / 初始化临时文件 ( 保存到本地临时目录 )
tempFile = File . createTempFile ( "export_" , ".xlsx" ) ;
outputStream = new FileOutputStream ( tempFile ) ; / / 使用文件输出流
/ / 从JSON中提取单个值
String text = requestDataNode . has ( "text" ) ? requestDataNode . get ( "text" ) . asText ( ) : null ;
Integer sort = requestDataNode . has ( "sort" ) ? requestDataNode . get ( "sort" ) . asInt ( ) : null ;
String field = requestDataNode . has ( "field" ) ? requestDataNode . get ( "field" ) . asText ( ) : null ;
String deptId = requestDataNode . has ( "deptId" ) ? requestDataNode . get ( "deptId" ) . asText ( ) : null ;
try {
/ / 6 . 初始化Excel写入器 ( 指向本地文件流 )
excelWriter = initExcelWriter ( outputStream , "user" ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( "Sheet1" ) . build ( ) ;
/ / 7 . 分页查询并写入数据
Page page = new Page ( ) ;
page . setPageNum ( 1 ) ;
page . setPageSize ( 1000 ) ;
Integer totalCount = 0 ;
boolean hasMore = true ;
while ( hasMore ) {
Result pageResult = rechargeController . selcetBy ( page ) ;
Integer code = pageResult . getCode ( ) ;
Object data = pageResult . getData ( ) ;
if ( code = = 200 ) {
/ / 判断 data 是否是 PageInfo 类型
if ( ! ( data instanceof PageInfo < ? > ) ) {
log . error ( "返回数据类型错误,期望 PageInfo,实际为:{}" , data . getClass ( ) ) ;
hasMore = false ;
continue ;
}
@SuppressWarnings ( "unchecked" )
PageInfo < GoldDetail > pageInfo = ( PageInfo < GoldDetail > ) data ;
Long total = ( long ) pageInfo . getTotal ( ) ; / / 转换为 long
List < GoldDetail > list = pageInfo . getList ( ) ;
if ( list = = null | | list . isEmpty ( ) ) {
hasMore = false ;
} else {
/ / 写入 Excel 数据
excelWriter . write ( list , writeSheet ) ;
page . setPageNum ( page . getPageNum ( ) + 1 ) ;
totalCount + = list . size ( ) ;
log . info ( "导出进度 recordId: {}, 已处理: {}条" , recordId , totalCount ) ;
hasMore = totalCount < total ;
}
} else {
hasMore = false ;
log . error ( "获取数据失败,状态码: {}" , code ) ;
}
}
/ / 7 . 完成Excel写入 ( 所有数据写入后关闭写入器 )
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . flush ( ) ; / / 确保所有数据写入
outputStream . close ( ) ; / / 关闭文件流
}
/ / 检查文件是否存在且不为空
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
/ / 8 . 上传到OSS ( 读取本地临时文件 )
/ / 获取接口的基础 URL
String uploadUrl = "http://39.101.133.168:8828/hljw/api/aws/upload" ;
try {
/ / 1 . 创建上传工具实例
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
/ / 2 . 准备要上传的文件
File excelFile = new File ( tempFile . toURI ( ) ) ;
try {
/ / 3 . 执行上传
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
/ / 1 . 解析JSON任务
/ / JsonNode uploadResult = objectMapper . readTree ( result ) ;
/ / System . out . println ( uploadResult + "11111111111111111111111" ) ;
/ / long code = uploadResult . path ( "code" ) . asLong ( ) ;
/ / String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
/ / url = url . replace ( "\"" , "" ) ;
/ / if ( code = = 1 ) {
/ / / / 3 . 验证导出记录decodecode
/ / aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
/ / } else {
/ / / / 更新失败
/ / aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
/ / }
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 ( ) ; / / 如果 data 是直接字符串 URL
}
log . info ( "解析到的URL: {}" , fileUrl ) ;
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
}
} catch ( Exception e ) {
/ / 更新失败
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败1" , e ) ;
}
} catch ( Exception e ) {
log . error ( "上传文件失败 recordId: {}, 文件名: {}" , recordId , fileName , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "文件上传云端失败2" , e ) ;
}
} else {
throw new Exception ( "导出的Excel文件不存在或为空" ) ;
}
} catch ( Exception e ) {
System . out . println ( "导出异常" + e . getMessage ( ) ) ;
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "导出异常" , e ) ;
} finally {
/ / 确保资源被关闭
try {
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . close ( ) ;
}
} catch ( Exception e ) {
log . error ( "关闭资源失败" , e ) ;
throw new Exception ( "excel文件关闭资源失败" , e ) ;
}
}
} catch ( Exception e ) {
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
System . out . println ( "<导出失败>" + e . getMessage ( ) ) ;
throw new Exception ( "导出任务处理失败" , e ) ;
} finally {
/ / 清理临时文件
if ( tempFile ! = null & & tempFile . exists ( ) ) {
try {
if ( tempFile . delete ( ) ) {
log . info ( "临时文件已删除: {}" , tempFile . getAbsolutePath ( ) ) ;
} else {
log . warn ( "无法删除临时文件: {}" , tempFile . getAbsolutePath ( ) ) ;
}
} catch ( Exception e ) {
log . error ( "删除临时文件失败" , e . getMessage ( ) ) ;
throw new Exception ( "删除临时文件失败" , e ) ;
}
}
long endTime = System . currentTimeMillis ( ) ;
log . info ( "导出任务完成,耗时: {}毫秒" , ( endTime - startTime ) ) ;
}
return null ;
}
@Transactional
@Override
public Exception consumeExcel ( String message ) throws Exception {
System . out . println ( "明细导出excel数据开始执行:" + message ) ;
long startTime = System . currentTimeMillis ( ) ;
Long recordId = null ;
String fileName = null ;
File tempFile = null ;
OutputStream outputStream = null ;
ExcelWriter excelWriter = null ;
try {
/ / 1 . 解析JSON任务
JsonNode rootNode = objectMapper . readTree ( message ) ;
/ / 2 . 获取基本参数
recordId = rootNode . path ( "recordId" ) . asLong ( ) ;
JsonNode requestDataNode = rootNode . path ( "requestData" ) ;
/ / 3 . 验证导出记录
AiEmotionExportRecordVO record = validateExportRecord ( recordId ) ;
if ( record = = null ) return null ;
/ / 4 . 更新状态为处理中
aiEmotionService . updateStatus ( recordId , 1 , "" , "" , 0 ) ;
/ / 5 . 准备Excel文件
fileName = record . getFileName ( ) ;
/ / 初始化临时文件 ( 保存到本地临时目录 )
tempFile = File . createTempFile ( "export_" , ".xlsx" ) ;
outputStream = new FileOutputStream ( tempFile ) ; / / 使用文件输出流
/ / 从JSON中提取单个值
String text = requestDataNode . has ( "text" ) ? requestDataNode . get ( "text" ) . asText ( ) : null ;
Integer sort = requestDataNode . has ( "sort" ) ? requestDataNode . get ( "sort" ) . asInt ( ) : null ;
String field = requestDataNode . has ( "field" ) ? requestDataNode . get ( "field" ) . asText ( ) : null ;
String deptId = requestDataNode . has ( "deptId" ) ? requestDataNode . get ( "deptId" ) . asText ( ) : null ;
try {
/ / 6 . 初始化Excel写入器 ( 指向本地文件流 )
excelWriter = initExcelWriter ( outputStream , "user" ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( "Sheet1" ) . build ( ) ;
/ / 7 . 分页查询并写入数据
Page page = new Page ( ) ;
page . setPageNum ( 1 ) ;
page . setPageSize ( 1000 ) ;
Integer totalCount = 0 ;
boolean hasMore = true ;
while ( hasMore ) {
Result pageResult = refundController . selcetBy ( page ) ;
Integer code = pageResult . getCode ( ) ;
Object data = pageResult . getData ( ) ;
if ( code = = 200 ) {
/ / 判断 data 是否是 PageInfo 类型
if ( ! ( data instanceof PageInfo < ? > ) ) {
log . error ( "返回数据类型错误,期望 PageInfo,实际为:{}" , data . getClass ( ) ) ;
hasMore = false ;
continue ;
}
@SuppressWarnings ( "unchecked" )
PageInfo < GoldDetail > pageInfo = ( PageInfo < GoldDetail > ) data ;
Long total = ( long ) pageInfo . getTotal ( ) ; / / 转换为 long
List < GoldDetail > list = pageInfo . getList ( ) ;
if ( list = = null | | list . isEmpty ( ) ) {
hasMore = false ;
} else {
/ / 写入 Excel 数据
excelWriter . write ( list , writeSheet ) ;
page . setPageNum ( page . getPageNum ( ) + 1 ) ;
totalCount + = list . size ( ) ;
log . info ( "导出进度 recordId: {}, 已处理: {}条" , recordId , totalCount ) ;
hasMore = totalCount < total ;
}
} else {
hasMore = false ;
log . error ( "获取数据失败,状态码: {}" , code ) ;
}
}
/ / 7 . 完成Excel写入 ( 所有数据写入后关闭写入器 )
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . flush ( ) ; / / 确保所有数据写入
outputStream . close ( ) ; / / 关闭文件流
}
/ / 检查文件是否存在且不为空
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
/ / 8 . 上传到OSS ( 读取本地临时文件 )
/ / 获取接口的基础 URL
String uploadUrl = "http://39.101.133.168:8828/hljw/api/aws/upload" ;
try {
/ / 1 . 创建上传工具实例
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
/ / 2 . 准备要上传的文件
File excelFile = new File ( tempFile . toURI ( ) ) ;
try {
/ / 3 . 执行上传
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
/ / 1 . 解析JSON任务
/ / JsonNode uploadResult = objectMapper . readTree ( result ) ;
/ / System . out . println ( uploadResult + "11111111111111111111111" ) ;
/ / long code = uploadResult . path ( "code" ) . asLong ( ) ;
/ / String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
/ / url = url . replace ( "\"" , "" ) ;
/ / if ( code = = 1 ) {
/ / / / 3 . 验证导出记录decodecode
/ / aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
/ / } else {
/ / / / 更新失败
/ / aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
/ / }
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 ( ) ; / / 如果 data 是直接字符串 URL
}
log . info ( "解析到的URL: {}" , fileUrl ) ;
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
}
} catch ( Exception e ) {
/ / 更新失败
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败1" , e ) ;
}
} catch ( Exception e ) {
log . error ( "上传文件失败 recordId: {}, 文件名: {}" , recordId , fileName , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "文件上传云端失败2" , e ) ;
}
} else {
throw new Exception ( "导出的Excel文件不存在或为空" ) ;
}
} catch ( Exception e ) {
System . out . println ( "导出异常" + e . getMessage ( ) ) ;
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "导出异常" , e ) ;
} finally {
/ / 确保资源被关闭
try {
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . close ( ) ;
}
} catch ( Exception e ) {
log . error ( "关闭资源失败" , e ) ;
throw new Exception ( "excel文件关闭资源失败" , e ) ;
}
}
} catch ( Exception e ) {
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
System . out . println ( "<导出失败>" + e . getMessage ( ) ) ;
throw new Exception ( "导出任务处理失败" , e ) ;
} finally {
/ / 清理临时文件
if ( tempFile ! = null & & tempFile . exists ( ) ) {
try {
if ( tempFile . delete ( ) ) {
log . info ( "临时文件已删除: {}" , tempFile . getAbsolutePath ( ) ) ;
} else {
log . warn ( "无法删除临时文件: {}" , tempFile . getAbsolutePath ( ) ) ;
}
} catch ( Exception e ) {
log . error ( "删除临时文件失败" , e . getMessage ( ) ) ;
throw new Exception ( "删除临时文件失败" , e ) ;
}
}
long endTime = System . currentTimeMillis ( ) ;
log . info ( "导出任务完成,耗时: {}毫秒" , ( endTime - startTime ) ) ;
}
return null ;
}
@Transactional
@Override
public Exception refundExcel ( String message ) throws Exception {
System . out . println ( "明细导出excel数据开始执行:" + message ) ;
long startTime = System . currentTimeMillis ( ) ;
Long recordId = null ;
String fileName = null ;
File tempFile = null ;
OutputStream outputStream = null ;
ExcelWriter excelWriter = null ;
try {
/ / 1 . 解析JSON任务
JsonNode rootNode = objectMapper . readTree ( message ) ;
/ / 2 . 获取基本参数
recordId = rootNode . path ( "recordId" ) . asLong ( ) ;
JsonNode requestDataNode = rootNode . path ( "requestData" ) ;
/ / 3 . 验证导出记录
AiEmotionExportRecordVO record = validateExportRecord ( recordId ) ;
if ( record = = null ) return null ;
/ / 4 . 更新状态为处理中
aiEmotionService . updateStatus ( recordId , 1 , "" , "" , 0 ) ;
/ / 5 . 准备Excel文件
fileName = record . getFileName ( ) ;
/ / 初始化临时文件 ( 保存到本地临时目录 )
tempFile = File . createTempFile ( "export_" , ".xlsx" ) ;
outputStream = new FileOutputStream ( tempFile ) ; / / 使用文件输出流
/ / 从JSON中提取单个值
String text = requestDataNode . has ( "text" ) ? requestDataNode . get ( "text" ) . asText ( ) : null ;
Integer sort = requestDataNode . has ( "sort" ) ? requestDataNode . get ( "sort" ) . asInt ( ) : null ;
String field = requestDataNode . has ( "field" ) ? requestDataNode . get ( "field" ) . asText ( ) : null ;
String deptId = requestDataNode . has ( "deptId" ) ? requestDataNode . get ( "deptId" ) . asText ( ) : null ;
try {
/ / 6 . 初始化Excel写入器 ( 指向本地文件流 )
excelWriter = initExcelWriter ( outputStream , "user" ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( "Sheet1" ) . build ( ) ;
/ / 7 . 分页查询并写入数据
Page page = new Page ( ) ;
page . setPageNum ( 1 ) ;
page . setPageSize ( 1000 ) ;
Integer totalCount = 0 ;
boolean hasMore = true ;
while ( hasMore ) {
Result pageResult = consumeController . selcetBy ( page ) ;
Integer code = pageResult . getCode ( ) ;
Object data = pageResult . getData ( ) ;
if ( code = = 200 ) {
/ / 判断 data 是否是 PageInfo 类型
if ( ! ( data instanceof PageInfo < ? > ) ) {
log . error ( "返回数据类型错误,期望 PageInfo,实际为:{}" , data . getClass ( ) ) ;
hasMore = false ;
continue ;
}
@SuppressWarnings ( "unchecked" )
PageInfo < GoldDetail > pageInfo = ( PageInfo < GoldDetail > ) data ;
Long total = ( long ) pageInfo . getTotal ( ) ; / / 转换为 long
List < GoldDetail > list = pageInfo . getList ( ) ;
if ( list = = null | | list . isEmpty ( ) ) {
hasMore = false ;
} else {
/ / 写入 Excel 数据
excelWriter . write ( list , writeSheet ) ;
page . setPageNum ( page . getPageNum ( ) + 1 ) ;
totalCount + = list . size ( ) ;
log . info ( "导出进度 recordId: {}, 已处理: {}条" , recordId , totalCount ) ;
hasMore = totalCount < total ;
}
} else {
hasMore = false ;
log . error ( "获取数据失败,状态码: {}" , code ) ;
}
}
/ / 7 . 完成Excel写入 ( 所有数据写入后关闭写入器 )
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . flush ( ) ; / / 确保所有数据写入
outputStream . close ( ) ; / / 关闭文件流
}
/ / 检查文件是否存在且不为空
if ( tempFile ! = null & & tempFile . exists ( ) & & tempFile . length ( ) > 0 ) {
/ / 8 . 上传到OSS ( 读取本地临时文件 )
/ / 获取接口的基础 URL
String uploadUrl = "http://39.101.133.168:8828/hljw/api/aws/upload" ;
try {
/ / 1 . 创建上传工具实例
ExcelUploadUtil uploadUtil = new ExcelUploadUtil ( uploadUrl ) ;
/ / 2 . 准备要上传的文件
File excelFile = new File ( tempFile . toURI ( ) ) ;
try {
/ / 3 . 执行上传
String result = uploadUtil . uploadExcel ( excelFile , "export/excel/" ) ;
/ / 1 . 解析JSON任务
/ / JsonNode uploadResult = objectMapper . readTree ( result ) ;
/ / System . out . println ( uploadResult + "11111111111111111111111" ) ;
/ / long code = uploadResult . path ( "code" ) . asLong ( ) ;
/ / String url = String . valueOf ( uploadResult . path ( "data" ) ) ;
/ / url = url . replace ( "\"" , "" ) ;
/ / if ( code = = 1 ) {
/ / / / 3 . 验证导出记录decodecode
/ / aiEmotionService . updateStatus ( recordId , 2 , url , "" , totalCount ) ;
/ / } else {
/ / / / 更新失败
/ / aiEmotionService . updateStatus ( recordId , 3 , "" , url , 0 ) ;
/ / }
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 ( ) ; / / 如果 data 是直接字符串 URL
}
log . info ( "解析到的URL: {}" , fileUrl ) ;
if ( code = = 200 & & ! fileUrl . isEmpty ( ) ) {
aiEmotionService . updateStatus ( recordId , 2 , fileUrl , "" , totalCount ) ;
} else {
aiEmotionService . updateStatus ( recordId , 3 , "" , "上传成功但URL为空或解析失败" , 0 ) ;
}
} catch ( Exception e ) {
/ / 更新失败
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
throw new Exception ( "文件上传云端失败1" , e ) ;
}
} catch ( Exception e ) {
log . error ( "上传文件失败 recordId: {}, 文件名: {}" , recordId , fileName , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "文件上传云端失败2" , e ) ;
}
} else {
throw new Exception ( "导出的Excel文件不存在或为空" ) ;
}
} catch ( Exception e ) {
System . out . println ( "导出异常" + e . getMessage ( ) ) ;
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
throw new Exception ( "导出异常" , e ) ;
} finally {
/ / 确保资源被关闭
try {
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
if ( outputStream ! = null ) {
outputStream . close ( ) ;
}
} catch ( Exception e ) {
log . error ( "关闭资源失败" , e ) ;
throw new Exception ( "excel文件关闭资源失败" , e ) ;
}
}
} catch ( Exception e ) {
log . error ( "导出任务处理失败 recordId: {}" , recordId , e ) ;
/ / 更新状态为失败
if ( recordId ! = null ) {
aiEmotionService . updateStatus ( recordId , 3 , "" , StringUtils . substring ( e . getMessage ( ) , 0 , 500 ) , 0 ) ;
}
System . out . println ( "<导出失败>" + e . getMessage ( ) ) ;
throw new Exception ( "导出任务处理失败" , e ) ;
} finally {
/ / 清理临时文件
if ( tempFile ! = null & & tempFile . exists ( ) ) {
try {
if ( tempFile . delete ( ) ) {
log . info ( "临时文件已删除: {}" , tempFile . getAbsolutePath ( ) ) ;
} else {
log . warn ( "无法删除临时文件: {}" , tempFile . getAbsolutePath ( ) ) ;
}
} catch ( Exception e ) {
log . error ( "删除临时文件失败" , e . getMessage ( ) ) ;
throw new Exception ( "删除临时文件失败" , e ) ;
}
}
long endTime = System . currentTimeMillis ( ) ;
log . info ( "导出任务完成,耗时: {}毫秒" , ( endTime - startTime ) ) ;
}
return null ;
}
@Override
public List < Export > getExcel ( Export export ) {
List < Export > list = exportMapper . getExportRecord ( export . getAccount ( ) ) ;
System . out . println ( list + "-------------------------------" ) ;
return list ;
}
/ * *
* 验证导出记录
* /