| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- use App\Module\Activity\Models\Activity;
- use App\Module\Task\Models\Task;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use Illuminate\Support\Facades\Log;
- /**
- * 奖励来源解析服务
- *
- * 根据source_type和source_id解析具体的业务来源信息
- */
- class RewardSourceResolver
- {
- /**
- * 解析奖励来源信息
- *
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return array 包含详细信息的数组
- */
- public static function resolve(string $sourceType, int $sourceId): array
- {
- try {
- // 首先获取枚举信息
- $typeInfo = REWARD_SOURCE_TYPE::getTypeInfo($sourceType);
- switch ($sourceType) {
- case REWARD_SOURCE_TYPE::TASK->value:
- return self::resolveTaskSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::ACTIVITY->value:
- return self::resolveActivitySource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::FARM_INIT->value:
- case REWARD_SOURCE_TYPE::FARM_HARVEST->value:
- case REWARD_SOURCE_TYPE::FARM_PLANT->value:
- return self::resolveFarmSource($sourceType, $sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::URSPROMOTION_REGISTER->value:
- case REWARD_SOURCE_TYPE::URSPROMOTION_REWARD->value:
- case REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value:
- return self::resolvePromotionSource($sourceType, $sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::SIGN_IN->value:
- return self::resolveSignInSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::ACHIEVEMENT->value:
- return self::resolveAchievementSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::LEVEL->value:
- return self::resolveLevelSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::CHEST->value:
- return self::resolveChestSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::CRAFT->value:
- return self::resolveCraftSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::SHOP_PURCHASE->value:
- return self::resolveShopSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::DAILY_LOGIN->value:
- return self::resolveDailyLoginSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::INVITE_FRIEND->value:
- return self::resolveInviteSource($sourceId, $typeInfo);
- case REWARD_SOURCE_TYPE::SYSTEM->value:
- case REWARD_SOURCE_TYPE::TEST->value:
- return self::resolveSystemSource($sourceType, $sourceId, $typeInfo);
- default:
- return self::getUnknownSource($sourceType, $sourceId);
- }
- } catch (\Exception $e) {
- Log::warning('解析奖励来源失败', [
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- return self::getErrorSource($sourceType, $sourceId, $e->getMessage());
- }
- }
- /**
- * 解析任务来源
- */
- private static function resolveTaskSource(int $taskId, array $typeInfo): array
- {
- // 由于Task模型可能不存在,先返回基础信息
- return [
- 'type' => $typeInfo['name'],
- 'name' => "任务奖励 (ID: {$taskId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/{$taskId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'task_id' => $taskId,
- 'source_type' => REWARD_SOURCE_TYPE::TASK->value
- ]
- ];
- }
- /**
- * 解析活动来源
- */
- private static function resolveActivitySource(int $activityId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "活动奖励 (ID: {$activityId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/{$activityId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'activity_id' => $activityId,
- 'source_type' => REWARD_SOURCE_TYPE::ACTIVITY->value
- ]
- ];
- }
- /**
- * 解析农场来源
- */
- private static function resolveFarmSource(string $sourceType, int $sourceId, array $typeInfo): array
- {
- // 根据不同的农场操作类型,sourceId可能指向不同的表
- if ($sourceType === REWARD_SOURCE_TYPE::FARM_INIT->value) {
- // 农场初始化,sourceId通常是用户ID
- return [
- 'type' => $typeInfo['name'],
- 'name' => "用户农场初始化 (用户ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/users/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'operation_type' => $sourceType,
- 'target_user_id' => $sourceId,
- 'source_type' => $sourceType
- ]
- ];
- }
- // 其他农场操作
- return [
- 'type' => $typeInfo['name'],
- 'name' => "农场操作奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'],
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'operation_type' => $sourceType,
- 'operation_id' => $sourceId,
- 'source_type' => $sourceType
- ]
- ];
- }
- /**
- * 解析推广来源
- */
- private static function resolvePromotionSource(string $sourceType, int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "URS推广奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/user-mappings/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'mapping_id' => $sourceId,
- 'source_type' => $sourceType
- ]
- ];
- }
- /**
- * 解析签到来源
- */
- private static function resolveSignInSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "每日签到奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/logs/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'sign_in_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::SIGN_IN->value
- ]
- ];
- }
- /**
- * 解析成就来源
- */
- private static function resolveAchievementSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "成就奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'achievement_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::ACHIEVEMENT->value
- ]
- ];
- }
- /**
- * 解析等级来源
- */
- private static function resolveLevelSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "等级奖励 (等级: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'],
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'level' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::LEVEL->value
- ]
- ];
- }
- /**
- * 解析宝箱来源
- */
- private static function resolveChestSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "宝箱奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'chest_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::CHEST->value
- ]
- ];
- }
- /**
- * 解析合成来源
- */
- private static function resolveCraftSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "合成奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'craft_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::CRAFT->value
- ]
- ];
- }
- /**
- * 解析商店来源
- */
- private static function resolveShopSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "商店购买奖励 (订单ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/orders/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'order_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::SHOP_PURCHASE->value
- ]
- ];
- }
- /**
- * 解析每日登录来源
- */
- private static function resolveDailyLoginSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "每日登录奖励 (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/logs/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'login_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::DAILY_LOGIN->value
- ]
- ];
- }
- /**
- * 解析邀请好友来源
- */
- private static function resolveInviteSource(int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "邀请好友奖励 (邀请ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'] ? "{$typeInfo['admin_link']}/invites/{$sourceId}" : null,
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'invite_id' => $sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::INVITE_FRIEND->value
- ]
- ];
- }
- /**
- * 解析系统来源
- */
- private static function resolveSystemSource(string $sourceType, int $sourceId, array $typeInfo): array
- {
- return [
- 'type' => $typeInfo['name'],
- 'name' => "{$typeInfo['name']} (ID: {$sourceId})",
- 'description' => $typeInfo['description'],
- 'link' => $typeInfo['admin_link'],
- 'status' => 'found',
- 'category' => $typeInfo['category'],
- 'extra' => [
- 'operation_id' => $sourceId,
- 'source_type' => $sourceType
- ]
- ];
- }
- /**
- * 获取未知来源信息
- */
- private static function getUnknownSource(string $sourceType, int $sourceId): array
- {
- return [
- 'type' => '未知来源',
- 'name' => "未知来源类型: {$sourceType}",
- 'description' => "来源ID: {$sourceId}",
- 'link' => null,
- 'status' => 'unknown',
- 'extra' => [
- 'raw_source_type' => $sourceType,
- 'raw_source_id' => $sourceId
- ]
- ];
- }
- /**
- * 获取错误来源信息
- */
- private static function getErrorSource(string $sourceType, int $sourceId, string $error): array
- {
- return [
- 'type' => '解析错误',
- 'name' => "解析失败: {$sourceType}",
- 'description' => "来源ID: {$sourceId}, 错误: {$error}",
- 'link' => null,
- 'status' => 'error',
- 'extra' => [
- 'error_message' => $error,
- 'raw_source_type' => $sourceType,
- 'raw_source_id' => $sourceId
- ]
- ];
- }
- /**
- * 获取格式化的显示文本
- *
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @param bool $includeLink 是否包含链接
- * @return string 格式化的显示文本
- */
- public static function getDisplayText(string $sourceType, int $sourceId, bool $includeLink = false): string
- {
- $info = self::resolve($sourceType, $sourceId);
-
- $text = "{$info['type']}: {$info['name']}";
-
- if ($includeLink && $info['link']) {
- return "<a href='{$info['link']}' target='_blank'>{$text}</a>";
- }
-
- return $text;
- }
- /**
- * 获取简短的显示文本
- *
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return string 简短的显示文本
- */
- public static function getShortDisplayText(string $sourceType, int $sourceId): string
- {
- $info = self::resolve($sourceType, $sourceId);
- return $info['name'];
- }
- }
|