【代码优化】商城:优惠券回收之后,领取数量和剩余数量的更新

This commit is contained in:
YunaiV 2024-08-08 23:35:20 +08:00
parent 55f2612369
commit a86ce73542

View File

@ -133,18 +133,17 @@ public class CouponServiceImpl implements CouponService {
@Transactional @Transactional
public void deleteCoupon(Long id) { public void deleteCoupon(Long id) {
// 校验存在 // 校验存在
validateCouponExists(id); CouponDO coupon = validateCouponExists(id);
// 查询优惠券信息
CouponDO couponDO = couponMapper.selectById(id);
// 更新优惠劵 // 更新优惠劵
int deleteCount = couponMapper.delete(id, int deleteCount = couponMapper.delete(id,
asList(CouponStatusEnum.UNUSED.getStatus(), CouponStatusEnum.EXPIRE.getStatus())); asList(CouponStatusEnum.UNUSED.getStatus(), CouponStatusEnum.EXPIRE.getStatus()));
if (deleteCount == 0) { if (deleteCount == 0) {
throw exception(COUPON_DELETE_FAIL_USED); throw exception(COUPON_DELETE_FAIL_USED);
} }
// 传入优惠券模板ID减少优惠劵模板的领取数量 -1
couponTemplateService.updateCouponTemplateTakeCount(couponDO.getTemplateId(), -1); // 减少优惠劵模板的领取数量 -1
couponTemplateService.updateCouponTemplateTakeCount(coupon.getTemplateId(), -1);
} }
@Override @Override
@ -152,10 +151,12 @@ public class CouponServiceImpl implements CouponService {
return couponMapper.selectListByUserIdAndStatus(userId, status); return couponMapper.selectListByUserIdAndStatus(userId, status);
} }
private void validateCouponExists(Long id) { private CouponDO validateCouponExists(Long id) {
if (couponMapper.selectById(id) == null) { CouponDO coupon = couponMapper.selectById(id);
if (coupon == null) {
throw exception(COUPON_NOT_EXISTS); throw exception(COUPON_NOT_EXISTS);
} }
return coupon;
} }
@Override @Override