根据用户需求,对商店系统的限购功能进行全面优化,实现:
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(单次购买限制)max_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表示无限制)';
-- 移除已被替代的字段
ALTER TABLE `kku_shop_items`
DROP COLUMN `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需要添加限购配置管理的路由和菜单项。
为相关用户分配限购配置管理权限。
app/Module/Shop/AdminControllers/ShopItemController.php$grid->column('max_buy', '总限购')->display(function ($maxBuy) {
return $maxBuy > 0 ? $maxBuy : '无限制';
});
$grid->column('max_single_buy', '单次限购')->display(function ($maxSingleBuy) {
return $maxSingleBuy > 0 ? $maxSingleBuy : '无限制';
});
$grid->actions(function (Grid\Displayers\Actions $actions) {
$actions->append('<a href="' . admin_url('shop/purchase-limits?shop_item_id=' . $actions->getKey()) . '" class="btn btn-sm btn-outline-primary" title="限购配置">
<i class="fa fa-cog"></i> 限购配置
</a>');
});
app/Module/Shop/AdminControllers/ShopPurchaseLimitController.php#[Resource('shop/purchase-limits', names: 'dcat.admin.shop.purchase-limits')]$grid->column('limit_type', '限购类型')->using(PURCHASE_LIMIT_TYPE::getAll());
$grid->column('limit_period', '限购周期')->using(PURCHASE_LIMIT_PERIOD::getAll());
$grid->column('is_active', '状态')->switch();
$form->saving(function (Form $form) {
if ($form->limit_type == PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE->value
&& $form->limit_period != PURCHASE_LIMIT_PERIOD::PERMANENT->value) {
return $form->response()->error('单次购买限制只能使用永久周期');
}
});
app/Module/Shop/Providers/ShopServiceProvider.php新增路由:
// 商店限购配置路由
$router->resource('shop/purchase-limits', \App\Module\Shop\AdminControllers\ShopPurchaseLimitController::class);
app/Console/Commands/InsertShopPurchaseLimitMenu.phpphp artisan admin:insert-shop-purchase-limit-menudatabase/seeders/ShopPurchaseLimitSeeder.phpphp artisan db:seed --class=ShopPurchaseLimitSeederapp/Module/Shop/Docs/后台管理部署说明.mdshop_purchase_limits.sql - 创建限购配置表shop_user_purchase_counters.sql - 创建用户购买计数表remove_shop_items_max_buy_field.sql - 移除已被替代的max_buy字段ShopItemController - 商品管理增强(移除max_buy相关功能)ShopPurchaseLimitController - 限购配置管理本次优化成功实现了商店系统的高级限购功能,提供了灵活、强大且易于管理的限购解决方案。新功能完全向后兼容,不影响现有业务流程,同时为未来的业务需求提供了良好的扩展性。
通过合理的数据库设计、优化的查询逻辑和完善的管理界面,新的限购系统能够满足各种复杂的业务场景需求。