Commit 2a9dffb6edeb057762c771b9858c8be2b0cce8b9
Committed by
Gitee
!1246 【性能优化】循环中减少不必要的查询 update r/yudao/module/promotion/service/coupon/CouponServiceImpl.java.
Merge pull request !1246 from 山野羡民/N/A
Showing
1 changed file
with
6 additions
and
2 deletions
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java
| ... | ... | @@ -121,6 +121,10 @@ public class CouponServiceImpl implements CouponService { |
| 121 | 121 | @Transactional(rollbackFor = Exception.class) |
| 122 | 122 | public Map<Long, List<Long>> takeCoupon(Long templateId, Set<Long> userIds, CouponTakeTypeEnum takeType) { |
| 123 | 123 | CouponTemplateDO template = couponTemplateService.getCouponTemplate(templateId); |
| 124 | + return takeCoupon(template, userIds, takeType); | |
| 125 | + } | |
| 126 | + | |
| 127 | + private Map<Long, List<Long>> takeCoupon(CouponTemplateDO template, Set<Long> userIds, CouponTakeTypeEnum takeType) { | |
| 124 | 128 | // 1. 过滤掉达到领取限制的用户 |
| 125 | 129 | removeTakeLimitUser(userIds, template); |
| 126 | 130 | // 2. 校验优惠劵是否可以领取 |
| ... | ... | @@ -131,7 +135,7 @@ public class CouponServiceImpl implements CouponService { |
| 131 | 135 | couponMapper.insertBatch(couponList); |
| 132 | 136 | |
| 133 | 137 | // 4. 增加优惠劵模板的领取数量 |
| 134 | - couponTemplateService.updateCouponTemplateTakeCount(templateId, userIds.size()); | |
| 138 | + couponTemplateService.updateCouponTemplateTakeCount(template.getId(), userIds.size()); | |
| 135 | 139 | |
| 136 | 140 | return convertMultiMap(couponList, CouponDO::getUserId, CouponDO::getId); |
| 137 | 141 | } |
| ... | ... | @@ -208,7 +212,7 @@ public class CouponServiceImpl implements CouponService { |
| 208 | 212 | public void takeCouponByRegister(Long userId) { |
| 209 | 213 | List<CouponTemplateDO> templates = couponTemplateService.getCouponTemplateListByTakeType(CouponTakeTypeEnum.REGISTER); |
| 210 | 214 | for (CouponTemplateDO template : templates) { |
| 211 | - takeCoupon(template.getId(), CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); | |
| 215 | + takeCoupon(template, CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); | |
| 212 | 216 | } |
| 213 | 217 | } |
| 214 | 218 | ... | ... |