From 8a85fc834d98b2c14c5b886a957ccf3197aad293 Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期日, 20 九月 2026 17:12:28 +0800
Subject: [PATCH] feat:优惠券使用入口:购买门票、预定场地、购买惠民卡

---
 cloud-server-account/src/main/java/com/dsh/account/service/impl/TAppUserServiceImpl.java |   74 +++++++++++++++++++++++++++++++------
 1 files changed, 62 insertions(+), 12 deletions(-)

diff --git a/cloud-server-account/src/main/java/com/dsh/account/service/impl/TAppUserServiceImpl.java b/cloud-server-account/src/main/java/com/dsh/account/service/impl/TAppUserServiceImpl.java
index 388efe3..b208819 100644
--- a/cloud-server-account/src/main/java/com/dsh/account/service/impl/TAppUserServiceImpl.java
+++ b/cloud-server-account/src/main/java/com/dsh/account/service/impl/TAppUserServiceImpl.java
@@ -12,6 +12,8 @@
 import com.alipay.api.response.AlipayTradeOrderSettleResponse;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.alipay.api.response.AlipayTradeRoyaltyRelationBindResponse;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -1572,6 +1574,47 @@
                 return ResultUtil.error("商品不存在");
             }
 
+            BigDecimal cashAmount = merchandise.getCash() == null ? BigDecimal.ZERO : merchandise.getCash();
+            UserCoupon usedCoupon = null;
+            if (exchangeType.getGoodsType() == 3 && exchangeType.getCouponId() != null && exchangeType.getCouponId() != 0) {
+                if (exchangeType.getExchangeType() == 1) {
+                    return ResultUtil.error("积分兑换不能使用优惠券");
+                }
+                usedCoupon = userConponClient.queryUserCouponById(
+                        new QueryUserCouponByIdAndUserId(exchangeType.getCouponId(), userIdFormRedis));
+                if (usedCoupon == null || !Integer.valueOf(1).equals(usedCoupon.getStatus())) {
+                    return ResultUtil.error("优惠券不可用");
+                }
+                Coupon coupon = userConponClient.queryCouponById(usedCoupon.getCouponId());
+                if (coupon == null || coupon.getEndTime() == null || !coupon.getEndTime().after(new Date())) {
+                    return ResultUtil.error("优惠券已过期");
+                }
+                if (Integer.valueOf(3).equals(coupon.getUseScope())
+                        && (exchangeType.getStoreId() == null || !userConponClient.getCouponStoreIds(coupon.getId()).contains(exchangeType.getStoreId()))) {
+                    return ResultUtil.error("该优惠券不适用于所选门店");
+                }
+                BigDecimal orderCash = cashAmount.multiply(BigDecimal.valueOf(exchangeType.getNums() == null ? 1 : exchangeType.getNums()));
+                JSONObject rule = JSON.parseObject(coupon.getContent());
+                if (Integer.valueOf(1).equals(coupon.getType())) {
+                    BigDecimal condition = rule.getBigDecimal("conditionalAmount");
+                    BigDecimal deduction = rule.getBigDecimal("deductionAmount");
+                    if (condition == null || deduction == null || orderCash.compareTo(condition) < 0) {
+                        return ResultUtil.error("该优惠券无法使用");
+                    }
+                    cashAmount = orderCash.subtract(deduction).max(BigDecimal.ZERO)
+                            .divide(BigDecimal.valueOf(exchangeType.getNums() == null ? 1 : exchangeType.getNums()), 2, java.math.RoundingMode.HALF_UP);
+                } else if (Integer.valueOf(2).equals(coupon.getType())) {
+                    BigDecimal deduction = rule.getBigDecimal("conditionalAmount");
+                    if (deduction == null) {
+                        return ResultUtil.error("该优惠券无法使用");
+                    }
+                    cashAmount = orderCash.subtract(deduction).max(BigDecimal.ZERO)
+                            .divide(BigDecimal.valueOf(exchangeType.getNums() == null ? 1 : exchangeType.getNums()), 2, java.math.RoundingMode.HALF_UP);
+                } else {
+                    return ResultUtil.error("该优惠券不支持购买门票");
+                }
+            }
+
             TAppUser tAppUser = this.baseMapper.selectById(userIdFormRedis);
 
             if (merchandise.getUserPopulation() == 2) {
@@ -1646,26 +1689,33 @@
             pointsMerchandise.setCode(code);
             pointsMerchandise.setInsertTime(new Date());
             pointsMerchandise.setPayType(exchangeType.getExchangeType());
+            pointsMerchandise.setUserCouponId(exchangeType.getGoodsType() == 3 ? exchangeType.getCouponId() : null);
             if (null != merchandise.getIntegral()) {
                 pointsMerchandise.setIntegral(merchandise.getIntegral() * goodsNums);
             }
             if (exchangeType.getExchangeType() == 2 || exchangeType.getExchangeType() == 3) {
-                pointsMerchandise.setCash(merchandise.getCash());
+                pointsMerchandise.setCash(cashAmount);
                 pointsMerchandise.setCashPayType(exchangeType.getPayType());
             }
 
-            Double m = new BigDecimal((merchandise.getIntegral() == null ? 0 : merchandise.getIntegral())).add((merchandise.getCash() == null ? BigDecimal.ZERO : merchandise.getCash())).doubleValue();
+            Double m = new BigDecimal((merchandise.getIntegral() == null ? 0 : merchandise.getIntegral())).add(cashAmount).doubleValue();
             if ((m * goodsNums) == 0) {
                 pointsMerchandise.setPayStatus(2);
                 pointsMerchandise.setPaymentTime(new Date());
             }
             returnId = mcClient.saveDetailsUserPointMercase(pointsMerchandise);
+            if (usedCoupon != null) {
+                usedCoupon.setStatus(2);
+                userConponClient.updateUserCoupon(usedCoupon);
+            }
             if(goodsNums>1){
                 System.err.println("购买商品数量"+goodsNums);
                 List<UserPointsMerchandise> userPointsMerchandises = new ArrayList<>();
                 for (int i = 1; i < goodsNums; i++) {
                     UserPointsMerchandise userPointsMerchandise = new UserPointsMerchandise();
                     BeanUtils.copyProperties(pointsMerchandise, userPointsMerchandise);
+                    // 一笔订单只能关联一次优惠券,避免拆分门票后重复计入优惠金额。
+                    userPointsMerchandise.setUserCouponId(null);
                     userPointsMerchandises.add(userPointsMerchandise);
                 }
                 System.err.println("新增额外购买记录"+userPointsMerchandises);
@@ -1766,7 +1816,7 @@
                                             break;
                                         }
                                     }
-                                    coursePackageOrder.setCashPayment(merchandise.getCash().multiply(new BigDecimal(exchangeType.getStuIds().size())));
+                                    coursePackageOrder.setCashPayment(cashAmount.multiply(new BigDecimal(exchangeType.getStuIds().size())));
                                     coursePackageOrder.setPlayPaiCoin(merchandise.getIntegral() * exchangeType.getStuIds().size());
                                     coursePackageOrder.setPayUserType(1);
                                     coursePackageOrder.setPayStatus(1);
@@ -1776,20 +1826,20 @@
                                     coursePackageOrder.setInsertTime(new Date());
                                     coursePackageOrder.setOrderType(2);
                                     paymentClient.savePaymentCoursePackage(coursePackageOrder);
-                                    return WechatPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
+                                    return WechatPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
                                 } else if (merchandise.getType() == 1) {
-                                    return WechatPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
+                                    return WechatPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
                                 } else if (merchandise.getType() == 3) {
-                                    return WechatPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
+                                    return WechatPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
                                 }
                             } catch (Exception e) {
                                 return ResultUtil.runErr();
                             }
                         case 2:
                             if (merchandise.getType() == 2) {
-                                return AlipayPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
+                                return AlipayPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
                             } else {
-                                return AlipayPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
+                                return AlipayPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
                             }
                         default:
                             break;
@@ -1800,18 +1850,18 @@
                         case 1:
                             try {
                                 if (merchandise.getType() == 2) {
-                                    return WechatPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
+                                    return WechatPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
                                 } else {
-                                    return WechatPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
+                                    return WechatPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
                                 }
                             } catch (Exception e) {
                                 return ResultUtil.runErr();
                             }
                         case 2:
                             if (merchandise.getType() == 2) {
-                                return AlipayPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
+                                return AlipayPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(exchangeType.getStuIds().size())), code, merchandise.getId());
                             } else {
-                                return AlipayPayment(merchandise.getType(), merchandise.getCash().multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
+                                return AlipayPayment(merchandise.getType(), cashAmount.multiply(new BigDecimal(goodsNums)), code, merchandise.getId());
                             }
                         default:
                             break;

--
Gitblit v1.7.1