Browse Source

feat(coupon): 实现优惠券有效期自动计算功能

- 添加优惠券开始日期和有效天数的基础校验逻辑
- 实现字符串类型天数转换为整数的安全转换机制
- 增加有效天数的数值合法性校验(必须大于0)
- 实现基于开始日期和天数计算结束日期的功能
- 完善优惠券日期相关字段的自动更新流程
fcw 2 tuần trước cách đây
mục cha
commit
62fe4f4424

+ 27 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -970,6 +970,33 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                     lifeDiscountCouponUser.setIssueSource(3); // 3-好评送券
                     lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
                     coupon.setSingleQty(coupon.getSingleQty() - 1);
+// 1. 获取开始日期
+                    LocalDate beginDate = coupon.getBeginGetDate();
+// 2. 获取字符串类型的天数
+                    String specifiedDayStr = coupon.getSpecifiedDay();
+
+// 3. 基础校验
+                    if (beginDate == null) {
+                        throw new IllegalArgumentException("优惠券开始日期不能为空");
+                    }
+                    if (specifiedDayStr == null || specifiedDayStr.isEmpty()) {
+                        throw new IllegalArgumentException("有效天数不能为空");
+                    }
+
+// 4. 字符串转整数 + 合法性校验
+                    int days;
+                    try {
+                        days = Integer.parseInt(specifiedDayStr);
+                    } catch (NumberFormatException e) {
+                        throw new IllegalArgumentException("有效天数必须是纯数字:" + specifiedDayStr);
+                    }
+                    if (days <= 0) {
+                        throw new IllegalArgumentException("有效天数必须大于0");
+                    }
+
+// 5. 计算结束日期并赋值
+                    LocalDate endDate = beginDate.plusDays(days);
+                    coupon.setEndGetDate(endDate);
                     lifeDiscountCouponMapper.updateById(coupon);
                     grantedCoupon = 1;
                     couponName = coupon.getName();  // 保存优惠券名称