叮咚买菜系统优惠券管理功能开发实现方案

分类:IT频道 时间:2026-01-22 02:55 浏览:10
概述
    一、功能概述    优惠券管理是电商系统中的重要组成部分,对于叮咚买菜这类生鲜电商平台,合理的优惠券策略能有效提升用户活跃度、促进订单转化。主要功能包括:    1.优惠券创建与配置  2.优惠券发放与领取  3.优惠券使用与核销  4.优惠券状态管理  5.数据分析与报表    二、数据库
内容
  
   一、功能概述
  
  优惠券管理是电商系统中的重要组成部分,对于叮咚买菜这类生鲜电商平台,合理的优惠券策略能有效提升用户活跃度、促进订单转化。主要功能包括:
  
  1. 优惠券创建与配置
  2. 优惠券发放与领取
  3. 优惠券使用与核销
  4. 优惠券状态管理
  5. 数据分析与报表
  
   二、数据库设计
  
   核心表结构
  
  1. 优惠券表(coupon)
   - id: 主键
   - name: 优惠券名称
   - type: 优惠券类型(满减/折扣/免运费等)
   - amount: 优惠金额/折扣比例
   - condition: 使用条件(满多少可用)
   - start_time: 生效时间
   - end_time: 失效时间
   - total_count: 总发放量
   - remaining_count: 剩余数量
   - status: 状态(未开始/进行中/已结束)
   - create_time: 创建时间
   - update_time: 更新时间
  
  2. 用户优惠券表(user_coupon)
   - id: 主键
   - user_id: 用户ID
   - coupon_id: 优惠券ID
   - status: 状态(未使用/已使用/已过期)
   - get_time: 领取时间
   - use_time: 使用时间
   - order_id: 关联订单ID(使用后填写)
  
  3. 优惠券发放记录表(coupon_issue_log)
   - id: 主键
   - coupon_id: 优惠券ID
   - issue_type: 发放类型(系统发放/用户领取/主动赠送)
   - issue_count: 发放数量
   - operator: 操作人(系统/管理员)
   - create_time: 创建时间
  
   三、核心功能实现
  
   1. 优惠券创建与配置
  
  ```java
  // 优惠券创建示例代码
  public Coupon createCoupon(CouponCreateRequest request) {
   Coupon coupon = new Coupon();
   coupon.setName(request.getName());
   coupon.setType(request.getType());
   coupon.setAmount(request.getAmount());
   coupon.setCondition(request.getCondition());
   coupon.setStartTime(request.getStartTime());
   coupon.setEndTime(request.getEndTime());
   coupon.setTotalCount(request.getTotalCount());
   coupon.setRemainingCount(request.getTotalCount());
   coupon.setStatus(CouponStatus.NOT_STARTED);
   coupon.setCreateTime(new Date());
   coupon.setUpdateTime(new Date());
  
   // 保存到数据库
   couponRepository.save(coupon);
  
   return coupon;
  }
  ```
  
   2. 优惠券发放功能
  
   2.1 系统自动发放(如新用户注册)
  
  ```java
  public void issueCouponToNewUser(Long userId) {
   // 查询适用于新用户的优惠券
   List newUserCoupons = couponRepository.findByTarget(CouponTarget.NEW_USER);
  
   for (Coupon coupon : newUserCoupons) {
   UserCoupon userCoupon = new UserCoupon();
   userCoupon.setUserId(userId);
   userCoupon.setCouponId(coupon.getId());
   userCoupon.setStatus(UserCouponStatus.UNUSED);
   userCoupon.setGetTime(new Date());
  
   // 减少优惠券剩余数量
   coupon.setRemainingCount(coupon.getRemainingCount() - 1);
   couponRepository.save(coupon);
  
   // 保存用户优惠券关系
   userCouponRepository.save(userCoupon);
   }
  }
  ```
  
   2.2 用户主动领取
  
  ```java
  public Result userGetCoupon(Long userId, Long couponId) {
   Coupon coupon = couponRepository.findById(couponId)
   .orElseThrow(() -> new RuntimeException("优惠券不存在"));
  
   // 检查优惠券状态
   if (!coupon.getStatus().equals(CouponStatus.ONGOING)) {
   return Result.fail("优惠券不可用");
   }
  
   // 检查是否已领完
   if (coupon.getRemainingCount() <= 0) {
   return Result.fail("优惠券已领完");
   }
  
   // 检查用户是否已领取过(根据业务需求)
   boolean hasGot = userCouponRepository.existsByUserIdAndCouponId(userId, couponId);
   if (hasGot && coupon.getLimitPerUser() == 1) {
   return Result.fail("您已领取过该优惠券");
   }
  
   // 创建用户优惠券记录
   UserCoupon userCoupon = new UserCoupon();
   userCoupon.setUserId(userId);
   userCoupon.setCouponId(couponId);
   userCoupon.setStatus(UserCouponStatus.UNUSED);
   userCoupon.setGetTime(new Date());
  
   // 减少优惠券剩余数量
   coupon.setRemainingCount(coupon.getRemainingCount() - 1);
   couponRepository.save(coupon);
  
   // 保存记录
   userCouponRepository.save(userCoupon);
  
   return Result.success(userCoupon);
  }
  ```
  
   3. 优惠券使用与核销
  
   3.1 订单结算时优惠券选择
  
  ```java
  public List getAvailableCoupons(Long userId, BigDecimal orderAmount) {
   // 查询用户未使用的优惠券
   List userCoupons = userCouponRepository.findByUserIdAndStatus(
   userId, UserCouponStatus.UNUSED);
  
   return userCoupons.stream()
   .filter(uc -> {
   Coupon coupon = couponRepository.findById(uc.getCouponId()).get();
   // 检查时间有效性
   Date now = new Date();
   if (now.before(coupon.getStartTime()) || now.after(coupon.getEndTime())) {
   return false;
   }
   // 检查使用条件
   if (coupon.getCondition() != null &&
   orderAmount.compareTo(coupon.getCondition()) < 0) {
   return false;
   }
   return true;
   })
   .collect(Collectors.toList());
  }
  ```
  
   3.2 优惠券核销
  
  ```java
  public Order createOrderWithCoupon(OrderCreateRequest request, Long couponId) {
   // 验证优惠券
   UserCoupon userCoupon = userCouponRepository.findById(couponId)
   .orElseThrow(() -> new RuntimeException("优惠券不存在"));
  
   if (!userCoupon.getStatus().equals(UserCouponStatus.UNUSED)) {
   throw new RuntimeException("优惠券不可用");
   }
  
   Coupon coupon = couponRepository.findById(userCoupon.getCouponId()).get();
   Date now = new Date();
   if (now.before(coupon.getStartTime()) || now.after(coupon.getEndTime())) {
   throw new RuntimeException("优惠券不在有效期内");
   }
  
   // 创建订单
   Order order = new Order();
   // 设置订单基本信息...
  
   // 计算优惠金额
   BigDecimal discountAmount = calculateDiscount(coupon, order.getTotalAmount());
   order.setCouponDiscount(discountAmount);
   order.setActualAmount(order.getTotalAmount().subtract(discountAmount));
  
   // 更新用户优惠券状态
   userCoupon.setStatus(UserCouponStatus.USED);
   userCoupon.setUseTime(new Date());
   userCoupon.setOrderId(order.getId());
   userCouponRepository.save(userCoupon);
  
   // 保存订单
   orderRepository.save(order);
  
   return order;
  }
  
  private BigDecimal calculateDiscount(Coupon coupon, BigDecimal orderAmount) {
   switch (coupon.getType()) {
   case FIXED_AMOUNT:
   return coupon.getAmount();
   case PERCENTAGE:
   return orderAmount.multiply(coupon.getAmount().divide(new BigDecimal(100)));
   case FREE_SHIPPING:
   return orderAmount.compareTo(coupon.getCondition()) >= 0 ?
   getShippingFee(orderAmount) : BigDecimal.ZERO;
   default:
   return BigDecimal.ZERO;
   }
  }
  ```
  
   4. 优惠券状态管理
  
  ```java
  // 定时任务更新优惠券状态
  @Scheduled(cron = "0 0 * * * ?")
  public void updateCouponStatus() {
   Date now = new Date();
   List coupons = couponRepository.findAll();
  
   for (Coupon coupon : coupons) {
   if (now.before(coupon.getStartTime()) &&
   !coupon.getStatus().equals(CouponStatus.NOT_STARTED)) {
   coupon.setStatus(CouponStatus.NOT_STARTED);
   couponRepository.save(coupon);
   } else if (now.after(coupon.getStartTime()) &&
   now.before(coupon.getEndTime()) &&
   !coupon.getStatus().equals(CouponStatus.ONGOING)) {
   coupon.setStatus(CouponStatus.ONGOING);
   couponRepository.save(coupon);
   } else if (now.after(coupon.getEndTime()) &&
   !coupon.getStatus().equals(CouponStatus.EXPIRED)) {
   coupon.setStatus(CouponStatus.EXPIRED);
   couponRepository.save(coupon);
   }
   }
  }
  ```
  
   四、前端交互设计
  
   1. 优惠券列表页
  - 展示用户拥有的所有优惠券
  - 按状态分类(未使用/已使用/已过期)
  - 显示优惠券基本信息(名称、金额、有效期、使用条件)
  
   2. 优惠券领取页
  - 展示可领取的优惠券列表
  - 显示优惠券详情
  - "立即领取"按钮
  
   3. 订单结算页
  - 优惠券选择区域
  - 显示可用优惠券列表
  - 显示使用优惠券后的实付金额
  - "不使用优惠券"选项
  
   五、安全与性能考虑
  
  1. 并发控制:
   - 优惠券领取接口需要加锁,防止超发
   - 使用Redis分布式锁或数据库行锁
  
  2. 幂等性处理:
   - 领取优惠券操作需要保证幂等
   - 使用唯一请求ID或优惠券ID+用户ID作为唯一键
  
  3. 性能优化:
   - 优惠券查询使用缓存
   - 批量查询用户可用优惠券
   - 异步处理优惠券发放日志
  
  4. 安全防护:
   - 防止优惠券ID伪造
   - 接口权限校验
   - 防止批量刷券
  
   六、扩展功能
  
  1. 优惠券分享:用户可将优惠券分享给好友
  2. 优惠券组合使用:支持多张优惠券叠加使用
  3. 优惠券模板:支持批量创建相似优惠券
  4. 优惠券效果分析:统计优惠券使用率、转化率等指标
  5. A/B测试:不同用户群体发放不同优惠券测试效果
  
   七、测试用例
  
  1. 正常场景:
   - 用户成功领取优惠券
   - 用户使用优惠券下单
   - 系统自动发放新用户优惠券
  
  2. 异常场景:
   - 领取已领完的优惠券
   - 使用过期的优惠券
   - 使用不满足条件的优惠券
   - 重复领取不可重复领取的优惠券
  
  3. 边界场景:
   - 优惠券刚好领完
   - 订单金额刚好满足使用条件
   - 优惠券在生效边界时间使用
  
  通过以上设计,叮咚买菜系统可以实现一个完整、高效、安全的优惠券管理功能,有效提升用户购物体验和平台营销效果。
评论
  • 下一篇

  • Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8192 bytes) in /www/wwwroot/www.sjwxsc.com/config/function.php on line 274