Selaa lähdekoodia

fix bug 充值权益

skyline 2 vuotta sitten
vanhempi
säilyke
9aecad7b16

+ 4 - 4
service/src/main/java/com/kym/service/admin/impl/ActivityServiceImpl.java

@@ -172,11 +172,11 @@ public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> i
             // 充值金额,匹配到到具体的充值权益,生成用户权益
             if (activity != null) {
                 var rechargeRights = rechargeRightsService.lambdaQuery().eq(RechargeRights::getActivityId, activity.getId())
-                        .ge(RechargeRights::getAmountMin, rechargeAmount)
-                        .le(RechargeRights::getAmountMax, rechargeAmount) // 最后一档最大值设置成10000
+                        .le(RechargeRights::getAmountMin, rechargeAmount)
+                        .ge(RechargeRights::getAmountMax, rechargeAmount) // 最后一档最大值设置成10000
                         .one();
-                if(rechargeRights!=null){
-                    var userRechargeRights = new UserRechargeRights().setUserId(this.useId).setRightsBalance(rechargeAmount);
+                if (rechargeRights != null) {
+                    var userRechargeRights = new UserRechargeRights().setRightsId(rechargeRights.getId()).setUserId(this.useId).setRightsBalance(rechargeAmount);
                     BeanUtils.copyProperties(rechargeRights, userRechargeRights, "id");
                     // 计算有效期
                     var endTime = LocalDateTime.now().with(LocalTime.MAX).plusDays(rechargeRights.getValidity() - 1);

+ 11 - 8
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -320,7 +320,7 @@ public class WxPayServiceImpl implements WxPayService {
 
                 // 异步处理充值服务费打折权益活动相关逻辑
                 activityService.handleRechargeActivity(walletDetail.getUserId(), transaction.getAmount().getTotal());
-                
+
                 // 支付记录
                 var payLog = new PayLog();
                 payLog.setUserId(walletDetail.getUserId());
@@ -391,6 +391,7 @@ public class WxPayServiceImpl implements WxPayService {
 
         // 余减去优惠金额作为退款金额
         AtomicInteger refundAmount = new AtomicInteger(account.getBalance() - account.getDiscountAmount());
+        var originalRefundAmount = refundAmount.get();
         // 充值记录
         var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, RefundLog.STATUS_退款成功).orderByDesc(PayLog::getSuccessTime).list();
         // 充值金额综合不足以支付余额退款
@@ -423,6 +424,7 @@ public class WxPayServiceImpl implements WxPayService {
             var size = refundPayLogs.size();
 
             var refundLogList = new ArrayList<RefundLog>(size);
+            var newRefundLogList = new ArrayList<RefundLog>(size);
 
             refundPayLogs.forEach(LambadaTools.forEachWithIndex((item, index) -> {
                 // 如果不是最后一笔,金额全退,最后一笔退剩余的金额
@@ -438,23 +440,25 @@ public class WxPayServiceImpl implements WxPayService {
             }));
 
             // 不可退金额按退款金额比例放入每笔退款中
+
             refundLogList.forEach(LambadaTools.forEachWithIndex((refundLog, index) -> {
-                refundLogList.remove(index.intValue());
+//                refundLogList.remove(index.intValue());
                 int discountAmount;
                 if (index < size) {
-                    discountAmount = account.getDiscountAmount() * (refundLog.getRefund() / refundAmount.get());
+                    discountAmount = account.getDiscountAmount() * (refundLog.getRefund() / originalRefundAmount);
                 } else {
-                    // 前面存在精度误差,最后一个元素用总数详见最精确
+                    // 前面存在精度误差,最后一个元素用总数相减最精确
                     discountAmount = account.getDiscountAmount() - refundLogList.stream().mapToInt(RefundLog::getDiscountAmount).sum();
                 }
                 refundLog.setDiscountAmount(discountAmount);
-                refundLogList.add(index, refundLog);
+//                refundLogList.add(index, refundLog);
+                newRefundLogList.add(index, refundLog);
             }));
 
-            refundLogService.saveBatch(refundLogList);
+            refundLogService.saveBatch(newRefundLogList);
         } else {
             // 退款异常
-            LOGGER.error("退款异常:userId:{},余额:{},payLogs:{}", userId, refundAmount.get(), payLogs);
+            LOGGER.error("退款异常:userId:{},退款金额:{},payLogs:{}", userId, originalRefundAmount, payLogs);
             throw new BusinessException("退款异常");
         }
     }
@@ -554,7 +558,6 @@ public class WxPayServiceImpl implements WxPayService {
                     .set(RefundLog::getStatus, refundNotification.getRefundStatus().name())
                     .set(RefundLog::getTotal, refundNotification.getAmount().getTotal().intValue())
                     .set(RefundLog::getRefund, refundNotification.getAmount().getRefund().intValue())
-                    .set(RefundLog::getCreateTime, LocalDateTimeUtil.of(DateUtil.parse(refundNotification.getCreateTime())))
                     .eq(RefundLog::getId, refundLog.getId()).update();
 
             if (RefundLog.STATUS_退款成功.equals(refundNotification.getRefundStatus().name())) {