AutoResetUserTrafficJob.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Order;
  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. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. $config = $this->systemConfig();
  19. if ($config['reset_traffic']) {
  20. $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->get();
  21. foreach ($userList as $user) {
  22. if (!$user->traffic_reset_day) {
  23. continue;
  24. }
  25. // 取出用户最后购买的有效套餐
  26. $order = Order::query()->with(['user', 'goods'])->whereHas('goods', function ($q) {
  27. $q->where('type', 2);
  28. })->where('user_id', $user->id)->where('is_expire', 0)->orderBy('oid', 'desc')->first();
  29. if (!$order) {
  30. continue;
  31. }
  32. $today = abs(date('d'));
  33. $reset_days = [$today, 29, 30, 31];
  34. if (in_array($order->user->traffic_reset_day, $reset_days)) {
  35. // 跳过本月,防止异常重置
  36. if (date('m') == date('m', strtotime($order->expire_at))) {
  37. continue;
  38. }
  39. if (date('m') == date('m', strtotime($order->created_at))) {
  40. continue;
  41. }
  42. User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
  43. }
  44. }
  45. }
  46. Log::info('定时任务:' . $this->description);
  47. }
  48. // 系统配置
  49. private function systemConfig()
  50. {
  51. $config = Config::query()->get();
  52. $data = [];
  53. foreach ($config as $vo) {
  54. $data[$vo->name] = $vo->value;
  55. }
  56. return $data;
  57. }
  58. }