| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Module\Game\Logics\UserLogCollectors;
- use App\Module\Farm\Models\FarmUpgradeLog;
- /**
- * 农场升级日志收集器
- *
- * 收集farm_upgrade_logs表的新增记录,转换为用户友好的日志消息
- */
- class FarmUpgradeLogCollector extends BaseLogCollector
- {
- /**
- * 源表名
- *
- * @var string
- */
- protected string $sourceTable = 'farm_upgrade_logs';
- /**
- * 源类型
- *
- * @var string
- */
- protected string $sourceType = 'farm';
- /**
- * 获取新的记录
- *
- * @param int $lastProcessedId 上次处理的最大ID
- * @return \Illuminate\Database\Eloquent\Collection
- */
- protected function getNewRecords(int $lastProcessedId)
- {
- return FarmUpgradeLog::where('id', '>', $lastProcessedId)
- ->orderBy('id')
- ->limit($this->maxRecords)
- ->get();
- }
- /**
- * 获取源表的最大ID
- *
- * 重写父类方法,使用模型查询以获得更好的性能
- *
- * @return int
- */
- public function getSourceTableMaxId(): int
- {
- return FarmUpgradeLog::max('id') ?: 0;
- }
- /**
- * 转换记录为用户日志数据
- *
- * @param FarmUpgradeLog $record 农场升级记录
- * @return array|null 用户日志数据,null表示跳过
- */
- protected function convertToUserLog($record): ?array
- {
- try {
- $message = $this->buildUpgradeMessage($record);
- return $this->createUserLogData(
- $record->user_id,
- $message,
- $record->id,
- $record->created_at
- );
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error("转换农场升级日志失败", [
- 'record_id' => $record->id,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 构建升级消息
- *
- * @param FarmUpgradeLog $record
- * @return string
- */
- private function buildUpgradeMessage(FarmUpgradeLog $record): string
- {
- // 使用UPGRADE_TYPE枚举判断升级类型
- switch ($record->upgrade_type) {
- case \App\Module\Farm\Enums\UPGRADE_TYPE::HOUSE->value:
- return "房屋升级到{$record->new_level}级";
- case \App\Module\Farm\Enums\UPGRADE_TYPE::LAND->value:
- $oldLandType = $this->getLandTypeName($record->old_level);
- $newLandType = $this->getLandTypeName($record->new_level);
- return "土地{$record->target_id}从{$oldLandType}升级为{$newLandType}";
- default:
- return "升级类型{$record->upgrade_type}从{$record->old_level}级升级到{$record->new_level}级";
- }
- }
- /**
- * 静态缓存:土地类型名称映射
- *
- * @var array|null
- */
- private static ?array $landTypeNames = null;
- /**
- * 获取土地类型名称
- *
- * @param int $typeId 土地类型ID
- * @return string
- */
- private function getLandTypeName(int $typeId): string
- {
- // 初始化静态缓存
- if (self::$landTypeNames === null) {
- $this->initLandTypeNamesCache();
- }
- return self::$landTypeNames[$typeId] ?? "未知土地类型{$typeId}";
- }
- /**
- * 初始化土地类型名称缓存
- *
- * @return void
- */
- private function initLandTypeNamesCache(): void
- {
- try {
- // 从数据库读取所有土地类型
- $landTypes = \App\Module\Farm\Models\FarmLandType::select('id', 'name')
- ->get()
- ->pluck('name', 'id')
- ->toArray();
- self::$landTypeNames = $landTypes;
- } catch (\Exception $e) {
- // 如果数据库查询失败,使用默认值
- self::$landTypeNames = [
- 1 => '普通土地',
- 2 => '红土地',
- 3 => '黑土地',
- 4 => '金色特殊土地',
- 5 => '蓝色特殊土地',
- 6 => '紫色特殊土地',
- ];
- }
- }
- /**
- * 清除土地类型名称缓存(用于测试或数据更新后)
- *
- * @return void
- */
- public static function clearLandTypeNamesCache(): void
- {
- self::$landTypeNames = null;
- }
- }
|