AutoResetUserTrafficJob.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\OrderGoods;
  4. use Illuminate\Console\Command;
  5. use App\Http\Models\Config;
  6. use App\Http\Models\User;
  7. use Log;
  8. class AutoResetUserTrafficJob extends Command
  9. {
  10. protected $signature = 'command:autoResetUserTrafficJob';
  11. protected $description = '自动重置用户流量';
  12. protected static $config;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $config = Config::query()->get();
  17. $data = [];
  18. foreach ($config as $vo) {
  19. $data[$vo->name] = $vo->value;
  20. }
  21. self::$config = $data;
  22. }
  23. public function handle()
  24. {
  25. if (self::$config['reset_traffic']) {
  26. $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->get();
  27. foreach ($userList as $user) {
  28. if (empty($user->traffic_reset_day)) {
  29. continue;
  30. }
  31. // 取出这个用户最后购买的有效套餐
  32. $orderGoods = OrderGoods::query()->with(['goods' => function($q) { $q->where('type', 2); }])->where('user_id', $user->id)->where('is_expire', 0)->orderBy('id', 'desc')->first();
  33. if (empty($orderGoods) || empty($orderGoods->goods)) {
  34. continue;
  35. }
  36. if ($user->traffic_reset_day == abs(date('d')) && date('m') == date('m', strtotime($orderGoods->created_at))) {
  37. continue;
  38. }
  39. User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
  40. }
  41. }
  42. Log::info('定时任务:' . $this->description);
  43. }
  44. }