From a641ac3ad115f1e7458c67158d63935abead3afa Mon Sep 17 00:00:00 2001 From: Yixian Date: Fri, 25 Mar 2022 14:59:29 +0800 Subject: [PATCH] feature: send customized settle file --- .../clearing/core/CleanService.java | 2 + .../clearing/core/impl/CleanServiceImpl.java | 77 +++++++++++++++++-- .../clearing/web/SettlementDevController.java | 6 ++ .../manage/support/abafile/ABAFile.java | 11 ++- src/main/ui/static/analysis/clearing-log.js | 15 +++- .../templates/dialog_customized_settle.html | 8 +- 6 files changed, 105 insertions(+), 14 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java index 08751c31a..19b8f81fe 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java @@ -60,6 +60,8 @@ public interface CleanService { void getCustomizedSettleABA(int logId, HttpServletResponse response); + JSONObject sendCustomizedSettleMail(List logIds); + void getSettlementFilesForBatch(String batchId, HttpServletResponse resp) throws IOException; List getCleanLogs(ClearingLogQuery query, JSONObject manager); diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java index a2feb922b..8c0a9b695 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java @@ -683,6 +683,18 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider if (log == null) { throw new NotFoundException("Settle log not found"); } + ABAFile file = buildCustomizedABAFile(log); + response.setContentType("application/octet-stream;"); + response.addHeader("Content-Disposition", "attachment; filename=" + file.filename()); + try (OutputStream ous = response.getOutputStream()) { + ous.write(file.output(1)); + ous.flush(); + } catch (IOException e) { + throw new ServerErrorException("Output failed", e); + } + } + + private ABAFile buildCustomizedABAFile(JSONObject log) { String bank = log.getString("settle_bank"); BalanceGroup group = BalanceGroup.valueOf(log.getString("balance_group")); JSONObject settle = new JSONObject(); @@ -694,15 +706,64 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider settle.put("account_name", log.getString("account_name")); settle.put("client_moniker", log.getString("client_moniker")); ABAFile file = generateSettleAbaFile(bank, group, new Date(), Collections.singletonList(settle)); - response.setContentType("application/octet-stream;"); - String filename = String.format("[%s]Manual_Settle_%s_%s.aba", logId, log.getString("client_moniker"), DateTime.now().toString("yyyyMMdd")); - response.addHeader("Content-Disposition", "attachment; filename=" + filename); - try (OutputStream ous = response.getOutputStream()) { - ous.write(file.output(1)); - ous.flush(); - } catch (IOException e) { - throw new ServerErrorException("Output failed", e); + String filename = String.format("[%s]Manual_Settle_%s_%s.aba", log.getIntValue("log_id"), log.getString("client_moniker"), DateTime.now().toString("yyyyMMdd")); + file.setFilename(filename); + return file; + } + + @Override + public JSONObject sendCustomizedSettleMail(List logIds) { + JSONObject result = new JSONObject(); + try { + Date date = DateTime.now().withTimeAtStartOfDay().toDate(); + String title = (PlatformEnvironment.getEnv().isDebug() ? "[TEST]" : "") + "Royalpay Manual Settlement Files " + DateFormatUtils.format(date, "yyyyMMdd"); + List customizedLogs = logIds.stream().map(customizedSettleLogMapper::find) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + List abaFileList = customizedLogs.stream().map(this::buildCustomizedABAFile).collect(Collectors.toList()); + List attachList = abaFileList.stream() + .map(aba -> new JSONObject(Map.of("name", aba.filename(), + "content", Base64.encodeBase64String(aba.output(0))))) + .collect(Collectors.toList()); + + Context ctx = new Context(); + ctx.setVariable("date", DateFormatUtils.format(date, "dd-MM-yyyy")); + ctx.setVariable("abaFiles", abaFileList); + BigDecimal total = abaFileList.stream().map(ABAFile::getTotalSettleAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); + ctx.setVariable("totalAmount", total.setScale(2, RoundingMode.DOWN).toPlainString()); + final String content = thymeleaf.process("mail/settlement_mail", ctx); + // 测试用地址 + JSONObject config = sysConfigManager.getSysConfig(); + String mailId = mailService.sendEmail(title, config.getString("settle_mail_to"), config.getString("settle_mail_cc"), content, attachList); + + JSONObject settleMailRecord = new JSONObject(); + settleMailRecord.put("send_date", new Date()); + settleMailRecord.put("clearing_date", date); + settleMailRecord.put("email_id", mailId); + if (StringUtils.isEmpty(mailId)) { + result.put("result", 1); + result.put("msg", "System error"); + settleMailRecord.put("mail_status", 0); + settleMailRecord.put("notice_status", 0); + logSettleMailMapper.save(settleMailRecord); + return result; + } else { + settleMailRecord.put("mail_status", 1); + settleMailRecord.put("notice_status", 0); + logSettleMailMapper.save(settleMailRecord); + } + String detailDescription = customizedLogs.stream() + .map(log -> String.format("[%s]%s-%s", log.getString("log_id"), + log.getString("client_moniker"), log.getBigDecimal("amount"))) + .collect(Collectors.joining(",")); + sendTaskFinishMessages(ManagerRole.FINANCIAL_STAFF, "人工清算文件已发送清算方:" + detailDescription, "发送清算通知"); + } catch (URISyntaxException | IOException e) { + logger.error("调用服务发送邮件时错误", e); + throw new ServerErrorException("IOError", e); } + result.put("result", 0); + result.put("msg", "已发送"); + return result; } @Override diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java index 7e0d4a5f4..0ae007f8e 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java @@ -182,6 +182,12 @@ public class SettlementDevController { cleanService.getCustomizedSettleABA(logId, response); } + @PostMapping("/customized_settle/sending_mail") + public JSONObject sendCustomizedSettleFile(@RequestBody JSONObject param){ + List ids = param.getJSONArray("ids").toJavaList(Integer.class); + return cleanService.sendCustomizedSettleMail(ids); + } + @GetMapping("/settle_batches/{batchId}/settle_files") public void getSettlementFilesForBatch(@PathVariable String batchId, HttpServletResponse resp) throws IOException { cleanService.getSettlementFilesForBatch(batchId, resp); diff --git a/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAFile.java b/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAFile.java index 889f3d0c5..8edcbd48a 100644 --- a/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAFile.java +++ b/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAFile.java @@ -13,6 +13,7 @@ import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Optional; public class ABAFile { private final String baseLine = StringUtils.repeat(" ", 120); @@ -21,6 +22,7 @@ public class ABAFile { private List settlements; private BigDecimal totalSettleAmount; private Date operateTime; + private String filename; private String remark; public ABAFile(ABAConfig.ABABase base, Date settleDate) { @@ -72,7 +74,7 @@ public class ABAFile { return format.format(totalSettleAmount); } - public BigDecimal getTotalSettleAmount(){ + public BigDecimal getTotalSettleAmount() { return totalSettleAmount; } @@ -138,8 +140,13 @@ public class ABAFile { return operateTime; } + public ABAFile setFilename(String filename) { + this.filename = filename; + return this; + } + public String filename() { - return "Merchant_Settlement_Info_" + bank() + "_" + getOperateTimeString() + ".aba"; + return Optional.ofNullable(filename).orElse("Merchant_Settlement_Info_" + bank() + "_" + getOperateTimeString() + ".aba"); } private class SettleMerchantInfo { diff --git a/src/main/ui/static/analysis/clearing-log.js b/src/main/ui/static/analysis/clearing-log.js index fd447036c..7bbad8870 100644 --- a/src/main/ui/static/analysis/clearing-log.js +++ b/src/main/ui/static/analysis/clearing-log.js @@ -388,8 +388,21 @@ define(['angular', 'decimal', 'uiBootstrap', 'uiRouter', 'angularEcharts'], func } }]); - app.controller('customizedDialogCtrl', ['$scope', 'customizedLogs', function ($scope, customizedLogs) { + app.controller('customizedDialogCtrl', ['$scope', '$http', 'commonDialog', 'customizedLogs', function ($scope, $http, commonDialog, customizedLogs) { $scope.customizedLogs = customizedLogs.data + $scope.sendMail = function () { + let selected = $scope.customizedLogs.filter(log => log.selected).map(log => log.log_id) + if (!selected.length) { + commonDialog.alert({type: 'error', title: 'Not Selected', content: 'No file selected to send'}) + return + } + $http.post('/sys/settlement/customized_settle/sending_mail', {ids: selected}).then(function (resp) { + $scope.$dismiss() + commonDialog.alert({type: 'success', title: 'Success', content: resp.data.msg}) + }, function (resp) { + commonDialog.alert({type: 'error', title: 'Send Email Failed', content: resp.data.message}) + }) + } }]) app.controller('settlementTransactionsCtrl', ['$scope', '$stateParams', 'detail', function ($scope, $stateParams, detail) { $scope.ctrl = {channel: null}; diff --git a/src/main/ui/static/analysis/templates/dialog_customized_settle.html b/src/main/ui/static/analysis/templates/dialog_customized_settle.html index cc9223f51..6d64ca3ea 100644 --- a/src/main/ui/static/analysis/templates/dialog_customized_settle.html +++ b/src/main/ui/static/analysis/templates/dialog_customized_settle.html @@ -1,5 +1,6 @@