清算文件合并

master
yixian 5 years ago
parent d19bc70a16
commit 1f20c0f34e

@ -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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> mergeBatchSettleClients(List<JSONObject> details) {
Map<String, List<JSONObject>> sameBankDetails = details.stream()
.filter(this::detailEnabledMergeSettle)
.collect(Collectors.groupingBy(this::detailGroupingId));
List<JSONObject> mergedSettleDetails = sameBankDetails.values().stream()
.map(this::mergeDetailGroup).collect(Collectors.toList());
List<Integer> 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<JSONObject> 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<ABAFile> getAba(Date dt, String bank) {
List<JSONObject> logs = clearingLogMapper.findByDate(dt);
@ -427,6 +495,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
List<ABAFile> files = new ArrayList<>();
for (JSONObject log : logs) {
List<JSONObject> 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<JSONObject> 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<JSONObject> 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<JSONObject> 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"))) {

@ -2,27 +2,33 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="au.com.royalpay.payment.manage.mappers.log.ClearingDetailMapper">
<update id="updateAllBanks">
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}
</update>
<select id="listReports" resultType="com.alibaba.fastjson.JSONObject">
<![CDATA[
SELECT
settle_date_from,
SELECT settle_date_from,
settle_date_to,
clearing_amount,
report_date
FROM log_clearing_detail
WHERE client_id = #{client_id} AND month(report_date) = month(#{month}) AND year(report_date) = year(#{month})
WHERE client_id = #{client_id}
AND month(report_date) = month(#{month})
AND year(report_date) = year(#{month})
]]>
</select>
<select id="listReportsOfSettlement" resultType="com.alibaba.fastjson.JSONObject">
SELECT
d.*,DATE_FORMAT(d.report_date,'%y-%m-%d')clear_date,DATE_FORMAT(d.settle_date_from,'%m-%d')settle_from,DATE_FORMAT(d.settle_date_to,'%m-%d')settle_to,
SELECT d.*,
DATE_FORMAT(d.report_date, '%y-%m-%d') clear_date,
DATE_FORMAT(d.settle_date_from, '%m-%d') settle_from,
DATE_FORMAT(d.settle_date_to, '%m-%d') settle_to,
c.client_id,
c.client_moniker,
c.parent_client_id
c.parent_client_id,
c.ext_params
FROM log_clearing_detail d
INNER JOIN sys_clients c ON c.client_id = d.client_id AND c.is_valid=1
INNER JOIN sys_clients c ON c.client_id = d.client_id AND c.is_valid = 1
WHERE d.clearing_id = #{clearing_id}
</select>
@ -57,12 +63,15 @@
and c.org_id = #{org_id}
</if>
<if test="org_ids!=null">and c.org_id in
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">#{org_id}</foreach></if>
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">#{org_id}</foreach>
</if>
</where>
</select>
<select id="listClientSettlementLog" resultType="com.alibaba.fastjson.JSONObject">
select *,DATE_FORMAT(report_date,'%y-%m-%d')clear_date,DATE_FORMAT(settle_date_from,'%m-%d')settle_from,DATE_FORMAT(settle_date_to,'%m-%d')settle_to FROM log_clearing_detail
select
*,DATE_FORMAT(report_date,'%y-%m-%d')clear_date,DATE_FORMAT(settle_date_from,'%m-%d')settle_from,DATE_FORMAT(settle_date_to,'%m-%d')settle_to
FROM log_clearing_detail
<where>
<if test="client_ids!=null">
AND client_id IN

Loading…
Cancel
Save