FarmUpgradeLogCollector.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\Farm\Models\FarmUpgradeLog;
  4. /**
  5. * 农场升级日志收集器
  6. *
  7. * 收集farm_upgrade_logs表的新增记录,转换为用户友好的日志消息
  8. */
  9. class FarmUpgradeLogCollector extends BaseLogCollector
  10. {
  11. /**
  12. * 源表名
  13. *
  14. * @var string
  15. */
  16. protected string $sourceTable = 'farm_upgrade_logs';
  17. /**
  18. * 源类型
  19. *
  20. * @var string
  21. */
  22. protected string $sourceType = 'farm';
  23. /**
  24. * 获取新的记录
  25. *
  26. * @param int $lastProcessedId 上次处理的最大ID
  27. * @return \Illuminate\Database\Eloquent\Collection
  28. */
  29. protected function getNewRecords(int $lastProcessedId)
  30. {
  31. return FarmUpgradeLog::where('id', '>', $lastProcessedId)
  32. ->orderBy('id')
  33. ->limit($this->maxRecords)
  34. ->get();
  35. }
  36. /**
  37. * 获取源表的最大ID
  38. *
  39. * 重写父类方法,使用模型查询以获得更好的性能
  40. *
  41. * @return int
  42. */
  43. public function getSourceTableMaxId(): int
  44. {
  45. return FarmUpgradeLog::max('id') ?: 0;
  46. }
  47. /**
  48. * 转换记录为用户日志数据
  49. *
  50. * @param FarmUpgradeLog $record 农场升级记录
  51. * @return array|null 用户日志数据,null表示跳过
  52. */
  53. protected function convertToUserLog($record): ?array
  54. {
  55. try {
  56. $message = $this->buildUpgradeMessage($record);
  57. return $this->createUserLogData(
  58. $record->user_id,
  59. $message,
  60. $record->id,
  61. $record->created_at
  62. );
  63. } catch (\Exception $e) {
  64. \Illuminate\Support\Facades\Log::error("转换农场升级日志失败", [
  65. 'record_id' => $record->id,
  66. 'error' => $e->getMessage()
  67. ]);
  68. return null;
  69. }
  70. }
  71. /**
  72. * 构建升级消息
  73. *
  74. * @param FarmUpgradeLog $record
  75. * @return string
  76. */
  77. private function buildUpgradeMessage(FarmUpgradeLog $record): string
  78. {
  79. // 使用UPGRADE_TYPE枚举判断升级类型
  80. switch ($record->upgrade_type) {
  81. case \App\Module\Farm\Enums\UPGRADE_TYPE::HOUSE->value:
  82. return "房屋升级到{$record->new_level}级";
  83. case \App\Module\Farm\Enums\UPGRADE_TYPE::LAND->value:
  84. $oldLandType = $this->getLandTypeName($record->old_level);
  85. $newLandType = $this->getLandTypeName($record->new_level);
  86. return "土地{$record->target_id}从{$oldLandType}升级为{$newLandType}";
  87. default:
  88. return "升级类型{$record->upgrade_type}从{$record->old_level}级升级到{$record->new_level}级";
  89. }
  90. }
  91. /**
  92. * 静态缓存:土地类型名称映射
  93. *
  94. * @var array|null
  95. */
  96. private static ?array $landTypeNames = null;
  97. /**
  98. * 获取土地类型名称
  99. *
  100. * @param int $typeId 土地类型ID
  101. * @return string
  102. */
  103. private function getLandTypeName(int $typeId): string
  104. {
  105. // 初始化静态缓存
  106. if (self::$landTypeNames === null) {
  107. $this->initLandTypeNamesCache();
  108. }
  109. return self::$landTypeNames[$typeId] ?? "未知土地类型{$typeId}";
  110. }
  111. /**
  112. * 初始化土地类型名称缓存
  113. *
  114. * @return void
  115. */
  116. private function initLandTypeNamesCache(): void
  117. {
  118. try {
  119. // 从数据库读取所有土地类型
  120. $landTypes = \App\Module\Farm\Models\FarmLandType::select('id', 'name')
  121. ->get()
  122. ->pluck('name', 'id')
  123. ->toArray();
  124. self::$landTypeNames = $landTypes;
  125. } catch (\Exception $e) {
  126. // 如果数据库查询失败,使用默认值
  127. self::$landTypeNames = [
  128. 1 => '普通土地',
  129. 2 => '红土地',
  130. 3 => '黑土地',
  131. 4 => '金色特殊土地',
  132. 5 => '蓝色特殊土地',
  133. 6 => '紫色特殊土地',
  134. ];
  135. }
  136. }
  137. /**
  138. * 清除土地类型名称缓存(用于测试或数据更新后)
  139. *
  140. * @return void
  141. */
  142. public static function clearLandTypeNamesCache(): void
  143. {
  144. self::$landTypeNames = null;
  145. }
  146. }