Commit 20226f96 by jianan

前端对接问题修复30

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