Parcourir la source

使用充值权益的订单结束时,余额、权益金等资金相关操作

skyline il y a 2 ans
Parent
commit
e35be80a73

+ 5 - 2
entity/src/main/java/com/kym/entity/miniapp/UserRechargeRights.java

@@ -24,6 +24,9 @@ public class UserRechargeRights extends BaseEntity {
 
     private static final long serialVersionUID = 1L;
 
+    public final static int STATUS_无效 = 0;
+    public final static int STATUS_有效 = 1;
+
     /**
      * 充值权益id
      */
@@ -67,7 +70,7 @@ public class UserRechargeRights extends BaseEntity {
     /**
      * 优惠允许叠加:0-不允许,1-允许
      */
-    private Byte allowStacke;
+    private Integer allowStacke;
 
     /**
      * 总优惠金额(分)
@@ -82,7 +85,7 @@ public class UserRechargeRights extends BaseEntity {
     /**
      * 状态:0-无效,1-有效
      */
-    private Byte status;
+    private Integer status;
 
     /**
      * 备注

+ 51 - 10
service/src/main/java/com/kym/service/enplus/impl/EnNotifyServiceImpl.java

@@ -11,15 +11,13 @@ import com.kym.entity.common.RedisKeys;
 import com.kym.entity.enplus.EnConnectorStatusInfo;
 import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.OrderRechargeRights;
+import com.kym.entity.miniapp.UserRechargeRights;
 import com.kym.entity.miniapp.WalletDetail;
 import com.kym.service.admin.EquipmentInfoService;
 import com.kym.service.admin.MonitorLogService;
 import com.kym.service.enplus.EnNotifyService;
 import com.kym.service.enplus.EnPlusService;
-import com.kym.service.miniapp.AccountService;
-import com.kym.service.miniapp.ChargeOrderService;
-import com.kym.service.miniapp.OrderRechargeRightsService;
-import com.kym.service.miniapp.WalletDetailService;
+import com.kym.service.miniapp.*;
 import com.kym.service.utils.KymCache;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -53,6 +51,8 @@ public class EnNotifyServiceImpl implements EnNotifyService {
 
     private final EquipmentInfoService equipmentInfoService;
 
+    private final UserRechargeRightsService userRechargeRightsService;
+
     private final OrderRechargeRightsService orderRechargeRightsService;
 
     private final KymCache kymCache;
@@ -65,13 +65,14 @@ public class EnNotifyServiceImpl implements EnNotifyService {
     public EnNotifyServiceImpl(EnPlusService enPlusService, ChargeOrderService chargeOrderService,
                                AccountService accountService, WalletDetailService walletDetailService,
                                MonitorLogService monitorLogService, EquipmentInfoService equipmentInfoService,
-                               OrderRechargeRightsService orderRechargeRightsService, KymCache kymCache, RedisDBChangeUtil redisDBChangeUtil) {
+                               UserRechargeRightsService userRechargeRightsService, OrderRechargeRightsService orderRechargeRightsService, KymCache kymCache, RedisDBChangeUtil redisDBChangeUtil) {
         this.enPlusService = enPlusService;
         this.chargeOrderService = chargeOrderService;
         this.accountService = accountService;
         this.walletDetailService = walletDetailService;
         this.monitorLogService = monitorLogService;
         this.equipmentInfoService = equipmentInfoService;
+        this.userRechargeRightsService = userRechargeRightsService;
         this.orderRechargeRightsService = orderRechargeRightsService;
         this.kymCache = kymCache;
         this.redisDBChangeUtil = redisDBChangeUtil;
@@ -252,6 +253,10 @@ public class EnNotifyServiceImpl implements EnNotifyService {
         LOGGER.info("【EN+推送】收到充电订单信息推送:\n{},解密数据:\n{}", json, data);
         var startChargeSeq = data.getString("StartChargeSeq");
         var chargeOrder = chargeOrderService.getChargingOrderByStartChargeSeq(startChargeSeq);
+
+        // 账户
+        var account = accountService.getAccountByUserId(chargeOrder.getUserId());
+
         // EN+平台推送重试策略是当天失败第二天再推送一次,仅此一次。EN+订单页面可以多次手动推送,所以这里要先判断订单状态,避免重复处理。
         if (chargeOrder.getChargeStatus() != ChargeOrder.CHARGE_STATUS_已结束 || chargeOrder.getOrderStatus() != ChargeOrder.ORDER_STATUS_成功) {
             // 更新订单信息
@@ -279,9 +284,46 @@ public class EnNotifyServiceImpl implements EnNotifyService {
                         .setDiscountAmount(serviceMoneyDiscount)
                         .setServiceMoney(serviceMoneyDiscount);
 
-                // 更新订单权益的最终优惠金额
                 orderRechargeRights.setDiscountAmount(serviceMoneyDiscount);
+
+                // 用户当前权益金余额是否足够支付本次充电费用
+                var userRechargeRights = userRechargeRightsService.lambdaQuery()
+                        .eq(UserRechargeRights::getUserId, chargeOrder.getUserId())
+                        .eq(UserRechargeRights::getActivityId, orderRechargeRights.getActivityId())
+                        .one();
+
+                userRechargeRights
+                        .setRightsBalance(userRechargeRights.getRightsBalance() - serviceMoneyDiscount)
+                        .setDiscountAmount(userRechargeRights.getDiscountAmount() + serviceMoneyDiscount);
+
+                // 账户设置优惠不可退金额
+                account.setDiscountAmount(account.getDiscountAmount() + serviceMoneyDiscount);
+
+                // 不足
+                if (userRechargeRights.getStatus() == UserRechargeRights.STATUS_有效) {
+                    // 如果优惠后的实付金额大于权益金余额,那么实际优惠金额需要通过权益金余额反推
+                    if (chargeOrder.getPayAmount() > userRechargeRights.getRightsBalance()) {
+                        var realDiscountAmount = userRechargeRights.getRightsBalance() / (100 - discount); // 只保留到分,分以下不进行四舍五入
+                        chargeOrder
+                                .setPayAmount(chargeOrder.getTotalMoney() - realDiscountAmount)
+                                .setDiscountAmount(realDiscountAmount)
+                                .setServiceMoney(realDiscountAmount);
+                        // 余额扣完,总优惠金额加上最后一次真实优惠金额,此条用户权益结束,状态设置为无效
+                        userRechargeRights
+                                .setRightsBalance(0)
+                                .setDiscountAmount(userRechargeRights.getDiscountAmount() + realDiscountAmount)
+                                .setStatus(UserRechargeRights.STATUS_无效);
+
+                        orderRechargeRights.setDiscountAmount(realDiscountAmount);
+                        // 此权益包消耗完毕,账户设置优惠不可退金额,将次权益金产生的优惠全部扣除
+                        account.setDiscountAmount(account.getDiscountAmount() + realDiscountAmount - userRechargeRights.getDiscountAmount());
+                    }
+                }
+
+                // 更新订单权益的最终优惠金额
                 orderRechargeRightsService.updateById(orderRechargeRights);
+                // 更新用户权益金等
+                userRechargeRightsService.updateById(userRechargeRights);
             }
 
             // 优惠券(将来如果有多种活动形式,进行链式处理)
@@ -292,9 +334,8 @@ public class EnNotifyServiceImpl implements EnNotifyService {
             chargeOrder.setChargeStatus(ChargeOrder.CHARGE_STATUS_已结束);
             chargeOrderService.updateById(chargeOrder);
 
-            // 扣费
-            var account = accountService.getAccountByUserId(chargeOrder.getUserId());
-            account.setBalance(account.getBalance() - chargeOrder.getTotalMoney());
+            // 账户扣费
+            account.setBalance(account.getBalance() - chargeOrder.getPayAmount());
             accountService.updateById(account);
 
             // 记录资金流水
@@ -303,7 +344,7 @@ public class EnNotifyServiceImpl implements EnNotifyService {
             walletDetail.setOrderNo(startChargeSeq);
             // 消费
             walletDetail.setType(WalletDetail.TYPE_消费);
-            walletDetail.setAmount(chargeOrder.getTotalMoney());
+            walletDetail.setAmount(chargeOrder.getPayAmount());
             walletDetail.setTransactionTime(LocalDateTime.parse(data.getString("EndTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
             // 已确认
             walletDetail.setStatus(WalletDetail.STATUS_已确认);