From afa61267844723f906a632e4ac8b30f91cf4a3c0 Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期一, 21 九月 2026 11:56:04 +0800
Subject: [PATCH] bug:获取优惠券接口问题

---
 cloud-server-activity/src/main/java/com/dsh/activity/service/impl/UserCouponServiceImpl.java |   95 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 91 insertions(+), 4 deletions(-)

diff --git a/cloud-server-activity/src/main/java/com/dsh/activity/service/impl/UserCouponServiceImpl.java b/cloud-server-activity/src/main/java/com/dsh/activity/service/impl/UserCouponServiceImpl.java
index dd8e9f2..1657b06 100644
--- a/cloud-server-activity/src/main/java/com/dsh/activity/service/impl/UserCouponServiceImpl.java
+++ b/cloud-server-activity/src/main/java/com/dsh/activity/service/impl/UserCouponServiceImpl.java
@@ -16,6 +16,8 @@
 import com.dsh.activity.feignclient.other.model.StoreDetailOfCourse;
 import com.dsh.activity.mapper.*;
 import com.dsh.activity.model.ConponJsonRuleModel;
+import com.dsh.activity.model.CouponDeductionQuery;
+import com.dsh.activity.model.CouponDeductionVO;
 import com.dsh.activity.model.CouponListVo;
 import com.dsh.activity.model.request.CouponPackageReq;
 import com.dsh.activity.model.response.CouponPackageResp;
@@ -24,12 +26,18 @@
 import com.dsh.activity.util.GDMapGeocodingUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.math.BigDecimal;
+import java.util.Collections;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -42,6 +50,8 @@
  */
 @Service
 public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(UserCouponServiceImpl.class);
 
     @Resource
     private CoursePackageClient coursePackageClient;
@@ -72,6 +82,63 @@
     @Resource
     private SiteClient siteClient;
 
+    /**
+     * 批量解析已核销优惠券的固定抵扣金额,避免统计页面逐条远程查询优惠券。
+     *
+     * @param query 用户优惠券领取记录 ID 集合
+     * @return 每条领取记录对应的抵扣金额;无固定抵扣规则时返回 0
+     */
+    @Override
+    public List<CouponDeductionVO> queryDeductionAmounts(CouponDeductionQuery query) {
+        if (query == null || query.getUserCouponIds() == null || query.getUserCouponIds().isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        Set<Long> userCouponIds = new LinkedHashSet<>(query.getUserCouponIds());
+        List<UserCoupon> userCoupons = this.list(new QueryWrapper<UserCoupon>()
+                .in("id", userCouponIds));
+        if (userCoupons.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        Set<Integer> couponIds = userCoupons.stream()
+                .map(UserCoupon::getCouponId)
+                .filter(java.util.Objects::nonNull)
+                .collect(Collectors.toSet());
+        List<Coupon> coupons = couponIds.isEmpty()
+                ? Collections.emptyList()
+                : couponMapper.selectBatchIds(couponIds);
+        Map<Integer, Coupon> couponMap = coupons.stream()
+                .collect(Collectors.toMap(Coupon::getId, coupon -> coupon));
+
+        List<CouponDeductionVO> result = new ArrayList<>();
+        for (UserCoupon userCoupon : userCoupons) {
+            CouponDeductionVO deduction = new CouponDeductionVO();
+            deduction.setUserCouponId(userCoupon.getId());
+            deduction.setDeductionAmount(getCouponDeductionAmount(couponMap.get(userCoupon.getCouponId())));
+            result.add(deduction);
+        }
+        return result;
+    }
+
+    /** 解析优惠券规则中的固定抵扣金额。 */
+    private BigDecimal getCouponDeductionAmount(Coupon coupon) {
+        if (coupon == null || coupon.getContent() == null) {
+            return BigDecimal.ZERO;
+        }
+        try {
+            JSONObject content = JSON.parseObject(coupon.getContent());
+            // 满减券使用 deductionAmount,代金券历史规则将抵扣额存放在 conditionalAmount。
+            BigDecimal deductionAmount = Integer.valueOf(2).equals(coupon.getType())
+                    ? content.getBigDecimal("conditionalAmount")
+                    : content.getBigDecimal("deductionAmount");
+            return deductionAmount == null ? BigDecimal.ZERO : deductionAmount.max(BigDecimal.ZERO);
+        } catch (Exception e) {
+            LOGGER.warn("解析优惠券抵扣规则失败,couponId={}", coupon.getId(), e);
+            return BigDecimal.ZERO;
+        }
+    }
+
 
     /**
      * 获取购买课程可用优惠券列表
@@ -91,6 +158,10 @@
         List<CouponListVo> listVos = new ArrayList<>();
         for (Map<String, Object> userCoupon : userCoupons) {
             Integer type = Integer.valueOf(userCoupon.get("type").toString());
+            // 支付减免只支持满减券和代金券;体验券/抵扣券不能用于订单金额计算。
+            if (type != 1 && type != 2) {
+                continue;
+            }
             CouponListVo couponListVo = new CouponListVo();
             couponListVo.setId(Long.valueOf(userCoupon.get("id").toString()));
             couponListVo.setName(userCoupon.get("name").toString());
@@ -140,7 +211,11 @@
     @Override
     public List<CouponListVo> querySiteCouponList(Integer uid, Integer siteId, Double price, String lon, String lat) throws Exception {
         Site site = siteClient.querySiteById(siteId);
-        Integer storeId = site.getStoreId();
+        return queryStoreCouponList(uid, site.getStoreId(), price, lon, lat);
+    }
+
+    @Override
+    public List<CouponListVo> queryStoreCouponList(Integer uid, Integer storeId, Double price, String lon, String lat) throws Exception {
         Map<String, String> geocode = gdMapGeocodingUtil.geocode(lon, lat);
         String provinceCode = geocode.get("provinceCode");
         String cityCode = geocode.get("cityCode");
@@ -148,12 +223,16 @@
         List<CouponListVo> listVos = new ArrayList<>();
         for (Map<String, Object> userCoupon : userCoupons) {
             Integer type = Integer.valueOf(userCoupon.get("type").toString());
+            if (type != 1 && type != 2) {
+                continue;
+            }
             CouponListVo couponListVo = new CouponListVo();
             couponListVo.setId(Long.valueOf(userCoupon.get("id").toString()));
             couponListVo.setName(userCoupon.get("name").toString());
             couponListVo.setType(type);
             couponListVo.setEffectiveTime(userCoupon.get("endTime").toString());
             String content = userCoupon.get("content").toString();
+            BigDecimal orderAmount = BigDecimal.valueOf(price == null ? 0D : price).max(BigDecimal.ZERO);
             if (type == 1) {//满减{"num1":1,"num2":1}
                 JSONObject jsonObject = JSON.parseObject(content);
                 Double num1 = jsonObject.getDouble("conditionalAmount");
@@ -162,6 +241,10 @@
                 }
                 couponListVo.setUseCondition("满" + num1 + "元可用");
                 couponListVo.setFavorable(jsonObject.getString("deductionAmount"));
+                BigDecimal discount = BigDecimal.valueOf(jsonObject.getDouble("deductionAmount"))
+                        .min(orderAmount);
+                couponListVo.setDiscountAmount(discount);
+                couponListVo.setPayableAmount(orderAmount.subtract(discount));
             }
             if (type == 2) {//代金券{"num1":1}
                 JSONObject jsonObject = JSON.parseObject(content);
@@ -169,11 +252,12 @@
                 if (num1 == null) {
                     num1 = 0d;
                 }
-                if (price.compareTo(num1) < 0) {
-                    continue;
-                }
+                // 代金券可全额抵扣,面额超过订单金额时按订单金额抵扣,不应从列表中剔除。
                 couponListVo.setUseCondition("");
                 couponListVo.setFavorable(num1 + "");
+                BigDecimal discount = BigDecimal.valueOf(num1).max(BigDecimal.ZERO).min(orderAmount);
+                couponListVo.setDiscountAmount(discount);
+                couponListVo.setPayableAmount(orderAmount.subtract(discount));
             }
             if (type == 3) {//体验券{"num1":1}
                 JSONObject jsonObject = JSON.parseObject(content);
@@ -277,6 +361,9 @@
                     if (userCoupon.getStatus() == 2) {
                         packageResp.setUseStatus(2);
                     }
+                    if (userCoupon.getStatus() == 3) {
+                        packageResp.setUseStatus(3);
+                    }
                     respList.add(packageResp);
                 }
                 if (null != req.getCouponType()) {

--
Gitblit v1.7.1