ResetTraffic.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ini_set('memory_limit', -1);
  40. DB::beginTransaction();
  41. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  42. switch ((int)$resetTrafficMethod) {
  43. // 1 a month
  44. case 0:
  45. $this->resetByMonthFirstDay();
  46. break;
  47. // expire day
  48. case 1:
  49. $this->resetByExpireDay();
  50. break;
  51. }
  52. DB::commit();
  53. }
  54. private function resetByMonthFirstDay():void
  55. {
  56. $builder = $this->builder;
  57. if ((string)date('d') === '01') {
  58. $builder->update([
  59. 'u' => 0,
  60. 'd' => 0
  61. ]);
  62. }
  63. }
  64. private function resetByExpireDay():void
  65. {
  66. $builder = $this->builder;
  67. $lastDay = date('d', strtotime('last day of +0 months'));
  68. $users = [];
  69. foreach ($builder->get() as $item) {
  70. $expireDay = date('d', $item->expired_at);
  71. $today = date('d');
  72. if ($expireDay === $today) {
  73. array_push($users, $item->id);
  74. }
  75. if (($today === $lastDay) && $expireDay >= $lastDay) {
  76. array_push($users, $item->id);
  77. }
  78. }
  79. User::whereIn('id', $users)->update([
  80. 'u' => 0,
  81. 'd' => 0
  82. ]);
  83. }
  84. }