修复优惠券模块

This commit is contained in:
2025-08-09 16:12:08 +08:00
parent a7b7589b2a
commit d214e4b3cc
8 changed files with 334 additions and 30 deletions

View File

@@ -20,6 +20,7 @@ import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -39,6 +40,7 @@ public class CmsDesignServiceImpl extends ServiceImpl<CmsDesignMapper, CmsDesign
@Resource
private CmsLangLogService cmsLangLogService;
@Resource
@Lazy
private CmsNavigationService cmsNavigationService;
@Resource
private CmsArticleContentService cmsArticleContentService;

View File

@@ -35,8 +35,6 @@ public class CmsNavigationServiceImpl extends ServiceImpl<CmsNavigationMapper, C
@Resource
private CmsModelService cmsModelService;
@Resource
private CmsNavigationService cmsNavigationService;
@Resource
private UserService userService;
@Override
@@ -67,7 +65,7 @@ public class CmsNavigationServiceImpl extends ServiceImpl<CmsNavigationMapper, C
}
// 父级栏目并且是page模型则读取子项目第一条
if (navigation.getParentId().equals(0) && navigation.getModel().equals("page")) {
final CmsNavigation parent = cmsNavigationService.getOne(new LambdaQueryWrapper<CmsNavigation>().eq(CmsNavigation::getParentId, navigation.getNavigationId()).last("limit 1"));
final CmsNavigation parent = this.getOne(new LambdaQueryWrapper<CmsNavigation>().eq(CmsNavigation::getParentId, navigation.getNavigationId()).last("limit 1"));
if (ObjectUtil.isNotEmpty(parent)) {
navigation = parent;
}

View File

@@ -22,6 +22,38 @@ import lombok.EqualsAndHashCode;
public class ShopUserCoupon implements Serializable {
private static final long serialVersionUID = 1L;
// 优惠券类型常量
/** 满减券 */
public static final Integer TYPE_REDUCE = 10;
/** 折扣券 */
public static final Integer TYPE_DISCOUNT = 20;
/** 免费券 */
public static final Integer TYPE_FREE = 30;
// 适用范围常量
/** 全部商品 */
public static final Integer APPLY_ALL = 10;
/** 指定商品 */
public static final Integer APPLY_GOODS = 20;
/** 指定分类 */
public static final Integer APPLY_CATEGORY = 30;
// 使用状态常量
/** 未使用 */
public static final Integer STATUS_UNUSED = 0;
/** 已使用 */
public static final Integer STATUS_USED = 1;
/** 已过期 */
public static final Integer STATUS_EXPIRED = 2;
// 获取方式常量
/** 主动领取 */
public static final Integer OBTAIN_ACTIVE = 10;
/** 系统发放 */
public static final Integer OBTAIN_SYSTEM = 20;
/** 活动赠送 */
public static final Integer OBTAIN_ACTIVITY = 30;
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;

View File

@@ -24,7 +24,7 @@ public class ShopUserCouponParam extends BaseParam {
@Schema(description = "id")
@QueryField(type = QueryType.EQ)
private Long id;
private Integer id;
@Schema(description = "优惠券模板ID")
@QueryField(type = QueryType.EQ)

View File

@@ -106,25 +106,25 @@ public class CouponUtils {
}
// 检查最低消费金额
if (userCoupon.getMinPrice() != null &&
if (userCoupon.getMinPrice() != null &&
orderAmount.compareTo(userCoupon.getMinPrice()) < 0) {
return BigDecimal.ZERO;
}
BigDecimal discountAmount = BigDecimal.ZERO;
if (userCoupon.getType() == ShopUserCoupon.TYPE_REDUCE) {
if (ShopUserCoupon.TYPE_REDUCE.equals(userCoupon.getType())) {
// 满减券
discountAmount = userCoupon.getReducePrice() != null ?
discountAmount = userCoupon.getReducePrice() != null ?
userCoupon.getReducePrice() : BigDecimal.ZERO;
} else if (userCoupon.getType() == ShopUserCoupon.TYPE_DISCOUNT) {
} else if (ShopUserCoupon.TYPE_DISCOUNT.equals(userCoupon.getType())) {
// 折扣券
if (userCoupon.getDiscount() != null && userCoupon.getDiscount() > 0 && userCoupon.getDiscount() < 100) {
BigDecimal discountRate = BigDecimal.valueOf(100 - userCoupon.getDiscount())
.divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
discountAmount = orderAmount.multiply(discountRate);
}
} else if (userCoupon.getType() == ShopUserCoupon.TYPE_FREE) {
} else if (ShopUserCoupon.TYPE_FREE.equals(userCoupon.getType())) {
// 免费券
discountAmount = orderAmount;
}
@@ -146,19 +146,19 @@ public class CouponUtils {
return false;
}
if (userCoupon.getApplyRange() == ShopUserCoupon.APPLY_ALL) {
if (ShopUserCoupon.APPLY_ALL.equals(userCoupon.getApplyRange())) {
// 全部商品可用
return true;
} else if (userCoupon.getApplyRange() == ShopUserCoupon.APPLY_GOODS) {
} else if (ShopUserCoupon.APPLY_GOODS.equals(userCoupon.getApplyRange())) {
// 指定商品可用
if (goodsId == null || userCoupon.getApplyRangeConfig() == null) {
if (goodsId == null || userCoupon.getApplyRangeConfig() == null || userCoupon.getApplyRangeConfig().trim().isEmpty()) {
return false;
}
List<String> goodsIds = Arrays.asList(userCoupon.getApplyRangeConfig().split(","));
return goodsIds.contains(goodsId.toString());
} else if (userCoupon.getApplyRange() == ShopUserCoupon.APPLY_CATEGORY) {
} else if (ShopUserCoupon.APPLY_CATEGORY.equals(userCoupon.getApplyRange())) {
// 指定分类可用
if (categoryId == null || userCoupon.getApplyRangeConfig() == null) {
if (categoryId == null || userCoupon.getApplyRangeConfig() == null || userCoupon.getApplyRangeConfig().trim().isEmpty()) {
return false;
}
List<String> categoryIds = Arrays.asList(userCoupon.getApplyRangeConfig().split(","));
@@ -191,23 +191,23 @@ public class CouponUtils {
if (userCoupon == null) {
return false;
}
// 检查状态
if (userCoupon.getStatus() != ShopUserCoupon.STATUS_UNUSED) {
if (!ShopUserCoupon.STATUS_UNUSED.equals(userCoupon.getStatus())) {
return false;
}
// 检查是否过期
if (isExpired(userCoupon)) {
return false;
}
// 检查是否在有效期内
LocalDateTime now = LocalDateTime.now();
if (userCoupon.getStartTime() != null && userCoupon.getStartTime().isAfter(now)) {
return false; // 还未开始
}
return true;
}
@@ -224,18 +224,18 @@ public class CouponUtils {
StringBuilder sb = new StringBuilder();
sb.append(userCoupon.getName());
if (userCoupon.getType() == ShopUserCoupon.TYPE_REDUCE && userCoupon.getReducePrice() != null) {
if (ShopUserCoupon.TYPE_REDUCE.equals(userCoupon.getType()) && userCoupon.getReducePrice() != null) {
sb.append("").append(userCoupon.getReducePrice()).append("");
if (userCoupon.getMinPrice() != null && userCoupon.getMinPrice().compareTo(BigDecimal.ZERO) > 0) {
sb.append("(满").append(userCoupon.getMinPrice()).append("可用)");
}
} else if (userCoupon.getType() == ShopUserCoupon.TYPE_DISCOUNT && userCoupon.getDiscount() != null) {
} else if (ShopUserCoupon.TYPE_DISCOUNT.equals(userCoupon.getType()) && userCoupon.getDiscount() != null) {
sb.append(" ").append(userCoupon.getDiscount()).append("");
if (userCoupon.getMinPrice() != null && userCoupon.getMinPrice().compareTo(BigDecimal.ZERO) > 0) {
sb.append("(满").append(userCoupon.getMinPrice()).append("可用)");
}
} else if (userCoupon.getType() == ShopUserCoupon.TYPE_FREE) {
} else if (ShopUserCoupon.TYPE_FREE.equals(userCoupon.getType())) {
sb.append(" 免费券");
}
@@ -255,7 +255,7 @@ public class CouponUtils {
*/
public static String generateCouponCode(Integer userId, Integer couponId) {
String timestamp = String.valueOf(System.currentTimeMillis());
return String.format("CPN%s%s%s",
return String.format("CPN%s%s%s",
String.format("%08d", userId),
String.format("%06d", couponId),
timestamp.substring(timestamp.length() - 6));

View File

@@ -48,11 +48,11 @@ public class CouponUtilsTest {
reduceCoupon.setMinPrice(new BigDecimal("50.00"));
BigDecimal discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("10.00"), discount);
assertEquals(0, new BigDecimal("10.00").compareTo(discount));
// 测试不满足最低消费
discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("30.00"));
assertEquals(BigDecimal.ZERO, discount);
assertEquals(0, BigDecimal.ZERO.compareTo(discount));
// 测试折扣券
ShopUserCoupon discountCoupon = new ShopUserCoupon();
@@ -61,14 +61,14 @@ public class CouponUtilsTest {
discountCoupon.setMinPrice(new BigDecimal("50.00"));
discount = CouponUtils.calculateDiscountAmount(discountCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("20.0000"), discount);
assertEquals(0, new BigDecimal("20.0000").compareTo(discount));
// 测试免费券
ShopUserCoupon freeCoupon = new ShopUserCoupon();
freeCoupon.setType(ShopUserCoupon.TYPE_FREE);
discount = CouponUtils.calculateDiscountAmount(freeCoupon, new BigDecimal("100.00"));
assertEquals(new BigDecimal("100.00"), discount);
assertEquals(0, new BigDecimal("100.00").compareTo(discount));
}
@Test
@@ -99,7 +99,7 @@ public class CouponUtilsTest {
@Test
public void testIsExpired() {
ShopUserCoupon coupon = new ShopUserCoupon();
// 测试未过期
coupon.setEndTime(LocalDateTime.now().plusDays(1));
assertFalse(CouponUtils.isExpired(coupon));