ResetTraffic.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. class ResetTraffic extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'reset:traffic';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '流量清空';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $user = User::where('expired_at', '!=', NULL)
  36. ->where('expired_at', '>', time());
  37. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  38. switch ((int)$resetTrafficMethod) {
  39. // 1 a month
  40. case 0:
  41. $this->resetByMonthFirstDay($user);
  42. break;
  43. // expire day
  44. case 1:
  45. $this->resetByExpireDay($user);
  46. break;
  47. }
  48. }
  49. private function resetByMonthFirstDay($user):void
  50. {
  51. if ((string)date('d') === '01') {
  52. $user->update([
  53. 'u' => 0,
  54. 'd' => 0
  55. ]);
  56. }
  57. }
  58. private function resetByExpireDay($user):void
  59. {
  60. $lastDay = date('d', strtotime('last day of +0 months'));
  61. $users = [];
  62. foreach ($user->get() as $item) {
  63. $expireDay = date('d', $item->expired_at);
  64. if ($expireDay === date('d') || (string)$lastDay === '29' || (string)$lastDay === '30') {
  65. array_push($users, $item->id);
  66. }
  67. }
  68. $user->whereIn('id', $users)->update([
  69. 'u' => 0,
  70. 'd' => 0
  71. ]);
  72. }
  73. }