From e30945a79411202ffe9481989c5aa07716007943 Mon Sep 17 00:00:00 2001 From: "taylor.dang" Date: Mon, 3 Aug 2020 14:38:27 +0800 Subject: [PATCH 1/2] [Y] securepay --- .../mappers/ClientAnalysisMapper.java | 2 + .../system/core/TradeSecureService.java | 2 + .../impl/TradeSecureServiceHanyinImpl.java | 61 ++++++ .../manage/task/SecurePaySettleFileTask.java | 28 +++ .../manage/tradelog/core/TradeLogService.java | 2 + .../core/impls/TradeLogServiceImpl.java | 44 +++++ .../analysis/mappers/ClientAnalysisMapper.xml | 177 ++++++++++-------- 7 files changed, 235 insertions(+), 81 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java diff --git a/src/main/java/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.java b/src/main/java/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.java index 2a0978077..e1bc95f50 100644 --- a/src/main/java/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.java @@ -51,4 +51,6 @@ public interface ClientAnalysisMapper { List tradeSubMerchantIdBy60Days(@Param("begin") Date begin); List countClientsSettlementCycle(JSONObject params); + + List listSecurePayMchs(); } diff --git a/src/main/java/au/com/royalpay/payment/manage/system/core/TradeSecureService.java b/src/main/java/au/com/royalpay/payment/manage/system/core/TradeSecureService.java index 599bfd2ca..33dcb175f 100644 --- a/src/main/java/au/com/royalpay/payment/manage/system/core/TradeSecureService.java +++ b/src/main/java/au/com/royalpay/payment/manage/system/core/TradeSecureService.java @@ -16,4 +16,6 @@ public interface TradeSecureService { void manualDownloadHanyinSecureReport(Date date, boolean upload, HttpServletResponse resp); TradeSecureFile getSecureReportFile(Date date); + + void sendSecurePaySettleFile(String dateStr); } diff --git a/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java index 060a41e1b..0f0e1ef92 100644 --- a/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java @@ -1,12 +1,17 @@ package au.com.royalpay.payment.manage.system.core.impl; import au.com.royalpay.payment.core.exceptions.ChannelNetworkException; +import au.com.royalpay.payment.manage.analysis.mappers.ClientAnalysisMapper; import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper; import au.com.royalpay.payment.manage.system.core.TradeSecureService; import au.com.royalpay.payment.manage.system.core.beans.TradeSecureFile; +import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery; +import au.com.royalpay.payment.manage.tradelog.core.TradeLogService; import au.com.royalpay.payment.tools.env.SysConfigManager; import com.alibaba.fastjson.JSONObject; import com.jcraft.jsch.*; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.vfs2.FileSystemOptions; import org.apache.commons.vfs2.provider.sftp.SftpClientFactory; import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder; @@ -40,10 +45,18 @@ public class TradeSecureServiceHanyinImpl implements TradeSecureService { private String sftpUsername; @Value("${app.hanyin-secure.sftp-pwd}") private String sftpPwd; + @Value("${app.securepay.sftp.username}") + private String securePaySftpName; + @Value("${app.securepay.sftp.password}") + private String securePaySftpPwd; @Resource private TransactionMapper transactionMapper; @Resource + private ClientAnalysisMapper clientAnalysisMapper; + @Resource private SysConfigManager sysConfigManager; + @Resource + private TradeLogService tradeLogService; @Override public void uploadSecureReportYesterday() { @@ -119,4 +132,52 @@ public class TradeSecureServiceHanyinImpl implements TradeSecureService { file.addTransactions(debits); return file; } + + @Override + public void sendSecurePaySettleFile(String dateStr) { + List scpayMchs = clientAnalysisMapper.listSecurePayMchs(); + scpayMchs.forEach(mch -> { + TradeLogQuery query = new TradeLogQuery(); + query.setDatefrom(dateStr); + query.setDatefrom(dateStr); + byte[] fileData = tradeLogService.exportPDFSettlement(query, mch); + String fileName = dateStr + "-SETTLEMENT-" + RandomStringUtils.random(9, true, true).toUpperCase() + "-" + + mch.getString("client_moniker") + ".pdf"; + sendSftpFile(dateStr, fileName, fileData); + }); + + } + + private void sendSftpFile(String dateStr, String fileName, byte[] data) { + Session session = null; + ChannelSftp command = null; + try { + session = initSFTPConnection(securePaySftpName, securePaySftpPwd); + Channel channel = session.openChannel("sftp"); + channel.connect(); + command = (ChannelSftp) channel; + try { + command.put(new ByteArrayInputStream(data), "/" + StringUtils.substring(dateStr, 0, 6) + "/" + fileName); + logger.info("文件发送成功!"); + } catch (SftpException e) { + logger.error(e.getMessage()); + } + } catch (IOException | JSchException e) { + throw new ChannelNetworkException("Send Settlement file failed", e); + } finally { + if (command != null && command.isConnected()) { + command.exit(); + session.disconnect(); + } + } + } + + private Session initSFTPConnection(String sftpUsername, String sftpPassword) throws IOException { + FileSystemOptions fso = new FileSystemOptions(); + SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso, "no"); + return SftpClientFactory.createConnection("sftp.royalpay.com.au", 2022, + sftpUsername.toCharArray(), sftpPassword.toCharArray(), fso); + } + + } diff --git a/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java b/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java new file mode 100644 index 000000000..3076e9384 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java @@ -0,0 +1,28 @@ +package au.com.royalpay.payment.manage.task; + +import au.com.royalpay.payment.manage.system.core.TradeSecureService; +import au.com.royalpay.payment.tools.scheduler.SynchronizedScheduler; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; + +@Component +@ConditionalOnProperty(value = "app.run-tasks", havingValue = "true") +public class SecurePaySettleFileTask { + + @Resource + private SynchronizedScheduler synchronizedScheduler; + @Resource + private TradeSecureService tradeSecureService; + + @Scheduled(cron = "0 0 3 * * *") + public void sendSecurePaySettleFile() { + synchronizedScheduler.executeProcess("task:securepay_settle_file_sending", 600_000, () -> + tradeSecureService.sendSecurePaySettleFile(DateFormatUtils.format(new Date(), "yyyyMMdd")) + ); + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java index 7dcf8e398..e79e3201a 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java @@ -69,6 +69,8 @@ public interface TradeLogService { void exportPDFSettlement(TradeLogQuery query, JSONObject partner, HttpServletResponse response); + byte[] exportPDFSettlement(TradeLogQuery query, JSONObject client); + void exportExcelAllPartner(TradeLogQuery query, JSONObject partner, HttpServletResponse httpResponse) throws Exception; } diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index d7ebb7de5..bc493adfd 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -1975,6 +1975,50 @@ public class TradeLogServiceImpl implements TradeLogService { } } + @Override + public byte[] exportPDFSettlement(TradeLogQuery query, JSONObject client) { + String begin = query.getDatefrom() == null ? "" : query.getDatefrom(); + String end = query.getDateto() == null ? "" : query.getDateto(); + String timezone = client.getString("timezone"); + JSONObject params = query.toParams(timezone); + if (params.get("client_ids") == null) { + params.put("client_id", client.getString("client_id")); + } + clientManager.queryModifyClientIds(client.getIntValue("client_id"), params); + List clientOrderList = transactionMapper.getClientOrderByTransactionTime(params); + List clientOrders = new ArrayList<>(clientOrderList.size()); + clientOrderList.parallelStream().forEach(p -> clientOrders.add(p.getInteger("clearing_order"))); + List settlementLogDetailList = transactionMapper.getSettlementLogDetailList(clientOrders); + TimeZoneUtils.switchTimeZoneToString(settlementLogDetailList, timezone, "yyyy-MM-dd HH:mm:ss", Collections.singletonList("transaction_time")); + try { + JSONObject parmerters = new JSONObject(); + parmerters.put("dateRange", "(Statement Period " + begin + "~" + + end + ")"); + parmerters.put("clientName", client.getString("company_name")); + parmerters.put("clientAddress", client.getString("address")); + parmerters.put("clientLocation", client.getString("suburb") + "," + client.getString("state") + "," + client.getString("postcode")); + parmerters.put("logo", logo.getInputStream()); + settlementLogDetailList.parallelStream().forEach(item -> { + scaleDecimalVal(item, "display_amount", item.getString("transaction_currency")); + String platformCurrency = PlatformEnvironment.getEnv().getForeignCurrency(); + scaleDecimalVal(item, "clearing_amount", platformCurrency); + scaleDecimalVal(item, "settle_amount", platformCurrency); + scaleDecimalVal(item, "total_surcharge", platformCurrency); + scaleDecimalVal(item, "transaction_amount", platformCurrency); + item.put("exchange_rate", item.getBigDecimal("exchange_rate").setScale(5, RoundingMode.DOWN)); + item.put("gateway", item.getInteger("gateway") == null ? "-" : TradeType.fromGatewayNumber(item.getIntValue("gateway")).getTradeType()); + item.put("rate", item.getBigDecimal("rate") == null ? "-" : item.getBigDecimal("rate").toPlainString() + "%"); + }); + if (settlementLogDetailList == null) { + settlementLogDetailList = new ArrayList<>(); + } + JRDataSource jrDataSource = new JRBeanCollectionDataSource(settlementLogDetailList); + return JasperRunManager.runReportToPdf(partner_settlement_flow.getInputStream(), parmerters, jrDataSource); + } catch (Exception e) { + throw new ServerErrorException(e); + } + } + @Override public void exportExcelAllPartner(TradeLogQuery query, JSONObject partner, HttpServletResponse resp) throws Exception { diff --git a/src/main/resources/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.xml index a04331f11..74eed6179 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/analysis/mappers/ClientAnalysisMapper.xml @@ -10,7 +10,8 @@ ]]> and org_id=#{org_id} and org_id in - #{org_id} + #{org_id} + @@ -77,7 +80,8 @@ ]]> and org_id=#{org_id} and org_id in - #{org_id} + #{org_id} + GROUP BY YEAR(create_time),MONTH(create_time),DAY(create_time) ORDER BY create_time desc @@ -90,7 +94,8 @@ ]]> and org_id=#{org_id} and org_id in - #{org_id} + #{org_id} + + + SELECT COUNT(DISTINCT c.client_id) client_count, + c.sub_merchant_id, + c.merchant_id + FROM sys_clients c + WHERE c.sub_merchant_id not in + (SELECT p.sub_merchant_id FROM sys_clients p INNER JOIN pmt_orders o ON o.client_id = p.client_id AND o.status >4 AND o.confirm_time IS NOT NULL + WHERE datediff(now(), o.create_time) <= 25 AND p.sub_merchant_id is not null GROUP BY p.sub_merchant_id) AND c.sub_merchant_id IS not null AND c.merchant_id IS NOT NULL + AND (c.approve_result = 1 OR c.approve_result= 2) AND c.is_valid = 1 + AND c.enable_wechat = 1 + ]]> + GROUP by c.sub_merchant_id + + + SELECT COUNT(DISTINCT c.client_id) client_count, + c.sub_merchant_id, + c.merchant_id, + tmp.create_time last_trade_time, + datediff(now(), tmp.create_time) diff_days + FROM sys_clients c INNER JOIN (SELECT sub_merchant_id, max(create_time) create_time + FROM pmt_orders where status>=5 and create_time>=#{begin} and create_time<=now() + and channel='Wechat' + GROUP BY sub_merchant_id + ) tmp on c.sub_merchant_id = tmp.sub_merchant_id + WHERE + c.sub_merchant_id IS not null AND c.merchant_id IS NOT NULL + AND (c.approve_result = 1 OR c.approve_result= 2) AND c.is_valid = 1 + AND c.enable_wechat = 1 + ]]> + GROUP BY c.sub_merchant_id ORDER BY diff_days desc + + From aa2e3acbb4c88b46c82a993bf862ed9af361abf2 Mon Sep 17 00:00:00 2001 From: "taylor.dang" Date: Mon, 3 Aug 2020 18:07:39 +0800 Subject: [PATCH 2/2] [Y] sec --- pom.xml | 2 +- .../manage/dev/web/TestController.java | 5 ++ .../impl/TradeSecureServiceHanyinImpl.java | 7 ++- .../manage/task/SecurePaySettleFileTask.java | 2 +- .../core/impls/TradeLogServiceImpl.java | 46 +++++++++---------- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 4c9989945..330d44d93 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 4.0.0 manage - 2.3.47 + 2.3.48 UTF-8 2.4.0 diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java b/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java index 6a7c5f977..fcdf0bccf 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/web/TestController.java @@ -579,4 +579,9 @@ public class TestController { params.put("client_moniker", clientMoniker); return params; } + + @GetMapping(value = "/securepay/settle/file") + public void sendSecurePaySettleFile() { + tradeSecureService.sendSecurePaySettleFile("20200803"); + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java index 0f0e1ef92..23ee9beaa 100644 --- a/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/TradeSecureServiceHanyinImpl.java @@ -45,9 +45,9 @@ public class TradeSecureServiceHanyinImpl implements TradeSecureService { private String sftpUsername; @Value("${app.hanyin-secure.sftp-pwd}") private String sftpPwd; - @Value("${app.securepay.sftp.username}") + @Value("${app.securepay.sftp.username:upitest}") private String securePaySftpName; - @Value("${app.securepay.sftp.password}") + @Value("${app.securepay.sftp.password:upitest}") private String securePaySftpPwd; @Resource private TransactionMapper transactionMapper; @@ -174,8 +174,7 @@ public class TradeSecureServiceHanyinImpl implements TradeSecureService { private Session initSFTPConnection(String sftpUsername, String sftpPassword) throws IOException { FileSystemOptions fso = new FileSystemOptions(); - SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso, "no"); - return SftpClientFactory.createConnection("sftp.royalpay.com.au", 2022, + return SftpClientFactory.createConnection("124.156.126.236", 2022, sftpUsername.toCharArray(), sftpPassword.toCharArray(), fso); } diff --git a/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java b/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java index 3076e9384..70f58b530 100644 --- a/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java +++ b/src/main/java/au/com/royalpay/payment/manage/task/SecurePaySettleFileTask.java @@ -21,7 +21,7 @@ public class SecurePaySettleFileTask { @Scheduled(cron = "0 0 3 * * *") public void sendSecurePaySettleFile() { - synchronizedScheduler.executeProcess("task:securepay_settle_file_sending", 600_000, () -> + synchronizedScheduler.executeProcess("task:securepay_settle_file_sending", 60_000, () -> tradeSecureService.sendSecurePaySettleFile(DateFormatUtils.format(new Date(), "yyyyMMdd")) ); } diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index bc493adfd..e8e35e2cb 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -1986,31 +1986,31 @@ public class TradeLogServiceImpl implements TradeLogService { } clientManager.queryModifyClientIds(client.getIntValue("client_id"), params); List clientOrderList = transactionMapper.getClientOrderByTransactionTime(params); - List clientOrders = new ArrayList<>(clientOrderList.size()); - clientOrderList.parallelStream().forEach(p -> clientOrders.add(p.getInteger("clearing_order"))); - List settlementLogDetailList = transactionMapper.getSettlementLogDetailList(clientOrders); - TimeZoneUtils.switchTimeZoneToString(settlementLogDetailList, timezone, "yyyy-MM-dd HH:mm:ss", Collections.singletonList("transaction_time")); + JSONObject parmerters = new JSONObject(); + parmerters.put("dateRange", "(Statement Period " + begin + "~" + + end + ")"); + parmerters.put("clientName", client.getString("company_name")); + parmerters.put("clientAddress", client.getString("address")); + parmerters.put("clientLocation", client.getString("suburb") + "," + client.getString("state") + "," + client.getString("postcode")); + List settlementLogDetailList = new ArrayList<>(); try { - JSONObject parmerters = new JSONObject(); - parmerters.put("dateRange", "(Statement Period " + begin + "~" - + end + ")"); - parmerters.put("clientName", client.getString("company_name")); - parmerters.put("clientAddress", client.getString("address")); - parmerters.put("clientLocation", client.getString("suburb") + "," + client.getString("state") + "," + client.getString("postcode")); parmerters.put("logo", logo.getInputStream()); - settlementLogDetailList.parallelStream().forEach(item -> { - scaleDecimalVal(item, "display_amount", item.getString("transaction_currency")); - String platformCurrency = PlatformEnvironment.getEnv().getForeignCurrency(); - scaleDecimalVal(item, "clearing_amount", platformCurrency); - scaleDecimalVal(item, "settle_amount", platformCurrency); - scaleDecimalVal(item, "total_surcharge", platformCurrency); - scaleDecimalVal(item, "transaction_amount", platformCurrency); - item.put("exchange_rate", item.getBigDecimal("exchange_rate").setScale(5, RoundingMode.DOWN)); - item.put("gateway", item.getInteger("gateway") == null ? "-" : TradeType.fromGatewayNumber(item.getIntValue("gateway")).getTradeType()); - item.put("rate", item.getBigDecimal("rate") == null ? "-" : item.getBigDecimal("rate").toPlainString() + "%"); - }); - if (settlementLogDetailList == null) { - settlementLogDetailList = new ArrayList<>(); + if (clientOrderList.size() > 0) { + List clientOrders = new ArrayList<>(clientOrderList.size()); + clientOrderList.parallelStream().forEach(p -> clientOrders.add(p.getInteger("clearing_order"))); + settlementLogDetailList = transactionMapper.getSettlementLogDetailList(clientOrders); + TimeZoneUtils.switchTimeZoneToString(settlementLogDetailList, timezone, "yyyy-MM-dd HH:mm:ss", Collections.singletonList("transaction_time")); + settlementLogDetailList.parallelStream().forEach(item -> { + scaleDecimalVal(item, "display_amount", item.getString("transaction_currency")); + String platformCurrency = PlatformEnvironment.getEnv().getForeignCurrency(); + scaleDecimalVal(item, "clearing_amount", platformCurrency); + scaleDecimalVal(item, "settle_amount", platformCurrency); + scaleDecimalVal(item, "total_surcharge", platformCurrency); + scaleDecimalVal(item, "transaction_amount", platformCurrency); + item.put("exchange_rate", item.getBigDecimal("exchange_rate").setScale(5, RoundingMode.DOWN)); + item.put("gateway", item.getInteger("gateway") == null ? "-" : TradeType.fromGatewayNumber(item.getIntValue("gateway")).getTradeType()); + item.put("rate", item.getBigDecimal("rate") == null ? "-" : item.getBigDecimal("rate").toPlainString() + "%"); + }); } JRDataSource jrDataSource = new JRBeanCollectionDataSource(settlementLogDetailList); return JasperRunManager.runReportToPdf(partner_settlement_flow.getInputStream(), parmerters, jrDataSource);