ResetTraffic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  67. break;
  68. }
  69. case ($resetMethod['method'] === 0): {
  70. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  71. $this->resetByMonthFirstDay($builder);
  72. break;
  73. }
  74. case ($resetMethod['method'] === 1): {
  75. $builder = with(clone($this->builder))->whereIn('plan_id', $planIds);
  76. $this->resetByExpireDay($builder);
  77. break;
  78. }
  79. case ($resetMethod['method'] === 2): {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. private function resetByMonthFirstDay($builder):void
  86. {
  87. if ((string)date('d') === '01') {
  88. $builder->update([
  89. 'u' => 0,
  90. 'd' => 0
  91. ]);
  92. }
  93. }
  94. private function resetByExpireDay($builder):void
  95. {
  96. $lastDay = date('d', strtotime('last day of +0 months'));
  97. $users = [];
  98. foreach ($builder->get() as $item) {
  99. $expireDay = date('d', $item->expired_at);
  100. $today = date('d');
  101. if ($expireDay === $today) {
  102. array_push($users, $item->id);
  103. }
  104. if (($today === $lastDay) && $expireDay >= $lastDay) {
  105. array_push($users, $item->id);
  106. }
  107. }
  108. User::whereIn('id', $users)->update([
  109. 'u' => 0,
  110. 'd' => 0
  111. ]);
  112. }
  113. }