From ff2fc5e424e7bb71d17a178404d8790b5b3dc4ec Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期二, 22 九月 2026 11:55:16 +0800
Subject: [PATCH] bug:返回字段修改

---
 cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java |  230 +++++++++++++++++++++++++++------------------------------
 1 files changed, 110 insertions(+), 120 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 966f980..b61094d 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,7 +9,6 @@
 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;
@@ -90,7 +89,14 @@
     private ISiteService siteService;
 
 
-
+//    public static void main(String[] args) {
+//        String now = DateUtil.now().substring(11, 19);
+//        System.err.println(now);
+//        if ("17:00:00".compareTo(now) <= 0 && "19:00:00".compareTo(now) >= 0) {
+//            System.err.println("yes");
+//        }
+//
+//    }
 
     /**
      * 获取场地列表
@@ -104,9 +110,12 @@
         querySiteList.setPageNum((querySiteList.getPageNum() - 1) * querySiteList.getPageSize());
         DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
         Map<Integer, List<TSitePrice>> priceMap = sitePriceService.lambdaQuery().eq(TSitePrice::getDayOfWeek, dayOfWeek.getValue())
+                .eq(querySiteList.getTargetAudience() != null, TSitePrice::getTargetAudience, querySiteList.getTargetAudience())
                 .list()
                 .stream().collect(Collectors.groupingBy(TSitePrice::getSiteId));
         List<QuerySiteListVo> querySiteListVos = this.baseMapper.querySiteList(querySiteList);
+        // 获取当前时间 只需要时分秒 格式hh:mm:ss
+        String now = DateUtil.now().substring(11, 19);
         for (QuerySiteListVo querySiteListVo : querySiteListVos) {
             querySiteListVo.setPrice(0.0);
             querySiteListVo.setTimes("");
@@ -115,16 +124,19 @@
                 continue;
             }
             List<TSitePrice> tSitePrices = priceMap.get(querySiteListVo.getId());
-            if (tSitePrices!=null&& !tSitePrices.isEmpty()){
-                BigDecimal bigDecimal = tSitePrices.get(0).getCashPrice();
+            if (tSitePrices != null && !tSitePrices.isEmpty()) {
+                BigDecimal bigDecimal = BigDecimal.ZERO;
                 // 遍历 查询最小的价格
                 for (TSitePrice tSitePrice : tSitePrices) {
-                    if (bigDecimal.compareTo(tSitePrice.getCashPrice()) > 0) {
-                        bigDecimal = tSitePrice.getCashPrice();
+                    // 当前时间再范围之内
+                    if (tSitePrice.getStartTime().compareTo(now) <= 0 && tSitePrice.getEndTime().compareTo(now) >= 0) {
+                        if (tSitePrice.getCashPrice().compareTo(bigDecimal) > 0) {
+                            bigDecimal = tSitePrice.getCashPrice();
+                        }
                     }
                 }
                 querySiteListVo.setPrice(bigDecimal.doubleValue());
-                querySiteListVo.setTimes(tSitePrices.get(0).getStartTime() + "-" + tSitePrices.get(tSitePrices.size()-1).getEndTime());
+                querySiteListVo.setTimes(tSitePrices.get(0).getStartTime() + "-" + tSitePrices.get(tSitePrices.size() - 1).getEndTime());
             }
             Map<String, Double> distance = GeodesyUtil.getDistance(querySiteList.getLon() + "," + querySiteList.getLat(), querySiteListVo.getStoreLon() + "," + querySiteListVo.getStoreLat());
             double wgs84 = new BigDecimal(distance.get("WGS84")).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
@@ -315,42 +327,42 @@
                     count++;
                 }
             }
-            if (querySiteTimes.getSelectable() == 1&&end.before(now)){
+            if (querySiteTimes.getSelectable() == 1 && end.before(now)) {
                 querySiteTimes.setSelectable(0);
             }
-            if (end.before(now)){
+            if (end.before(now)) {
                 querySiteTimes.setSelectable(0);
             }
             querySiteTimes.setSelectCount(count);
-            if (flag == 0 &&  querySiteTimes.getSelectable() == 1) {
+            if (flag == 0 && querySiteTimes.getSelectable() == 1) {
                 // 当前时间段还没有预约
                 Integer walkinCapacity = site.getWalkinCapacity();
                 // 计算散客预约人数和最大散客容纳数
                 System.err.println("预约人数" + count);
                 System.err.println("最大散客容纳数" + walkinCapacity);
                 if (walkinCapacity <= count) {
-                    if (targetAudience==1 && walkinCapacity!=0){
+                    if (targetAudience == 1 && walkinCapacity != 0) {
                         // 不可预约
                         System.err.println("不可预约 也不能散客");
                         querySiteTimes.setSelectable(0);
                         querySiteTimes.setIsReserve(0);
-                    }else if (targetAudience==2){
+                    } else if (targetAudience == 2) {
                         // 不能散客
                         System.err.println("不可预约 也不能散客");
                         querySiteTimes.setSelectable(0);
                         querySiteTimes.setIsReserve(0);
-                    }else{
+                    } else {
                         querySiteTimes.setSelectable(1);
                     }
-                }else{
-                    if (targetAudience==2){
+                } else {
+                    if (targetAudience == 2) {
                         // 散客只能预约当前时间段
                         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                         String nowStr = simpleDateFormat.format(now);
                         // 判断当前时间有没有在timePriceStart和timePriceEnd之间
-                        if (DateComparisonExample.isStringWithinTimeRange(nowStr, simpleDateFormat.parse(timePriceStart),  simpleDateFormat.parse(timePriceEnd))) {
+                        if (DateComparisonExample.isStringWithinTimeRange(nowStr, simpleDateFormat.parse(timePriceStart), simpleDateFormat.parse(timePriceEnd))) {
                             querySiteTimes.setSelectable(1);
-                        }else{
+                        } else {
                             querySiteTimes.setSelectable(0);
                         }
                     }
@@ -553,42 +565,42 @@
                     count++;
                 }
             }
-            if (querySiteTimes.getSelectable() == 1&&end.before(now)){
+            if (querySiteTimes.getSelectable() == 1 && end.before(now)) {
                 querySiteTimes.setSelectable(0);
             }
-            if (end.before(now)){
+            if (end.before(now)) {
                 querySiteTimes.setSelectable(0);
             }
             querySiteTimes.setSelectCount(count);
-            if (flag == 0 &&  querySiteTimes.getSelectable() == 1) {
+            if (flag == 0 && querySiteTimes.getSelectable() == 1) {
                 // 当前时间段还没有预约
                 Integer walkinCapacity = site.getWalkinCapacity();
                 // 计算散客预约人数和最大散客容纳数
                 System.err.println("预约人数" + count);
                 System.err.println("最大散客容纳数" + walkinCapacity);
                 if (walkinCapacity <= count) {
-                    if (targetAudience==1 && walkinCapacity!=0){
+                    if (targetAudience == 1 && walkinCapacity != 0) {
                         // 不可预约
                         System.err.println("不可预约 也不能散客");
                         querySiteTimes.setSelectable(0);
                         querySiteTimes.setIsReserve(0);
-                    }else if (targetAudience==2){
+                    } else if (targetAudience == 2) {
                         // 不能散客
                         System.err.println("不可预约 也不能散客");
                         querySiteTimes.setSelectable(0);
                         querySiteTimes.setIsReserve(0);
-                    }else{
+                    } else {
                         querySiteTimes.setSelectable(1);
                     }
-                }else{
-                    if (targetAudience==2){
+                } else {
+                    if (targetAudience == 2) {
                         // 散客只能预约当前时间段
                         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                         String nowStr = simpleDateFormat.format(now);
                         // 判断当前时间有没有在timePriceStart和timePriceEnd之间
-                        if (DateComparisonExample.isStringWithinTimeRange(nowStr, simpleDateFormat.parse(timePriceStart),  simpleDateFormat.parse(timePriceEnd))) {
+                        if (DateComparisonExample.isStringWithinTimeRange(nowStr, simpleDateFormat.parse(timePriceStart), simpleDateFormat.parse(timePriceEnd))) {
                             querySiteTimes.setSelectable(1);
-                        }else{
+                        } else {
                             querySiteTimes.setSelectable(0);
                         }
                     }
@@ -684,12 +696,14 @@
 
 
     }
+
     /**
      * 判断两个时间段是否有交集
+     *
      * @param start1 第一个时间段开始时间
-     * @param end1 第一个时间段结束时间
+     * @param end1   第一个时间段结束时间
      * @param start2 第二个时间段开始时间
-     * @param end2 第二个时间段结束时间
+     * @param end2   第二个时间段结束时间
      * @return 如果有交集返回true,否则返回false
      */
     private boolean isTimePeriodsIntersect(String start1, String end1, String start2, String end2) {
@@ -787,7 +801,7 @@
                 }
                 if (querySiteTime.getTime().equals(time)) {
                     money = money.add(querySiteTime.getCashPrice());
-                    playCoin = playCoin+querySiteTime.getCoinPrice();
+                    playCoin = playCoin + querySiteTime.getCoinPrice();
                     break;
                 }
             }
@@ -813,8 +827,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();
@@ -829,15 +843,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);
@@ -860,9 +874,9 @@
         siteBooking.setBooker(reservationSite.getBooker());
         siteBooking.setPhone(reservationSite.getPhone());
         siteBooking.setPayType(reservationSite.getPayType());
-        if (reservationSite.getPayType()==3){
+        if (reservationSite.getPayType() == 3) {
             siteBooking.setPayMoney(playCoin.doubleValue());
-        }else{
+        } else {
             siteBooking.setPayMoney(money.setScale(2, RoundingMode.HALF_EVEN).doubleValue());
         }
         siteBooking.setUserCouponId(reservationSite.getCouponId());
@@ -870,14 +884,24 @@
         siteBooking.setState(1);
         siteBooking.setTargetAudience(reservationSite.getTargetAudience());
         siteBooking.setInsertTime(new Date());
-        siteBooking.setMoney(money+","+playCoin);
+        siteBooking.setMoney(money + "," + playCoin);
         siteBooking.setNextName(reservationSite.getNextName());
         siteBooking.setIsHalf(reservationSite.getIsHalf());
         siteBooking.setHalfName(reservationSite.getHalfName());
         siteBookingService.save(siteBooking);
 
-
-        if (reservationSite.getPayType() == 1) {//微信支付
+        if (money.compareTo(BigDecimal.ZERO)==0){
+            siteBooking.setStatus(1);
+            siteBooking.setPayMoney(BigDecimal.ZERO.doubleValue());
+            siteBooking.setPayTime(new Date());
+            siteBookingService.updateById(siteBooking);
+            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 ) {//微信支付
             ResultUtil resultUtil = weChatPaymentSite(money.setScale(2, RoundingMode.HALF_EVEN).doubleValue(), siteBooking);
             return resultUtil;
         }
@@ -886,8 +910,8 @@
         }
         if (reservationSite.getPayType() == 3) {//玩湃币支付
             return playPaiCoinPaymentSite(appUser, playCoin.doubleValue(), siteBooking);
-
         }
+
         return ResultUtil.success();
     }
 
@@ -909,15 +933,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);
         }
 
 
@@ -955,60 +979,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 {
@@ -1074,9 +1045,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);
+        }
     }
 
 
@@ -1095,7 +1094,7 @@
         pageNo = (pageNo - 1) * pageSize;
         List<QueryMySiteVo> queryMySiteVos = this.baseMapper.queryMySite(uid, status, pageNo, pageSize);
         for (QueryMySiteVo queryMySiteVo : queryMySiteVos) {
-            if (queryMySiteVo.getTargetAudience()==null){
+            if (queryMySiteVo.getTargetAudience() == null) {
                 queryMySiteVo.setTargetAudience(1);
             }
             SiteBooking siteBooking = siteBookingService.getById(queryMySiteVo.getId());
@@ -1126,7 +1125,7 @@
 //                    }
                     if (querySiteTime.getTime().equals(time)) {
                         money = money.add(querySiteTime.getCashPrice());
-                        playCoin = playCoin+querySiteTime.getCoinPrice();
+                        playCoin = playCoin + querySiteTime.getCoinPrice();
                         break;
                     }
                 }
@@ -1153,9 +1152,9 @@
         if (siteBooking.getTargetAudience() == 1 && format.format(new Date()).equals(format.format(startTime)) && siteBooking.getStatus() != 0) {
             return ResultUtil.error("预约当天,不能取消");
         }
-        if (siteBooking.getTargetAudience() == 2){
+        if (siteBooking.getTargetAudience() == 2) {
             Date date = new Date();
-            if (siteBooking.getEndTime().before( date)){
+            if (siteBooking.getEndTime().before(date)) {
                 return ResultUtil.error("已过期,不能取消");
             }
         }
@@ -1290,7 +1289,7 @@
                 String timePriceEnd = day + " " + querySiteTime.getTime().split("-")[1];
                 if (querySiteTime.getTime().equals(time)) {
                     money = money.add(querySiteTime.getCashPrice());
-                    playCoin = playCoin+querySiteTime.getCoinPrice();
+                    playCoin = playCoin + querySiteTime.getCoinPrice();
                     break;
                 }
             }
@@ -1311,28 +1310,19 @@
             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));
 
         siteBooking.setPayType(payType);
-        if (payType==3){
-            siteBooking.setPayMoney(playCoin.doubleValue());
-        }else{
-            siteBooking.setPayMoney(money.doubleValue());
-        }
         if (payType == 3) {
-            siteBooking.setUserCouponId(null);
+            siteBooking.setPayMoney(playCoin.doubleValue());
+        } else {
+            siteBooking.setPayMoney(money.doubleValue());
         }
         siteBookingService.updateById(siteBooking);
         if (payType == 1) {//微信支付
@@ -1378,7 +1368,7 @@
                 String timePriceEnd = day + " " + querySiteTime.getTime().split("-")[1];
                 if (querySiteTime.getTime().equals(time)) {
                     money = money.add(querySiteTime.getCashPrice());
-                    playCoin = playCoin+querySiteTime.getCoinPrice();
+                    playCoin = playCoin + querySiteTime.getCoinPrice();
                     break;
                 }
             }
@@ -1399,10 +1389,10 @@
             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()){
+        switch (siteBooking.getPayType()) {
             case 1:
                 map.put("cash", money.doubleValue());
                 break;

--
Gitblit v1.7.1