UserTrafficWarning.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\User;
  4. use App\Notifications\DataExhaust;
  5. use Illuminate\Console\Command;
  6. use Log;
  7. class UserTrafficWarning extends Command
  8. {
  9. protected $signature = 'userTrafficWarning';
  10. protected $description = '用户流量超过警告阈值自动发邮件提醒';
  11. public function handle()
  12. {
  13. $jobStartTime = microtime(true);
  14. if (sysConfig('data_exhaust_notification')) {// 用户流量超过警告阈值提醒
  15. $this->userTrafficWarning();
  16. }
  17. $jobEndTime = microtime(true);
  18. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  19. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  20. }
  21. private function userTrafficWarning()// 用户流量超过警告阈值提醒
  22. {
  23. $trafficWarningPercent = sysConfig('traffic_warning_percent');
  24. User::activeUser()->where('transfer_enable', '>', 0)->chunk(config('tasks.chunk'), function ($users) use ($trafficWarningPercent) {
  25. foreach ($users as $user) {
  26. // 用户账号不是邮箱的跳过
  27. if (filter_var($user->email, FILTER_VALIDATE_EMAIL) === false) {
  28. continue;
  29. }
  30. $usedPercent = $user->used_traffic_percentage * 100; // 已使用流量百分比
  31. if ($usedPercent >= $trafficWarningPercent) {
  32. $user->notify(new DataExhaust($usedPercent));
  33. }
  34. }
  35. });
  36. }
  37. }