Files
xk-api-yii/common/jobs/PrescripRefundJob.php

70 lines
2.3 KiB
PHP

<?php
namespace common\jobs;
use common\forms\PrescripRefundForm;
use common\helpers\FuncHelper;
use common\models\oldDb\PrescripOrderLog;
use common\models\oldDb\PrescripOrderRefund;
use common\models\oldDb\Prescription;
use yii\base\Exception;
use yii\queue\JobInterface;
use yii\queue\Queue;
//处方订单退款
class PrescripRefundJob 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::find()->where([
'id' => $this->orderId,
'is_pay' => 1,
'cancel_status' => 0,
])->one();
if(!$Prescription){
throw new Exception('处方订单不存在');
}
//生成记录
$PrescripOrderRefund = new PrescripOrderRefund();
$PrescripOrderRefund->user_id = $Prescription->user_id;
$PrescripOrderRefund->order_id = $Prescription->id;
$PrescripOrderRefund->refund_no = FuncHelper::generate_order_no('RF');
$PrescripOrderRefund->refund_price = $Prescription->total_pay_price;
$PrescripOrderRefund->remark = '超时未接诊自动退款';
$PrescripOrderRefund->saveOrFail();
//取消状态
$Prescription->cancel_status = 1;
$Prescription->cancel_time = time();
$Prescription->cancel_remark = '超时未接诊自动取消';
//退款状态
$Prescription->refund_status = 1;
$Prescription->refund_time = time();
$Prescription->saveOrFail();
//退款操作
$PrescripRefundForm = new PrescripRefundForm();
$PrescripRefundForm->refundMoney($PrescripOrderRefund);
$t->commit();
PrescripOrderLog::saveLog($this->orderId,'订单自动退款队列end');
} catch (\Exception $exception) {
$t->rollBack();
PrescripOrderLog::saveLog($this->orderId,'订单自动退款队列异常:'.$exception->getMessage());
return;
}
}
}