时间: 2025年06月23日 01:30-01:32
状态: ✅ 已完成
在app/Module/Game/Commands/CollectUserLogsCommand.php中维护了一套获取MaxId的方法,这是重复的,应该使用收集器的方法。
CollectUserLogsCommand中的getTableMaxId方法:
private function getTableMaxId(string $tableName): int
{
try {
// 根据表名使用对应的模型
switch ($tableName) {
case 'fund_logs':
return \App\Module\Fund\Models\FundLogModel::max('id') ?: 0;
case 'item_transaction_logs':
return \App\Module\GameItems\Models\ItemTransactionLog::max('id') ?: 0;
// ... 更多case
}
} catch (\Exception $e) {
return 0;
}
}
问题:
/**
* 获取源表的最大ID
*
* 子类可以重写此方法以提供更高效的实现
* 默认使用通用的DB查询方式
*
* @return int
*/
public function getSourceTableMaxId(): int
{
try {
// 使用通用的DB查询方式作为默认实现
$result = \Illuminate\Support\Facades\DB::table($this->sourceTable)->max('id');
return $result ? (int)$result : 0;
} catch (\Exception $e) {
Log::error("获取源表最大ID失败", [
'collector' => $this->collectorName,
'source_table' => $this->sourceTable,
'error' => $e->getMessage()
]);
return 0;
}
}
// FundLogCollector
public function getSourceTableMaxId(): int
{
return FundLogModel::max('id') ?: 0;
}
// ItemLogCollector
public function getSourceTableMaxId(): int
{
return ItemTransactionLog::max('id') ?: 0;
}
/**
* 获取收集器的源表最大ID
*
* @param string $name 收集器名称
* @return int
*/
public function getCollectorSourceTableMaxId(string $name): int
{
if (!isset($this->collectors[$name])) {
return 0;
}
return $this->collectors[$name]->getSourceTableMaxId();
}
// 修改前
$maxId = $this->getTableMaxId($info['source_table']);
// 修改后
$maxId = $manager->getCollectorSourceTableMaxId($name);
php artisan game:collect-user-logs --detail
输出正常:
📈 收集器进度状态:
🔧 fund: 最后处理ID 515845, 待处理 0 条
🔧 item: 最后处理ID 11802, 待处理 0 条
🔧 farm_harvest: 最后处理ID 432, 待处理 0 条
🔧 farm_upgrade: 最后处理ID 432, 待处理 0 条
🔧 point: 最后处理ID 372, 待处理 0 条
CollectUserLogsCommand
├── getTableMaxId() (重复逻辑)
│ ├── switch case for fund_logs
│ ├── switch case for item_transaction_logs
│ └── ...
└── 直接使用模型查询
CollectUserLogsCommand
└── UserLogCollectorManager
└── BaseLogCollector
├── getSourceTableMaxId() (通用实现)
└── 各收集器重写 (优化实现)
├── FundLogCollector
├── ItemLogCollector
└── ...
只需要:
无需修改:
各收集器可以独立优化:
git commit -m "重构用户日志收集器架构,消除重复的MaxId获取逻辑
- 在BaseLogCollector中添加getSourceTableMaxId方法,提供通用的DB查询实现
- 各收集器重写此方法,使用模型查询以获得更好的性能
- 在UserLogCollectorManager中添加getCollectorSourceTableMaxId方法
- 修改CollectUserLogsCommand使用收集器的方法而不是维护单独的getTableMaxId
- 删除CollectUserLogsCommand中重复的getTableMaxId方法
- 提高了代码的可维护性和扩展性,新增收集器无需修改Command代码"
✅ 重构完成:
关键改进:
这次重构为用户日志收集系统奠定了更好的架构基础,为后续功能扩展提供了良好的支持。