73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace common\jobs;
|
|
|
|
use common\core\BaseModel;
|
|
use common\forms\ProductRefundForm;
|
|
use common\models\Prescription;
|
|
use common\models\PrescriptionLog;
|
|
use common\enums\PrescriptionEnum;
|
|
use common\forms\ProductCancelForm;
|
|
use common\models\ProductOrder;
|
|
use yii\queue\JobInterface;
|
|
use yii\queue\Queue;
|
|
|
|
//处方自动失效
|
|
class PrescriptionAutoExpireJob extends BaseJob implements JobInterface
|
|
{
|
|
public $orderId;
|
|
|
|
/**
|
|
* @param Queue $queue which pushed and is handling the job
|
|
*/
|
|
public function execute($queue)
|
|
{
|
|
$t = \YII::$app->db->beginTransaction();
|
|
try {
|
|
PrescriptionLog::saveLog($this->orderId,'处方自动失效队列start'.$this->orderId);
|
|
|
|
$this->setRequest();
|
|
$Prescription = Prescription::findOne([
|
|
'id' => $this->orderId,
|
|
'status' => PrescriptionEnum::WAIT_CHECK
|
|
]);
|
|
|
|
if (!$Prescription) {
|
|
throw new \Exception('处方不存在或已是最终态');
|
|
}
|
|
|
|
$Prescription->status = PrescriptionEnum::UNPASSED;
|
|
$Prescription->saveOrFail();
|
|
|
|
// 判断订单是否已付款且代发货,是进行退款操作
|
|
$productOrder = ProductOrder::findOne([
|
|
'p_id' => $this->orderId,
|
|
'cancel_status' => 0,
|
|
'refund_status' => 0,
|
|
]);
|
|
|
|
if($productOrder->status == 1 && $productOrder->is_pay == 1){
|
|
// 自动退款
|
|
$ProductRefundForm = new ProductRefundForm();
|
|
$ProductRefundForm->refund([
|
|
'order_id' => $productOrder->id,
|
|
'user_id' => $productOrder->user_id
|
|
]);
|
|
} else {
|
|
// 订单自动失效
|
|
$ProductCancelForm = new ProductCancelForm();
|
|
$ProductCancelForm->cancel([
|
|
'order_id' => $productOrder->id,
|
|
'user_id' => $productOrder->user_id
|
|
], '处方失效,订单自动失效');
|
|
}
|
|
|
|
$t->commit();
|
|
PrescriptionLog::saveLog($this->orderId,'处方自动失效队列end'.$this->orderId);
|
|
} catch (\Exception $exception) {
|
|
$t->rollBack();
|
|
PrescriptionLog::saveLog($this->orderId,'处方自动失效队列异常:'.$exception->getMessage());
|
|
return;
|
|
}
|
|
}
|
|
} |