AutoDecGoodsTrafficJob.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Models\Goods;
  5. use App\Http\Models\OrderGoods;
  6. use App\Http\Models\User;
  7. use Log;
  8. class AutoDecGoodsTrafficJob extends Command
  9. {
  10. protected $signature = 'command:autoDecGoodsTrafficJob';
  11. protected $description = '自动扣除到期流量包的流量';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. $orderGoods = OrderGoods::query()->where('is_expire', 0)->get();
  19. foreach ($orderGoods as $og) {
  20. $goods = Goods::query()->where('id', $og->goods_id)->first();
  21. if (empty($goods)) {
  22. continue;
  23. }
  24. if (date("Y-m-d H:i:s", strtotime("-" . $goods->days . " days")) >= $og->created_at) {
  25. $u = User::query()->where('id', $og->user_id)->first();
  26. if (empty($u)) {
  27. continue;
  28. }
  29. // 流量包到期自动扣总流量
  30. //if ($goods->type == 1) {
  31. if ($u->transfer_enable - $goods->traffic * 1048576 <= 0) {
  32. User::query()->where('id', $og->user_id)->update(['transfer_enable' => 0]);
  33. } else {
  34. User::query()->where('id', $og->user_id)->decrement('transfer_enable', $goods->traffic * 1048576);
  35. }
  36. //}
  37. OrderGoods::query()->where('id', $og->id)->update(['is_expire' => 1]);
  38. }
  39. }
  40. Log::info('定时任务:' . $this->description);
  41. }
  42. }