| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Module\GameItems\Repositories;
- use App\Module\GameItems\Models\ItemChestConfig;
- use Dcat\Admin\Repositories\EloquentRepository;
- /**
- * 宝箱配置仓库类
- *
- * 提供宝箱配置的数据访问方法,专门用于后台管理
- */
- class ItemChestConfigRepository extends EloquentRepository
- {
- protected $eloquentClass = ItemChestConfig::class;
- /**
- * 获取激活的宝箱配置
- *
- * @param int $itemId 宝箱物品ID
- * @return ItemChestV2Config|null
- */
- public function getActiveConfig(int $itemId): ?ItemChestConfig
- {
- return ItemChestConfig::with(['consumeGroup', 'rewardGroup', 'conditionGroup'])
- ->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;
- }
- }
- }
|