From 0681a81bb4cdc5c638e84a5b4131e71ee0184013 Mon Sep 17 00:00:00 2001 From: yixian Date: Thu, 11 Jan 2018 15:02:11 +0800 Subject: [PATCH 1/3] sysParams --- src/main/ui/static/boot/managerMainApp.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/ui/static/boot/managerMainApp.js b/src/main/ui/static/boot/managerMainApp.js index c9988febf..5909fa751 100644 --- a/src/main/ui/static/boot/managerMainApp.js +++ b/src/main/ui/static/boot/managerMainApp.js @@ -13,8 +13,11 @@ define(['angular', 'angularSanitize', 'angularAnimate', 'angularMessages', 'uiRo $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; $httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; }]); - app.controller('managerIndexCtrl', ['$scope', '$rootScope', '$http', '$log', '$timeout', '$interval', '$uibModal','$filter', 'myLoginLogView', 'commonDialog', - function ($scope, $rootScope, $http, $log, $timeout, $interval, $uibModal,$filter, myLoginLogView, commonDialog) { + app.controller('managerIndexCtrl', ['$scope', '$rootScope', '$http', '$log', '$timeout', '$interval', '$uibModal', '$filter', 'myLoginLogView', 'commonDialog', + function ($scope, $rootScope, $http, $log, $timeout, $interval, $uibModal, $filter, myLoginLogView, commonDialog) { + $http.get('/sysconfig/base').then(function (resp) { + $rootScope.sysParams = resp.data; + }); var stompClient = null; $scope.loadCurrentUser = function () { $http.get('/global/userstatus/current_manager').then(function (resp) { @@ -31,9 +34,9 @@ define(['angular', 'angularSanitize', 'angularAnimate', 'angularMessages', 'uiRo // $scope.loadPartnerApplyNotice(); // } if ($rootScope.currentUser.wx_openid == null) { - if($filter('withRole')('100')){ + if ($filter('withRole')('100')) { $scope.managerBindWechat('static'); - }else { + } else { $scope.managerBindWechat(true); } @@ -103,7 +106,7 @@ define(['angular', 'angularSanitize', 'angularAnimate', 'angularMessages', 'uiRo $scope.hideSideBar = !$scope.hideSideBar; }; - $scope.showQrCode = function (data,backdrop) { + $scope.showQrCode = function (data, backdrop) { return $uibModal.open({ template: '
请使用微信扫描

{{wx_nickname}}

', controller: ['$scope', 'data', function ($scope, data) { @@ -117,8 +120,8 @@ define(['angular', 'angularSanitize', 'angularAnimate', 'angularMessages', 'uiRo return data; } }, - keyboard:false, - backdrop:backdrop + keyboard: false, + backdrop: backdrop }) }; @@ -166,10 +169,10 @@ define(['angular', 'angularSanitize', 'angularAnimate', 'angularMessages', 'uiRo $scope.managerBindWechat = function (backdrop) { $http.post('/system/manager_wechat_binds').then(function (resp) { $scope.checkCode = resp.data.bind_id; - if(resp.data.wx_nickname){ + if (resp.data.wx_nickname) { backdrop = true; } - $scope.checkModal = $scope.showQrCode(resp.data,backdrop); + $scope.checkModal = $scope.showQrCode(resp.data, backdrop); // $scope.showQrCode(resp.data.url); }) } From 4901bd8b1cf61b931c6a35289f58c0e111ac395c Mon Sep 17 00:00:00 2001 From: yixian Date: Mon, 15 Jan 2018 18:03:48 +0800 Subject: [PATCH 2/3] settle delay cashback --- .../manage/cashback/core/CashbackService.java | 3 + .../core/impl/CashbackServiceImp.java | 23 ++- .../clearing/core/SettleDelayConfigurer.java | 4 + .../core/impl/SettleDelayConfigurerImpl.java | 132 +++++++++++++----- .../SendWeekendDelayCashbackSaver.java | 26 ++++ .../SettleDelayCashbackForNewYearDay.java | 60 ++++++++ .../core/SettleDelayConfigurerTest.java | 33 +++++ 7 files changed, 248 insertions(+), 33 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/management/clearing/handlers/SendWeekendDelayCashbackSaver.java create mode 100644 src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayCashbackForNewYearDay.java create mode 100644 src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurerTest.java diff --git a/src/main/java/au/com/royalpay/payment/manage/cashback/core/CashbackService.java b/src/main/java/au/com/royalpay/payment/manage/cashback/core/CashbackService.java index 52ba05f10..faa58ad37 100644 --- a/src/main/java/au/com/royalpay/payment/manage/cashback/core/CashbackService.java +++ b/src/main/java/au/com/royalpay/payment/manage/cashback/core/CashbackService.java @@ -6,6 +6,7 @@ import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import com.github.miemiedev.mybatis.paginator.domain.PageList; import java.math.BigDecimal; +import java.util.Date; import java.util.List; /** @@ -32,4 +33,6 @@ public interface CashbackService { JSONObject analysisCashback(JSONObject params); void drawDeposits(BigDecimal drawAmount, int clientId, JSONObject operator); + + void saveSurchargeDiscountCashback(int clientId, String clientMoniker, String transactionOrderId, BigDecimal cashbackAmount, Date date, String remark); } \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/cashback/core/impl/CashbackServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/cashback/core/impl/CashbackServiceImp.java index f93ed8d3a..4cd24c53e 100644 --- a/src/main/java/au/com/royalpay/payment/manage/cashback/core/impl/CashbackServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/cashback/core/impl/CashbackServiceImp.java @@ -1,11 +1,14 @@ package au.com.royalpay.payment.manage.cashback.core.impl; import java.math.BigDecimal; +import java.util.Date; import java.util.List; import javax.annotation.Resource; import au.com.royalpay.payment.core.PmtCashbackService; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; @@ -111,7 +114,25 @@ public class CashbackServiceImp implements CashbackService { @Override public void drawDeposits(BigDecimal drawAmount, int clientId, JSONObject operator) { - pmtCashbackService.drawDeposits(drawAmount,clientId,operator); + pmtCashbackService.drawDeposits(drawAmount, clientId, operator); + } + + @Override + public void saveSurchargeDiscountCashback(int clientId, String clientMoniker, String transactionOrderId, BigDecimal cashbackAmount, Date date, String remark) { + if (!cashbackRecordsMapper.findBySurchargeDiscountLog(clientId, date).isEmpty()) { + return; + } + String cashback_id = 'R' + clientMoniker + "-" + DateFormatUtils.format(date, "yyyyMMddHHmmssSSS") + "-" + RandomStringUtils.random(3, true, false).toUpperCase(); + //营销账户 + JSONObject cashback = new JSONObject(); + cashback.put("cashback_id", cashback_id); + cashback.put("client_id", clientId); + cashback.put("cashback_amount", cashbackAmount); + cashback.put("order_id", transactionOrderId); + cashback.put("cashback_type", 2); + cashback.put("remark", remark); + cashback.put("create_time", date); + cashbackRecordsMapper.save(cashback); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurer.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurer.java index 38be7a244..f5e4e0235 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurer.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurer.java @@ -3,7 +3,9 @@ package au.com.royalpay.payment.manage.management.clearing.core; import au.com.royalpay.payment.manage.management.clearing.beans.SettleDelayConfig; import com.alibaba.fastjson.JSONObject; import com.github.miemiedev.mybatis.paginator.domain.PageList; +import org.springframework.transaction.annotation.Transactional; +import java.util.Date; import java.util.List; /** @@ -20,4 +22,6 @@ public interface SettleDelayConfigurer { PageList getClientRank(JSONObject params); + @Transactional + void addCashback(Date date); } diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java index 34926db28..c5b9ce5a5 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java @@ -1,39 +1,40 @@ package au.com.royalpay.payment.manage.management.clearing.core.impl; -import java.math.BigDecimal; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.annotation.Resource; - -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.DateUtils; -import org.joda.time.DateTime; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import com.alibaba.fastjson.JSONObject; -import com.github.miemiedev.mybatis.paginator.domain.Order; -import com.github.miemiedev.mybatis.paginator.domain.PageBounds; -import com.github.miemiedev.mybatis.paginator.domain.PageList; - import au.com.royalpay.payment.core.exceptions.InvalidShortIdException; +import au.com.royalpay.payment.manage.appclient.core.RetailAppService; import au.com.royalpay.payment.manage.cashback.core.CashbackService; import au.com.royalpay.payment.manage.management.clearing.beans.SettleDelayConfig; import au.com.royalpay.payment.manage.management.clearing.core.SettleDelayConfigurer; +import au.com.royalpay.payment.manage.mappers.log.ClearingDetailMapper; import au.com.royalpay.payment.manage.mappers.log.ClearingLogMapper; import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper; import au.com.royalpay.payment.manage.mappers.system.ClientSettleDelayConfMapper; import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.tools.CommonConsts; +import au.com.royalpay.payment.tools.env.PlatformEnvironment; +import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import au.com.royalpay.payment.tools.utils.CurrencyAmountUtils; import au.com.royalpay.payment.tools.utils.PageListUtils; +import com.alibaba.fastjson.JSONObject; +import com.github.miemiedev.mybatis.paginator.domain.Order; +import com.github.miemiedev.mybatis.paginator.domain.PageBounds; +import com.github.miemiedev.mybatis.paginator.domain.PageList; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.joda.time.DateTime; +import org.joda.time.DateTimeConstants; +import org.joda.time.LocalDate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; /** * Create by yixian at 2017-09-08 15:06 @@ -50,6 +51,10 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { private TransactionMapper transactionMapper; @Resource private ClearingLogMapper clearingLogMapper; + @Resource + private ClearingDetailMapper clearingDetailMapper; + @Resource + private RetailAppService retailAppService; private Logger logger = LoggerFactory.getLogger(getClass()); @Override @@ -147,9 +152,9 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { DateTime dt = new DateTime(jsonObject.getDate("create_time")); int weekOfYear = dt.getWeekOfWeekyear(); JSONObject cashBackMapElement = cashBackMap.get(weekOfYear); - if(cashBackMapElement!=null){ - cashBackMapElement.put("cashback_amount",cashBackMapElement.getBigDecimal("cashback_amount").add(jsonObject.getBigDecimal("cashback_amount"))); - }else { + if (cashBackMapElement != null) { + cashBackMapElement.put("cashback_amount", cashBackMapElement.getBigDecimal("cashback_amount").add(jsonObject.getBigDecimal("cashback_amount"))); + } else { cashBackMap.put(weekOfYear, jsonObject); } } @@ -159,7 +164,7 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { JSONObject sunday = weekendAnalysisMap.get(resultEntry.getInteger("weekend") + 1); Date saturday = null; try { - saturday = sf.parse(String.valueOf(resultEntry.getInteger("weekend"))); + saturday = sf.parse(String.valueOf(resultEntry.getInteger("weekend"))); } catch (ParseException e) { logger.info("获取延迟清算金额时 日期转换错误"); } @@ -167,11 +172,11 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { if (resultEntry.getBigDecimal("settle_amount") == null) { continue; } - resultEntry.put("weekend",resultEntry.getIntValue("weekend")+2); + resultEntry.put("weekend", resultEntry.getIntValue("weekend") + 2); resultEntry.put("settle_amount", resultEntry.getBigDecimal("settle_amount").add(sunday.getBigDecimal("settle_amount"))); resultEntry.put("transaction_amount", resultEntry.getBigDecimal("transaction_amount").add(sunday.getBigDecimal("transaction_amount"))); - if (cashBackMap.containsKey(new DateTime(saturday).getWeekOfWeekyear()+1)) { - resultEntry.put("cashback", cashBackMap.get(new DateTime(saturday).getWeekOfWeekyear()+1).getBigDecimal("cashback_amount")); + if (cashBackMap.containsKey(new DateTime(saturday).getWeekOfWeekyear() + 1)) { + resultEntry.put("cashback", cashBackMap.get(new DateTime(saturday).getWeekOfWeekyear() + 1).getBigDecimal("cashback_amount")); } else { resultEntry.put("cashback", 0); } @@ -190,4 +195,67 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { public PageList getClientRank(JSONObject params) { return cashbackService.getSettleDelayRank(params, new PageBounds(params.getIntValue("page"), params.getIntValue("limit"))); } + + @Override + @Transactional + public void addCashback(Date date) { + date = DateUtils.truncate(date, Calendar.DATE); + LocalDate processDate = LocalDate.fromDateFields(date); + if (processDate.getDayOfWeek() != DateTimeConstants.TUESDAY) { + throw new BadRequestException("Not tuesday"); + } + LocalDate sunday = processDate.minusDays(processDate.getDayOfWeek()); + LocalDate saturday = sunday.minusDays(1); + List clientAttends = clientSettleDelayConfMapper.listClients(new JSONObject(), new PageBounds()); + for (JSONObject attend : clientAttends) { + Date monday = DateUtils.addDays(date, -1); + if (attend.getDate("from_date").after(monday) || attend.getDate("to_date").before(monday)) { + continue; + } + int clientId = attend.getIntValue("client_id"); + JSONObject client = clientManager.getClientInfo(clientId); + if (client == null) { + continue; + } + String clientMoniker = client.getString("client_moniker"); + JSONObject clearing = clearingDetailMapper.listReport(date, clientId); + if (clearing == null) { + continue; + } + int detailId = clearing.getIntValue("clear_detail_id"); + List settleTrans = transactionMapper.listSettlementTransactions(detailId); + Assert.notEmpty(settleTrans, "No Settle History"); + String transactionOrderId = settleTrans.get(0).getString("order_id"); + List transactions = transactionMapper.listTransactionsOfClearingOrder(detailId, new PageBounds()); + BigDecimal totalSurcharge = BigDecimal.ZERO; + for (JSONObject transaction : transactions) { + Date day = transaction.getDate("transaction_time"); + LocalDate transactionDate = LocalDate.fromDateFields(day); + if (transactionDate.equals(sunday) || transactionDate.equals(saturday)) { + continue; + } + BigDecimal surcharge = transaction.getBigDecimal("clearing_amount").subtract(transaction.getBigDecimal("settle_amount")); + if ("Debit".equals(transaction.getString("transaction_type"))) { + surcharge = surcharge.multiply(BigDecimal.valueOf(-1)); + } + totalSurcharge = totalSurcharge.add(surcharge); + } + BigDecimal cashbackAmount = CurrencyAmountUtils.scale(totalSurcharge.multiply(attend.getBigDecimal("surcharge_discount")).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP), PlatformEnvironment.getEnv().getForeignCurrency()); + String remark = "Surcharge cashback for " + saturday.toString("dd/MMM/yyyy", Locale.ENGLISH) + " ~ " + sunday.toString("dd/MMM/yyyy", Locale.ENGLISH); + if (cashbackAmount.compareTo(BigDecimal.ZERO) > 0) { + cashbackService.saveSurchargeDiscountCashback(clientId, clientMoniker, transactionOrderId, cashbackAmount, date, remark); + try { + if (!PlatformEnvironment.getEnv().isDebug()) { + JSONObject order = new JSONObject(); + order.put("order_id", transactionOrderId); + order.put("client_id", clientId); + retailAppService.sendCashbackMessage(cashbackAmount.toPlainString(), order);// app message + } + } catch (Exception e) { + logger.error("发送App消息失败", e); + } + } + } + + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/handlers/SendWeekendDelayCashbackSaver.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/handlers/SendWeekendDelayCashbackSaver.java new file mode 100644 index 000000000..881694f43 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/handlers/SendWeekendDelayCashbackSaver.java @@ -0,0 +1,26 @@ +package au.com.royalpay.payment.manage.management.clearing.handlers; + +import au.com.royalpay.payment.core.events.SettleNotifySendingEvent; +import au.com.royalpay.payment.manage.management.clearing.core.SettleDelayConfigurer; +import org.apache.commons.lang3.time.DateUtils; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Calendar; +import java.util.Date; + +/** + * Create by yixian at 2017-12-25 13:48 + */ +@Service +public class SendWeekendDelayCashbackSaver implements ApplicationListener { + @Resource + private SettleDelayConfigurer settleDelayConfigurer; + + @Override + public void onApplicationEvent(SettleNotifySendingEvent event) { + Date eventTime = new Date(event.getEventTime()); + settleDelayConfigurer.addCashback(DateUtils.truncate(eventTime, Calendar.DATE)); + } +} diff --git a/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayCashbackForNewYearDay.java b/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayCashbackForNewYearDay.java new file mode 100644 index 000000000..33eacbd70 --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayCashbackForNewYearDay.java @@ -0,0 +1,60 @@ +package au.com.royalpay.payment.manage.management.clearing.core; + +import au.com.royalpay.payment.manage.appclient.core.RetailAppService; +import au.com.royalpay.payment.manage.cashback.core.CashbackService; +import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper; +import au.com.royalpay.payment.manage.mappers.system.ClientSettleDelayConfMapper; +import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.tools.CommonConsts; +import au.com.royalpay.payment.tools.env.PlatformEnvironment; +import au.com.royalpay.payment.tools.utils.CurrencyAmountUtils; +import com.alibaba.fastjson.JSONObject; +import com.github.miemiedev.mybatis.paginator.domain.PageBounds; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.joda.time.DateTime; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.test.annotation.Commit; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.text.ParseException; +import java.util.*; + +/** + * Create by yixian at 2018-01-15 16:03 + */ +@ActiveProfiles("proxy") +@TestPropertySource(properties = "app.debug=false") +@Commit +public class SettleDelayCashbackForNewYearDay { + + @Resource + private ClientManager clientManager; + @Resource + private TransactionMapper transactionMapper; + @Resource + private CashbackService cashbackService; + @Resource + private RetailAppService retailAppService; + @Resource + private ClientSettleDelayConfMapper clientSettleDelayConfMapper; + private Logger logger = LoggerFactory.getLogger(getClass()); + + + @Test + @Transactional + public void addCashback() throws ParseException { + } + + private void processCashback(Date date, List cashbackNotices, String clientMoniker, int clientId, List transactions) { + } + +} diff --git a/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurerTest.java b/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurerTest.java new file mode 100644 index 000000000..fb9e8a491 --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/management/clearing/core/SettleDelayConfigurerTest.java @@ -0,0 +1,33 @@ +package au.com.royalpay.payment.manage.management.clearing.core; + +import org.joda.time.DateTime; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Commit; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +import static org.junit.Assert.*; + +/** + * Create by yixian at 2018-01-15 15:59 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +@ActiveProfiles("proxy") +@TestPropertySource(properties = "app.debug=false") +@Commit +public class SettleDelayConfigurerTest { + @Resource + private SettleDelayConfigurer settleDelayConfigurer; + + @Test + public void addCashback() { + settleDelayConfigurer.addCashback(DateTime.parse("2018-01-09").toDate()); + } + +} \ No newline at end of file From 708dbfcd443f9edd6db0bf2a45351838b3b33dd8 Mon Sep 17 00:00:00 2001 From: yixian Date: Mon, 15 Jan 2018 18:05:28 +0800 Subject: [PATCH 3/3] settle delay cashback --- .../core/impl/SettleDelayConfigurerImpl.java | 107 ++++++++++-------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java index c5b9ce5a5..96b5e91c2 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/SettleDelayConfigurerImpl.java @@ -13,6 +13,7 @@ import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.tools.CommonConsts; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import au.com.royalpay.payment.tools.lock.Locker; import au.com.royalpay.payment.tools.utils.CurrencyAmountUtils; import au.com.royalpay.payment.tools.utils.PageListUtils; import com.alibaba.fastjson.JSONObject; @@ -54,6 +55,8 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { @Resource private ClearingDetailMapper clearingDetailMapper; @Resource + private Locker locker; + @Resource private RetailAppService retailAppService; private Logger logger = LoggerFactory.getLogger(getClass()); @@ -199,62 +202,70 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer { @Override @Transactional public void addCashback(Date date) { - date = DateUtils.truncate(date, Calendar.DATE); - LocalDate processDate = LocalDate.fromDateFields(date); - if (processDate.getDayOfWeek() != DateTimeConstants.TUESDAY) { - throw new BadRequestException("Not tuesday"); + String key = "settle_delay_cashback"; + if (!locker.lock(key, 600_000)) { + return; } - LocalDate sunday = processDate.minusDays(processDate.getDayOfWeek()); - LocalDate saturday = sunday.minusDays(1); - List clientAttends = clientSettleDelayConfMapper.listClients(new JSONObject(), new PageBounds()); - for (JSONObject attend : clientAttends) { - Date monday = DateUtils.addDays(date, -1); - if (attend.getDate("from_date").after(monday) || attend.getDate("to_date").before(monday)) { - continue; - } - int clientId = attend.getIntValue("client_id"); - JSONObject client = clientManager.getClientInfo(clientId); - if (client == null) { - continue; - } - String clientMoniker = client.getString("client_moniker"); - JSONObject clearing = clearingDetailMapper.listReport(date, clientId); - if (clearing == null) { - continue; + try { + date = DateUtils.truncate(date, Calendar.DATE); + LocalDate processDate = LocalDate.fromDateFields(date); + if (processDate.getDayOfWeek() != DateTimeConstants.TUESDAY) { + throw new BadRequestException("Not tuesday"); } - int detailId = clearing.getIntValue("clear_detail_id"); - List settleTrans = transactionMapper.listSettlementTransactions(detailId); - Assert.notEmpty(settleTrans, "No Settle History"); - String transactionOrderId = settleTrans.get(0).getString("order_id"); - List transactions = transactionMapper.listTransactionsOfClearingOrder(detailId, new PageBounds()); - BigDecimal totalSurcharge = BigDecimal.ZERO; - for (JSONObject transaction : transactions) { - Date day = transaction.getDate("transaction_time"); - LocalDate transactionDate = LocalDate.fromDateFields(day); - if (transactionDate.equals(sunday) || transactionDate.equals(saturday)) { + LocalDate sunday = processDate.minusDays(processDate.getDayOfWeek()); + LocalDate saturday = sunday.minusDays(1); + List clientAttends = clientSettleDelayConfMapper.listClients(new JSONObject(), new PageBounds()); + for (JSONObject attend : clientAttends) { + Date monday = DateUtils.addDays(date, -1); + if (attend.getDate("from_date").after(monday) || attend.getDate("to_date").before(monday)) { continue; } - BigDecimal surcharge = transaction.getBigDecimal("clearing_amount").subtract(transaction.getBigDecimal("settle_amount")); - if ("Debit".equals(transaction.getString("transaction_type"))) { - surcharge = surcharge.multiply(BigDecimal.valueOf(-1)); + int clientId = attend.getIntValue("client_id"); + JSONObject client = clientManager.getClientInfo(clientId); + if (client == null) { + continue; } - totalSurcharge = totalSurcharge.add(surcharge); - } - BigDecimal cashbackAmount = CurrencyAmountUtils.scale(totalSurcharge.multiply(attend.getBigDecimal("surcharge_discount")).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP), PlatformEnvironment.getEnv().getForeignCurrency()); - String remark = "Surcharge cashback for " + saturday.toString("dd/MMM/yyyy", Locale.ENGLISH) + " ~ " + sunday.toString("dd/MMM/yyyy", Locale.ENGLISH); - if (cashbackAmount.compareTo(BigDecimal.ZERO) > 0) { - cashbackService.saveSurchargeDiscountCashback(clientId, clientMoniker, transactionOrderId, cashbackAmount, date, remark); - try { - if (!PlatformEnvironment.getEnv().isDebug()) { - JSONObject order = new JSONObject(); - order.put("order_id", transactionOrderId); - order.put("client_id", clientId); - retailAppService.sendCashbackMessage(cashbackAmount.toPlainString(), order);// app message + String clientMoniker = client.getString("client_moniker"); + JSONObject clearing = clearingDetailMapper.listReport(date, clientId); + if (clearing == null) { + continue; + } + int detailId = clearing.getIntValue("clear_detail_id"); + List settleTrans = transactionMapper.listSettlementTransactions(detailId); + Assert.notEmpty(settleTrans, "No Settle History"); + String transactionOrderId = settleTrans.get(0).getString("order_id"); + List transactions = transactionMapper.listTransactionsOfClearingOrder(detailId, new PageBounds()); + BigDecimal totalSurcharge = BigDecimal.ZERO; + for (JSONObject transaction : transactions) { + Date day = transaction.getDate("transaction_time"); + LocalDate transactionDate = LocalDate.fromDateFields(day); + if (transactionDate.equals(sunday) || transactionDate.equals(saturday)) { + continue; + } + BigDecimal surcharge = transaction.getBigDecimal("clearing_amount").subtract(transaction.getBigDecimal("settle_amount")); + if ("Debit".equals(transaction.getString("transaction_type"))) { + surcharge = surcharge.multiply(BigDecimal.valueOf(-1)); + } + totalSurcharge = totalSurcharge.add(surcharge); + } + BigDecimal cashbackAmount = CurrencyAmountUtils.scale(totalSurcharge.multiply(attend.getBigDecimal("surcharge_discount")).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP), PlatformEnvironment.getEnv().getForeignCurrency()); + String remark = "Surcharge cashback for " + saturday.toString("dd/MMM/yyyy", Locale.ENGLISH) + " ~ " + sunday.toString("dd/MMM/yyyy", Locale.ENGLISH); + if (cashbackAmount.compareTo(BigDecimal.ZERO) > 0) { + cashbackService.saveSurchargeDiscountCashback(clientId, clientMoniker, transactionOrderId, cashbackAmount, date, remark); + try { + if (!PlatformEnvironment.getEnv().isDebug()) { + JSONObject order = new JSONObject(); + order.put("order_id", transactionOrderId); + order.put("client_id", clientId); + retailAppService.sendCashbackMessage(cashbackAmount.toPlainString(), order);// app message + } + } catch (Exception e) { + logger.error("发送App消息失败", e); } - } catch (Exception e) { - logger.error("发送App消息失败", e); } } + } finally { + locker.unlock(key); } }