revoke settlement pay back to distributed surcharge

master
yixian 6 years ago
parent 4e150e4555
commit 7ad3c813a0

@ -31,6 +31,7 @@ import au.com.royalpay.payment.tools.exceptions.BadRequestException;
import au.com.royalpay.payment.tools.exceptions.ForbiddenException; import au.com.royalpay.payment.tools.exceptions.ForbiddenException;
import au.com.royalpay.payment.tools.exceptions.NotFoundException; import au.com.royalpay.payment.tools.exceptions.NotFoundException;
import au.com.royalpay.payment.tools.locale.LocaleSupport; import au.com.royalpay.payment.tools.locale.LocaleSupport;
import au.com.royalpay.payment.tools.lock.Locker;
import au.com.royalpay.payment.tools.permission.enums.ManagerRole; import au.com.royalpay.payment.tools.permission.enums.ManagerRole;
import au.com.royalpay.payment.tools.tasksupport.TaskFinishNotifyEvent; import au.com.royalpay.payment.tools.tasksupport.TaskFinishNotifyEvent;
import au.com.royalpay.payment.tools.utils.PageListUtils; import au.com.royalpay.payment.tools.utils.PageListUtils;
@ -153,6 +154,12 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
private ClientConfigService clientConfigService; private ClientConfigService clientConfigService;
@Resource @Resource
private SysConfigManager sysConfigManager; private SysConfigManager sysConfigManager;
@Resource
private ClientsSurchargeAccountsMapper clientsSurchargeAccountsMapper;
@Resource
private ClearingDistributedSurchargeMapper clearingDistributedSurchargeMapper;
@Resource
private Locker locker;
@Resource @Resource
private ClientDeviceMapper clientDeviceMapper; private ClientDeviceMapper clientDeviceMapper;
@ -1241,6 +1248,13 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
if (!log.getBooleanValue("editable")) { if (!log.getBooleanValue("editable")) {
throw new ForbiddenException("Settlement log has been sent and unable to edit"); throw new ForbiddenException("Settlement log has been sent and unable to edit");
} }
List<JSONObject> details = clearingDetailMapper.listReportsOfSettlement(clearingId);
List<JSONObject> detailsWithDistributedSurcharge = details.stream().filter(detail -> detail.getBigDecimal("distributed_surcharge").compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
if (!detailsWithDistributedSurcharge.isEmpty()) {
for (JSONObject detail : detailsWithDistributedSurcharge) {
releaseDistributedSurcharge(detail);
}
}
transactionMapper.deleteSettlementTransaction(clearingId); transactionMapper.deleteSettlementTransaction(clearingId);
transactionMapper.removeSettleRemark(clearingId); transactionMapper.removeSettleRemark(clearingId);
clearingDetailAnalysisMapper.clearAnalysis(clearingId); clearingDetailAnalysisMapper.clearAnalysis(clearingId);
@ -1249,6 +1263,47 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider
clearingLogMapper.deleteSettleLogs(clearingId); clearingLogMapper.deleteSettleLogs(clearingId);
} }
private void releaseDistributedSurcharge(JSONObject clearingDetail) {
int clientId = clearingDetail.getIntValue("client_id");
BigDecimal distributedSurcharge = clearingDetail.getBigDecimal("distributed_surcharge");
String lockKey = "client_surcharge_account_processing:" + clientId;
locker.lock(lockKey, 30_000, 1000);
try {
JSONObject account = getClientSurchargeAccount(clientId);
BigDecimal balance = account.getBigDecimal("balance");
BigDecimal newBalance = balance.add(distributedSurcharge);
JSONObject surchargeLog = new JSONObject();
String logId = DateFormatUtils.format(new Date(), "yyyyMMdd") + clientId + RandomStringUtils.random(5, true, true).toUpperCase();
surchargeLog.put("log_id", logId);
surchargeLog.put("client_id", clientId);
surchargeLog.put("clearing_detail_id", clearingDetail.getString("clear_detail_id"));
surchargeLog.put("settle_date", clearingDetail.getDate("report_date"));
surchargeLog.put("create_time", new Date());
surchargeLog.put("amount", distributedSurcharge);
surchargeLog.put("total_surcharge", clearingDetail.getBigDecimal("total_charge"));
surchargeLog.put("tax_amount", clearingDetail.getBigDecimal("tax_amount"));
surchargeLog.put("type", "Credit");
surchargeLog.put("post_balance", newBalance);
surchargeLog.put("remark", "settle revoke");
clearingDistributedSurchargeMapper.save(surchargeLog);
clientsSurchargeAccountsMapper.updateBalance(clientId, newBalance);
} finally {
locker.unlock(lockKey);
}
}
private JSONObject getClientSurchargeAccount(int clientId) {
JSONObject account = clientsSurchargeAccountsMapper.find(clientId);
if (account == null) {
account = new JSONObject();
account.put("client_id", clientId);
account.put("create_time", new Date());
account.put("balance", 0);
clientsSurchargeAccountsMapper.save(account);
}
return account;
}
private byte[] getZipByteArr(List<JSONObject> fileByteArrWithName) throws IOException { private byte[] getZipByteArr(List<JSONObject> fileByteArrWithName) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos); ZipOutputStream zos = new ZipOutputStream(bos);

@ -7,7 +7,7 @@ import cn.yixblog.support.mybatis.autosql.annotations.SqlType;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List; import java.math.BigDecimal;
@AutoMapper(tablename = "sys_clients_surcharge_accounts", pkName = "client_id") @AutoMapper(tablename = "sys_clients_surcharge_accounts", pkName = "client_id")
@ -22,4 +22,7 @@ public interface ClientsSurchargeAccountsMapper {
@AutoSql(type = SqlType.UPDATE) @AutoSql(type = SqlType.UPDATE)
void update(JSONObject surchargeAccount); void update(JSONObject surchargeAccount);
@AutoSql(type = SqlType.UPDATE)
void updateBalance(@Param("client_id") int clientId, @Param("balance") BigDecimal balance);
} }

Loading…
Cancel
Save