|
|
@@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.LifeCollect;
|
|
|
import shop.alien.entity.store.LifeDiscountCoupon;
|
|
|
@@ -18,6 +19,7 @@ import shop.alien.mapper.LifeCollectMapper;
|
|
|
import shop.alien.mapper.LifeDiscountCouponMapper;
|
|
|
import shop.alien.mapper.LifeDiscountCouponUserMapper;
|
|
|
import shop.alien.mapper.StoreInfoMapper;
|
|
|
+import shop.alien.store.config.BaseRedisService;
|
|
|
import shop.alien.store.service.LifeDiscountCouponUserService;
|
|
|
import shop.alien.util.common.constant.DiscountCouponEnum;
|
|
|
|
|
|
@@ -46,9 +48,20 @@ public class LifeDiscountCouponUserServiceImpl extends ServiceImpl<LifeDiscountC
|
|
|
|
|
|
private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;
|
|
|
|
|
|
+ private final BaseRedisService baseRedisService;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public R<Boolean> receiveCoupon(LifeDiscountCouponUserDto lifeDiscountCouponUserDto) {
|
|
|
+ // 锁value
|
|
|
+ String identifier = null;
|
|
|
try {
|
|
|
+ identifier = baseRedisService.lock(lifeDiscountCouponUserDto.getCouponId().toString());
|
|
|
+ if (StringUtils.isEmpty(identifier)) {
|
|
|
+ throw new RuntimeException("抢购人数过多,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
Integer receiveQuantity = lifeDiscountCouponUserDto.getReceiveQuantity();
|
|
|
LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(lifeDiscountCouponUserDto.getCouponId());
|
|
|
// 判断是否在领取时间内
|
|
|
@@ -64,6 +77,15 @@ public class LifeDiscountCouponUserServiceImpl extends ServiceImpl<LifeDiscountC
|
|
|
if (getStatus == 0) {
|
|
|
return R.fail("该优惠劵已经关闭领取");
|
|
|
}
|
|
|
+
|
|
|
+ if(lifeDiscountCoupon.getSingleQty() != null && lifeDiscountCoupon.getSingleQty() <= 0){
|
|
|
+ return R.fail("已发放完毕");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(lifeDiscountCoupon.getSingleQty() != null && lifeDiscountCoupon.getSingleQty() < receiveQuantity){
|
|
|
+ return R.fail("已发放完毕");
|
|
|
+ }
|
|
|
+
|
|
|
String specifiedDay = lifeDiscountCoupon.getSpecifiedDay();
|
|
|
|
|
|
//判断领取数量
|
|
|
@@ -107,9 +129,12 @@ public class LifeDiscountCouponUserServiceImpl extends ServiceImpl<LifeDiscountC
|
|
|
storeInfoMapper.update(null, new LambdaUpdateWrapper<StoreInfo>().eq(StoreInfo::getId, lifeCollect.getStoreId()).set(StoreInfo::getCollectNum, lifeCollectMapper.selectCount(queryWrapper)));
|
|
|
}
|
|
|
}
|
|
|
- } catch (BeansException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("LifeDiscountCouponController.receiveCoupon ERROR Msg=" + e.getMessage());
|
|
|
+ baseRedisService.unlock(lifeDiscountCouponUserDto.getCouponId().toString(), identifier);
|
|
|
return R.fail("领取失败");
|
|
|
+ } finally {
|
|
|
+ baseRedisService.unlock(lifeDiscountCouponUserDto.getCouponId().toString(), identifier);
|
|
|
}
|
|
|
return R.success("领取成功");
|
|
|
}
|