ConsumeGroupService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Fund\Models\FundConfigModel;
  4. use App\Module\Fund\Models\FundCurrencyModel;
  5. use App\Module\Game\Enums\CONSUME_TYPE;
  6. use App\Module\Game\Models\GameConsumeGroup;
  7. use App\Module\GameItems\Models\Item;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 消耗组服务类
  11. *
  12. * 提供消耗组相关的服务,包括获取消耗组详情、获取消耗组材料等功能
  13. */
  14. class ConsumeGroupService
  15. {
  16. /**
  17. * 获取消耗组详情
  18. *
  19. * @param int|string $consumeGroupCode 消耗组ID或编码
  20. * @return GameConsumeGroup|null 消耗组详情
  21. */
  22. public static function getConsumeGroup($consumeGroupCode): ?GameConsumeGroup
  23. {
  24. try {
  25. // 获取消耗组
  26. $consumeGroup = is_numeric($consumeGroupCode)
  27. ? GameConsumeGroup::with('consumeItems')->find($consumeGroupCode)
  28. : GameConsumeGroup::with('consumeItems')->where('code', $consumeGroupCode)->first();
  29. return $consumeGroup;
  30. } catch (\Exception $e) {
  31. Log::error('获取消耗组详情失败', [
  32. 'consume_group' => $consumeGroupCode,
  33. 'error' => $e->getMessage()
  34. ]);
  35. return null;
  36. }
  37. }
  38. /**
  39. * 获取消耗组材料
  40. *
  41. * @param int|string $consumeGroupCode 消耗组ID或编码
  42. * @return array 消耗组材料
  43. */
  44. public static function getConsumeMaterials($consumeGroupCode): array
  45. {
  46. try {
  47. // 获取消耗组
  48. $consumeGroup = self::getConsumeGroup($consumeGroupCode);
  49. if (!$consumeGroup || $consumeGroup->consumeItems->isEmpty()) {
  50. return [];
  51. }
  52. $materials = [];
  53. // 处理每个消耗项
  54. foreach ($consumeGroup->consumeItems as $item) {
  55. if ($item->consume_type == CONSUME_TYPE::ITEM->value) {
  56. // 获取物品信息
  57. $itemInfo = Item::find($item->target_id);
  58. $materials[] = [
  59. 'type' => $item->consume_type,
  60. 'item_id' => $item->target_id,
  61. 'item_name' => $itemInfo ? $itemInfo->name : "物品 {$item->target_id}",
  62. 'amount' => $item->quantity
  63. ];
  64. } elseif ($item->consume_type == CONSUME_TYPE::CURRENCY->value) {
  65. // 获取货币信息
  66. $currencyInfo = FundCurrencyModel::find($item->target_id);
  67. $materials[] = [
  68. 'type' => $item->consume_type,
  69. 'currency_id' => $item->target_id,
  70. 'currency_name' => $currencyInfo ? $currencyInfo->name : "货币 {$item->target_id}",
  71. 'amount' => $item->quantity
  72. ];
  73. } elseif ($item->consume_type == CONSUME_TYPE::FUND->value) {
  74. // 获取代币账户信息
  75. $fundInfo = FundConfigModel::find($item->target_id);
  76. $materials[] = [
  77. 'type' => $item->consume_type,
  78. 'fund_id' => $item->target_id,
  79. 'fund_name' => $fundInfo ? $fundInfo->name : "代币账户 {$item->target_id}",
  80. 'amount' => $item->quantity
  81. ];
  82. }
  83. }
  84. return $materials;
  85. } catch (\Exception $e) {
  86. Log::error('获取消耗组材料失败', [
  87. 'consume_group' => $consumeGroupCode,
  88. 'error' => $e->getMessage()
  89. ]);
  90. return [];
  91. }
  92. }
  93. }