Scenario
Storage
- Coupon Batch 券批次表
- Coupon Rule 券规则表
- Coupon 优惠券表
券批次表:
| column | notes |
|---|---|
| batch_id | 批次ID |
| batch_name | 批次名称 |
| coupon_name | 优惠券名称 |
| rule_ie | 规则id |
| total_cnt | 总数量 |
| assign_cnt | 已发放数量 |
| user_cnt | 已使用数量 |
规则表;
| column | notes |
|---|---|
| rule_id | 规则ID,主键,自增 |
| name | 规则名称 |
| type | 优惠券类型 (满减,折扣) |
| content | 规则内容,json存储(使用门槛,优惠金额) |
优惠券表:
| column | notes |
|---|---|
| coupon_id | 优惠券ID |
| user_id | 用户ID |
| batch_id | 批次ID |
| status | 优惠券状态(未使用,已使用,已过期....) |
| order_id | 订单ID |
| received_time | 领取时间 |
| validate_start_time | 有效开始时间 |
| validate_end_time | 有效结束时间 |
| used_time | 使用时间 |
发券
insert into coupon (user_id,batch_id) values();
update coupon_batch
set total_cnt = total_cnt -1 ,assign_cnt = assign_cnt + 1
where batch = xx and total_cnt > 0;
领券
这个跟秒杀系统有点类似,比如商家发了十张优惠券,100个用户来抢,需要考虑大流量并发问题。
用券
用券会涉及到多个系统:订单,支付,优惠券。如果这个三个功能模块不在同一个工程(服务)中,需要使用分布式事务来保证数据一致性。
TCC:
- Try : 订单创建成功,将券的状态更新为“锁定”
- Confirm : 订单支付成功,将券的状态更新为“已使用”
- Cancel:订单支付失败,将券的状态更新为“未使用”
Scale
券过期提醒
新增优惠券过期通知表
| column | notes |
|---|---|
| coupon_id | 优惠券ID |
| user_id | 用户ID |
| need_notify_day | 需要进行通知的日期 |
| notify_type | 通知类型 |
| notify_time | 实际通知时间 |
| status | 状态 (初始化,成功,失败) |
表数据的插入:发/领券的时候,插入数据。