Files
chatroom/app/Console/Commands/PurgeOldMessages.php

92 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* 文件功能:定期清理聊天记录 Artisan 命令
*
* 每天自动清理超过指定天数的聊天记录,保持数据库体积可控。
* 保留天数可通过 sysparam 表的 message_retention_days 配置,默认 30 天。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Console\Commands;
use App\Models\Message;
use App\Models\Sysparam;
use Carbon\Carbon;
use Illuminate\Console\Command;
class PurgeOldMessages extends Command
{
/**
* 命令签名
*
* @var string
*/
protected $signature = 'messages:purge
{--days= : 覆盖默认保留天数}
{--dry-run : 仅预览不实际删除}';
/**
* 命令描述
*
* @var string
*/
protected $description = '清理超过指定天数的聊天记录(保留天数由 sysparam message_retention_days 配置,默认 30 天)';
/**
* 执行命令
*
* 按批次删除旧消息,避免长时间锁表。
*/
public function handle(): int
{
// 保留天数:命令行参数 > sysparam 配置 > 默认 30 天
$days = (int) ($this->option('days')
?: Sysparam::getValue('message_retention_days', '30'));
$cutoff = Carbon::now()->subDays($days);
$isDryRun = $this->option('dry-run');
// 统计待清理数量
$totalCount = Message::where('sent_at', '<', $cutoff)->count();
if ($totalCount === 0) {
$this->info("✅ 没有超过 {$days} 天的聊天记录需要清理。");
return self::SUCCESS;
}
if ($isDryRun) {
$this->warn("🔍 [预览模式] 将删除 {$totalCount} 条超过 {$days} 天的聊天记录(截止 {$cutoff->toDateTimeString()}");
return self::SUCCESS;
}
$this->info("🧹 开始清理超过 {$days} 天的聊天记录(截止 {$cutoff->toDateTimeString()}...");
$this->info(" 待清理数量:{$totalCount}");
// 分批删除,每批 1000 条,避免长时间锁表
$deleted = 0;
$batchSize = 1000;
do {
$batch = Message::where('sent_at', '<', $cutoff)
->limit($batchSize)
->delete();
$deleted += $batch;
if ($batch > 0) {
$this->line(" 已删除 {$deleted}/{$totalCount} 条...");
}
} while ($batch === $batchSize);
$this->info("✅ 清理完成!共删除 {$deleted} 条聊天记录。");
return self::SUCCESS;
}
}