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