where('item_id', $itemId) ->where('is_active', true) ->first(); } /** * 获取所有宝箱配置(包括未激活的) * * @param int $itemId 宝箱物品ID * @return \Illuminate\Database\Eloquent\Collection */ public function getAllConfigs(int $itemId) { return ItemChestConfig::with(['consumeGroup', 'rewardGroup', 'conditionGroup']) ->where('item_id', $itemId) ->orderBy('is_active', 'desc') ->orderBy('created_at', 'desc') ->get(); } /** * 检查宝箱是否已有配置 * * @param int $itemId 宝箱物品ID * @return bool */ public function hasConfig(int $itemId): bool { return ItemChestConfig::where('item_id', $itemId)->exists(); } /** * 激活指定配置(同时停用其他配置) * * @param int $configId 配置ID * @return bool */ public function activateConfig(int $configId): bool { $config = ItemChestConfig::find($configId); if (!$config) { return false; } // 停用同一宝箱的其他配置 ItemChestConfig::where('item_id', $config->item_id) ->where('id', '!=', $configId) ->update(['is_active' => false]); // 激活当前配置 return $config->update(['is_active' => true]); } /** * 停用指定配置 * * @param int $configId 配置ID * @return bool */ public function deactivateConfig(int $configId): bool { return ItemChestConfig::where('id', $configId)->update(['is_active' => false]); } /** * 获取配置统计信息 * * @return array */ public function getConfigStats(): array { $total = ItemChestConfig::count(); $active = ItemChestConfig::where('is_active', true)->count(); $complete = ItemChestConfig::whereNotNull('reward_group_id')->where('is_active', true)->count(); return [ 'total' => $total, 'active' => $active, 'inactive' => $total - $active, 'complete' => $complete, 'incomplete' => $active - $complete, ]; } /** * 获取未配置V2系统的宝箱列表 * * @return \Illuminate\Database\Eloquent\Collection */ public function getUnconfiguredChests() { return \App\Module\GameItems\Models\Item::where('type', \App\Module\GameItems\Enums\ITEM_TYPE::CHEST) ->whereNotIn('id', function ($query) { $query->select('item_id')->from('item_chest_configs'); }) ->select(['id', 'name']) ->get(); } /** * 批量创建配置 * * @param array $configs 配置数组 * @return bool */ public function batchCreate(array $configs): bool { try { foreach ($configs as $config) { ItemChestConfig::create($config); } return true; } catch (\Exception $e) { \Illuminate\Support\Facades\Log::error('批量创建宝箱配置失败', [ 'configs' => $configs, 'error' => $e->getMessage() ]); return false; } } }