FundConfigRewardProcessor.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * 处理账户种类(FUND_CONFIG)类型的奖励发放
  10. */
  11. class FundConfigRewardProcessor
  12. {
  13. /**
  14. * 处理账户种类奖励(FUND_CONFIG)
  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. // 使用Fund模块的User类来处理资金变更
  27. // target_id 是 fund_config 表的 id(账户种类ID)
  28. $result = \App\Module\Fund\Logic\User::handle(
  29. $userId,
  30. $item->targetId, // fund_config_id
  31. $item->quantity, // 金额
  32. \App\Module\Fund\Enums\LOG_TYPE::TRADE, // 日志类型为贸易
  33. $sourceId, // 关联ID,使用来源ID
  34. "奖励发放:账户种类奖励 - {$sourceType}#{$sourceId}"
  35. );
  36. if (is_string($result)) {
  37. throw new Exception("账户种类奖励发放失败: " . $result);
  38. }
  39. Log::info("账户种类奖励发放成功", [
  40. 'userId' => $userId,
  41. 'fundConfigId' => $item->targetId,
  42. 'amount' => $item->quantity,
  43. 'sourceType' => $sourceType,
  44. 'sourceId' => $sourceId
  45. ]);
  46. } catch (Exception $e) {
  47. Log::error("账户种类奖励发放失败", [
  48. 'userId' => $userId,
  49. 'fundConfigId' => $item->targetId,
  50. 'amount' => $item->quantity,
  51. 'sourceType' => $sourceType,
  52. 'sourceId' => $sourceId,
  53. 'error' => $e->getMessage()
  54. ]);
  55. throw new Exception("账户种类奖励发放失败: " . $e->getMessage());
  56. }
  57. }
  58. }