CurrencyRewardProcessor.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Module\Game\Logics\RewardProcessors;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use Exception;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 币种奖励处理器
  8. *
  9. * 处理币种(CURRENCY)类型的奖励发放
  10. */
  11. class CurrencyRewardProcessor
  12. {
  13. /**
  14. * 处理币种奖励(CURRENCY)
  15. *
  16. * @param int $userId 用户ID
  17. * @param RewardItemDto $item 奖励项
  18. * @param string $sourceType 来源类型
  19. * @param int $sourceId 来源ID
  20. * @return void
  21. * @throws Exception
  22. */
  23. public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
  24. {
  25. try {
  26. // 对于币种奖励,需要先根据币种ID找到对应的账户种类ID
  27. // 这里需要查询 fund_config 表,找到对应币种的账户种类
  28. $fundConfig = \App\Module\Fund\Models\FundConfigModel::where('currency_id', $item->targetId)->first();
  29. if (!$fundConfig) {
  30. throw new Exception("找不到币种ID为 {$item->targetId} 的账户配置");
  31. }
  32. // 使用找到的账户种类ID来发放奖励
  33. $result = \App\Module\Fund\Logic\User::handle(
  34. $userId,
  35. $fundConfig->id, // fund_config_id
  36. $item->quantity, // 金额
  37. \App\Module\Fund\Enums\LOG_TYPE::TRADE, // 日志类型
  38. $sourceId, // 关联ID,使用来源ID
  39. "奖励发放:币种奖励 - {$sourceType}#{$sourceId}"
  40. );
  41. if (is_string($result)) {
  42. throw new Exception("币种奖励发放失败: " . $result);
  43. }
  44. Log::info("币种奖励发放成功", [
  45. 'userId' => $userId,
  46. 'currencyId' => $item->targetId,
  47. 'fundConfigId' => $fundConfig->id,
  48. 'amount' => $item->quantity,
  49. 'sourceType' => $sourceType,
  50. 'sourceId' => $sourceId
  51. ]);
  52. } catch (Exception $e) {
  53. Log::error("币种奖励发放失败", [
  54. 'userId' => $userId,
  55. 'currencyId' => $item->targetId,
  56. 'amount' => $item->quantity,
  57. 'sourceType' => $sourceType,
  58. 'sourceId' => $sourceId,
  59. 'error' => $e->getMessage()
  60. ]);
  61. throw new Exception("币种奖励发放失败: " . $e->getMessage());
  62. }
  63. }
  64. }