ItemChestOpenCostRepository.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Module\GameItems\Repositorys;
  3. use App\Module\GameItems\Models\ItemChestOpenCost;
  4. use UCore\DcatAdmin\Repository\EloquentRepository;
  5. /**
  6. * 宝箱开启消耗配置数据仓库
  7. */
  8. class ItemChestOpenCostRepository extends EloquentRepository
  9. {
  10. /**
  11. * 关联的Eloquent模型类
  12. *
  13. * @var string
  14. */
  15. protected $eloquentClass = ItemChestOpenCost::class;
  16. /**
  17. * 获取指定宝箱的所有激活消耗配置
  18. *
  19. * @param int $chestId 宝箱ID
  20. * @return \Illuminate\Database\Eloquent\Collection
  21. */
  22. public function getActiveByChestId(int $chestId)
  23. {
  24. return $this->eloquentClass::where('chest_id', $chestId)
  25. ->where('is_active', true)
  26. ->get();
  27. }
  28. /**
  29. * 批量更新消耗配置的激活状态
  30. *
  31. * @param array $ids 消耗配置ID数组
  32. * @param bool $isActive 是否激活
  33. * @return int 更新的记录数
  34. */
  35. public function batchUpdateActiveStatus(array $ids, bool $isActive): int
  36. {
  37. return $this->eloquentClass::whereIn('id', $ids)
  38. ->update(['is_active' => $isActive]);
  39. }
  40. /**
  41. * 复制消耗配置到另一个宝箱
  42. *
  43. * @param int $sourceChestId 源宝箱ID
  44. * @param int $targetChestId 目标宝箱ID
  45. * @return int 复制的记录数
  46. */
  47. public function copyToAnotherChest(int $sourceChestId, int $targetChestId): int
  48. {
  49. $sourceCosts = $this->eloquentClass::where('chest_id', $sourceChestId)->get();
  50. $count = 0;
  51. foreach ($sourceCosts as $cost) {
  52. $newCost = $cost->replicate();
  53. $newCost->chest_id = $targetChestId;
  54. $newCost->save();
  55. $count++;
  56. }
  57. return $count;
  58. }
  59. }