|
@@ -2,11 +2,14 @@ package com.etotem.cfc.task;
|
|
|
|
|
|
|
|
import com.etotem.cfc.entity.CommissionRecord;
|
|
import com.etotem.cfc.entity.CommissionRecord;
|
|
|
import com.etotem.cfc.entity.PendingRefund;
|
|
import com.etotem.cfc.entity.PendingRefund;
|
|
|
|
|
+import com.etotem.cfc.entity.ProductOrder;
|
|
|
import com.etotem.cfc.entity.User;
|
|
import com.etotem.cfc.entity.User;
|
|
|
import com.etotem.cfc.mapper.CommissionRecordMapper;
|
|
import com.etotem.cfc.mapper.CommissionRecordMapper;
|
|
|
import com.etotem.cfc.mapper.PendingRefundMapper;
|
|
import com.etotem.cfc.mapper.PendingRefundMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
import com.etotem.cfc.mapper.UserMapper;
|
|
import com.etotem.cfc.mapper.UserMapper;
|
|
|
import com.etotem.cfc.service.CommissionService;
|
|
import com.etotem.cfc.service.CommissionService;
|
|
|
|
|
+import com.etotem.cfc.service.PaymentService;
|
|
|
import com.etotem.cfc.service.PendingRefundService;
|
|
import com.etotem.cfc.service.PendingRefundService;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -38,6 +41,12 @@ public class RefundScheduledTasks {
|
|
|
@Resource
|
|
@Resource
|
|
|
private UserMapper userMapper;
|
|
private UserMapper userMapper;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PaymentService paymentService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProductOrderMapper productOrderMapper;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 每 5 分钟执行一次
|
|
* 每 5 分钟执行一次
|
|
|
* 处理待退款队列
|
|
* 处理待退款队列
|
|
@@ -80,20 +89,25 @@ public class RefundScheduledTasks {
|
|
|
* 处理单个退款记录
|
|
* 处理单个退款记录
|
|
|
*/
|
|
*/
|
|
|
private void processSingleRefund(PendingRefund pendingRefund) {
|
|
private void processSingleRefund(PendingRefund pendingRefund) {
|
|
|
|
|
+ if ("product".equals(pendingRefund.getOrderType())) {
|
|
|
|
|
+ processProductRefund(pendingRefund);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ processWithdrawalRefund(pendingRefund);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void processWithdrawalRefund(PendingRefund pendingRefund) {
|
|
|
Long userId = pendingRefund.getUserId();
|
|
Long userId = pendingRefund.getUserId();
|
|
|
Integer amount = pendingRefund.getAmount();
|
|
Integer amount = pendingRefund.getAmount();
|
|
|
|
|
|
|
|
- // 检查用户当前可提现余额
|
|
|
|
|
Integer availableAmount = commissionService.getAvailableAmount(userId);
|
|
Integer availableAmount = commissionService.getAvailableAmount(userId);
|
|
|
|
|
|
|
|
if (availableAmount < amount) {
|
|
if (availableAmount < amount) {
|
|
|
throw new RuntimeException("余额不足,当前可提现:" + availableAmount + ",需要:" + amount);
|
|
throw new RuntimeException("余额不足,当前可提现:" + availableAmount + ",需要:" + amount);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 标记为处理中
|
|
|
|
|
pendingRefundService.markProcessing(pendingRefund.getId());
|
|
pendingRefundService.markProcessing(pendingRefund.getId());
|
|
|
|
|
|
|
|
- // 将该用户所有已结算的佣金记录标记为已提现
|
|
|
|
|
List<CommissionRecord> settledRecords = commissionRecordMapper.selectList(
|
|
List<CommissionRecord> settledRecords = commissionRecordMapper.selectList(
|
|
|
new LambdaQueryWrapper<CommissionRecord>()
|
|
new LambdaQueryWrapper<CommissionRecord>()
|
|
|
.eq(CommissionRecord::getReferrerId, userId)
|
|
.eq(CommissionRecord::getReferrerId, userId)
|
|
@@ -111,7 +125,6 @@ public class RefundScheduledTasks {
|
|
|
int recordAmount = record.getCommissionAmount() != null ? record.getCommissionAmount() : 0;
|
|
int recordAmount = record.getCommissionAmount() != null ? record.getCommissionAmount() : 0;
|
|
|
int refundAmount = Math.min(recordAmount, remainingAmount);
|
|
int refundAmount = Math.min(recordAmount, remainingAmount);
|
|
|
|
|
|
|
|
- // 标记佣金记录为已提现
|
|
|
|
|
record.setStatus("withdrawn");
|
|
record.setStatus("withdrawn");
|
|
|
record.setWithdrawnAt(new Date());
|
|
record.setWithdrawnAt(new Date());
|
|
|
commissionRecordMapper.updateById(record);
|
|
commissionRecordMapper.updateById(record);
|
|
@@ -120,13 +133,38 @@ public class RefundScheduledTasks {
|
|
|
actualRefundedAmount += refundAmount;
|
|
actualRefundedAmount += refundAmount;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 更新退款记录为成功
|
|
|
|
|
pendingRefundService.markSuccess(pendingRefund.getId(), actualRefundedAmount);
|
|
pendingRefundService.markSuccess(pendingRefund.getId(), actualRefundedAmount);
|
|
|
|
|
|
|
|
- log.info("退款处理成功:userId={}, amount={}, actualRefunded={}",
|
|
|
|
|
|
|
+ log.info("提现退款处理成功:userId={}, amount={}, actualRefunded={}",
|
|
|
userId, amount, actualRefundedAmount);
|
|
userId, amount, actualRefundedAmount);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void processProductRefund(PendingRefund pendingRefund) {
|
|
|
|
|
+ Long userId = pendingRefund.getUserId();
|
|
|
|
|
+ String orderNo = pendingRefund.getOrderNo();
|
|
|
|
|
+ Integer amount = pendingRefund.getAmount();
|
|
|
|
|
+
|
|
|
|
|
+ pendingRefundService.markProcessing(pendingRefund.getId());
|
|
|
|
|
+
|
|
|
|
|
+ paymentService.refund(orderNo, amount, "商品退款(待退款队列)");
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<ProductOrder> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(ProductOrder::getOrderNo, orderNo);
|
|
|
|
|
+ ProductOrder order = productOrderMapper.selectOne(wrapper);
|
|
|
|
|
+ if (order != null) {
|
|
|
|
|
+ order.setRefundStatus(2);
|
|
|
|
|
+ order.setRefundAmount(amount);
|
|
|
|
|
+ order.setRefundTime(new Date());
|
|
|
|
|
+ order.setStatus("refunded");
|
|
|
|
|
+ order.setUpdatedAt(new Date());
|
|
|
|
|
+ productOrderMapper.updateById(order);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pendingRefundService.markSuccess(pendingRefund.getId(), amount);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("商品退款处理成功:orderNo={}, userId={}, amount={}", orderNo, userId, amount);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 处理余额不足的情况
|
|
* 处理余额不足的情况
|
|
|
*/
|
|
*/
|