ItemChestConfigRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\GameItems\Repositories;
  3. use App\Module\GameItems\Models\ItemChestConfig;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. /**
  6. * 宝箱配置仓库类
  7. *
  8. * 提供宝箱配置的数据访问方法,专门用于后台管理
  9. */
  10. class ItemChestConfigRepository extends EloquentRepository
  11. {
  12. protected $eloquentClass = ItemChestConfig::class;
  13. /**
  14. * 获取激活的宝箱配置
  15. *
  16. * @param int $itemId 宝箱物品ID
  17. * @return ItemChestV2Config|null
  18. */
  19. public function getActiveConfig(int $itemId): ?ItemChestConfig
  20. {
  21. return ItemChestConfig::with(['consumeGroup', 'rewardGroup', 'conditionGroup'])
  22. ->where('item_id', $itemId)
  23. ->where('is_active', true)
  24. ->first();
  25. }
  26. /**
  27. * 获取所有宝箱配置(包括未激活的)
  28. *
  29. * @param int $itemId 宝箱物品ID
  30. * @return \Illuminate\Database\Eloquent\Collection
  31. */
  32. public function getAllConfigs(int $itemId)
  33. {
  34. return ItemChestConfig::with(['consumeGroup', 'rewardGroup', 'conditionGroup'])
  35. ->where('item_id', $itemId)
  36. ->orderBy('is_active', 'desc')
  37. ->orderBy('created_at', 'desc')
  38. ->get();
  39. }
  40. /**
  41. * 检查宝箱是否已有配置
  42. *
  43. * @param int $itemId 宝箱物品ID
  44. * @return bool
  45. */
  46. public function hasConfig(int $itemId): bool
  47. {
  48. return ItemChestConfig::where('item_id', $itemId)->exists();
  49. }
  50. /**
  51. * 激活指定配置(同时停用其他配置)
  52. *
  53. * @param int $configId 配置ID
  54. * @return bool
  55. */
  56. public function activateConfig(int $configId): bool
  57. {
  58. $config = ItemChestConfig::find($configId);
  59. if (!$config) {
  60. return false;
  61. }
  62. // 停用同一宝箱的其他配置
  63. ItemChestConfig::where('item_id', $config->item_id)
  64. ->where('id', '!=', $configId)
  65. ->update(['is_active' => false]);
  66. // 激活当前配置
  67. return $config->update(['is_active' => true]);
  68. }
  69. /**
  70. * 停用指定配置
  71. *
  72. * @param int $configId 配置ID
  73. * @return bool
  74. */
  75. public function deactivateConfig(int $configId): bool
  76. {
  77. return ItemChestConfig::where('id', $configId)->update(['is_active' => false]);
  78. }
  79. /**
  80. * 获取配置统计信息
  81. *
  82. * @return array
  83. */
  84. public function getConfigStats(): array
  85. {
  86. $total = ItemChestConfig::count();
  87. $active = ItemChestConfig::where('is_active', true)->count();
  88. $complete = ItemChestConfig::whereNotNull('reward_group_id')->where('is_active', true)->count();
  89. return [
  90. 'total' => $total,
  91. 'active' => $active,
  92. 'inactive' => $total - $active,
  93. 'complete' => $complete,
  94. 'incomplete' => $active - $complete,
  95. ];
  96. }
  97. /**
  98. * 获取未配置V2系统的宝箱列表
  99. *
  100. * @return \Illuminate\Database\Eloquent\Collection
  101. */
  102. public function getUnconfiguredChests()
  103. {
  104. return \App\Module\GameItems\Models\Item::where('type', \App\Module\GameItems\Enums\ITEM_TYPE::CHEST)
  105. ->whereNotIn('id', function ($query) {
  106. $query->select('item_id')->from('item_chest_configs');
  107. })
  108. ->select(['id', 'name'])
  109. ->get();
  110. }
  111. /**
  112. * 批量创建配置
  113. *
  114. * @param array $configs 配置数组
  115. * @return bool
  116. */
  117. public function batchCreate(array $configs): bool
  118. {
  119. try {
  120. foreach ($configs as $config) {
  121. ItemChestConfig::create($config);
  122. }
  123. return true;
  124. } catch (\Exception $e) {
  125. \Illuminate\Support\Facades\Log::error('批量创建宝箱配置失败', [
  126. 'configs' => $configs,
  127. 'error' => $e->getMessage()
  128. ]);
  129. return false;
  130. }
  131. }
  132. }