BaseLogCollector.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. $userLogData = $this->convertToUserLog($record);
  109. if ($userLogData) {
  110. $userLogs[] = $userLogData;
  111. $maxId = max($maxId, $record->id);
  112. }
  113. }
  114. if (!empty($userLogs)) {
  115. // 按原始时间排序后批量保存
  116. usort($userLogs, function($a, $b) {
  117. return strtotime($a['original_time']) <=> strtotime($b['original_time']);
  118. });
  119. $batchResult = UserLogService::batchLog($userLogs);
  120. if (!$batchResult) {
  121. Log::error("批量保存用户日志失败", [
  122. 'collector' => $this->collectorName,
  123. 'logs_count' => count($userLogs),
  124. 'max_id' => $maxId
  125. ]);
  126. return 0; // 保存失败时返回0,避免更新进度
  127. }
  128. }
  129. $processedCount = count($userLogs);
  130. Log::info("ID日志收集完成", [
  131. 'collector' => $this->collectorName,
  132. 'processed_count' => $processedCount,
  133. 'last_id' => $maxId,
  134. 'records_found' => $records->count()
  135. ]);
  136. return $processedCount;
  137. }
  138. /**
  139. * 获取新的记录(子类实现)
  140. *
  141. * @param int $lastProcessedId 上次处理的最大ID
  142. * @return \Illuminate\Database\Eloquent\Collection
  143. */
  144. abstract protected function getNewRecords(int $lastProcessedId);
  145. /**
  146. * 公共方法:转换记录为用户日志数据
  147. *
  148. * @param mixed $record 原始记录
  149. * @return array|null 用户日志数据,null表示跳过
  150. */
  151. public function convertToUserLogPublic($record): ?array
  152. {
  153. return $this->convertToUserLog($record);
  154. }
  155. /**
  156. * 转换记录为用户日志数据(子类实现)
  157. *
  158. * @param mixed $record 原始记录
  159. * @return array|null 用户日志数据,null表示跳过
  160. */
  161. abstract protected function convertToUserLog($record): ?array;
  162. /**
  163. * 获取上次处理的最大ID
  164. * 从user_logs表中查询该收集器最后处理的记录ID
  165. *
  166. * @return int
  167. */
  168. protected function getLastProcessedId(): int
  169. {
  170. try {
  171. // 直接查询对应表的最大source_id
  172. $maxId = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
  173. ->where('source_type', $this->sourceType)
  174. ->max('source_id');
  175. return $maxId ?: 0;
  176. } catch (\Exception $e) {
  177. Log::error("获取最后处理ID失败", [
  178. 'collector' => $this->collectorName,
  179. 'source_table' => $this->sourceTable,
  180. 'error' => $e->getMessage()
  181. ]);
  182. return 0;
  183. }
  184. }
  185. /**
  186. * 创建用户日志数据数组
  187. *
  188. * @param int $userId 用户ID
  189. * @param string $message 日志消息
  190. * @param int $sourceId 来源记录ID
  191. * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
  192. * @return array
  193. */
  194. protected function createUserLogData(int $userId, string $message, int $sourceId, ?string $originalTime = null): array
  195. {
  196. $now = now()->toDateTimeString();
  197. $originalTime = $originalTime ?? $now;
  198. return [
  199. 'user_id' => $userId,
  200. 'message' => $message,
  201. 'source_type' => $this->sourceType,
  202. 'source_id' => $sourceId,
  203. 'source_table' => $this->sourceTable,
  204. 'original_time' => $originalTime, // 原始业务时间
  205. 'collected_at' => $now, // 收集时间
  206. 'created_at' => $now, // 兼容字段
  207. ];
  208. }
  209. /**
  210. * 获取收集器名称
  211. *
  212. * @return string
  213. */
  214. public function getCollectorName(): string
  215. {
  216. return $this->collectorName;
  217. }
  218. /**
  219. * 获取源表名
  220. *
  221. * @return string
  222. */
  223. public function getSourceTable(): string
  224. {
  225. return $this->sourceTable;
  226. }
  227. /**
  228. * 获取源类型
  229. *
  230. * @return string
  231. */
  232. public function getSourceType(): string
  233. {
  234. return $this->sourceType;
  235. }
  236. /**
  237. * 检查是否为重复记录
  238. *
  239. * @param string $sourceTable 源表名
  240. * @param int $sourceId 源记录ID
  241. * @return bool
  242. */
  243. protected function isDuplicateRecord(string $sourceTable, int $sourceId): bool
  244. {
  245. try {
  246. $exists = \App\Module\Game\Models\UserLog::where('source_type', $this->sourceType)
  247. ->where('source_table', $sourceTable)
  248. ->where('source_id', $sourceId)
  249. ->exists();
  250. return $exists;
  251. } catch (\Exception $e) {
  252. Log::error("检查重复记录失败", [
  253. 'collector' => $this->collectorName,
  254. 'source_table' => $sourceTable,
  255. 'source_id' => $sourceId,
  256. 'error' => $e->getMessage()
  257. ]);
  258. return false;
  259. }
  260. }
  261. }