|
|
|
@ -3,6 +3,7 @@ package au.com.royalpay.payment.manage.settlement.core.impls;
|
|
|
|
|
import au.com.royalpay.payment.manage.mappers.log.ClearingLogMapper;
|
|
|
|
|
import au.com.royalpay.payment.manage.mappers.payment.TaskManualSettleMapper;
|
|
|
|
|
import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper;
|
|
|
|
|
import au.com.royalpay.payment.manage.mappers.system.CalendarMapper;
|
|
|
|
|
import au.com.royalpay.payment.manage.mappers.system.ClientAccountMapper;
|
|
|
|
|
import au.com.royalpay.payment.manage.settlement.core.ManualSettleSupport;
|
|
|
|
|
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
|
|
|
|
@ -11,20 +12,18 @@ import au.com.royalpay.payment.tools.locale.LocaleSupport;
|
|
|
|
|
import au.com.royalpay.payment.tools.merchants.core.MerchantInfoProvider;
|
|
|
|
|
import au.com.royalpay.payment.tools.permission.enums.PartnerRole;
|
|
|
|
|
import au.com.royalpay.payment.tools.utils.CurrencyAmountUtils;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
|
|
import org.apache.commons.lang3.time.DateUtils;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create by yixian at 2018-03-20 17:44
|
|
|
|
|
*/
|
|
|
|
@ -41,6 +40,8 @@ public class ManualSettleSupportImpl implements ManualSettleSupport {
|
|
|
|
|
private ClearingLogMapper clearingLogMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
private ClientAccountMapper clientAccountMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
private CalendarMapper calendarMapper;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject requestManualSettle(Date settleTo, String accountId) {
|
|
|
|
@ -53,6 +54,9 @@ public class ManualSettleSupportImpl implements ManualSettleSupport {
|
|
|
|
|
throw new BadRequestException("Cannot settle today's transactions");
|
|
|
|
|
}
|
|
|
|
|
JSONObject currentTask = findCurrentSettle(clientId, false);
|
|
|
|
|
if (currentTask.getBooleanValue("locked")) {
|
|
|
|
|
throw new BadRequestException("You cannot do this at the moment");
|
|
|
|
|
}
|
|
|
|
|
String taskId = currentTask.getString("task_id");
|
|
|
|
|
currentTask.put("request_time", new Date());
|
|
|
|
|
currentTask.put("client_id", clientId);
|
|
|
|
@ -73,6 +77,9 @@ public class ManualSettleSupportImpl implements ManualSettleSupport {
|
|
|
|
|
if (!client.getBooleanValue("manual_settle")) {
|
|
|
|
|
throw new ForbiddenException("Manual Settlement Not Enabled");
|
|
|
|
|
}
|
|
|
|
|
if (client.getBooleanValue("skip_clearing")) {
|
|
|
|
|
throw new ForbiddenException("Manual Settlement Not Enabled");
|
|
|
|
|
}
|
|
|
|
|
JSONObject todayTask = taskManualSettleMapper.findTodayTask(clientId);
|
|
|
|
|
if (todayTask != null) {
|
|
|
|
|
todayTask.put("settle_to", DateFormatUtils.format(todayTask.getDate("settle_to"), "yyyy-MM-dd"));
|
|
|
|
@ -80,10 +87,12 @@ public class ManualSettleSupportImpl implements ManualSettleSupport {
|
|
|
|
|
todayTask = new JSONObject();
|
|
|
|
|
}
|
|
|
|
|
List<JSONObject> settleLogs = clearingLogMapper.findByDate(new Date());
|
|
|
|
|
List<JSONObject> unsettleRequests = taskManualSettleMapper.listActiveTasks(clientId);
|
|
|
|
|
//今天未清算则锁定
|
|
|
|
|
todayTask.put("locked", settleLogs.isEmpty());
|
|
|
|
|
todayTask.put("locked", settleLogs.isEmpty() || !unsettleRequests.isEmpty());
|
|
|
|
|
if (includingUnsettleData) {
|
|
|
|
|
List<JSONObject> unsettleReports = transactionMapper.listClientUnsettleDataByDate(clientId);
|
|
|
|
|
Date end = findSettleTo(clientId);
|
|
|
|
|
List<JSONObject> unsettleReports = transactionMapper.listClientUnsettleDataByDate(clientId, end);
|
|
|
|
|
unsettleReports.parallelStream().forEach(report -> {
|
|
|
|
|
report.put("clearing_amount", CurrencyAmountUtils.scalePlatformCurrency(report.getBigDecimal("clearing_amount")));
|
|
|
|
|
BigDecimal settleAmount = report.getBigDecimal("settle_amount");
|
|
|
|
@ -101,6 +110,20 @@ public class ManualSettleSupportImpl implements ManualSettleSupport {
|
|
|
|
|
return todayTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Date findSettleTo(int clientId) {
|
|
|
|
|
Date today = DateUtils.truncate(new Date(), Calendar.DATE);
|
|
|
|
|
JSONObject rateConfig = merchantInfoProvider.clientCurrentRate(clientId, today, "Wechat");
|
|
|
|
|
int cleanDays = rateConfig.getIntValue("clean_days");
|
|
|
|
|
List<Date> settleDateRange = calendarMapper.findClearingDateRange(today, cleanDays - 1);
|
|
|
|
|
Date end;
|
|
|
|
|
if (settleDateRange.size() < 2) {
|
|
|
|
|
end = DateUtils.addDays(today, -cleanDays);
|
|
|
|
|
} else {
|
|
|
|
|
end = DateUtils.addDays(settleDateRange.get(0), -1);
|
|
|
|
|
}
|
|
|
|
|
return end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<JSONObject> listWithClearInfo() {
|
|
|
|
|
List<JSONObject> manuals = taskManualSettleMapper.getEveryLatestRecord();
|
|
|
|
|