本次优化为商店系统添加了更加灵活和强大的限购功能,支持单次购买限制和周期性购买限制,满足不同的业务需求。
max_single_buy 字段| 字段名 | 类型 | 说明 |
|---|---|---|
| id | int | 限购配置ID,主键 |
| shop_item_id | int | 商品ID,外键 |
| limit_type | tinyint | 限购类型(1:单次购买限制, 2:周期性购买限制) |
| limit_period | tinyint | 限购周期(0:永久, 1:每日, 2:每周, 3:每月, 4:每年) |
| max_quantity | int | 最大购买数量 |
| name | varchar | 限购规则名称 |
| description | text | 限购规则描述 |
| is_active | tinyint | 是否激活 |
| sort_order | int | 排序权重 |
| 字段名 | 类型 | 说明 |
|---|---|---|
| id | int | 记录ID,主键 |
| limit_id | int | 限购配置ID,外键 |
| user_id | int | 用户ID |
| current_count | int | 当前购买计数 |
| last_reset_time | timestamp | 上次重置时间 |
新增字段:
max_single_buy:单次最大购买数量(0表示无限制)// 设置商品单次最多购买5个
$shopItem = ShopItem::find(1);
$shopItem->max_single_buy = 5;
$shopItem->save();
// 创建每日限购规则:每天最多购买3个
ShopPurchaseLimit::create([
'shop_item_id' => 1,
'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
'limit_period' => PURCHASE_LIMIT_PERIOD::DAILY,
'max_quantity' => 3,
'name' => '每日限购',
'description' => '每天最多购买3个',
'is_active' => true,
'sort_order' => 0,
]);
$shopItem = ShopItem::find(1);
$userId = 123;
$quantity = 2;
list($canPurchase, $errorMessage, $remainingQuantity) =
$shopItem->canUserPurchaseWithLimits($userId, $quantity);
if (!$canPurchase) {
echo "购买失败:" . $errorMessage;
} else {
echo "可以购买,剩余可购买数量:" . $remainingQuantity;
}
max_buy 字段的兼容性