57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
namespace common\jobs;
|
|
|
|
use common\core\BaseModel;
|
|
use common\events\OrderEvent;
|
|
use common\models\oldDb\Order;
|
|
use common\models\oldDb\OrderLog;
|
|
use yii\queue\JobInterface;
|
|
use yii\queue\Queue;
|
|
|
|
class OrderCancelJob extends BaseJob implements JobInterface
|
|
{
|
|
public $orderId;
|
|
|
|
/**
|
|
* @param Queue $queue which pushed and is handling the job
|
|
*/
|
|
public function execute($queue)
|
|
{
|
|
OrderLog::saveLog($this->orderId,'订单自动取消队列start');
|
|
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
try {
|
|
$this->setRequest();
|
|
$order = Order::findOne([
|
|
'id' => $this->orderId,
|
|
'is_pay' => 0,
|
|
// 'pay_type' => 0,
|
|
]);
|
|
if (!$order || $order->cancel_status == 1) {
|
|
throw new \Exception('未支付订单不存在或已取消');
|
|
}
|
|
|
|
$order->cancel_status = 1;
|
|
$order->cancel_time = time();
|
|
$order->cancel_remark = '超时自动取消订单';
|
|
if ($order->save()) {
|
|
|
|
OrderLog::saveLog($this->orderId,'队列触发订单取消canceled事件');
|
|
$event = new OrderEvent([
|
|
'order' => $order,
|
|
]);
|
|
\Yii::$app->trigger(Order::EVENT_CANCELED, $event);
|
|
$t->commit();
|
|
} else {
|
|
throw new \Exception((new BaseModel())->getErrorMsg($order));
|
|
}
|
|
|
|
OrderLog::saveLog($this->orderId,'订单自动取消队列end');
|
|
} catch (\Exception $exception) {
|
|
$t->rollBack();
|
|
OrderLog::saveLog($this->orderId,'订单自动取消队列异常:'.$exception->getMessage());
|
|
return;
|
|
}
|
|
}
|
|
}
|