| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Models\Coupon;
- use App\Http\Models\Order;
- use App\Http\Models\Payment;
- use Illuminate\Console\Command;
- use Log;
- use DB;
- class AutoCloseOrderJob extends Command
- {
- protected $signature = 'autoCloseOrderJob';
- protected $description = '自动关闭超时未支付订单';
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- // 有赞云超过60分钟未支付则自动关闭,我们这则只给30分钟
- $paymentList = Payment::query()->with(['order'])->where('status', 0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-30 minutes")))->get();
- if (!$paymentList->isEmpty()) {
- DB::beginTransaction();
- try {
- foreach ($paymentList as $payment) {
- // 关闭支付单
- Payment::query()->where('id', $payment->id)->update(['status' => -1]);
- // 关闭订单
- Order::query()->where('oid', $payment->oid)->update(['status' => -1]);
- // 退回优惠券
- if ($payment->order->coupon_id) {
- Coupon::query()->where('id', $payment->order->coupon_id)->update(['status' => 0]);
- }
- }
- DB::commit();
- } catch (\Exception $e) {
- Log::info('【异常】自动关闭超时未支付订单:' . $e->getMessage());
- DB::rollBack();
- }
- }
- Log::info('定时任务:' . $this->description);
- }
- }
|