152 lines
5.6 KiB
PHP
152 lines
5.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserPatient;
|
||
use App\Model\UserPharmacist;
|
||
use App\Services\MessagePush;
|
||
use App\Utils\Log;
|
||
use Hyperf\Amqp\Result;
|
||
use Hyperf\Amqp\Annotation\Consumer;
|
||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||
use Hyperf\DbConnection\Db;
|
||
use PhpAmqpLib\Message\AMQPMessage;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
/**
|
||
* 分配药师-暂时废弃,现修改为创建处方时分配
|
||
*/
|
||
#[Consumer(exchange: 'amqp.direct', routingKey: 'AssignPharmacist', queue: 'assign.pharmacist.queue', nums: 1)]
|
||
class AssignPharmacistConsumer extends ConsumerMessage
|
||
{
|
||
public function consumeMessage($data, AMQPMessage $message): string
|
||
{
|
||
Log::getInstance()->error("开始执行 分配药师 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
// 获取订单-处方表数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $data['order_prescription_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)) {
|
||
Db::rollBack();
|
||
Log::getInstance()->info("分配药师队列执行结束:未查询到对应处方订单数据");
|
||
return Result::DROP;// 销毁
|
||
}
|
||
|
||
// 检测处方状态(1:待审核 3:待使用 4:已失效 5:已使用)
|
||
if ($order_prescription['prescription_status'] != 1) {
|
||
Db::rollBack();
|
||
Log::getInstance()->info("分配药师队列执行结束:处方状态非待审核,当前为" . $order_prescription['prescription_status']);
|
||
return Result::ACK;// 销毁
|
||
}
|
||
|
||
// 检测药师审核状态
|
||
if ($order_prescription['pharmacist_audit_status'] == 1) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("分配药师队列执行结束:药师审核状态为已通过");
|
||
return Result::ACK;// 销毁
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_prescription['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("分配药师队列执行结束:缺少医生数据");
|
||
return Result::ACK;// 销毁
|
||
}
|
||
|
||
// 获取用户数据
|
||
$params = array();
|
||
$params['patient_id'] = $order_prescription['patient_id'];
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("分配药师队列执行结束:缺少患者数据");
|
||
return Result::ACK;// 销毁
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 分配药师
|
||
$params = array();
|
||
$params['pharmacist_id'] = "534534546";
|
||
$user_pharmacist = UserPharmacist::getOne($params);
|
||
if (empty($user_pharmacist)) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("分配药师队列执行失败:药师数据错误");
|
||
|
||
// 分配失败,按照驳回处理
|
||
$this->reject($data['order_prescription_id'], $user_doctor['user_id'], $user_patient['user_id'], $order_prescription['order_inquiry_id']);
|
||
|
||
return Result::DROP;// 销毁
|
||
}
|
||
|
||
// 分配药师
|
||
$save_data = array();
|
||
$save_data['pharmacist_id'] = $user_pharmacist['pharmacist_id'];
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $data['order_prescription_id'];
|
||
OrderPrescription::edit($params, $save_data);
|
||
|
||
Db::commit();
|
||
} catch (\Throwable $e) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("分配药师队列执行失败原因:" . $e->getMessage());
|
||
|
||
// 分配失败,按照驳回处理
|
||
$this->reject($data['order_prescription_id'], $user_doctor['user_id'],$user_patient['user_id'], $order_prescription['order_inquiry_id']);
|
||
|
||
return Result::DROP; // 重回队列
|
||
}
|
||
|
||
Log::getInstance()->info("分配药师队列执行成功");
|
||
|
||
return Result::ACK;
|
||
}
|
||
|
||
/**
|
||
* 分配失败
|
||
* @param string $order_prescription_id
|
||
* @param string $doctor_user_id
|
||
* @param string $patient_user_id
|
||
* @param string $order_inquiry_id
|
||
* @return void
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function reject(string $order_prescription_id, string $doctor_user_id, string $patient_user_id, string $order_inquiry_id): void
|
||
{
|
||
try {
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
|
||
$data = array();
|
||
$data['pharmacist_audit_status'] = 2;
|
||
$data['pharmacist_verify_time'] = date('Y-m-d H:i:s', time());
|
||
$data['pharmacist_fail_reason'] = "请联系平台客服,请勿重开处方";
|
||
|
||
OrderPrescription::edit($params, $data);
|
||
|
||
// 医生-开具的处方审核未通过
|
||
$MessagePush = new MessagePush($doctor_user_id, $order_inquiry_id);
|
||
$MessagePush->prescriptionVerifyFail($order_prescription_id);
|
||
|
||
// 患者-处方审核未通过
|
||
$MessagePush = new MessagePush($patient_user_id, $order_inquiry_id);
|
||
$MessagePush->patientPrescriptionVerifyFail();
|
||
|
||
} catch (\Exception $e) {
|
||
Log::getInstance()->error("分配药师队列执行失败原因:" . $e->getMessage());
|
||
}
|
||
}
|
||
}
|