| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- namespace App\Module\Game\Logics\UserLogCollectors;
- use App\Module\Game\Services\UserLogService;
- use Illuminate\Support\Facades\Log;
- /**
- * 用户日志收集器基类
- *
- * 为各个模块的日志收集器提供基础功能
- */
- abstract class BaseLogCollector
- {
- /**
- * 收集器名称
- *
- * @var string
- */
- protected string $collectorName;
- /**
- * 源表名
- *
- * @var string
- */
- protected string $sourceTable;
- /**
- * 源类型
- *
- * @var string
- */
- protected string $sourceType;
- /**
- * 最大处理记录数
- *
- * @var int
- */
- protected int $maxRecords = 1000;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->collectorName = static::class;
- }
- /**
- * 收集日志
- *
- * @return int 处理的记录数
- */
- public function collect(): int
- {
- try {
- // 优先使用时间线处理,如果没有时间戳记录则回退到ID处理
- $lastProcessedTimestamp = $this->getLastProcessedTimestamp();
- if ($lastProcessedTimestamp > 0) {
- return $this->collectByTimeline();
- } else {
- return $this->collectByIdWithTimelineUpgrade();
- }
- } catch (\Exception $e) {
- Log::error("日志收集失败", [
- 'collector' => $this->collectorName,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 0;
- }
- }
- /**
- * 按时间线收集日志
- *
- * @return int 处理的记录数
- */
- private function collectByTimeline(): int
- {
- $lastProcessedTimestamp = $this->getLastProcessedTimestamp();
- $records = $this->getNewRecordsByTime($lastProcessedTimestamp);
- if ($records->isEmpty()) {
- return 0;
- }
- $processedCount = 0;
- $userLogs = [];
- $maxTimestamp = $lastProcessedTimestamp;
- foreach ($records as $record) {
- try {
- $userLogData = $this->convertToUserLog($record);
- if ($userLogData) {
- $userLogs[] = $userLogData;
- $processedCount++;
- }
- $recordTimestamp = $this->getRecordTimestamp($record);
- if ($recordTimestamp > $maxTimestamp) {
- $maxTimestamp = $recordTimestamp;
- }
- } catch (\Exception $e) {
- Log::error("转换日志记录失败", [
- 'collector' => $this->collectorName,
- 'record_id' => $record->id ?? null,
- 'error' => $e->getMessage()
- ]);
- }
- }
- if (!empty($userLogs)) {
- UserLogService::batchLog($userLogs);
- }
- // 不再需要手动更新时间戳,进度通过user_logs表自动追踪
- Log::info("时间线日志收集完成", [
- 'collector' => $this->collectorName,
- 'processed_count' => $processedCount,
- 'last_timestamp' => $maxTimestamp
- ]);
- return $processedCount;
- }
- /**
- * 按ID收集日志并升级到时间线处理
- *
- * @return int 处理的记录数
- */
- private function collectByIdWithTimelineUpgrade(): int
- {
- $lastProcessedId = $this->getLastProcessedId();
- $records = $this->getNewRecords($lastProcessedId);
- if ($records->isEmpty()) {
- return 0;
- }
- $processedCount = 0;
- $userLogs = [];
- $maxId = $lastProcessedId;
- $maxTimestamp = 0;
- foreach ($records as $record) {
- try {
- $userLogData = $this->convertToUserLog($record);
- if ($userLogData) {
- $userLogs[] = $userLogData;
- $processedCount++;
- }
- if ($record->id > $maxId) {
- $maxId = $record->id;
- }
- $recordTimestamp = $this->getRecordTimestamp($record);
- if ($recordTimestamp > $maxTimestamp) {
- $maxTimestamp = $recordTimestamp;
- }
- } catch (\Exception $e) {
- Log::error("转换日志记录失败", [
- 'collector' => $this->collectorName,
- 'record_id' => $record->id ?? null,
- 'error' => $e->getMessage()
- ]);
- }
- }
- if (!empty($userLogs)) {
- UserLogService::batchLog($userLogs);
- }
- // 不再需要手动更新进度,进度通过user_logs表自动追踪
- Log::info("ID日志收集完成并升级到时间线", [
- 'collector' => $this->collectorName,
- 'processed_count' => $processedCount,
- 'last_id' => $maxId,
- 'last_timestamp' => $maxTimestamp
- ]);
- return $processedCount;
- }
- /**
- * 获取新的记录(子类实现)
- *
- * @param int $lastProcessedId 上次处理的最大ID
- * @return \Illuminate\Database\Eloquent\Collection
- */
- abstract protected function getNewRecords(int $lastProcessedId);
- /**
- * 按时间获取新的记录(子类实现)
- *
- * @param int $lastProcessedTimestamp 上次处理的最大时间戳
- * @return \Illuminate\Database\Eloquent\Collection
- */
- abstract protected function getNewRecordsByTime(int $lastProcessedTimestamp);
- /**
- * 获取记录的时间戳(子类实现)
- *
- * @param mixed $record 原始记录
- * @return int 时间戳
- */
- abstract protected function getRecordTimestamp($record): int;
- /**
- * 根据记录ID获取原始记录的时间戳(子类实现)
- *
- * @param int $recordId 记录ID
- * @return int 时间戳
- */
- abstract protected function getOriginalRecordTimestamp(int $recordId): int;
- /**
- * 公共方法:按时间获取新的记录
- *
- * @param int $lastProcessedTimestamp 上次处理的最大时间戳
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function getNewRecordsByTimePublic(int $lastProcessedTimestamp)
- {
- return $this->getNewRecordsByTime($lastProcessedTimestamp);
- }
- /**
- * 公共方法:获取记录的时间戳
- *
- * @param mixed $record 原始记录
- * @return int 时间戳
- */
- public function getRecordTimestampPublic($record): int
- {
- return $this->getRecordTimestamp($record);
- }
- /**
- * 公共方法:转换记录为用户日志数据
- *
- * @param mixed $record 原始记录
- * @return array|null 用户日志数据,null表示跳过
- */
- public function convertToUserLogPublic($record): ?array
- {
- return $this->convertToUserLog($record);
- }
- /**
- * 转换记录为用户日志数据(子类实现)
- *
- * @param mixed $record 原始记录
- * @return array|null 用户日志数据,null表示跳过
- */
- abstract protected function convertToUserLog($record): ?array;
- /**
- * 获取上次处理的最大ID
- * 从user_logs表中查询该收集器最后处理的记录ID
- *
- * @return int
- */
- protected function getLastProcessedId(): int
- {
- try {
- $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
- ->where('source_type', $this->sourceType)
- ->orderBy('source_id', 'desc')
- ->first();
- return $lastLog ? $lastLog->source_id : 0;
- } catch (\Exception $e) {
- Log::error("获取最后处理ID失败", [
- 'collector' => $this->collectorName,
- 'source_table' => $this->sourceTable,
- 'error' => $e->getMessage()
- ]);
- return 0;
- }
- }
- /**
- * 更新最后处理的ID
- * 不再需要手动更新,因为进度通过user_logs表自动追踪
- *
- * @param int $id
- * @return void
- */
- protected function updateLastProcessedId(int $id): void
- {
- // 不再需要手动更新,进度通过user_logs表自动追踪
- // 这个方法保留是为了兼容性
- }
- /**
- * 获取上次处理的最大时间戳
- * 从user_logs表中查询该收集器最后处理的记录的原始时间戳
- *
- * @return int
- */
- protected function getLastProcessedTimestamp(): int
- {
- try {
- $lastLog = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
- ->where('source_type', $this->sourceType)
- ->whereNotNull('original_time')
- ->orderBy('original_time', 'desc')
- ->first();
- if (!$lastLog || !$lastLog->original_time) {
- return 0;
- }
- // 直接返回原始时间戳
- return $lastLog->original_time->timestamp;
- } catch (\Exception $e) {
- Log::error("获取最后处理时间戳失败", [
- 'collector' => $this->collectorName,
- 'source_table' => $this->sourceTable,
- 'error' => $e->getMessage()
- ]);
- return 0;
- }
- }
- /**
- * 更新最后处理的时间戳
- * 不再需要手动更新,因为进度通过user_logs表自动追踪
- *
- * @param int $timestamp
- * @return void
- */
- protected function updateLastProcessedTimestamp(int $timestamp): void
- {
- // 不再需要手动更新,进度通过user_logs表自动追踪
- // 这个方法保留是为了兼容性
- }
- /**
- * 获取最后处理ID的缓存键
- *
- * @return string
- */
- protected function getLastProcessedIdCacheKey(): string
- {
- return "user_log_collector:last_processed_id:" . $this->sourceTable;
- }
- /**
- * 获取最后处理时间戳的缓存键
- *
- * @return string
- */
- protected function getLastProcessedTimestampCacheKey(): string
- {
- return "user_log_collector:last_processed_timestamp:" . $this->sourceTable;
- }
- /**
- * 创建用户日志数据数组
- *
- * @param int $userId 用户ID
- * @param string $message 日志消息
- * @param int $sourceId 来源记录ID
- * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
- * @return array
- */
- protected function createUserLogData(int $userId, string $message, int $sourceId, ?string $originalTime = null): array
- {
- $now = now()->toDateTimeString();
- $originalTime = $originalTime ?? $now;
- return [
- 'user_id' => $userId,
- 'message' => $message,
- 'source_type' => $this->sourceType,
- 'source_id' => $sourceId,
- 'source_table' => $this->sourceTable,
- 'original_time' => $originalTime, // 原始业务时间
- 'collected_at' => $now, // 收集时间
- 'created_at' => $now, // 兼容字段
- ];
- }
- /**
- * 获取收集器名称
- *
- * @return string
- */
- public function getCollectorName(): string
- {
- return $this->collectorName;
- }
- /**
- * 获取源表名
- *
- * @return string
- */
- public function getSourceTable(): string
- {
- return $this->sourceTable;
- }
- /**
- * 获取源类型
- *
- * @return string
- */
- public function getSourceType(): string
- {
- return $this->sourceType;
- }
- /**
- * 检查是否为重复记录
- *
- * @param string $sourceTable 源表名
- * @param int $sourceId 源记录ID
- * @return bool
- */
- protected function isDuplicateRecord(string $sourceTable, int $sourceId): bool
- {
- try {
- $exists = \App\Module\Game\Models\UserLog::where('source_type', $this->sourceType)
- ->where('source_table', $sourceTable)
- ->where('source_id', $sourceId)
- ->exists();
- return $exists;
- } catch (\Exception $e) {
- Log::error("检查重复记录失败", [
- 'collector' => $this->collectorName,
- 'source_table' => $sourceTable,
- 'source_id' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- }
|