ResetTraffic.php 2.1 KB

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