From 3f45e8fdfa31d7b764d60a7c0cb5dd11944ed418 Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期一, 21 九月 2026 23:38:59 +0800
Subject: [PATCH] bug:统计数据修正

---
 cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java |   32 ++++++++++++++++++++------------
 1 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
index 3515c38..d05d8818ea 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
@@ -848,7 +848,7 @@
                 if (userCoupon != null && coupon.getType() == 2) {//代金券
                     JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                     Double num1 = jsonObject.getDouble("conditionalAmount");
-                    if (money.compareTo(new BigDecimal(num1)) <= 0) {
+                    if (num1 == null || num1 <= 0) {
                         return ResultUtil.error("该优惠券无法使用");
                     }
                     money = money.subtract(new BigDecimal(num1)).max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_EVEN);
@@ -895,6 +895,7 @@
             siteBooking.setPayMoney(BigDecimal.ZERO.doubleValue());
             siteBooking.setPayTime(new Date());
             siteBookingService.updateById(siteBooking);
+            consumeCouponAfterSuccessfulPayment(siteBooking);
             HashMap<String, String> map = new HashMap<>();
             map.put("siteBooking", siteBooking.getId().toString());
             return ResultUtil.success(map);
@@ -1043,9 +1044,25 @@
         siteBooking.setPayTime(new Date());
         siteBooking.setStatus(1);
         siteBookingService.updateById(siteBooking);
+        consumeCouponAfterSuccessfulPayment(siteBooking);
         HashMap<String, String> map = new HashMap<>();
         map.put("siteBooking", siteBooking.getId().toString());
         return ResultUtil.success(map);
+    }
+
+    /**
+     * 场地订单实际支付成功后才核销所选优惠券;避免仅创建待支付订单就提前消耗优惠券。
+     */
+    private void consumeCouponAfterSuccessfulPayment(SiteBooking siteBooking) {
+        if (siteBooking.getUserCouponId() == null || siteBooking.getUserCouponId() == 0) {
+            return;
+        }
+        UserCoupon userCoupon = userCouponClient.queryUserCouponById(
+                new QueryUserCouponByIdAndUserId(siteBooking.getUserCouponId(), siteBooking.getAppUserId()));
+        if (userCoupon != null && Integer.valueOf(1).equals(userCoupon.getStatus())) {
+            userCoupon.setStatus(2);
+            userCouponClient.updateUserCoupon(userCoupon);
+        }
     }
 
 
@@ -1280,17 +1297,11 @@
             if (coupon.getType() == 2) {//代金券
                 JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                 Double num1 = jsonObject.getDouble("conditionalAmount");
-                money = money.subtract(new BigDecimal(num1)).setScale(2, RoundingMode.HALF_EVEN);
+                money = money.subtract(new BigDecimal(num1)).max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_EVEN);
             }
             userCoupon.setStatus(2);
             userCouponClient.updateUserCoupon(userCoupon);
         }
-        if (null != couponId && payType == 3) {
-            UserCoupon userCoupon = userCouponClient.queryUserCouponById(new QueryUserCouponByIdAndUserId(siteBooking.getUserCouponId(), appUser.getId()));
-            userCoupon.setStatus(1);
-            userCouponClient.updateUserCoupon(userCoupon);
-        }
-
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
         siteBooking.setOrderNo(sdf.format(new Date()) + UUIDUtil.getNumberRandom(3));
 
@@ -1299,9 +1310,6 @@
             siteBooking.setPayMoney(playCoin.doubleValue());
         } else {
             siteBooking.setPayMoney(money.doubleValue());
-        }
-        if (payType == 3) {
-            siteBooking.setUserCouponId(null);
         }
         siteBookingService.updateById(siteBooking);
         if (payType == 1) {//微信支付
@@ -1368,7 +1376,7 @@
             if (coupon.getType() == 2) {//代金券
                 JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                 Double num1 = jsonObject.getDouble("conditionalAmount");
-                money = money.subtract(new BigDecimal(num1)).setScale(2, RoundingMode.HALF_EVEN);
+                money = money.subtract(new BigDecimal(num1)).max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_EVEN);
             }
         }
         switch (siteBooking.getPayType()) {

--
Gitblit v1.7.1