BaseLogCollector.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\Game\Services\UserLogService;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 用户日志收集器基类
  7. *
  8. * 为各个模块的日志收集器提供基础功能
  9. */
  10. abstract class BaseLogCollector
  11. {
  12. /**
  13. * 收集器名称
  14. *
  15. * @var string
  16. */
  17. protected string $collectorName;
  18. /**
  19. * 源表名
  20. *
  21. * @var string
  22. */
  23. protected string $sourceTable;
  24. /**
  25. * 源类型
  26. *
  27. * @var string
  28. */
  29. protected string $sourceType;
  30. /**
  31. * 最大处理记录数
  32. *
  33. * @var int
  34. */
  35. protected int $maxRecords = 500;
  36. /**
  37. * 设置最大处理记录数
  38. *
  39. * @param int $maxRecords
  40. * @return void
  41. */
  42. public function setMaxRecords(int $maxRecords): void
  43. {
  44. $this->maxRecords = $maxRecords;
  45. }
  46. /**
  47. * 构造函数
  48. */
  49. public function __construct()
  50. {
  51. $this->collectorName = static::class;
  52. }
  53. /**
  54. * 收集日志
  55. *
  56. * @return int 处理的记录数
  57. */
  58. public function collect(): int
  59. {
  60. try {
  61. // 添加执行时间限制,防止死循环
  62. $startTime = time();
  63. $maxExecutionTime = 30; // 最大执行30秒
  64. Log::info("开始收集日志", [
  65. 'collector' => $this->collectorName,
  66. 'start_time' => date('Y-m-d H:i:s', $startTime)
  67. ]);
  68. $result = $this->collectById();
  69. $endTime = time();
  70. $executionTime = $endTime - $startTime;
  71. Log::info("日志收集完成", [
  72. 'collector' => $this->collectorName,
  73. 'processed_count' => $result,
  74. 'execution_time' => $executionTime . 's'
  75. ]);
  76. if ($executionTime > $maxExecutionTime) {
  77. Log::warning("日志收集执行时间过长", [
  78. 'collector' => $this->collectorName,
  79. 'execution_time' => $executionTime . 's',
  80. 'max_time' => $maxExecutionTime . 's'
  81. ]);
  82. }
  83. return $result;
  84. } catch (\Exception $e) {
  85. Log::error("日志收集失败", [
  86. 'collector' => $this->collectorName,
  87. 'error' => $e->getMessage(),
  88. 'trace' => $e->getTraceAsString()
  89. ]);
  90. return 0;
  91. }
  92. }
  93. /**
  94. * 按ID收集日志(推荐方式)
  95. *
  96. * @return int 处理的记录数
  97. */
  98. private function collectById(): int
  99. {
  100. $lastProcessedId = $this->getLastProcessedId();
  101. $records = $this->getNewRecords($lastProcessedId);
  102. if ($records->isEmpty()) {
  103. return 0;
  104. }
  105. $userLogs = [];
  106. $maxId = 0;
  107. foreach ($records as $record) {
  108. // 检查是否已经收集过此记录,避免重复收集
  109. if ($this->isDuplicateRecord($this->sourceTable, $record->id)) {
  110. $maxId = max($maxId, $record->id);
  111. continue;
  112. }
  113. $userLogData = $this->convertToUserLog($record);
  114. if ($userLogData) {
  115. $userLogs[] = $userLogData;
  116. $maxId = max($maxId, $record->id);
  117. }
  118. }
  119. if (!empty($userLogs)) {
  120. // 按原始时间排序后批量保存
  121. usort($userLogs, function($a, $b) {
  122. return strtotime($a['original_time']) <=> strtotime($b['original_time']);
  123. });
  124. $batchResult = UserLogService::batchLog($userLogs);
  125. if (!$batchResult) {
  126. Log::error("批量保存用户日志失败", [
  127. 'collector' => $this->collectorName,
  128. 'logs_count' => count($userLogs),
  129. 'max_id' => $maxId
  130. ]);
  131. return 0; // 保存失败时返回0,避免更新进度
  132. }
  133. }
  134. $processedCount = count($userLogs);
  135. Log::info("ID日志收集完成", [
  136. 'collector' => $this->collectorName,
  137. 'processed_count' => $processedCount,
  138. 'last_id' => $maxId,
  139. 'records_found' => $records->count()
  140. ]);
  141. return $processedCount;
  142. }
  143. /**
  144. * 获取新的记录(子类实现)
  145. *
  146. * @param int $lastProcessedId 上次处理的最大ID
  147. * @return \Illuminate\Database\Eloquent\Collection
  148. */
  149. abstract protected function getNewRecords(int $lastProcessedId);
  150. /**
  151. * 公共方法:转换记录为用户日志数据
  152. *
  153. * @param mixed $record 原始记录
  154. * @return array|null 用户日志数据,null表示跳过
  155. */
  156. public function convertToUserLogPublic($record): ?array
  157. {
  158. return $this->convertToUserLog($record);
  159. }
  160. /**
  161. * 转换记录为用户日志数据(子类实现)
  162. *
  163. * @param mixed $record 原始记录
  164. * @return array|null 用户日志数据,null表示跳过
  165. */
  166. abstract protected function convertToUserLog($record): ?array;
  167. /**
  168. * 获取上次处理的最大ID
  169. * 从user_logs表中查询该收集器最后处理的记录ID
  170. *
  171. * 注意:对于同一个source_table可能有多个source_type的情况(如FundLogCollector),
  172. * 需要查询该表的所有记录的最大source_id,而不是仅查询当前source_type的记录
  173. *
  174. * @return int
  175. */
  176. protected function getLastProcessedId(): int
  177. {
  178. try {
  179. // 查询该source_table的所有记录的最大source_id
  180. // 这样可以避免因为动态source_type导致的重复收集问题
  181. $maxId = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
  182. ->max('source_id');
  183. return $maxId ?: 0;
  184. } catch (\Exception $e) {
  185. Log::error("获取最后处理ID失败", [
  186. 'collector' => $this->collectorName,
  187. 'source_table' => $this->sourceTable,
  188. 'error' => $e->getMessage()
  189. ]);
  190. return 0;
  191. }
  192. }
  193. /**
  194. * 创建用户日志数据数组
  195. *
  196. * @param int $userId 用户ID
  197. * @param string $message 日志消息
  198. * @param int $sourceId 来源记录ID
  199. * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
  200. * @return array
  201. */
  202. protected function createUserLogData(int $userId, string $message, int $sourceId, ?string $originalTime = null): array
  203. {
  204. $now = now()->toDateTimeString();
  205. $originalTime = $originalTime ?? $now;
  206. return [
  207. 'user_id' => $userId,
  208. 'message' => $message,
  209. 'source_type' => $this->sourceType,
  210. 'source_id' => $sourceId,
  211. 'source_table' => $this->sourceTable,
  212. 'original_time' => $originalTime, // 原始业务时间
  213. 'collected_at' => $now, // 收集时间
  214. 'created_at' => $now, // 兼容字段
  215. ];
  216. }
  217. /**
  218. * 获取收集器名称
  219. *
  220. * @return string
  221. */
  222. public function getCollectorName(): string
  223. {
  224. return $this->collectorName;
  225. }
  226. /**
  227. * 获取源表名
  228. *
  229. * @return string
  230. */
  231. public function getSourceTable(): string
  232. {
  233. return $this->sourceTable;
  234. }
  235. /**
  236. * 获取源类型
  237. *
  238. * @return string
  239. */
  240. public function getSourceType(): string
  241. {
  242. return $this->sourceType;
  243. }
  244. /**
  245. * 检查是否为重复记录
  246. *
  247. * 注意:不使用source_type进行检查,因为同一个source_table的记录可能有不同的source_type
  248. * 只要source_table和source_id相同,就认为是重复记录
  249. *
  250. * @param string $sourceTable 源表名
  251. * @param int $sourceId 源记录ID
  252. * @return bool
  253. */
  254. protected function isDuplicateRecord(string $sourceTable, int $sourceId): bool
  255. {
  256. try {
  257. $exists = \App\Module\Game\Models\UserLog::where('source_table', $sourceTable)
  258. ->where('source_id', $sourceId)
  259. ->exists();
  260. return $exists;
  261. } catch (\Exception $e) {
  262. Log::error("检查重复记录失败", [
  263. 'collector' => $this->collectorName,
  264. 'source_table' => $sourceTable,
  265. 'source_id' => $sourceId,
  266. 'error' => $e->getMessage()
  267. ]);
  268. return false;
  269. }
  270. }
  271. }