功能:新店初始化VIP自动券模板

This commit is contained in:
xiaowang
2026-03-21 16:51:17 +08:00
parent 760dd64912
commit 234e0f9240
4 changed files with 124 additions and 9 deletions
@@ -94,6 +94,13 @@ public interface CouponService extends IService<CouponDO> {
* @return 可用优惠券列表
*/
List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice, Long shopId);
/**
* 为新店铺初始化默认 VIP 自动券模板
*
* @param shopId 店铺ID
*/
void initializeDefaultVipAutoCoupons(Long shopId);
/**
* 发放优惠券
@@ -42,6 +42,13 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
private static final String COUPON_SOURCE_USER = "USER_COUPON";
private static final String COUPON_SOURCE_SYSTEM_VIP = "SYSTEM_VIP";
private static final String COUPON_SCENE_VIP_AUTO = "VIP_AUTO";
private static final int DEFAULT_VIP_AUTO_COUPON_TOTAL_COUNT = 999999;
private static final LocalDateTime DEFAULT_VIP_AUTO_COUPON_END_TIME = LocalDateTime.of(2099, 12, 31, 23, 59, 59);
private static final List<VipAutoCouponTemplate> DEFAULT_SHOP_VIP_AUTO_COUPONS = List.of(
new VipAutoCouponTemplate("BRONZE_95", "青铜VIP自动95折券", "青铜", BigDecimal.valueOf(9.5)),
new VipAutoCouponTemplate("SILVER_92", "白银VIP自动92折券", "白银", BigDecimal.valueOf(9.2)),
new VipAutoCouponTemplate("GOLD_90", "黄金VIP自动90折券", "黄金", BigDecimal.valueOf(9.0))
);
private static final String VIP_LEVEL_NORMAL = "普通";
private final CouponTagMapper couponTagMapper;
@@ -285,15 +292,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
UserDO userDO = userMapper.selectById(userId);
String currentVipLevel = userDO != null ? userDO.getLevel() : null;
if (currentVipLevel != null && !currentVipLevel.isBlank() && !VIP_LEVEL_NORMAL.equals(currentVipLevel)) {
List<CouponDO> vipCoupons = list(new LambdaQueryWrapper<CouponDO>()
.eq(CouponDO::getIsDelete, 0)
.eq(CouponDO::getStatus, 1)
.eq(CouponDO::getAutoMatch, 1)
.eq(CouponDO::getCouponScene, COUPON_SCENE_VIP_AUTO)
.eq(CouponDO::getRequiredVipLevel, currentVipLevel)
.orderByDesc(CouponDO::getDiscountValue)
.orderByAsc(CouponDO::getEndTime)
);
List<CouponDO> vipCoupons = listVipAutoCouponsByShop(currentVipLevel, shopId);
for (CouponDO couponDO : vipCoupons) {
if (!isCouponApplicable(couponDO, now, orderAmount, productTags, shopId)) {
@@ -308,6 +307,44 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
.thenComparing(UserCouponVO::getEndTime, Comparator.nullsLast(LocalDateTime::compareTo)));
return availableCoupons;
}
@Override
@Transactional
public void initializeDefaultVipAutoCoupons(Long shopId) {
if (shopId == null || shopId <= 0) {
return;
}
LocalDateTime startTime = LocalDateTime.now();
for (VipAutoCouponTemplate template : DEFAULT_SHOP_VIP_AUTO_COUPONS) {
String couponCode = buildShopVipAutoCouponCode(shopId, template.codeSuffix());
CouponDO existingCoupon = lambdaQuery()
.eq(CouponDO::getCouponCode, couponCode)
.one();
if (existingCoupon != null) {
continue;
}
CouponDO couponDO = new CouponDO();
couponDO.setCouponCode(couponCode);
couponDO.setCouponName(template.couponName());
couponDO.setCouponType(1);
couponDO.setDiscountValue(template.discountValue());
couponDO.setMinAmount(BigDecimal.ZERO);
couponDO.setReduceAmount(BigDecimal.ZERO);
couponDO.setTotalCount(DEFAULT_VIP_AUTO_COUPON_TOTAL_COUNT);
couponDO.setUsedCount(0);
couponDO.setStartTime(startTime);
couponDO.setEndTime(DEFAULT_VIP_AUTO_COUPON_END_TIME);
couponDO.setStatus(1);
couponDO.setShopId(shopId);
couponDO.setCouponScene(COUPON_SCENE_VIP_AUTO);
couponDO.setRequiredVipLevel(template.requiredVipLevel());
couponDO.setAutoMatch(1);
couponDO.setIsDelete(0);
save(couponDO);
}
}
@Override
@Transactional
@@ -492,6 +529,34 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
/**
* 转换为CouponVO
*/
private List<CouponDO> listVipAutoCouponsByShop(String requiredVipLevel, Long shopId) {
if (shopId != null && shopId > 0) {
List<CouponDO> shopCoupons = list(buildVipAutoCouponQuery(requiredVipLevel)
.eq(CouponDO::getShopId, shopId));
if (!shopCoupons.isEmpty()) {
return shopCoupons;
}
return list(buildVipAutoCouponQuery(requiredVipLevel)
.isNull(CouponDO::getShopId));
}
return list(buildVipAutoCouponQuery(requiredVipLevel));
}
private LambdaQueryWrapper<CouponDO> buildVipAutoCouponQuery(String requiredVipLevel) {
return new LambdaQueryWrapper<CouponDO>()
.eq(CouponDO::getIsDelete, 0)
.eq(CouponDO::getStatus, 1)
.eq(CouponDO::getAutoMatch, 1)
.eq(CouponDO::getCouponScene, COUPON_SCENE_VIP_AUTO)
.eq(CouponDO::getRequiredVipLevel, requiredVipLevel)
.orderByDesc(CouponDO::getDiscountValue)
.orderByAsc(CouponDO::getEndTime);
}
private String buildShopVipAutoCouponCode(Long shopId, String codeSuffix) {
return String.format("VIP_AUTO_SHOP_%d_%s", shopId, codeSuffix);
}
private CouponVO convertToCouponVO(CouponDO couponDO) {
CouponVO couponVO = new CouponVO();
couponVO.setId(couponDO.getId());
@@ -665,4 +730,12 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
}
return BigDecimal.ZERO;
}
private record VipAutoCouponTemplate(
String codeSuffix,
String couponName,
String requiredVipLevel,
BigDecimal discountValue
) {
}
}
@@ -7,11 +7,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.yuminailcore.dto.ShopDTO;
import com.xiaowang.yuminailcore.entity.ShopDO;
import com.xiaowang.yuminailcore.mapper.ShopMapper;
import com.xiaowang.yuminailcore.service.CouponService;
import com.xiaowang.yuminailcore.service.ShopService;
import com.xiaowang.yuminailcore.vo.PageResultVO;
import com.xiaowang.yuminailcore.vo.ShopVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@@ -29,8 +31,10 @@ import java.util.stream.Collectors;
public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopDO> implements ShopService {
private final ShopMapper shopMapper;
private final CouponService couponService;
@Override
@Transactional(rollbackFor = Exception.class)
public Long createShop(ShopDTO shopDTO) {
ShopDO shopDO = new ShopDO();
shopDO.setShopName(shopDTO.getShopName());
@@ -41,6 +45,7 @@ public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopDO> implements
shopDO.setImageFileIds(shopDTO.getImageFileIds());
shopDO.setIsDelete(0);
shopMapper.insert(shopDO);
couponService.initializeDefaultVipAutoCoupons(shopDO.getId());
return shopDO.getId();
}
@@ -0,0 +1,30 @@
# 2026-03-21 店铺默认 VIP 自动券初始化
## 本次调整
- 新增店铺时,系统会自动为该店铺初始化 3 张店铺级 VIP 自动券:
- 青铜 VIP 自动 95 折券
- 白银 VIP 自动 92 折券
- 黄金 VIP 自动 90 折券
- 默认券按店铺维度创建,不再继续依赖“未指定店铺”的全局模板。
- 店长原本就具备 `coupon:create`、`coupon:edit`、`coupon:delete` 权限,本轮保留这一口径,因此店长后续可以在自己店铺下继续新建、编辑和调整这些 VIP 自动券。
## 代码处理
- 在 [ShopServiceImpl.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/ShopServiceImpl.java) 中,把店铺创建改为事务执行,并在创建成功后自动初始化默认 VIP 自动券。
- 在 [CouponService.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/CouponService.java) 中新增店铺默认 VIP 自动券初始化接口。
- 在 [CouponServiceImpl.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/CouponServiceImpl.java) 中:
- 新增店铺默认 VIP 自动券初始化实现
- 默认券编码改为店铺维度唯一编码,避免不同店铺互相冲突
- 用户下单可用券查询改为“优先匹配店铺专属 VIP 自动券,若店铺没有专属券再回退全局券”,避免店铺券和历史全局券同时生效
## 影响说明
- 新建店铺后,在后台优惠券列表里会直接看到该店铺自己的 3 张 VIP 自动券。
- 店长进入自己店铺后台后,可以继续调整这些默认券,或者新增自己店铺的 VIP 自动券模板。
- 历史全局 VIP 自动券不会被这次代码直接删除;如果某个店铺已经拥有店铺专属 VIP 自动券,用户侧结算时会优先使用店铺专属配置。
## 验证
- 已执行:`mvn -q -DskipTests compile`
- 结果:通过