|
|
@ -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.CommonConsts;
|
|
|
|
import au.com.royalpay.payment.tools.env.PlatformEnvironment;
|
|
|
|
import au.com.royalpay.payment.tools.env.PlatformEnvironment;
|
|
|
|
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
|
|
|
|
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.CurrencyAmountUtils;
|
|
|
|
import au.com.royalpay.payment.tools.utils.PageListUtils;
|
|
|
|
import au.com.royalpay.payment.tools.utils.PageListUtils;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
@ -54,6 +55,8 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer {
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
private ClearingDetailMapper clearingDetailMapper;
|
|
|
|
private ClearingDetailMapper clearingDetailMapper;
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
private Locker locker;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private RetailAppService retailAppService;
|
|
|
|
private RetailAppService retailAppService;
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
|
|
|
@ -199,62 +202,70 @@ public class SettleDelayConfigurerImpl implements SettleDelayConfigurer {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
@Transactional
|
|
|
|
public void addCashback(Date date) {
|
|
|
|
public void addCashback(Date date) {
|
|
|
|
date = DateUtils.truncate(date, Calendar.DATE);
|
|
|
|
String key = "settle_delay_cashback";
|
|
|
|
LocalDate processDate = LocalDate.fromDateFields(date);
|
|
|
|
if (!locker.lock(key, 600_000)) {
|
|
|
|
if (processDate.getDayOfWeek() != DateTimeConstants.TUESDAY) {
|
|
|
|
return;
|
|
|
|
throw new BadRequestException("Not tuesday");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LocalDate sunday = processDate.minusDays(processDate.getDayOfWeek());
|
|
|
|
try {
|
|
|
|
LocalDate saturday = sunday.minusDays(1);
|
|
|
|
date = DateUtils.truncate(date, Calendar.DATE);
|
|
|
|
List<JSONObject> clientAttends = clientSettleDelayConfMapper.listClients(new JSONObject(), new PageBounds());
|
|
|
|
LocalDate processDate = LocalDate.fromDateFields(date);
|
|
|
|
for (JSONObject attend : clientAttends) {
|
|
|
|
if (processDate.getDayOfWeek() != DateTimeConstants.TUESDAY) {
|
|
|
|
Date monday = DateUtils.addDays(date, -1);
|
|
|
|
throw new BadRequestException("Not tuesday");
|
|
|
|
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");
|
|
|
|
LocalDate sunday = processDate.minusDays(processDate.getDayOfWeek());
|
|
|
|
List<JSONObject> settleTrans = transactionMapper.listSettlementTransactions(detailId);
|
|
|
|
LocalDate saturday = sunday.minusDays(1);
|
|
|
|
Assert.notEmpty(settleTrans, "No Settle History");
|
|
|
|
List<JSONObject> clientAttends = clientSettleDelayConfMapper.listClients(new JSONObject(), new PageBounds());
|
|
|
|
String transactionOrderId = settleTrans.get(0).getString("order_id");
|
|
|
|
for (JSONObject attend : clientAttends) {
|
|
|
|
List<JSONObject> transactions = transactionMapper.listTransactionsOfClearingOrder(detailId, new PageBounds());
|
|
|
|
Date monday = DateUtils.addDays(date, -1);
|
|
|
|
BigDecimal totalSurcharge = BigDecimal.ZERO;
|
|
|
|
if (attend.getDate("from_date").after(monday) || attend.getDate("to_date").before(monday)) {
|
|
|
|
for (JSONObject transaction : transactions) {
|
|
|
|
|
|
|
|
Date day = transaction.getDate("transaction_time");
|
|
|
|
|
|
|
|
LocalDate transactionDate = LocalDate.fromDateFields(day);
|
|
|
|
|
|
|
|
if (transactionDate.equals(sunday) || transactionDate.equals(saturday)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BigDecimal surcharge = transaction.getBigDecimal("clearing_amount").subtract(transaction.getBigDecimal("settle_amount"));
|
|
|
|
int clientId = attend.getIntValue("client_id");
|
|
|
|
if ("Debit".equals(transaction.getString("transaction_type"))) {
|
|
|
|
JSONObject client = clientManager.getClientInfo(clientId);
|
|
|
|
surcharge = surcharge.multiply(BigDecimal.valueOf(-1));
|
|
|
|
if (client == null) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalSurcharge = totalSurcharge.add(surcharge);
|
|
|
|
String clientMoniker = client.getString("client_moniker");
|
|
|
|
}
|
|
|
|
JSONObject clearing = clearingDetailMapper.listReport(date, clientId);
|
|
|
|
BigDecimal cashbackAmount = CurrencyAmountUtils.scale(totalSurcharge.multiply(attend.getBigDecimal("surcharge_discount")).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP), PlatformEnvironment.getEnv().getForeignCurrency());
|
|
|
|
if (clearing == null) {
|
|
|
|
String remark = "Surcharge cashback for " + saturday.toString("dd/MMM/yyyy", Locale.ENGLISH) + " ~ " + sunday.toString("dd/MMM/yyyy", Locale.ENGLISH);
|
|
|
|
continue;
|
|
|
|
if (cashbackAmount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
|
}
|
|
|
|
cashbackService.saveSurchargeDiscountCashback(clientId, clientMoniker, transactionOrderId, cashbackAmount, date, remark);
|
|
|
|
int detailId = clearing.getIntValue("clear_detail_id");
|
|
|
|
try {
|
|
|
|
List<JSONObject> settleTrans = transactionMapper.listSettlementTransactions(detailId);
|
|
|
|
if (!PlatformEnvironment.getEnv().isDebug()) {
|
|
|
|
Assert.notEmpty(settleTrans, "No Settle History");
|
|
|
|
JSONObject order = new JSONObject();
|
|
|
|
String transactionOrderId = settleTrans.get(0).getString("order_id");
|
|
|
|
order.put("order_id", transactionOrderId);
|
|
|
|
List<JSONObject> transactions = transactionMapper.listTransactionsOfClearingOrder(detailId, new PageBounds());
|
|
|
|
order.put("client_id", clientId);
|
|
|
|
BigDecimal totalSurcharge = BigDecimal.ZERO;
|
|
|
|
retailAppService.sendCashbackMessage(cashbackAmount.toPlainString(), order);// app message
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|