trangshopeeaffiliate/app/Console/Commands/CleanupExpiredLinks.php
2026-07-11 22:56:22 +07:00

46 lines
1.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\ConversionHistory;
use App\Services\SettingService;
class CleanupExpiredLinks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:cleanup-expired-links';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Dọn dẹp các link rút gọn đã hết hạn dựa trên số click (30 ngày gốc + 3 ngày/click)';
/**
* Execute the console command.
*/
public function handle(SettingService $settingService)
{
$enableCleanup = $settingService->get('enable_auto_cleanup', '1');
if ($enableCleanup !== '1') {
$this->info('Tính năng tự động dọn dẹp link đang bị tắt trong Settings.');
return;
}
$this->info('Đang kiểm tra và dọn dẹp các link đã hết hạn...');
// Delete records where (created_at + 30 days + clicks * 3 days) < now
$deletedCount = ConversionHistory::whereRaw('DATE_ADD(created_at, INTERVAL (30 + clicks * 3) DAY) < NOW()')
->delete();
$this->info("Đã xóa thành công {$deletedCount} link hết hạn khỏi hệ thống.");
}
}