ResetTraffic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $resetMethods = Plan::select(
  42. DB::raw("GROUP_CONCAT(`id`) as plan_ids"),
  43. DB::raw("reset_traffic_method as method")
  44. )
  45. ->groupBy('reset_traffic_method')
  46. ->get()
  47. ->toArray();
  48. foreach ($resetMethods as $resetMethod) {
  49. $planIds = explode(',', $resetMethod['plan_ids']);
  50. switch (true) {
  51. case ($resetMethod['method'] === NULL): {
  52. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  53. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  54. switch ((int)$resetTrafficMethod) {
  55. // month first day
  56. case 0:
  57. $this->resetByMonthFirstDay($builder);
  58. break;
  59. // expire day
  60. case 1:
  61. $this->resetByExpireDay($builder);
  62. break;
  63. // no action
  64. case 2:
  65. break;
  66. // year
  67. case 3:
  68. $this->resetByYear($builder);
  69. }
  70. break;
  71. }
  72. case ($resetMethod['method'] === 0): {
  73. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  74. $this->resetByMonthFirstDay($builder);
  75. break;
  76. }
  77. case ($resetMethod['method'] === 1): {
  78. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  79. $this->resetByExpireDay($builder);
  80. break;
  81. }
  82. case ($resetMethod['method'] === 2): {
  83. break;
  84. }
  85. case ($resetMethod['method'] === 3): {
  86. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  87. $this->resetByYear($builder);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. private function resetByYear($builder):void
  94. {
  95. if ((string)date('md') === '0101') {
  96. $builder->update([
  97. 'u' => 0,
  98. 'd' => 0
  99. ]);
  100. }
  101. }
  102. private function resetByMonthFirstDay($builder):void
  103. {
  104. if ((string)date('d') === '01') {
  105. $builder->update([
  106. 'u' => 0,
  107. 'd' => 0
  108. ]);
  109. }
  110. }
  111. private function resetByExpireDay($builder):void
  112. {
  113. $lastDay = date('d', strtotime('last day of +0 months'));
  114. $users = [];
  115. foreach ($builder->get() as $item) {
  116. $expireDay = date('d', $item->expired_at);
  117. $today = date('d');
  118. if ($expireDay === $today) {
  119. array_push($users, $item->id);
  120. }
  121. if (($today === $lastDay) && $expireDay >= $lastDay) {
  122. array_push($users, $item->id);
  123. }
  124. }
  125. User::whereIn('id', $users)->update([
  126. 'u' => 0,
  127. 'd' => 0
  128. ]);
  129. }
  130. }