物品冻结功能已成功实现,支持统一属性物品和单独属性物品的冻结/解冻操作,采用拆堆模式确保数据一致性。
freezeNormalItem(): 冻结统一属性物品(拆堆模式)freezeUniqueItem(): 冻结单独属性物品unfreezeByLogId(): 通过冻结日志ID解冻物品getAvailableQuantity(): 获取可用数量(排除冻结)batchFreezeItems(): 批量冻结操作getFrozenItems(): 获取冻结物品列表getFreezeStatistics(): 获取冻结统计信息freezeItem(): 冻结物品服务接口unfreezeItem(): 解冻物品服务接口getAvailableQuantity(): 获取可用数量服务接口getFrozenItems(): 获取冻结物品服务接口batchFreezeItems(): 批量冻结服务接口getFreezeStatistics(): 获取冻结统计服务接口consumeNormalItem()和consumeUniqueItem()方法,确保只消耗未冻结的物品addNormalItem()方法,确保查找可堆叠物品时排除冻结的物品use App\Module\GameItems\Services\ItemService;
use App\Module\GameItems\Enums\FREEZE_REASON_TYPE;
// 冻结用户的物品
$result = ItemService::freezeItem(
$userId,
$itemId,
null, // 统一属性物品
20, // 冻结数量
'交易订单冻结',
[
'reason_type' => FREEZE_REASON_TYPE::TRADE_ORDER->value,
'source_id' => $orderId,
'source_type' => 'order'
]
);
// 通过冻结日志ID解冻
$result = ItemService::unfreezeItem($freezeLogId);
// 获取用户可用物品数量(排除冻结的)
$availableQuantity = ItemService::getAvailableQuantity($userId, $itemId);
$items = [
['item_id' => 1001, 'quantity' => 10],
['item_id' => 1002, 'quantity' => 5],
];
$result = ItemService::batchFreezeItems(
$userId,
$items,
'系统批量冻结',
['reason_type' => FREEZE_REASON_TYPE::SYSTEM_FREEZE->value]
);
-- 冻结记录表
CREATE TABLE `kku_item_freeze_logs` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`item_id` int NOT NULL,
`instance_id` int DEFAULT NULL,
`quantity` int NOT NULL,
`action_type` tinyint NOT NULL,
`reason` varchar(255) NOT NULL,
`source_id` int DEFAULT NULL,
`source_type` varchar(50) DEFAULT NULL,
`operator_id` int DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
-- 索引省略...
);
-- 为item_users表添加冻结字段
ALTER TABLE `kku_item_users`
ADD COLUMN `is_frozen` tinyint(1) NOT NULL DEFAULT 0,
ADD COLUMN `frozen_log_id` int DEFAULT NULL,
-- 索引省略...
已创建完整的测试用例覆盖以下场景:
实现完成时间: 2025年06月12日
版本: v1.0
状态: 已完成并测试通过