| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace App\Module\Game\Logics\UserLogCollectors;
- use App\Module\Game\Services\UserLogService;
- use Illuminate\Support\Facades\Cache;
- 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
- $lastProcessedId = $this->getLastProcessedId();
- // 获取新的记录
- $records = $this->getNewRecords($lastProcessedId);
- if ($records->isEmpty()) {
- return 0;
- }
- $processedCount = 0;
- $userLogs = [];
- foreach ($records as $record) {
- try {
- // 转换记录为用户日志
- $userLogData = $this->convertToUserLog($record);
-
- if ($userLogData) {
- $userLogs[] = $userLogData;
- $processedCount++;
- }
- // 更新最后处理的ID
- $this->updateLastProcessedId($record->id);
- } catch (\Exception $e) {
- Log::error("转换日志记录失败", [
- 'collector' => $this->collectorName,
- 'record_id' => $record->id ?? null,
- 'error' => $e->getMessage()
- ]);
- }
- }
- // 批量保存用户日志
- if (!empty($userLogs)) {
- UserLogService::batchLog($userLogs);
- }
- Log::info("日志收集完成", [
- 'collector' => $this->collectorName,
- 'processed_count' => $processedCount,
- 'total_records' => $records->count()
- ]);
- return $processedCount;
- } catch (\Exception $e) {
- Log::error("日志收集失败", [
- 'collector' => $this->collectorName,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 0;
- }
- }
- /**
- * 获取新的记录(子类实现)
- *
- * @param int $lastProcessedId 上次处理的最大ID
- * @return \Illuminate\Database\Eloquent\Collection
- */
- abstract protected function getNewRecords(int $lastProcessedId);
- /**
- * 转换记录为用户日志数据(子类实现)
- *
- * @param mixed $record 原始记录
- * @return array|null 用户日志数据,null表示跳过
- */
- abstract protected function convertToUserLog($record): ?array;
- /**
- * 获取上次处理的最大ID
- *
- * @return int
- */
- protected function getLastProcessedId(): int
- {
- $cacheKey = $this->getLastProcessedIdCacheKey();
- return Cache::get($cacheKey, 0);
- }
- /**
- * 更新最后处理的ID
- *
- * @param int $id
- * @return void
- */
- protected function updateLastProcessedId(int $id): void
- {
- $cacheKey = $this->getLastProcessedIdCacheKey();
- Cache::put($cacheKey, $id, 86400); // 缓存24小时
- }
- /**
- * 获取最后处理ID的缓存键
- *
- * @return string
- */
- protected function getLastProcessedIdCacheKey(): string
- {
- return "user_log_collector:last_processed_id:" . $this->sourceTable;
- }
- /**
- * 创建用户日志数据数组
- *
- * @param int $userId 用户ID
- * @param string $message 日志消息
- * @param int $sourceId 来源记录ID
- * @return array
- */
- protected function createUserLogData(int $userId, string $message, int $sourceId): array
- {
- return [
- 'user_id' => $userId,
- 'message' => $message,
- 'source_type' => $this->sourceType,
- 'source_id' => $sourceId,
- 'source_table' => $this->sourceTable,
- 'created_at' => now()->toDateTimeString(),
- ];
- }
- /**
- * 获取收集器名称
- *
- * @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;
- }
- /**
- * 重置最后处理的ID(用于重新处理)
- *
- * @return void
- */
- public function resetLastProcessedId(): void
- {
- $cacheKey = $this->getLastProcessedIdCacheKey();
- Cache::forget($cacheKey);
- }
- }
|