8
@GetMapping("/consume/list/download")
public Result exportConsumeList(Boolean income, @RequestParam String company, String orderId, Long startTime, Long endTime) throws Exception {
int batchMaxCount = 5000;
int total = (Integer) walletService.findConsumeCount(income, company,orderId, startTime, endTime).getData();
int pageNums = total / batchMaxCount;
boolean flag = total % batchMaxCount == 0;
int pages = pageNums > 1 && flag ? pageNums + 1 : pageNums;
final ArrayList<String> file = Lists.newArrayList();
String tmpdir = System.getProperty("java.io.tmpdir");
File writeFile = new File(tmpdir + File.separator + UUID.randomUUID().toString() + ExcelTypeEnum.XLSX.getValue());
IntStream.range(1,pages).forEach(i->{
PageVo<ChannelTurnover> data = channelUserService.findChannelConsumePage(income, company, orderId, startTime, endTime, i, batchMaxCount);
List<ConsumeExport> exportList = getConsumeExportList(data);
logger.info("data:"+ JSON.toJSONString(exportList));
try {
String tmp = getDownloadUri(writeFile, exportList,ConsumeExport.class,"消费列表",i).get();
file.add(tmp);
}catch (Exception e) {
e.printStackTrace();
}
});
return renderSuccess("成功",file.get(0));
}
@Async("scheduledExecutorService")
public Future<String> getDownloadUri(File writeFile, List dataList, Class entityClass, String sheetName,int sheetNo) throws IOException{
ExcelWriter writer = new ExcelWriter(new FileOutputStream(writeFile),ExcelTypeEnum.XLSX);
Sheet sheet = new Sheet(sheetNo,0, entityClass);
sheet.setSheetName(sheetName + sheetNo);
writer.write(dataList,sheet).finish();
log.info("saveFile:" + writeFile.getAbsolutePath() + " file size: " + writeFile.length());
String filePath = "/download/" + writeFile.getName();
executorService.schedule(()->{
writeFile.delete();
},10, TimeUnit.MINUTES);
return new AsyncResult<>(filePath);
}