ソースを参照

优惠券结算

skyline 2 年 前
コミット
6bb3fd1658

+ 29 - 54
service/src/main/java/com/kym/service/enplus/impl/EnNotifyServiceImpl.java

@@ -5,7 +5,6 @@ import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.kym.common.cache.ConnectorStatusCache;
-import com.kym.common.config.RedisDBChangeUtil;
 import com.kym.entity.admin.ConnectorInfo;
 import com.kym.entity.admin.EquipmentInfo;
 import com.kym.entity.admin.MonitorLog;
@@ -14,7 +13,6 @@ import com.kym.entity.enplus.EnConnectorStatusInfo;
 import com.kym.entity.miniapp.Account;
 import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.WalletDetail;
-import com.kym.entity.miniapp.other.DiscountCompute;
 import com.kym.service.admin.ConnectorInfoService;
 import com.kym.service.admin.EquipmentInfoService;
 import com.kym.service.admin.MonitorLogService;
@@ -48,26 +46,15 @@ import java.util.stream.Collectors;
 @Service
 public class EnNotifyServiceImpl implements EnNotifyService {
     private static final Logger LOGGER = LoggerFactory.getLogger(EnNotifyServiceImpl.class);
-
+    public final StringRedisTemplate redisTemplate;
     private final EnPlusService enPlusService;
-
     private final ChargeOrderService chargeOrderService;
-
     private final ChargeService chargeService;
-
     private final AccountService accountService;
-
     private final WalletDetailService walletDetailService;
-
     private final MonitorLogService monitorLogService;
-
     private final EquipmentInfoService equipmentInfoService;
-
     private final ConnectorInfoService connectorInfoService;
-
-
-    public final StringRedisTemplate redisTemplate;
-
     @Value("${kym.notify-email}")
     private String notifyEmail;
 
@@ -279,35 +266,35 @@ public class EnNotifyServiceImpl implements EnNotifyService {
         var account = accountService.getAccountByUserId(chargeOrder.getUserId());
 
         // EN+平台推送重试策略是当天失败第二天再推送一次,仅此一次。EN+订单页面可以多次手动推送,所以这里要先判断订单状态,避免重复处理。
-        if (chargeOrder.getChargeStatus() != ChargeOrder.CHARGE_STATUS_已结束 || chargeOrder.getOrderStatus() != ChargeOrder.ORDER_STATUS_成功) {
-            // 更新订单信息
-            chargeOrder
-                    .setStartTime(LocalDateTime.parse(data.getString("StartTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
-                    .setEndTime(LocalDateTime.parse(data.getString("EndTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
-                    .setTotalPower(data.getDoubleValue("TotalPower"))
-                    .setElecMoney((int) Math.round(data.getDouble("TotalElecMoney") * 100))
-                    .setServiceMoney((int) Math.round(data.getDoubleValue("TotalSeviceMoney") * 100)) // 这里文档service单词错误,按文档填写
-                    .setTotalMoney((int) Math.round(data.getDoubleValue("TotalMoney") * 100))
-                    .setStopReason(data.getIntValue("StopReason"))
-                    .setSumPeriod(data.getIntValue("SumPeriod"))
-                    .setChargeDetail(data.getString("ChargeDetails"))
-                    // 实付金额初始化为订单总金额
-                    .setPayAmount(chargeOrder.getTotalMoney());
-
-            // 结束时间
-            var endTime = LocalDateTime.parse(data.getString("EndTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-
-            // 处理充值权益优惠逻辑/优惠券优惠逻辑
-            // 优惠金额
-            var discountCompute = DiscountStrategyFactory.getDiscountStrategy(chargeOrder.getDiscountType()).computeDiscount(new DiscountCompute(chargeOrder, account, endTime));
-
-            // 更新订单优惠金额
-            updateOrderDiscount(chargeOrder, discountCompute);
-
-            // 扣费等资金操作
-            deductions(discountCompute.chargeOrder, discountCompute.account, endTime);
+//        if (chargeOrder.getChargeStatus() != ChargeOrder.CHARGE_STATUS_已结束 || chargeOrder.getOrderStatus() != ChargeOrder.ORDER_STATUS_成功) {
+        // 更新订单信息
+        chargeOrder
+                .setStartTime(LocalDateTime.parse(data.getString("StartTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
+                .setEndTime(LocalDateTime.parse(data.getString("EndTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
+                .setTotalPower(data.getDoubleValue("TotalPower"))
+                .setElecMoney((int) Math.round(data.getDouble("TotalElecMoney") * 100))
+                .setServiceMoney((int) Math.round(data.getDoubleValue("TotalSeviceMoney") * 100)) // 这里文档service单词错误,按文档填写
+                .setTotalMoney((int) Math.round(data.getDoubleValue("TotalMoney") * 100))
+                .setStopReason(data.getIntValue("StopReason"))
+                .setSumPeriod(data.getIntValue("SumPeriod"))
+                .setChargeDetail(data.getString("ChargeDetails"))
+                // 实付金额初始化为订单总金额
+                .setPayAmount(chargeOrder.getTotalMoney());
+
+        // 结束时间
+        var endTime = LocalDateTime.parse(data.getString("EndTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+
+        // 处理充值权益优惠逻辑/优惠券优惠逻辑
+        // 优惠金额
+        DiscountStrategyFactory.getDiscountStrategy(chargeOrder.getDiscountType()).computeDiscount(chargeOrder, account);
+
+        // 更新订单优惠金额
+        chargeOrderService.updateById(chargeOrder);
 
-        }
+        // 扣费等资金操作
+        deductions(chargeOrder, account, endTime);
+
+//        }
         return """
                 {
                     "StartChargeSeq":"%s",
@@ -318,18 +305,6 @@ public class EnNotifyServiceImpl implements EnNotifyService {
     }
 
 
-    /**
-     * 更新订单优惠金额
-     *
-     * @param chargeOrder
-     * @param discountCompute
-     */
-    void updateOrderDiscount(ChargeOrder chargeOrder, DiscountCompute discountCompute) {
-        chargeOrder.setDiscountAmount(discountCompute.discountAmount);
-        chargeOrder.setServiceMoneyDiscount(discountCompute.discountAmount);
-        chargeOrderService.updateById(chargeOrder);
-    }
-
     /**
      * 扣费等资金操作
      *

+ 17 - 3
service/src/main/java/com/kym/service/factory/DiscountStrategyFactory.java

@@ -1,7 +1,7 @@
 package com.kym.service.factory;
 
 import com.kym.entity.admin.Activity;
-import com.kym.service.miniapp.DiscountService;
+import com.kym.service.miniapp.*;
 import com.kym.service.miniapp.impl.CouponDiscountHandle;
 import com.kym.service.miniapp.impl.NoDiscountHandle;
 import com.kym.service.miniapp.impl.RechargeRightsDiscountHandle;
@@ -14,11 +14,25 @@ import org.springframework.stereotype.Component;
  */
 @Component
 public class DiscountStrategyFactory {
+
+    private static OrderRechargeRightsService orderRechargeRightsService;
+    private static UserRechargeRightsService userRechargeRightsService;
+
+    private static OrderCouponService orderCouponService;
+    private static UserCouponService userCouponService;
+
+    public DiscountStrategyFactory(OrderRechargeRightsService orderRechargeRightsService, UserRechargeRightsService userRechargeRightsService, OrderCouponService orderCouponService, UserCouponService userCouponService) {
+        this.orderRechargeRightsService = orderRechargeRightsService;
+        this.userRechargeRightsService = userRechargeRightsService;
+        this.orderCouponService = orderCouponService;
+        this.userCouponService = userCouponService;
+    }
+
     public static DiscountService getDiscountStrategy(String discountType) {
         if (Activity.DISCOUNT_TYPE_优惠券.equals(discountType)) {
-            return new CouponDiscountHandle();
+            return new CouponDiscountHandle(orderCouponService, userCouponService);
         } else if (Activity.DISCOUNT_TYPE_服务费折扣权益.equals(discountType)) {
-            return new RechargeRightsDiscountHandle();
+            return new RechargeRightsDiscountHandle(orderRechargeRightsService, userRechargeRightsService);
         } else {
             return new NoDiscountHandle();
         }

+ 3 - 1
service/src/main/java/com/kym/service/miniapp/DiscountService.java

@@ -1,7 +1,9 @@
 package com.kym.service.miniapp;
 
+import com.kym.entity.miniapp.Account;
+import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.other.DiscountCompute;
 
 public interface DiscountService {
-    DiscountCompute computeDiscount(DiscountCompute discountCompute);
+    void computeDiscount(ChargeOrder chargeOrder, Account account);
 }

+ 16 - 11
service/src/main/java/com/kym/service/miniapp/impl/CouponDiscountHandle.java

@@ -1,13 +1,13 @@
 package com.kym.service.miniapp.impl;
 
 import com.kym.entity.admin.Coupon;
+import com.kym.entity.miniapp.Account;
+import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.OrderCoupon;
 import com.kym.entity.miniapp.UserCoupon;
-import com.kym.entity.miniapp.other.DiscountCompute;
 import com.kym.service.miniapp.DiscountService;
 import com.kym.service.miniapp.OrderCouponService;
 import com.kym.service.miniapp.UserCouponService;
-import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
@@ -19,16 +19,18 @@ import org.springframework.stereotype.Service;
 @Service
 @Slf4j
 public class CouponDiscountHandle implements DiscountService {
-    @Resource
-    private OrderCouponService orderCouponService;
-    @Resource
-    private UserCouponService userCouponService;
+    private final OrderCouponService orderCouponService;
+    private final UserCouponService userCouponService;
+
+    public CouponDiscountHandle(OrderCouponService orderCouponService, UserCouponService userCouponService) {
+        this.orderCouponService = orderCouponService;
+        this.userCouponService = userCouponService;
+    }
 
 
     @Override
-    public DiscountCompute computeDiscount(DiscountCompute discountCompute) {
-        var chargeOrder = discountCompute.chargeOrder;
-        int discountAmount = discountCompute.discountAmount;
+    public void computeDiscount(ChargeOrder chargeOrder, Account account) {
+        int discountAmount = 0;
 
         try {
             // 获取订单优惠券
@@ -60,6 +62,11 @@ public class CouponDiscountHandle implements DiscountService {
                     // 其他类型券
                     break;
             }
+
+            // 订单优惠金额更新
+            chargeOrder.setDiscountAmount(discountAmount);
+            chargeOrder.setServiceMoneyDiscount(discountAmount);
+
             // 更新用户优惠券信息
             userCoupon.setDiscount(discountAmount);
             userCouponService.updateById(userCoupon);
@@ -67,8 +74,6 @@ public class CouponDiscountHandle implements DiscountService {
             log.error("处理优惠券失败", e);
             // 可以添加一些错误处理逻辑,比如重试机制、发送告警等
         }
-
-        return discountCompute;
     }
 
 

+ 7 - 3
service/src/main/java/com/kym/service/miniapp/impl/NoDiscountHandle.java

@@ -1,6 +1,7 @@
 package com.kym.service.miniapp.impl;
 
-import com.kym.entity.miniapp.other.DiscountCompute;
+import com.kym.entity.miniapp.Account;
+import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.service.miniapp.DiscountService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -15,9 +16,12 @@ import org.springframework.stereotype.Service;
 public class NoDiscountHandle implements DiscountService {
 
     @Override
-    public DiscountCompute computeDiscount(DiscountCompute discountCompute) {
-        return discountCompute;
+    public void computeDiscount(ChargeOrder chargeOrder, Account account) {
+        chargeOrder.setDiscountAmount(0);
+        chargeOrder.setServiceMoneyDiscount(0);
     }
 
+    ;
+
 
 }

+ 13 - 11
service/src/main/java/com/kym/service/miniapp/impl/RechargeRightsDiscountHandle.java

@@ -1,5 +1,7 @@
 package com.kym.service.miniapp.impl;
 
+import com.kym.entity.miniapp.Account;
+import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.OrderRechargeRights;
 import com.kym.entity.miniapp.UserRechargeRights;
 import com.kym.entity.miniapp.other.DiscountCompute;
@@ -8,6 +10,7 @@ import com.kym.service.miniapp.OrderRechargeRightsService;
 import com.kym.service.miniapp.UserRechargeRightsService;
 import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
@@ -23,16 +26,17 @@ import java.math.RoundingMode;
 public class RechargeRightsDiscountHandle implements DiscountService {
 
 
-    @Resource
-    private OrderRechargeRightsService orderRechargeRightsService;
-    @Resource
-    private UserRechargeRightsService userRechargeRightsService;
+    private final OrderRechargeRightsService orderRechargeRightsService;
+    private final UserRechargeRightsService userRechargeRightsService;
+
+    public RechargeRightsDiscountHandle(OrderRechargeRightsService orderRechargeRightsService, UserRechargeRightsService userRechargeRightsService) {
+        this.orderRechargeRightsService = orderRechargeRightsService;
+        this.userRechargeRightsService = userRechargeRightsService;
+    }
 
     @Override
-    public DiscountCompute computeDiscount(DiscountCompute discountCompute) {
-        var chargeOrder = discountCompute.chargeOrder;
-        var account = discountCompute.account;
-        int discountAmount = discountCompute.discountAmount;
+    public void computeDiscount(ChargeOrder chargeOrder, Account account) {
+        int discountAmount;
 
         try {
             // 获取订单权益,计算订单优惠金额,实付金额,服务费优惠金额
@@ -113,11 +117,9 @@ public class RechargeRightsDiscountHandle implements DiscountService {
 
 
         } catch (Exception e) {
-            log.error("处理优惠失败", e);
+            log.error("处理充值优惠失败", e);
             // 可以添加一些错误处理逻辑,比如重试机制、发送告警等
         }
-
-        return discountCompute;
     }