CheckOrderExpire.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Goods;
  4. use App\Models\Order;
  5. use App\Models\OrderItem;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. class CheckOrderExpire extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'check:orderExpire';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '检查订单到期未支付';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. while (true){
  39. $date = date('Y-m-d H:i:s', time() - 1800);//30分钟
  40. Order::where('order_status', 1)
  41. ->where('created_at', '<=', $date)
  42. ->chunkById(100,function ($list){
  43. foreach ($list as $item){
  44. DB::beginTransaction();
  45. try {
  46. //取消订单
  47. Order::where('id',$item->{'id'})->update(['order_status'=>3]);
  48. DB::commit();
  49. }catch (\Exception $e){
  50. DB::rollBack();
  51. }
  52. }
  53. });
  54. sleep(5);
  55. }
  56. }
  57. }