', $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; } }