CleanExpiredReferralCodesCommand.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Module\Promotion\Commands;
  3. use App\Module\Promotion\Services\ReferralCodeService;
  4. use Illuminate\Console\Command;
  5. /**
  6. * 清理过期推荐码命令
  7. *
  8. * 用于清理过期的推荐码,释放数据库空间。
  9. */
  10. class CleanExpiredReferralCodesCommand extends Command
  11. {
  12. /**
  13. * 命令名称
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'promotion:clean-expired-referral-codes {--debug-info : 显示详细信息}';
  18. /**
  19. * 命令描述
  20. *
  21. * @var string
  22. */
  23. protected $description = '清理过期的推荐码';
  24. /**
  25. * 执行命令
  26. *
  27. * @return int
  28. */
  29. public function handle()
  30. {
  31. $this->info('开始清理过期推荐码...');
  32. $showDebugInfo = $this->option('debug-info');
  33. try {
  34. $count = ReferralCodeService::cleanExpiredReferralCodes();
  35. if ($showDebugInfo) {
  36. $this->info("共清理了 {$count} 个过期推荐码");
  37. } else {
  38. $this->info("清理完成");
  39. }
  40. return 0;
  41. } catch (\Exception $e) {
  42. $this->error("清理过期推荐码失败: " . $e->getMessage());
  43. if ($showDebugInfo) {
  44. $this->error($e->getTraceAsString());
  45. }
  46. return 1;
  47. }
  48. }
  49. }