AutoDecGoodsTrafficJob.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Models\Order;
  5. use App\Http\Models\User;
  6. use Log;
  7. class AutoDecGoodsTrafficJob extends Command
  8. {
  9. protected $signature = 'command:autoDecGoodsTrafficJob';
  10. protected $description = '自动扣除用户到期流量包的流量';
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. public function handle()
  16. {
  17. $order = Order::query()->with(['user', 'goods'])->where('is_expire', 0)->get();
  18. if (!$order->isEmpty()) {
  19. foreach ($order as $vo) {
  20. if (empty($vo->user) || empty($vo->goods)) {
  21. continue;
  22. }
  23. // 到期自动处理
  24. if (date("Y-m-d H:i:s") >= $vo->expire_at) {
  25. if ($vo->user->transfer_enable - $vo->goods->traffic * 1048576 <= 0) {
  26. User::query()->where('id', $vo->user_id)->update(['transfer_enable' => 0]);
  27. } else {
  28. User::query()->where('id', $vo->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
  29. }
  30. Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
  31. }
  32. }
  33. }
  34. Log::info('定时任务:' . $this->description);
  35. }
  36. }