feat: 解决优惠券可以领取多张的问题

This commit is contained in:
weizhiqiang 2024-12-13 09:18:34 +08:00
parent 4a7240a087
commit 4d110f964f
3 changed files with 39 additions and 41 deletions

View file

@ -18,7 +18,6 @@ import com.skyeye.coupon.enums.PromotionMaterialScope;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* @ClassName: CouponUse
@ -43,7 +42,7 @@ public class CouponUse extends OperatorUserInfo {
@TableField(exist = false)
@Property(value = "优惠券信息")
private Map<String, Object> couponMation;
private Coupon couponMation;
@TableField(value = "state")
@Property(value = "状态", enumClass = CouponUseState.class)

View file

@ -68,8 +68,8 @@ public class CouponServiceImpl extends SkyeyeBusinessServiceImpl<CouponDao, Coup
public void validatorEntity(Coupon coupon) {
// 模板新增
if (StrUtil.isEmpty(coupon.getId()) && StrUtil.isEmpty(coupon.getTemplateId()) && // 主键和模板id为空时即为模板
coupon.getProductScope() != PromotionMaterialScope.ALL.getKey() && // 判断适用商品类型
CollectionUtil.isEmpty(coupon.getCouponMaterialList())) // 不适用全部商品时适用对象不能为空
coupon.getProductScope() != PromotionMaterialScope.ALL.getKey() && // 判断适用商品类型
CollectionUtil.isEmpty(coupon.getCouponMaterialList())) // 不适用全部商品时适用对象不能为空
{
throw new CustomException("需要指定优惠券适用的商品范围,适用全部商品时可为空");
}
@ -217,6 +217,7 @@ public class CouponServiceImpl extends SkyeyeBusinessServiceImpl<CouponDao, Coup
updateWrapper.eq(CommonConstants.ID, couponId);
updateWrapper.set(MybatisPlusUtil.toColumns(Coupon::getTakeCount), takeCount);
update(updateWrapper);
refreshCache(couponId);
}
@Override
@ -237,10 +238,10 @@ public class CouponServiceImpl extends SkyeyeBusinessServiceImpl<CouponDao, Coup
String materialId = inputObject.getParams().get("materialId").toString();
String typeKey = MybatisPlusUtil.toColumns(Coupon::getTemplateId);
MPJLambdaWrapper<Coupon> wrapper = new MPJLambdaWrapper<Coupon>()
.innerJoin(CouponMaterial.class, CouponMaterial::getCouponId, Coupon::getId)
.eq(CouponMaterial::getMaterialId, materialId)
.eq(MybatisPlusUtil.toColumns(Coupon::getEnabled), EnableEnum.ENABLE_USING.getKey())
.isNotNull(typeKey).ne(typeKey, StrUtil.EMPTY);
.innerJoin(CouponMaterial.class, CouponMaterial::getCouponId, Coupon::getId)
.eq(CouponMaterial::getMaterialId, materialId)
.eq(MybatisPlusUtil.toColumns(Coupon::getEnabled), EnableEnum.ENABLE_USING.getKey())
.isNotNull(typeKey).ne(typeKey, StrUtil.EMPTY);
List<Coupon> list = skyeyeBaseMapper.selectJoinList(Coupon.class, wrapper);
setDrawState(list);// 设置是否可以领取状态
outputObject.setBean(list);

View file

@ -101,46 +101,44 @@ public class CouponUseServiceImpl extends SkyeyeBusinessServiceImpl<CouponUseDao
couponUseMaterial.setMaterialId(couponMaterial.getMaterialId());
couponUseMaterialList.add(couponUseMaterial);
}
if (StrUtil.isNotEmpty(couponUse.getCouponId())) {
// 状态
couponUse.setState(CouponUseState.UNUSED.getKey());
//满减
couponUse.setUsePrice(coupon.getUsePrice());
//使用范围
couponUse.setProductScope(coupon.getProductScope());
//生效时间
if (Objects.equals(CouponValidityType.DATE.getKey(), coupon.getValidityType())) {
couponUse.setValidStartTime(coupon.getValidStartTime());
couponUse.setValidEndTime(coupon.getValidEndTime());
} else {
couponUse.setValidStartTime(DateUtil.getAfDate(LocalDate.now().toDate(), coupon.getFixedStartTime(), "d").toString());
couponUse.setValidEndTime(DateUtil.getAfDate(LocalDate.now().toDate(), coupon.getFixedEndTime(), "d").toString());
// 领取非固定类型优惠券时借助couponMation成员变量存储优惠券信息便于后置执行新增定时任务
couponUse.setCouponMation(JSONUtil.toBean(JSONUtil.toJsonStr(coupon), null));
}
//折扣类型
couponUse.setDiscountType(coupon.getDiscountType());
//折扣值
if (Objects.equals(PromotionDiscountType.PERCENT.getKey(), coupon.getDiscountType())) {
couponUse.setDiscountPercent(coupon.getDiscountPercent());
} else {
couponUse.setDiscountPrice(coupon.getDiscountPrice());
}
//折扣上限
couponUse.setDiscountLimitPrice(coupon.getDiscountLimitPrice());
// 状态
couponUse.setState(CouponUseState.UNUSED.getKey());
//满减
couponUse.setUsePrice(coupon.getUsePrice());
//使用范围
couponUse.setProductScope(coupon.getProductScope());
//生效时间
if (Objects.equals(CouponValidityType.DATE.getKey(), coupon.getValidityType())) {
couponUse.setValidStartTime(coupon.getValidStartTime());
couponUse.setValidEndTime(coupon.getValidEndTime());
} else {
couponUse.setValidStartTime(DateUtil.getAfDate(LocalDate.now().toDate(), coupon.getFixedStartTime(), "d").toString());
couponUse.setValidEndTime(DateUtil.getAfDate(LocalDate.now().toDate(), coupon.getFixedEndTime(), "d").toString());
}
// 领取非固定类型优惠券时借助couponMation成员变量存储优惠券信息便于后置执行新增定时任务
couponUse.setCouponMation(coupon);
//折扣类型
couponUse.setDiscountType(coupon.getDiscountType());
//折扣值
if (Objects.equals(PromotionDiscountType.PERCENT.getKey(), coupon.getDiscountType())) {
couponUse.setDiscountPercent(coupon.getDiscountPercent());
} else {
couponUse.setDiscountPrice(coupon.getDiscountPrice());
}
//折扣上限
couponUse.setDiscountLimitPrice(coupon.getDiscountLimitPrice());
}
@Override
public void createPostpose(CouponUse couponUse, String userId) {
QueryWrapper<CouponUse> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(MybatisPlusUtil.toColumns(CouponUse::getCouponId), couponUse.getCouponId());
couponService.updateTakeCount(couponUse.getCouponId(), (int) count(queryWrapper));
// 更新优惠券领取数量
couponService.updateTakeCount(couponUse.getCouponId(), couponUse.getCouponMation().getTakeCount() + 1);
// 新增优惠券可使用的商品信息
couponUseMaterialService.createEntity(couponUse.getCouponUseMaterialList(), userId);
// 定时任务
Map<String, Object> couponMation = couponUse.getCouponMation();
if (ObjectUtil.isNotEmpty(couponMation) && Objects.equals(couponMation.get("validityType"), CouponValidityType.TERM.getKey())) {
startUpTaskQuartz(couponUse.getId(), couponMation.get("name").toString(), couponUse.getValidEndTime());
Coupon couponMation = couponUse.getCouponMation();
if (Objects.equals(couponMation.getValidityType(), CouponValidityType.TERM.getKey())) {
startUpTaskQuartz(couponUse.getId(), couponMation.getName(), couponUse.getValidEndTime());
}
}