96 lines
3.2 KiB
PHP
96 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\YiiModels\ProductOrder;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProductOrderCancelJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
private ?int $productOrderId = null;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($productOrderId)
|
|
{
|
|
$this->productOrderId = $productOrderId;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return bool
|
|
* @throws \Throwable
|
|
*/
|
|
public function handle()
|
|
{
|
|
$productOrder = ProductOrder::find($this->productOrderId);
|
|
if (!$productOrder) {
|
|
log_s('订单信息不存在:' . "[productOrderId:$this->productOrderId]", 'job', 'order-cancel');
|
|
return true;
|
|
}
|
|
if ($productOrder->is_pay == ProductOrder::IS_PAY_TRUE) {
|
|
log_s('订单已支付:' . "[productOrderId:$this->productOrderId]", 'job', 'order-cancel');
|
|
return true;
|
|
}
|
|
if ($productOrder->status != ProductOrder::STATUS_UNPAID) {
|
|
log_s('取消失败,订单状态为' . $productOrder->status_string . "[productOrderId:$this->productOrderId]", 'job', 'order-cancel');
|
|
return true;
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$productOrder->status = ProductOrder::STATUS_CANCEL;
|
|
$productOrder->cancel_status = ProductOrder::CANCEL_STATUS_TRUE;
|
|
$productOrder->cancel_time = time();
|
|
$productOrder->cancel_remark = '未支付超时取消';
|
|
$productOrder->save();
|
|
|
|
$productOrder->logs()->create([
|
|
'content' => '未支付超时取消订单'
|
|
]);
|
|
|
|
$this->createNotice($productOrder, '您的订单因超时已取消');
|
|
|
|
// // 同步数据
|
|
// $productOrder->refresh();
|
|
// $productOrder->load(['items']);
|
|
// Artisan::call('data:sync', [
|
|
// '--type' => 'product_order_sync',
|
|
// '--data' => $productOrder->toArray(),
|
|
// ]);
|
|
|
|
if ($productOrder->is_online == ProductOrder::IS_ONLINE_TRUE && $productOrder->sync_order_no) {
|
|
// 同步数据(老)
|
|
Artisan::call('data:sync-old', [
|
|
'--type' => 'syncOrder',
|
|
'--data' => json_encode([
|
|
'order_no' => $productOrder->sync_order_no,
|
|
'status' => 2, // 1已支付 2已取消 3已退款
|
|
'time' => Carbon::createFromTimestamp($productOrder->cancel_time)->toDateTimeString(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (Exception $exception) {
|
|
DB::rollBack();
|
|
log_s('超时取消错误:' . "[productOrderId:$this->productOrderId]" . $exception->getMessage() . $exception->getTraceAsString(), 'order', 'error');
|
|
}
|
|
return true;
|
|
}
|
|
}
|