zhangchen 2 сар өмнө
parent
commit
3349bb42ce

+ 42 - 12
alien-store/src/main/java/shop/alien/store/service/impl/TrackEventServiceImpl.java

@@ -443,22 +443,25 @@ public class TrackEventServiceImpl extends ServiceImpl<StoreTrackEventMapper, St
         Map<String, Object> result = new HashMap<>();
         
         try {
-            // 赠送好友相关数据(累计数据,从life_discount_coupon_user表统计)
-            String storeIdStr = String.valueOf(storeId);
-            List<LifeDiscountCouponUser> couponUsers = queryCouponUsersByStore(storeIdStr, endDate);
-            
-            // 赠送好友数量
-            long giveToFriendCount = couponUsers.size();
+            // 赠送好友数量:从 LifeDiscountCoupon 查店铺优惠券 id,再按优惠券 id 查 LifeDiscountCouponStoreFriend 记录数
+            List<LifeDiscountCouponStoreFriend> giveToFriendRecords = queryGiveToFriendRecordsByStore(storeId, endDate);
+            long giveToFriendCount = giveToFriendRecords.size();
             result.put("giveToFriendCount", giveToFriendCount);
-            
-            // 赠送好友金额合计(优惠券面值的总和)
-            BigDecimal giveToFriendAmount = couponUsers.stream()
-                    .map(cu -> {
-                        LifeDiscountCoupon coupon = lifeDiscountCouponMapper.selectById(cu.getCouponId());
-                        return coupon != null && coupon.getNominalValue() != null ? coupon.getNominalValue() : BigDecimal.ZERO;
+
+            // 赠送好友金额合计:用 LifeDiscountCouponStoreFriend 的券 id 和数量(singleQty),从 LifeDiscountCoupon 取面值,计算面值金额合计
+            BigDecimal giveToFriendAmount = giveToFriendRecords.stream()
+                    .map(record -> {
+                        LifeDiscountCoupon coupon = lifeDiscountCouponMapper.selectById(record.getCouponId());
+                        BigDecimal faceValue = coupon != null && coupon.getNominalValue() != null ? coupon.getNominalValue() : BigDecimal.ZERO;
+                        int qty = record.getSingleQty() != null && record.getSingleQty() > 0 ? record.getSingleQty() : 0;
+                        return faceValue.multiply(BigDecimal.valueOf(qty));
                     })
                     .reduce(BigDecimal.ZERO, BigDecimal::add);
             result.put("giveToFriendAmount", giveToFriendAmount);
+
+            // 赠送好友使用数/使用金额:从 life_discount_coupon_user 统计
+            String storeIdStr = String.valueOf(storeId);
+            List<LifeDiscountCouponUser> couponUsers = queryCouponUsersByStore(storeIdStr, endDate);
             
             // 赠送好友使用数量(status=1已使用的数量)
             long giveToFriendUseCount = couponUsers.stream()
@@ -538,6 +541,33 @@ public class TrackEventServiceImpl extends ServiceImpl<StoreTrackEventMapper, St
     }
     
     /**
+     * 根据店铺优惠券 id 查询 LifeDiscountCouponStoreFriend 赠送好友记录(累计数据,截至 endDate)
+     * 从 LifeDiscountCoupon 查店铺优惠券 id,再按优惠券 id 查 LifeDiscountCouponStoreFriend
+     */
+    private List<LifeDiscountCouponStoreFriend> queryGiveToFriendRecordsByStore(Integer storeId, Date endDate) {
+        try {
+            LambdaQueryWrapper<LifeDiscountCoupon> couponWrapper = new LambdaQueryWrapper<>();
+            couponWrapper.eq(LifeDiscountCoupon::getStoreId, String.valueOf(storeId))
+                    .eq(LifeDiscountCoupon::getType, 1)
+                    .eq(LifeDiscountCoupon::getDeleteFlag, 0);
+            List<LifeDiscountCoupon> coupons = lifeDiscountCouponMapper.selectList(couponWrapper);
+            if (coupons == null || coupons.isEmpty()) {
+                return Collections.emptyList();
+            }
+            List<Integer> couponIds = coupons.stream()
+                    .map(LifeDiscountCoupon::getId)
+                    .collect(Collectors.toList());
+            LambdaQueryWrapper<LifeDiscountCouponStoreFriend> wrapper = new LambdaQueryWrapper<>();
+            wrapper.in(LifeDiscountCouponStoreFriend::getCouponId, couponIds)
+                    .lt(LifeDiscountCouponStoreFriend::getCreatedTime, endDate);
+            return lifeDiscountCouponStoreFriendMapper.selectList(wrapper);
+        } catch (Exception e) {
+            log.error("查询店铺赠送好友记录失败: storeId={}", storeId, e);
+            return Collections.emptyList();
+        }
+    }
+
+    /**
      * 查询店铺关联的优惠券用户数据(累计数据,截至到endDate)
      */
     private List<LifeDiscountCouponUser> queryCouponUsersByStore(String storeId, Date endDate) {