根据用户需求,对商店系统的限购功能进行全面优化,实现:
app/Module/Shop/Enums/PURCHASE_LIMIT_PERIOD.phpapp/Module/Shop/Enums/PURCHASE_LIMIT_TYPE.phpapp/Module/Shop/Models/ShopPurchaseLimit.phpapp/Module/Shop/Models/ShopUserPurchaseCounter.phpmax_single_buy(单次购买限制)purchaseLimits、activePurchaseLimitscanUserPurchaseWithLimits():统一限购检查updatePurchaseLimitCounters():更新限购计数kku_shop_purchase_limits(限购配置表)
CREATE TABLE `kku_shop_purchase_limits` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`shop_item_id` int unsigned NOT NULL,
`limit_type` tinyint NOT NULL,
`limit_period` tinyint NOT NULL DEFAULT '0',
`max_quantity` int NOT NULL,
`name` varchar(100) NOT NULL,
`description` text,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`sort_order` int NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `shop_purchase_limits_shop_item_id_index` (`shop_item_id`),
CONSTRAINT `shop_purchase_limits_shop_item_id_foreign` FOREIGN KEY (`shop_item_id`) REFERENCES `kku_shop_items` (`id`) ON DELETE CASCADE
);
kku_shop_user_purchase_counters(用户购买计数表)
CREATE TABLE `kku_shop_user_purchase_counters` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`limit_id` int unsigned NOT NULL,
`user_id` int unsigned NOT NULL,
`current_count` int NOT NULL DEFAULT '0',
`last_reset_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `shop_user_purchase_counters_limit_user_unique` (`limit_id`, `user_id`),
CONSTRAINT `shop_user_purchase_counters_limit_id_foreign` FOREIGN KEY (`limit_id`) REFERENCES `kku_shop_purchase_limits` (`id`) ON DELETE CASCADE
);
kku_shop_items表新增字段
ALTER TABLE `kku_shop_items`
ADD COLUMN `max_single_buy` int NOT NULL DEFAULT '0' COMMENT '单次最大购买数量(0表示无限制)' AFTER `max_buy`;
app/Module/Shop/Validators/ShopBuyLimitValidator.phpapp/Module/AppGame/Handler/Shop/BuyHandler.phpapp/Module/Shop/Repositorys/ShopPurchaseLimitRepository.phpapp/Module/Shop/Controllers/ShopPurchaseLimitController.phpapp/Module/Shop/Repositorys/ShopItemRepository.phpapp/Module/Shop/Docs/商店限购功能优化说明.mdmax_buy字段的兼容$shopItem = ShopItem::find(1);
$shopItem->max_single_buy = 5; // 单次最多购买5个
$shopItem->save();
ShopPurchaseLimit::create([
'shop_item_id' => 1,
'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
'limit_period' => PURCHASE_LIMIT_PERIOD::DAILY,
'max_quantity' => 3,
'name' => '每日限购',
'is_active' => true,
]);
list($canPurchase, $errorMessage, $remainingQuantity) =
$shopItem->canUserPurchaseWithLimits($userId, $quantity);
执行以下SQL文件:
app/Module/Shop/Databases/GenerateSql/modify_shop_items_add_single_buy_limit.sqlapp/Module/Shop/Databases/GenerateSql/shop_purchase_limits.sqlapp/Module/Shop/Databases/GenerateSql/shop_user_purchase_counters.sql需要添加限购配置管理的路由和菜单项。
为相关用户分配限购配置管理权限。
本次优化成功实现了商店系统的高级限购功能,提供了灵活、强大且易于管理的限购解决方案。新功能完全向后兼容,不影响现有业务流程,同时为未来的业务需求提供了良好的扩展性。
通过合理的数据库设计、优化的查询逻辑和完善的管理界面,新的限购系统能够满足各种复杂的业务场景需求。