ResetTraffic.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. class ResetTraffic extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'reset:traffic';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '流量清空';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $user = User::where('expired_at', '!=', NULL);
  36. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  37. switch ((int)$resetTrafficMethod) {
  38. // 1 a month
  39. case 0:
  40. $this->resetByMonthFirstDay($user);
  41. break;
  42. // expire day
  43. case 1:
  44. $this->resetByExpireDay($user);
  45. break;
  46. }
  47. }
  48. private function resetByMonthFirstDay($user):void
  49. {
  50. if ((string)date('d') === '01') {
  51. $user->update([
  52. 'u' => 0,
  53. 'd' => 0
  54. ]);
  55. }
  56. }
  57. private function resetByExpireDay($user):void
  58. {
  59. $date = date('Y-m-d', time());
  60. $startAt = strtotime((string)$date);
  61. $endAt = (int)$startAt + 24 * 3600;
  62. $lastDay = date('d', strtotime('last day of +0 months'));
  63. if ((string)$lastDay === '29') {
  64. $endAt = (int)$startAt + 72 * 3600;
  65. }
  66. if ((string)$lastDay === '30') {
  67. $endAt = (int)$startAt + 48 * 3600;
  68. }
  69. $user->where('expired_at', '>=', (int)$startAt)
  70. ->where('expired_at', '<', (int)$endAt)
  71. ->update([
  72. 'u' => 0,
  73. 'd' => 0
  74. ]);
  75. }
  76. }