From 1f20c0f34ea6f744891c9a51f401e2f983a301cc Mon Sep 17 00:00:00 2001 From: yixian Date: Thu, 6 Feb 2020 15:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=AE=97=E6=96=87=E4=BB=B6=E5=90=88?= =?UTF-8?q?=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clearing/core/impl/CleanServiceImpl.java | 78 ++++++++++++++++++- .../mappers/log/ClearingDetailMapper.xml | 43 ++++++---- 2 files changed, 101 insertions(+), 20 deletions(-) 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 32d66a4e0..7f1cb41ec 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 @@ -36,6 +36,7 @@ import au.com.royalpay.payment.tools.utils.PageListUtils; import au.com.royalpay.payment.tools.utils.TimeZoneUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.github.miemiedev.mybatis.paginator.domain.Order; import com.github.miemiedev.mybatis.paginator.domain.PageBounds; @@ -325,6 +326,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider String filename = "Merchant_Settlement_Info_" + dateString + RandomStringUtils.random(8, false, true) + ".csv"; zos.putNextEntry(new ZipEntry(filename)); List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); byte[] csv = generateSettleCSVFile(dt, details); IOUtils.write(csv, zos); } @@ -369,6 +371,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider logger.info("using newest version test"); for (JSONObject log : logs) { List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); exportAllBankXlsFiles(zos, details, log.getDate("operate_time")); } zos.flush(); @@ -400,7 +403,9 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider String filename = "Merchant_Settlement_Info_" + DateFormatUtils.format(dt, "yyyyMMdd") + "_" + fileIndex + ".xlsx"; JSONObject file = new JSONObject(); file.put("name", filename); - file.put("byteArr", generateSettleXlsxFile(dt, clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")), bank)); + List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); + file.put("byteArr", generateSettleXlsxFile(dt, details, bank)); result.add(file); fileIndex++; } @@ -409,7 +414,9 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider String filename = "Merchant_Settlement_Info_" + DateFormatUtils.format(dt, "yyyyMMdd") + ".xlsx"; JSONObject file = new JSONObject(); file.put("name", filename); - file.put("byteArr", generateSettleXlsxFile(dt, clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")), bank)); + List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); + file.put("byteArr", generateSettleXlsxFile(dt, details, bank)); result.add(file); } } @@ -417,6 +424,67 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider return result; } + private List mergeBatchSettleClients(List details) { + Map> sameBankDetails = details.stream() + .filter(this::detailEnabledMergeSettle) + .collect(Collectors.groupingBy(this::detailGroupingId)); + List mergedSettleDetails = sameBankDetails.values().stream() + .map(this::mergeDetailGroup).collect(Collectors.toList()); + List relatedDetailIds = sameBankDetails.values().stream() + .flatMap(group -> group.stream().map(detail -> detail.getInteger("clear_detail_id"))) + .collect(Collectors.toList()); + details.removeIf(detail -> relatedDetailIds.contains(detail.getInteger("clear_detail_id"))); + details.addAll(mergedSettleDetails); + return details; + } + + private JSONObject mergeDetailGroup(List details) { + JSONObject first = details.get(0); + int clientId = details.stream().map(detail -> detail.getInteger("parent_client_id")) + .filter(Objects::nonNull) + .findAny().orElse(first.getIntValue("client_id")); + JSONObject cli = clientManager.getClientInfo(clientId); + String moniker = cli.getString("client_moniker"); + String bsb = first.getString("bsb_no"); + String accountNo = first.getString("account_no"); + String accountName = first.getString("account_name"); + String settleBank = first.getString("settle_bank"); + BigDecimal clearingAmount = details.stream().map(detail -> detail.getBigDecimal("clearing_amount")) + .reduce(BigDecimal::add).orElse(BigDecimal.ZERO); + JSONObject merged = new JSONObject(); + merged.put("client_id", clientId); + merged.put("client_moniker", moniker); + merged.put("bsb_no", bsb); + merged.put("account_no", accountNo); + merged.put("account_name", accountName); + merged.put("settle_bank", settleBank); + merged.put("clearing_amount", clearingAmount); + return merged; + } + + private String detailGroupingId(JSONObject detail) { + String cliPId = detail.getString("parent_client_id"); + if (cliPId == null) { + cliPId = detail.getString("client_id"); + } + String bsb = detail.getString("bsb_no"); + String accountNo = detail.getString("account_no"); + return String.join("_", cliPId, bsb, accountNo); + } + + private boolean detailEnabledMergeSettle(JSONObject detail) { + String extParams = detail.getString("ext_params"); + if (extParams != null) { + try { + JSONObject ext = JSON.parseObject(extParams); + return ext.getBooleanValue("merge_settle"); + } catch (JSONException e) { + return false; + } + } + return false; + } + @Override public List getAba(Date dt, String bank) { List logs = clearingLogMapper.findByDate(dt); @@ -427,6 +495,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider List files = new ArrayList<>(); for (JSONObject log : logs) { List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); files.addAll(generateSettleAbaFiles(dt, details, log.getDate("operate_time"))); } return files.stream().filter(file -> bank.equals(file.bank())).collect(Collectors.toList()); @@ -447,7 +516,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue(settle.getString("bsb_no")); row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue(settle.getString("account_no")); row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue(settle.getString("account_name")); - row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("clearing_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString()); + row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("clearing_amount").setScale(2, RoundingMode.DOWN).toPlainString()); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); wb.write(bos); @@ -466,6 +535,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider for (JSONObject log : logs) { List details = clearingDetailMapper.listReportsOfSettlement(log.getIntValue("clearing_id")); + details = mergeBatchSettleClients(details); files.addAll(generateSettleAbaFiles(dt, details, log.getDate("operate_time"))); } OutputStream ous = resp.getOutputStream(); @@ -502,6 +572,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider throw new NotFoundException("Clearing batch " + batchId + " not found"); } List details = clearingDetailMapper.listReportsOfSettlement(clearingId); + details = mergeBatchSettleClients(details); Date settleDate = clearing.getDate("settle_date"); Date opTime = clearing.getDate("operate_time"); String zipName = "Merchant_Settlement_Info_" + DateFormatUtils.format(opTime, "yyyyMMddHHmmss") + "_all.zip"; @@ -1214,6 +1285,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider String defaultBank = config.getRemainsTo(); clearingDetailMapper.updateAllBanks(defaultBank, clearingId); List details = clearingDetailMapper.listReportsOfSettlement(clearingId); + details = mergeBatchSettleClients(details); details.sort((log1, log2) -> { if (StringUtils.equals(defaultBank, log1.getString("account_bank")) == StringUtils.equals(defaultBank, log2.getString("account_bank"))) { diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml index 473d41a81..cd6d3e435 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml @@ -2,28 +2,34 @@ - update log_clearing_detail set settle_bank=#{settle_bank} where clearing_id=#{clearing_id} + update log_clearing_detail + set settle_bank=#{settle_bank} + where clearing_id = #{clearing_id}