ResetTraffic.php 3.0 KB

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