ResetTraffic.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 first day
  67. case 3:
  68. $this->resetByYearFirstDay($builder);
  69. // year expire day
  70. case 4:
  71. $this->resetByExpireYear($builder);
  72. }
  73. break;
  74. }
  75. case ($resetMethod['method'] === 0): {
  76. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  77. $this->resetByMonthFirstDay($builder);
  78. break;
  79. }
  80. case ($resetMethod['method'] === 1): {
  81. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  82. $this->resetByExpireDay($builder);
  83. break;
  84. }
  85. case ($resetMethod['method'] === 2): {
  86. break;
  87. }
  88. case ($resetMethod['method'] === 3): {
  89. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  90. $this->resetByYearFirstDay($builder);
  91. break;
  92. }
  93. case ($resetMethod['method'] === 4): {
  94. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  95. $this->resetByExpireYear($builder);
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. private function resetByExpireYear($builder):void
  102. {
  103. $users = [];
  104. foreach ($builder->get() as $item) {
  105. $expireDay = date('m-d', $item->expired_at);
  106. $today = date('m-d');
  107. if ($expireDay === $today) {
  108. array_push($users, $item->id);
  109. }
  110. }
  111. User::whereIn('id', $users)->update([
  112. 'u' => 0,
  113. 'd' => 0
  114. ]);
  115. }
  116. private function resetByYearFirstDay($builder):void
  117. {
  118. if ((string)date('md') === '0101') {
  119. $builder->update([
  120. 'u' => 0,
  121. 'd' => 0
  122. ]);
  123. }
  124. }
  125. private function resetByMonthFirstDay($builder):void
  126. {
  127. if ((string)date('d') === '01') {
  128. $builder->update([
  129. 'u' => 0,
  130. 'd' => 0
  131. ]);
  132. }
  133. }
  134. private function resetByExpireDay($builder):void
  135. {
  136. $lastDay = date('d', strtotime('last day of +0 months'));
  137. $users = [];
  138. foreach ($builder->get() as $item) {
  139. $expireDay = date('d', $item->expired_at);
  140. $today = date('d');
  141. if ($expireDay === $today) {
  142. array_push($users, $item->id);
  143. }
  144. if (($today === $lastDay) && $expireDay >= $lastDay) {
  145. array_push($users, $item->id);
  146. }
  147. }
  148. User::whereIn('id', $users)->update([
  149. 'u' => 0,
  150. 'd' => 0
  151. ]);
  152. }
  153. }