浏览代码

冗余代码整理

feature/report-forms
JianShengFei 9 个月前
父节点
当前提交
22af0f5883

+ 1
- 1
src/main/java/com/qtzl/alterSales/manager/service/AflReportExportDataLoader.java 查看文件

@@ -339,7 +339,7 @@ public class AflReportExportDataLoader extends Thread {
predicates.add(allLikePredicate);
}
predicates.add(criteriaBuilder.equal(root.get("tradeState").as(String.class), "SUCCESS"));
// 按照tradeState字段排序
// 按照timeEnd字段排序
query.orderBy(criteriaBuilder.asc(root.get("timeEnd").as(String.class)));
Predicate[] pre = new Predicate[predicates.size()];
return query.where(predicates.toArray(pre)).getRestriction();

+ 46
- 34
src/main/java/com/qtzl/alterSales/manager/service/AflReportExportServiceImpl.java 查看文件

@@ -51,7 +51,8 @@ public class AflReportExportServiceImpl implements AflReportExportService {
/**
* 队列 =》存储
*/
private final BlockingQueue<QueueDataBean<?>> dataQueue = new LinkedBlockingQueue<>(MAX_SIZE);
private static final ThreadLocal<BlockingQueue<QueueDataBean<?>>> threadLocalDataQueue =
ThreadLocal.withInitial(() -> new LinkedBlockingQueue<>(MAX_SIZE));

private final Random random = new Random();

@@ -77,32 +78,52 @@ public class AflReportExportServiceImpl implements AflReportExportService {

@Override
public String export50ServiceFee(ReportServiceFee50Request request) throws ServiceHandleException {
AflReportExportDataLoader reportExportDataLoader = new AflReportExportDataLoader(dataQueue, request, ReportEnum.SERVICE_FEE_50);
AflReportExportDataLoader reportExportDataLoader = new AflReportExportDataLoader(threadLocalDataQueue.get(), request, ReportEnum.SERVICE_FEE_50);
reportExportDataLoader.start();
return writeFileAndReturnFilePathForServiceFee(reportExportDataLoader, request);
return writeFileAndReturnFilePathForServiceFee(reportExportDataLoader);
}

@Override
public String exportIncomeCost(ReportIncomeCostRequest request) throws ServiceHandleException {
AflReportExportDataLoader reportExportDataLoader = new AflReportExportDataLoader(dataQueue, request, ReportEnum.INCOME_COST);
AflReportExportDataLoader reportExportDataLoader = new AflReportExportDataLoader(threadLocalDataQueue.get(), request, ReportEnum.INCOME_COST);
reportExportDataLoader.start();
return writeFileAndReturnFilePathForIncomeCost(reportExportDataLoader, request);
return writeFileAndReturnFilePathForIncomeCost(request);
}


// ======================================== 额外的数据处理方法 ========================================


private String writeFileAndReturnFilePathForIncomeCost(AflReportExportDataLoader reportExportDataLoader, ReportIncomeCostRequest request) throws ServiceHandleException {
private String writeFileAndReturnFilePathForIncomeCost(ReportIncomeCostRequest request) throws ServiceHandleException {
File file = null;
try {
// 构建文件
String fileName = getFileName(ReportEnum.INCOME_COST);
writeFile(request, fileName);
file = new File(fileName);
return fmsService.uploadFile(constantConfig.getUploadFile(), file.getPath(), 9000);
} catch (Exception e) {
log.error("导出文件失败", e);
throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage());
} finally {
threadLocalDataQueue.remove();
if(file != null) {
try {
Files.delete(file.toPath());
} catch (IOException e) {
log.error("删除文件失败", e);
}
}
}
}

private void writeFile(ReportIncomeCostRequest request, String fileName) throws ServiceHandleException {
String templateFilePath;
try {
templateFilePath = ResourceUtils.getFile("classpath:excel-template/BydIncomeCostTemplate.xlsx").getAbsolutePath();
} catch (Exception e) {
throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage());
}

// 构建文件
String fileName = getFileName(ReportEnum.INCOME_COST);
ExcelWriter excelWriter = EasyExcelFactory.write(fileName).withTemplate(templateFilePath).build();
WriteSheet writeSheet1 = EasyExcelFactory.writerSheet(1).build();
WriteSheet writeSheet2 = EasyExcelFactory.writerSheet(2).build();
@@ -115,7 +136,7 @@ public class AflReportExportServiceImpl implements AflReportExportService {
while (isFinish) {
QueueDataBean<?> queueDataBean;
try {
queueDataBean = dataQueue.take();
queueDataBean = threadLocalDataQueue.get().take();
} catch (InterruptedException e) {
// 恢复中断信号
Thread.currentThread().interrupt();
@@ -212,43 +233,35 @@ public class AflReportExportServiceImpl implements AflReportExportService {

// 将数据写入文件
excelWriter.finish();

File file = new File(fileName);
String filePath = fmsService.uploadFile(constantConfig.getUploadFile(), file.getPath(), 9000);
// 删除本地临时文件
try {
Files.delete(file.toPath());
} catch (IOException e) {
log.error("删除本地临时文件失败", e);
}

return filePath;
}

private String writeFileAndReturnFilePathForServiceFee(AflReportExportDataLoader reportExportDataLoader, ReportServiceFee50Request request) throws ServiceHandleException {
private String writeFileAndReturnFilePathForServiceFee(AflReportExportDataLoader reportExportDataLoader) throws ServiceHandleException {
// 主线程就是消费者线程 也可能使用异步渲染 主线程接收文件路径返回
File file = null;
try {
log.info("消费者线程开始获取数据渲染文件...");
String fileTempPath = String.format("%s/%s", System.getProperty("user.dir"), getFileName(ReportEnum.SERVICE_FEE_50));
File file = new File(fileTempPath);
file = new File(fileTempPath);
writeExcel(file);

// 上传文件到文件服务器并返回文件请求路径
String filePath = fmsService.uploadFile(constantConfig.getUploadFile(), file.getPath(), 9000);
try {
// 删除本地临时文件
Files.delete(file.toPath());
} catch (IOException e) {
log.error("删除本地临时文件失败", e);
}
return filePath;
return fmsService.uploadFile(constantConfig.getUploadFile(), file.getPath(), 9000);
} catch (Exception e) {
// 其他的异常处理 如果出现文件渲染失败 则给生产者线程发送中断信号
log.error("消费者线程获取数据渲染文件失败", e);
reportExportDataLoader.interrupt();
throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage());
} finally {
if(file != null) {
try {
// 删除本地临时文件
Files.delete(file.toPath());
} catch (IOException e) {
log.error("删除本地临时文件失败", e);
}
}
threadLocalDataQueue.remove();
}

}

private void writeExcel(File file) {
@@ -275,10 +288,9 @@ public class AflReportExportServiceImpl implements AflReportExportService {
// 渲染数据到excel
boolean blnContinue = true;
while (blnContinue) {
// 这里的InterruptedException异常往外抛 交给上层方法处理
QueueDataBean<?> queueDataBean;
try {
queueDataBean = dataQueue.take();
queueDataBean = threadLocalDataQueue.get().take();
} catch (InterruptedException e) {
// 恢复中断信号 继续获取数据
Thread.currentThread().interrupt();

正在加载...
取消
保存