Commit 77f776356011b878d8d191407cb5ef22e350e8a7
1 parent
b1ac3a0b
优惠券:完善优惠券领取接口
Showing
1 changed file
with
24 additions
and
8 deletions
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/coupon/AppCouponController.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.controller.app.coupon; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | +import cn.hutool.core.map.MapUtil; | |
| 3 | 5 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 7 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; |
| 6 | -import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponMatchReqVO; | |
| 7 | -import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponMatchRespVO; | |
| 8 | -import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponPageReqVO; | |
| 9 | -import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponRespVO; | |
| 10 | -import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.template.AppCouponTemplatePageReqVO; | |
| 8 | +import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.*; | |
| 11 | 9 | import cn.iocoder.yudao.module.promotion.convert.coupon.CouponConvert; |
| 12 | 10 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO; |
| 11 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; | |
| 12 | +import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; | |
| 13 | 13 | import cn.iocoder.yudao.module.promotion.service.coupon.CouponService; |
| 14 | +import cn.iocoder.yudao.module.promotion.service.coupon.CouponTemplateService; | |
| 14 | 15 | import io.swagger.v3.oas.annotations.Operation; |
| 16 | +import io.swagger.v3.oas.annotations.Parameter; | |
| 15 | 17 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 16 | 18 | import org.springframework.validation.annotation.Validated; |
| 17 | 19 | import org.springframework.web.bind.annotation.*; |
| 18 | 20 | |
| 19 | 21 | import javax.annotation.Resource; |
| 22 | +import javax.validation.Valid; | |
| 20 | 23 | import java.util.Collections; |
| 21 | 24 | import java.util.List; |
| 22 | 25 | |
| ... | ... | @@ -31,12 +34,25 @@ public class AppCouponController { |
| 31 | 34 | |
| 32 | 35 | @Resource |
| 33 | 36 | private CouponService couponService; |
| 37 | + @Resource | |
| 38 | + private CouponTemplateService couponTemplateService; | |
| 34 | 39 | |
| 35 | - // TODO 芋艿:待实现 | |
| 36 | 40 | @PostMapping("/take") |
| 37 | 41 | @Operation(summary = "领取优惠劵") |
| 38 | - public CommonResult<Long> takeCoupon(@RequestBody AppCouponTemplatePageReqVO pageReqVO) { | |
| 39 | - return success(1L); | |
| 42 | + @Parameter(name = "templateId", description = "优惠券模板编号", required = true, example = "1024") | |
| 43 | + public CommonResult<Boolean> takeCoupon(@Valid @RequestBody AppCouponTakeReqVO reqVO) { | |
| 44 | + Long userId = getLoginUserId(); | |
| 45 | + // 领取 | |
| 46 | + couponService.takeCoupon(reqVO.getTemplateId(), CollUtil.newHashSet(userId), CouponTakeTypeEnum.USER); | |
| 47 | + // 检查是否可以继续领取 | |
| 48 | + CouponTemplateDO couponTemplate = couponTemplateService.getCouponTemplate(reqVO.getTemplateId()); | |
| 49 | + boolean canTakeAgain = true; | |
| 50 | + if (couponTemplate.getTakeLimitCount() != null && couponTemplate.getTakeLimitCount() > 0) { | |
| 51 | + Integer takeCount = MapUtil.getInt(couponService.getTakeCountMapByTemplateIds( | |
| 52 | + Collections.singleton(reqVO.getTemplateId()), userId), reqVO.getTemplateId(), 0); | |
| 53 | + canTakeAgain = takeCount < couponTemplate.getTakeLimitCount(); | |
| 54 | + } | |
| 55 | + return success(canTakeAgain); | |
| 40 | 56 | } |
| 41 | 57 | |
| 42 | 58 | @GetMapping("/match-list") | ... | ... |