| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Module\Game\Logics\RewardProcessors;
- use App\Module\Game\Dtos\RewardItemDto;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 币种奖励处理器
- *
- * 处理币种(CURRENCY)类型的奖励发放
- */
- class CurrencyRewardProcessor
- {
- /**
- * 处理币种奖励(CURRENCY)
- *
- * @param int $userId 用户ID
- * @param RewardItemDto $item 奖励项
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return void
- * @throws Exception
- */
- public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
- {
- try {
- // 对于币种奖励,需要先根据币种ID找到对应的账户种类ID
- // 这里需要查询 fund_config 表,找到对应币种的账户种类
- $fundConfig = \App\Module\Fund\Models\FundConfigModel::where('currency_id', $item->targetId)->first();
- if (!$fundConfig) {
- throw new Exception("找不到币种ID为 {$item->targetId} 的账户配置");
- }
- // 使用找到的账户种类ID来发放奖励
- $result = \App\Module\Fund\Logic\User::handle(
- $userId,
- $fundConfig->id, // fund_config_id
- $item->quantity, // 金额
- \App\Module\Fund\Enums\LOG_TYPE::TRADE, // 日志类型
- $sourceId, // 关联ID,使用来源ID
- "奖励发放:币种奖励 - {$sourceType}#{$sourceId}"
- );
- if (is_string($result)) {
- throw new Exception("币种奖励发放失败: " . $result);
- }
- Log::info("币种奖励发放成功", [
- 'userId' => $userId,
- 'currencyId' => $item->targetId,
- 'fundConfigId' => $fundConfig->id,
- 'amount' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId
- ]);
- } catch (Exception $e) {
- Log::error("币种奖励发放失败", [
- 'userId' => $userId,
- 'currencyId' => $item->targetId,
- 'amount' => $item->quantity,
- 'sourceType' => $sourceType,
- 'sourceId' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- throw new Exception("币种奖励发放失败: " . $e->getMessage());
- }
- }
- }
|