From 8a89ba02db5c19db669a5cf9bb7d443223bdfb71 Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期二, 29 九月 2026 10:48:21 +0800
Subject: [PATCH] 会员管理及取消场地预约问题

---
 cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java |  135 ++++++++++++++++++--------------------------
 1 files changed, 55 insertions(+), 80 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 c60af91..a3f5bd2 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
@@ -9,11 +9,11 @@
 import com.alipay.api.DefaultAlipayClient;
 import com.alipay.api.request.AlipayTradeOrderSettleRequest;
 import com.alipay.api.response.AlipayTradeOrderSettleResponse;
-import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.dsh.other.entity.*;
+import com.dsh.other.enums.SiteBookingStatusEnum;
 import com.dsh.other.feignclient.StoreClient;
 import com.dsh.other.feignclient.account.AppUserClient;
 import com.dsh.other.feignclient.account.model.AppUser;
@@ -828,8 +828,8 @@
         if (couponId != null && couponId != 0) {
             if (null != couponId && reservationSite.getPayType() != 3) {
                 UserCoupon userCoupon = userCouponClient.queryUserCouponById(new QueryUserCouponByIdAndUserId(reservationSite.getCouponId(), uid));
-                if (userCoupon != null && userCoupon.getStatus() == 2) {
-                    return ResultUtil.error("优惠券已被核销");
+                if (userCoupon == null || userCoupon.getStatus() == null || userCoupon.getStatus() != 1) {
+                    return ResultUtil.error("优惠券不可用");
                 }
                 Coupon coupon = couponClient.queryCouponById(userCoupon.getCouponId());
                 long time = coupon.getEndTime().getTime();
@@ -844,15 +844,15 @@
                         return ResultUtil.error("该优惠券无法使用");
                     }
 
-                    money = money.subtract(new BigDecimal(num2)).setScale(2, RoundingMode.HALF_EVEN);
+                    money = money.subtract(new BigDecimal(num2)).max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_EVEN);
                 }
                 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)).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);
@@ -896,8 +896,10 @@
             siteBooking.setPayMoney(BigDecimal.ZERO.doubleValue());
             siteBooking.setPayTime(new Date());
             siteBookingService.updateById(siteBooking);
-            HashMap<String, String> map = new HashMap<>();
+            consumeCouponAfterSuccessfulPayment(siteBooking);
+            HashMap<String, Object> map = new HashMap<>();
             map.put("siteBooking", siteBooking.getId().toString());
+            map.put("paySuccess", true);
             return ResultUtil.success(map);
         }
         if (reservationSite.getPayType() == 1 ) {//微信支付
@@ -932,15 +934,15 @@
             // 平台
             ResultUtil weixinpay = payMoneyUtil.weixinpay("预约场地", id.toString(), code, paymentPrice.toString(),
                     "/base/site/weChatPaymentSiteCallback", "APP", "");
-            return weixinpay;
+            return appendPaySuccess(weixinpay, false);
         } else {
             String smidVx = operatorUserService.getmerchantNumberByOperatorId(store.getOperatorId());
             if (!StringUtils.hasLength(smidVx)) {
                 return ResultUtil.error("运营商未配置微信商户号,获取支付失败!");
             }
-            return payMoneyUtil.weixinpayV3(smidVx, "预约场地"
+            return appendPaySuccess(payMoneyUtil.weixinpayV3(smidVx, "预约场地"
                     , code, "/base/site/weChatPaymentSiteCallback1",
-                    paymentPrice.toString(), id.toString());
+                    paymentPrice.toString(), id.toString()), false);
         }
 
 
@@ -978,60 +980,7 @@
         }
         ResultUtil alipay = payMoneyUtil.alipay(smid1, "预约场地", "预约场地", id.toString(), code, paymentPrice.toString(), "/base/site/aliPaymentSiteCallback");
         System.out.println("预约场地----" + alipay.getCode());
-        if (alipay.getCode() == 200) {
-            String finalSmid = smid1;
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        int num = 1;
-                        int wait = 0;
-                        while (num <= 10) {
-                            int min = 5000;
-                            wait += (min * num);
-                            Thread.sleep(wait);
-                            SiteBooking siteBooking = siteBookingService.getById(id);
-                            if (siteBooking.getStatus() != 0) {
-                                break;
-                            }
-                            AlipayTradeQueryResponse resultUtil = payMoneyUtil.queryALIOrder(code);
-                            if (resultUtil.getCode().equals("10000") && siteBooking.getStatus() == 0) {
-                                /**
-                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
-                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
-                                 * TRADE_SUCCESS(交易支付成功)、
-                                 * TRADE_FINISHED(交易结束,不可退款)
-                                 */
-//                                Map<String, String> data1 = resultUtil.getData();
-//                                String s = data1.get("tradeStatus");
-                                String tradeNo = resultUtil.getTradeNo();
-                                String s = resultUtil.getTradeStatus();
-                                System.out.println("ssssss" + s);
-                                if ("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10) {
-                                    break;
-                                }
-                                if ("TRADE_SUCCESS".equals(s)) {
-                                    siteBooking.setPayTime(new Date());
-                                    siteBooking.setStatus(1);
-                                    siteBooking.setPayOrderNo(tradeNo);
-                                    siteBookingService.updateById(siteBooking);
-                                    System.err.println("======完成支付");
-                                    Thread.sleep(30000);
-                                    payMoneyUtil.confirm(finalSmid, code, tradeNo, paymentPrice.toString());
-                                    break;
-                                }
-                                if ("WAIT_BUYER_PAY".equals(s)) {
-                                    num++;
-                                }
-                            }
-                        }
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                }
-            }).start();
-        }
-        return alipay;
+        return appendPaySuccess(alipay, false);
     }
 
     private void extracted(Integer operatorId, BigDecimal coursePackageOrder1, String tradeNo) throws Exception {
@@ -1097,9 +1046,37 @@
         siteBooking.setPayTime(new Date());
         siteBooking.setStatus(1);
         siteBookingService.updateById(siteBooking);
-        HashMap<String, String> map = new HashMap<>();
+        consumeCouponAfterSuccessfulPayment(siteBooking);
+        HashMap<String, Object> map = new HashMap<>();
         map.put("siteBooking", siteBooking.getId().toString());
+        map.put("paySuccess", true);
         return ResultUtil.success(map);
+    }
+
+    /** 支付参数生成成功不等于已支付,前端仍需拉起微信或支付宝。 */
+    private ResultUtil appendPaySuccess(ResultUtil paymentResult, boolean paySuccess) {
+        JSONObject data = paymentResult.getData() == null ? new JSONObject()
+                : JSON.parseObject(JSON.toJSONString(paymentResult.getData()));
+        if (data == null) {
+            data = new JSONObject();
+        }
+        data.put("paySuccess", paySuccess);
+        return ResultUtil.getResult(paymentResult.getCode(), paymentResult.getMsg(), data);
+    }
+
+    /**
+     * 场地订单实际支付成功后才核销所选优惠券;避免仅创建待支付订单就提前消耗优惠券。
+     */
+    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);
+        }
     }
 
 
@@ -1210,7 +1187,10 @@
                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                 String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);// 退款单号
                 // 运营商退款
-                payMoneyUtil.weixinRefundV3(smidVx, code, siteBooking.getPayOrderNo(), siteBooking.getPayMoney().toString(), "/base/site/cancelMySiteCallback1");
+                ResultUtil refundResult = payMoneyUtil.weixinRefundV3(smidVx, code, siteBooking.getPayOrderNo(), siteBooking.getPayMoney().toString(), "/base/site/cancelMySiteCallback1");
+                if (!ResultUtil.SUCCESS.equals(refundResult.getCode())) {
+                    return refundResult;
+                }
             } else {
                 Map<String, String> map = payMoneyUtil.wxRefund(siteBooking.getPayOrderNo(), siteBooking.getOrderNo(), siteBooking.getPayMoney().toString(),
                         siteBooking.getPayMoney().toString(), "/base/site/cancelMySiteCallback");
@@ -1221,15 +1201,19 @@
                 if (!"SUCCESS".equals(return_code)) {
                     return ResultUtil.error(map.get("return_msg"));
                 }
-                siteBooking.setCancelUserId(uid);
-                siteBookingService.updateById(siteBooking);
-
                 TBackRecord tBackRecord = new TBackRecord();
                 tBackRecord.setMoney(siteBooking.getPayMoney());
                 tBackRecord.setUserId(siteBooking.getAppUserId());
                 tBackRecord.setTime(new Date());
                 backRecordService.save(tBackRecord);
             }
+
+            // 退款请求已受理即结束该预约,不能继续在“待核销”列表中展示。
+            // 微信退款回调用于补充退款流水号及恢复优惠券,重复更新状态不会影响结果。
+            siteBooking.setStatus(SiteBookingStatusEnum.CANCELED.getCode());
+            siteBooking.setCancelUserId(uid);
+            siteBooking.setCancelTime(new Date());
+            siteBookingService.updateById(siteBooking);
 
 
         }
@@ -1334,17 +1318,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));
 
@@ -1353,9 +1331,6 @@
             siteBooking.setPayMoney(playCoin.doubleValue());
         } else {
             siteBooking.setPayMoney(money.doubleValue());
-        }
-        if (payType == 3) {
-            siteBooking.setUserCouponId(null);
         }
         siteBookingService.updateById(siteBooking);
         if (payType == 1) {//微信支付
@@ -1422,7 +1397,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