TaskMonthly.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\NotificationLog;
  4. use App\Models\Order;
  5. use App\Models\Payment;
  6. use App\Models\User;
  7. use App\Models\UserBanedLog;
  8. use App\Models\UserLoginLog;
  9. use Exception;
  10. use Illuminate\Console\Command;
  11. use Log;
  12. class TaskMonthly extends Command
  13. {
  14. protected $signature = 'task:monthly';
  15. protected $description = '每月任务';
  16. public function handle(): void
  17. {
  18. $jobTime = microtime(true);
  19. $this->cleanAccounts(); // 清理僵尸账号
  20. if (sysConfig('is_clear_log')) {
  21. $this->clearLog(); // 清除小日志
  22. }
  23. $jobTime = round(microtime(true) - $jobTime, 4);
  24. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  25. }
  26. private function cleanAccounts(): void
  27. {
  28. User::where('expired_at', '<', date('Y-m-d'))->where('transfer_enable', '=', 0)->whereEnable(0)->where(function ($query) {
  29. $query->where('u', '>', 0)->orWhere('d', '>', 0);
  30. })->update(['u' => 0, 'd' => 0]);
  31. }
  32. private function clearLog(): void
  33. {
  34. try {
  35. NotificationLog::where('updated_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.notification_logs'))))->delete(); // 清理通知日志
  36. UserLoginLog::where('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.login_logs'))))->delete(); // 清除用户登陆日志
  37. Payment::where('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.payments'))))->delete(); // 清理在线支付日志
  38. UserBanedLog::where('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.user_baned_logs'))))->delete(); // 清理用户封禁日志
  39. Order::whereStatus(-1)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.unpaid_orders'))))->delete(); // 清理用户未支付订单
  40. } catch (Exception $e) {
  41. Log::emergency(trans('common.error_item', ['attribute' => trans('admin.system.is_clear_log')]).': '.$e->getMessage());
  42. }
  43. }
  44. }