AutoCloseOrderJob.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Coupon;
  4. use App\Http\Models\Order;
  5. use App\Http\Models\Payment;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. use DB;
  9. class AutoCloseOrderJob extends Command
  10. {
  11. protected $signature = 'autoCloseOrderJob';
  12. protected $description = '自动关闭超时未支付订单';
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. public function handle()
  18. {
  19. // 有赞云超过60分钟未支付则自动关闭,我们这则只给30分钟
  20. $paymentList = Payment::query()->with(['order'])->where('status', 0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-30 minutes")))->get();
  21. if (!$paymentList->isEmpty()) {
  22. DB::beginTransaction();
  23. try {
  24. foreach ($paymentList as $payment) {
  25. // 关闭支付单
  26. Payment::query()->where('id', $payment->id)->update(['status' => -1]);
  27. // 关闭订单
  28. Order::query()->where('oid', $payment->oid)->update(['status' => -1]);
  29. // 退回优惠券
  30. if ($payment->order->coupon_id) {
  31. Coupon::query()->where('id', $payment->order->coupon_id)->update(['status' => 0]);
  32. }
  33. }
  34. DB::commit();
  35. } catch (\Exception $e) {
  36. Log::info('【异常】自动关闭超时未支付订单:' . $e->getMessage());
  37. DB::rollBack();
  38. }
  39. }
  40. Log::info('定时任务:' . $this->description);
  41. }
  42. }