Commit 20226f96 by jianan

前端对接问题修复30

parent a360bd18
......@@ -198,24 +198,28 @@ public class ApiFortuneController {
*/
@Operation(summary = "生成出账清单")
@PostMapping("/download/account")
public void downloadAccount(@RequestBody FortuneDownloadRequest fortuneDownloadRequest, HttpServletResponse response) throws IOException {
public Result<Boolean> downloadAccount(@RequestBody FortuneDownloadRequest fortuneDownloadRequest, HttpServletResponse response) throws IOException {
if (CollectionUtils.isEmpty(fortuneDownloadRequest.getFortuneBizIdList())) {
throw new BusinessException(ResultCode.PARAMS_ERROR.getCode(), "请选择要出账的发佣数据");
}
try {
fortuneService.downloadAccount(fortuneDownloadRequest, response);
return Result.success(true);
} catch (Exception e) {
// 设置响应类型为 JSON,而不是文件流
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");
Result<String> errorResult = Result.fail(
ResultCode.FAIL.getCode(),
e.getMessage()
);
// // 设置响应类型为 JSON,而不是文件流
// response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
// response.setContentType(MediaType.APPLICATION_JSON_VALUE);
// response.setCharacterEncoding("UTF-8");
//
// Result<String> errorResult = Result.fail(
// ResultCode.FAIL.getCode(),
// e.getMessage()
// );
//
// response.getWriter().write(new ObjectMapper().writeValueAsString(errorResult));
response.getWriter().write(new ObjectMapper().writeValueAsString(errorResult));
log.error("生成出账清单失败", e);
return Result.success(false);
}
}
......
......@@ -265,54 +265,54 @@ public class FortuneServiceImpl extends ServiceImpl<FortuneMapper, Fortune>
accountExportDTOList.add(accountDTO);
}
// 设置响应头
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("utf-8");
String fileName = "fortune_account_" + System.currentTimeMillis() + ".xlsx";
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// 创建动态表头
List<List<String>> head = new ArrayList<>();
head.add(Collections.singletonList("转介人"));
head.add(Collections.singletonList("所属团队"));
head.add(Collections.singletonList("出账币种"));
head.add(Collections.singletonList("出账总额"));
// 添加所有fortune项目作为表头
for (String fortuneName : allFortuneNames) {
head.add(Collections.singletonList(fortuneName + "金额"));
}
// 构建数据行
List<List<Object>> dataList = new ArrayList<>();
for (FortuneAccountExportDTO dto : accountExportDTOList) {
List<Object> row = new ArrayList<>();
row.add(dto.getBroker());
row.add(dto.getTeam());
row.add(dto.getCurrency());
row.add(dto.getAmount());
for (String fortuneName : allFortuneNames) {
row.add(dto.getFortuneAmount(fortuneName));
}
dataList.add(row);
}
// // 设置响应头
// response.setContentType("application/vnd.ms-excel;charset=UTF-8");
// response.setCharacterEncoding("utf-8");
// String fileName = "fortune_account_" + System.currentTimeMillis() + ".xlsx";
// response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//
// // 创建动态表头
// List<List<String>> head = new ArrayList<>();
// head.add(Collections.singletonList("转介人"));
// head.add(Collections.singletonList("所属团队"));
// head.add(Collections.singletonList("出账币种"));
// head.add(Collections.singletonList("出账总额"));
//
// // 添加所有fortune项目作为表头
// for (String fortuneName : allFortuneNames) {
// head.add(Collections.singletonList(fortuneName + "金额"));
// }
//
// // 构建数据行
// List<List<Object>> dataList = new ArrayList<>();
// for (FortuneAccountExportDTO dto : accountExportDTOList) {
// List<Object> row = new ArrayList<>();
// row.add(dto.getBroker());
// row.add(dto.getTeam());
// row.add(dto.getCurrency());
// row.add(dto.getAmount());
// for (String fortuneName : allFortuneNames) {
// row.add(dto.getFortuneAmount(fortuneName));
// }
// dataList.add(row);
// }
// 写入数据库
fortuneAccountService.saveFortuneAccount(accountExportDTOList);
// 使用try-with-resources确保流正确关闭
try (ServletOutputStream outputStream = response.getOutputStream()) {
// 写入 Excel
EasyExcel.write(outputStream)
.head(head)
.sheet("出账清单")
.doWrite(dataList);
outputStream.flush();
} catch (Exception e) {
log.error("导出失败", e);
throw new BusinessException(ResultCode.FAIL.getCode(), "导出失败");
}
// // 使用try-with-resources确保流正确关闭
// try (ServletOutputStream outputStream = response.getOutputStream()) {
// // 写入 Excel
// EasyExcel.write(outputStream)
// .head(head)
// .sheet("出账清单")
// .doWrite(dataList);
//
// outputStream.flush();
// } catch (Exception e) {
// log.error("导出失败", e);
// throw new BusinessException(ResultCode.FAIL.getCode(), "导出失败");
// }
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment