CheckOrderExpire.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. $date = date('Y-m-d H:i:s', time() - 1800);//30分钟
  39. Order::where('order_status', 1)
  40. ->where('created_at', '<=', $date)
  41. ->chunkById(100,function ($list){
  42. foreach ($list as $item){
  43. DB::beginTransaction();
  44. try {
  45. //取消订单
  46. Order::where('id',$item->{'id'})->update(['order_status'=>3]);
  47. DB::commit();
  48. }catch (\Exception $e){
  49. DB::rollBack();
  50. }
  51. }
  52. });
  53. return true;
  54. }
  55. }