ResetTraffic.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // no action
  52. case 2:
  53. break;
  54. }
  55. DB::commit();
  56. }
  57. private function resetByMonthFirstDay():void
  58. {
  59. $builder = $this->builder;
  60. if ((string)date('d') === '01') {
  61. $builder->update([
  62. 'u' => 0,
  63. 'd' => 0
  64. ]);
  65. }
  66. }
  67. private function resetByExpireDay():void
  68. {
  69. $builder = $this->builder;
  70. $lastDay = date('d', strtotime('last day of +0 months'));
  71. $users = [];
  72. foreach ($builder->get() as $item) {
  73. $expireDay = date('d', $item->expired_at);
  74. $today = date('d');
  75. if ($expireDay === $today) {
  76. array_push($users, $item->id);
  77. }
  78. if (($today === $lastDay) && $expireDay >= $lastDay) {
  79. array_push($users, $item->id);
  80. }
  81. }
  82. User::whereIn('id', $users)->update([
  83. 'u' => 0,
  84. 'd' => 0
  85. ]);
  86. }
  87. }