settle delay cashback

master
yixian 7 years ago
parent 0681a81bb4
commit 4901bd8b1c

@ -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);
}

@ -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);
}
}

@ -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<JSONObject> getClientRank(JSONObject params);
@Transactional
void addCashback(Date date);
}

@ -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<JSONObject> 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<JSONObject> 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<JSONObject> settleTrans = transactionMapper.listSettlementTransactions(detailId);
Assert.notEmpty(settleTrans, "No Settle History");
String transactionOrderId = settleTrans.get(0).getString("order_id");
List<JSONObject> 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);
}
}
}
}
}

@ -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<SettleNotifySendingEvent> {
@Resource
private SettleDelayConfigurer settleDelayConfigurer;
@Override
public void onApplicationEvent(SettleNotifySendingEvent event) {
Date eventTime = new Date(event.getEventTime());
settleDelayConfigurer.addCashback(DateUtils.truncate(eventTime, Calendar.DATE));
}
}

@ -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<JSONObject> cashbackNotices, String clientMoniker, int clientId, List<JSONObject> transactions) {
}
}

@ -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());
}
}
Loading…
Cancel
Save