diff --git a/app/Console/Commands/SyncUserMsgToSearch.php b/app/Console/Commands/SyncUserMsgToSearch.php new file mode 100644 index 000000000..12e3a5236 --- /dev/null +++ b/app/Console/Commands/SyncUserMsgToSearch.php @@ -0,0 +1,90 @@ +option('c')) { + $this->info('清除索引...'); + ZincSearchKeyValue::clear(); + ZincSearchUserMsg::clear(); + $this->info("索引删除成功"); + return 0; + } + + $this->info('开始同步聊天数据...'); + + // 同步消息数据 + $this->syncDialogMsgs(); + + // 完成 + $this->info("\n同步完成"); + return 0; + } + + /** + * 同步消息数据 + * + * @return void + */ + private function syncDialogMsgs(): void + { + $this->info("\n同步消息数据..."); + + // 获取上次同步的最后ID + $lastKey = "sync:userMsgLastId"; + $lastId = $this->option('i') ? intval(ZincSearchKeyValue::get($lastKey, 0)) : 0; + + $num = 0; + $count = WebSocketDialogMsg::where('id', '>', $lastId)->count(); + $batchSize = $this->option('batch'); + + do { + // 获取一批消息 + $dialogMsgs = WebSocketDialogMsg::where('id', '>', $lastId) + ->orderBy('id') + ->limit($batchSize) + ->get(); + + if ($dialogMsgs->isEmpty()) { + break; + } + + $num += count($dialogMsgs); + $progress = round($num / $count * 100, 2); + $this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$lastId} ~ {$dialogMsgs->last()->id}"); + + // 批量索引数据 + ZincSearchUserMsg::batchSyncMsgs($dialogMsgs); + + // 更新最后ID + $lastId = $dialogMsgs->last()->id; + ZincSearchKeyValue::set($lastKey, $lastId); + } while (count($dialogMsgs) == $batchSize); + + $this->info("同步消息结束 - 最后ID {$lastId}"); + } +} diff --git a/app/Module/ZincSearch/ZincSearchKeyValue.php b/app/Module/ZincSearch/ZincSearchKeyValue.php index cd2570bda..f9b9e44d5 100644 --- a/app/Module/ZincSearch/ZincSearchKeyValue.php +++ b/app/Module/ZincSearch/ZincSearchKeyValue.php @@ -2,28 +2,26 @@ namespace App\Module\ZincSearch; -use App\Module\Base; - /** * ZincSearch 键值存储类 * * 使用方法: * * 1. 基础方法 - * - 确保索引存在: ZincSearchKeyValue::ensureIndex(); - * - 清空所有数据: ZincSearchKeyValue::clear(); + * - 确保索引存在: ensureIndex(); + * - 清空所有数据: clear(); * * 2. 基本操作 - * - 设置键值: ZincSearchKeyValue::set('site_name', '我的网站'); - * - 设置复杂数据: ZincSearchKeyValue::set('site_config', ['logo' => 'logo.png', 'theme' => 'dark']); - * - 合并现有数据: ZincSearchKeyValue::set('site_config', ['footer' => '版权所有'], true); - * - 获取键值: $siteName = ZincSearchKeyValue::get('site_name'); - * - 获取键值带默认值: $theme = ZincSearchKeyValue::get('theme', 'light'); - * - 删除键值: ZincSearchKeyValue::delete('temporary_data'); + * - 设置键值: set('site_name', '我的网站'); + * - 设置复杂数据: set('site_config', ['logo' => 'logo.png', 'theme' => 'dark']); + * - 合并现有数据: set('site_config', ['footer' => '版权所有'], true); + * - 获取键值: $siteName = get('site_name'); + * - 获取键值带默认值: $theme = get('theme', 'light'); + * - 删除键值: delete('temporary_data'); * * 3. 批量操作 - * - 批量设置: ZincSearchKeyValue::batchSet(['user_count' => 100, 'active_users' => 50]); - * - 批量获取: $stats = ZincSearchKeyValue::batchGet(['user_count', 'active_users']); + * - 批量设置: batchSet(['user_count' => 100, 'active_users' => 50]); + * - 批量获取: $stats = batchGet(['user_count', 'active_users']); */ class ZincSearchKeyValue { diff --git a/app/Module/ZincSearch/ZincSearchUserMsg.php b/app/Module/ZincSearch/ZincSearchUserMsg.php index f44910241..77eb55175 100644 --- a/app/Module/ZincSearch/ZincSearchUserMsg.php +++ b/app/Module/ZincSearch/ZincSearchUserMsg.php @@ -12,21 +12,21 @@ use Illuminate\Support\Facades\Log; * 使用方法: * * 1. 基础方法 - * - 确保索引存在: ZincSearchKeyValue::ensureIndex(); - * - 清空所有数据: ZincSearchKeyValue::clear(); + * - 确保索引存在: ensureIndex(); + * - 清空所有数据: clear(); * * 2. 搜索方法 - * - 关键词搜索: ZincSearchUserMsg::searchByKeyword('用户ID', '关键词'); + * - 关键词搜索: searchByKeyword('用户ID', '关键词'); * * 3. 基本方法 - * - 单个同步: ZincSearchUserMsg::syncMsg($dialogMsg); - * - 批量同步: ZincSearchUserMsg::batchSyncMsgs($dialogMsgs); - * - 删除消息: ZincSearchUserMsg::deleteMsg($dialogMsg); + * - 单个同步: syncMsg($dialogMsg); + * - 批量同步: batchSyncMsgs($dialogMsgs); + * - 删除消息: deleteMsg($dialogMsg); * * 4. 用户方法 - * - 单个同步: ZincSearchUserMsg::syncUser($dialogUser); - * - 批量同步: ZincSearchUserMsg::batchSyncUsers($dialogUsers); - * - 删除消息: ZincSearchUserMsg::deleteUser($dialogUser); + * - 单个同步: syncUser($dialogUser); + * - 批量同步: batchSyncUsers($dialogUsers); + * - 删除消息: deleteUser($dialogUser); */ class ZincSearchUserMsg {