ResetTraffic.php 2.0 KB

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