515 lines
19 KiB
PHP
515 lines
19 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Amqp\Producer\PrescriptionExpiredDelayDirectProducer;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionFile;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\ReportRegulatory;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserPatient;
|
||
use App\Model\UserPharmacist;
|
||
use App\Services\CaService;
|
||
use App\Services\ImService;
|
||
use App\Services\MessagePush;
|
||
use App\Services\OrderPrescriptionService;
|
||
use App\Utils\Log;
|
||
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
||
use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
||
use Hyperf\Amqp\Message\Type;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\Amqp\Result;
|
||
use Hyperf\Amqp\Annotation\Consumer;
|
||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Redis\Redis;
|
||
use PhpAmqpLib\Message\AMQPMessage;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
#[Consumer(nums: 1)]
|
||
class AutoPharmacistCaVerifyDelayDirectConsumer extends ConsumerMessage
|
||
{
|
||
use ProducerDelayedMessageTrait;
|
||
use ConsumerDelayedMessageTrait;
|
||
|
||
protected string $exchange = 'amqp.delay.direct';
|
||
|
||
protected ?string $queue = 'auto.pharmacist.ca.verify.delay.queue';
|
||
|
||
protected string $type = Type::DIRECT; //Type::FANOUT;
|
||
|
||
protected string|array $routingKey = 'AutoPharmacistCaVerify';
|
||
|
||
public function consumeMessage($data, AMQPMessage $message): string
|
||
{
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("开始:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
// 检测入参参数
|
||
$res = $this->checkParams($data);
|
||
if (!$res) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:执行参数错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $data['order_prescription_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:处方数据错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测处方数据
|
||
$res = $this->checkPrescriptionData($order_prescription);
|
||
if (!empty($res)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:" . $res);
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取问诊订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_prescription['order_inquiry_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:问诊订单数据错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测问诊订单数据
|
||
$res = $this->checkOrderInquiryData($order_inquiry);
|
||
if (!empty($res)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:" . $res);
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_prescription['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:医生数据错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取用户数据
|
||
$params = array();
|
||
$params['patient_id'] = $order_prescription['patient_id'];
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:患者数据错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取药师数据
|
||
$params = array();
|
||
$params['pharmacist_id'] = $order_prescription['pharmacist_id'];
|
||
$user_pharmacist = UserPharmacist::getOne($params);
|
||
if (empty($user_pharmacist)) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:药师数据错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取处方商品数据
|
||
$product_name = "";
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (!empty($order_prescription_product)){
|
||
$product_name_array = array_column($order_prescription_product->toArray(),'product_name');
|
||
if (count($product_name_array) > 1){
|
||
$product_name = implode('、',$product_name_array);
|
||
}else{
|
||
$product_name = $product_name_array[0];
|
||
}
|
||
}
|
||
|
||
try {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("信息:检测执行次数");
|
||
|
||
// 检测执行次数
|
||
$res = $this->checkHandleNumber($data['prescription_file_id']);
|
||
}catch (\Exception $e){
|
||
// 检测次数失败
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("错误:" . $e->getMessage());
|
||
return Result::ACK;
|
||
}
|
||
|
||
if (!$res) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:超出最大执行次数或检测错误");
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 修改处方表为未通过
|
||
$this->modifyOrderPrescription($data['order_prescription_id'], 2, "请联系平台客服,请勿重开处方");
|
||
|
||
// 医生-开具的处方审核未通过
|
||
$MessagePush = new MessagePush($user_doctor['user_id'], $order_prescription['order_inquiry_id']);
|
||
$MessagePush->prescriptionVerifyFail($data['order_prescription_id']);
|
||
|
||
// 患者-处方审核未通过
|
||
$MessagePush = new MessagePush($user_patient['user_id'], $order_prescription['order_inquiry_id']);
|
||
$MessagePush->patientPrescriptionVerifyFail();
|
||
Db::commit();
|
||
|
||
return Result::ACK;
|
||
}catch (\Exception $e){
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("错误:" . $e->getMessage());
|
||
return Result::ACK;
|
||
}
|
||
}
|
||
|
||
|
||
// 下载医生签章文件
|
||
Db::beginTransaction();
|
||
try {
|
||
// 检测执行次数
|
||
$res = $this->checkHandleNumber($data['prescription_file_id']);
|
||
if (!$res) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:超出最大执行次数或检测错误");
|
||
|
||
// 修改处方表为未通过
|
||
$this->modifyOrderPrescription($data['order_prescription_id'],2,"请联系平台客服,请勿重开处方");
|
||
|
||
Db::commit();
|
||
}
|
||
|
||
// 获取处方文件数据
|
||
$params = array();
|
||
$params['prescription_file_id'] = $data['prescription_file_id'];
|
||
$params['order_prescription_id'] = $data['order_prescription_id'];
|
||
$order_prescription_file = OrderPrescriptionFile::getOne($params);
|
||
if (empty($order_prescription_file)) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:处方文件数据错误");
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
// 检测处方文件数据
|
||
$res = $this->checkPrescriptionFileData($order_prescription_file);
|
||
if (!empty($res)) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:" . $res);
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("下载医生的签章pdf并上传至oss");
|
||
|
||
// 下载医生的签章pdf并上传至oss
|
||
$prescription_pdf_oss_path = $this->downCaPdfToOss($order_prescription, $user_doctor['user_id'],2,$order_prescription_file['doctor_ca_file_id']);
|
||
if (!$prescription_pdf_oss_path) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:" . $prescription_pdf_oss_path);
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
// 药师/医院签章
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("药师/医院签章");
|
||
$OrderPrescriptionService = new OrderPrescriptionService();
|
||
$prescription_open_result = $OrderPrescriptionService->openPrescription($data['order_prescription_id'],$user_pharmacist['user_id']);
|
||
if (empty($prescription_open_result['file_id'])){
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:处方开具失败");
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
// 下载医院的签章pdf并上传至oss
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("下载医院的签章pdf并上传至oss");
|
||
$prescription_pdf_oss_path = $this->downCaPdfToOss($order_prescription, $user_pharmacist['user_id'],3,$prescription_open_result['file_id']);
|
||
if (!$prescription_pdf_oss_path) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:" . $prescription_pdf_oss_path);
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
// 修改处方文件表,添加prescription_pdf_oss_path字段
|
||
$this->modifyOrderPrescriptionFile($data['prescription_file_id'],$prescription_pdf_oss_path,$prescription_open_result['file_id']);
|
||
|
||
// 修改处方表为通过
|
||
$this->modifyOrderPrescription($data['order_prescription_id'],1);
|
||
|
||
// 增加上报监管平台表
|
||
$res = $this->addReportRegulatory($order_inquiry['order_inquiry_id'],$order_prescription['order_prescription_id']);
|
||
if (!$res){
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:增加监管平台上报表失败");
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
Db::commit();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->info("成功");
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("异常错误:" . $e->getMessage());
|
||
|
||
return Result::REQUEUE; // 重回队列
|
||
}
|
||
|
||
// 发送消息
|
||
try {
|
||
// 药师审核成功
|
||
// 发送IM消息-处方已开具
|
||
$imService = new ImService();
|
||
$imService->prescriptionIssued($order_inquiry,$user_doctor['user_id'],$order_inquiry['user_id'],$product_name,(string)$data['order_prescription_id'],7);
|
||
|
||
// 发送站内、短信消息-患者的处方被药师审核通过
|
||
$MessagePush = new MessagePush($order_inquiry['user_id'],$order_inquiry['order_inquiry_id']);
|
||
$MessagePush->patientPrescriptionVerifyPass();
|
||
|
||
// 站内、订阅失败发送短信-医生开具的处方审核通过
|
||
// 发送目标不同,重新实例化
|
||
$MessagePush = new MessagePush($user_doctor['user_id'],$order_inquiry['order_inquiry_id']);
|
||
$MessagePush->prescriptionVerifySuccess();
|
||
|
||
// 添加处方过期队列
|
||
$data = array();
|
||
$data['order_prescription_id'] = (string)$order_prescription['order_prescription_id'];
|
||
|
||
$message = new PrescriptionExpiredDelayDirectProducer($data);
|
||
$message->setDelayMs(1000 * 60 * 60 * 24 * 3);
|
||
$producer = $this->container->get(Producer::class);
|
||
$res = $producer->produce($message);
|
||
if (!$res) {
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("添加处方过期队列失败!");
|
||
return Result::ACK;
|
||
}
|
||
}catch(\Exception $e){
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("发送消息异常错误:" . $e->getMessage());
|
||
return Result::ACK;
|
||
}
|
||
|
||
return Result::ACK;
|
||
}
|
||
|
||
/**
|
||
* 检测执行参数
|
||
* @param array $data
|
||
* @return bool
|
||
*/
|
||
protected function checkParams(array $data): bool
|
||
{
|
||
if (!isset($data['prescription_file_id'])) {
|
||
return false;
|
||
}
|
||
|
||
if (!isset($data['order_prescription_id'])) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 检测处方数据
|
||
* @param array|object $order_prescription
|
||
* @return string
|
||
*/
|
||
protected function checkPrescriptionData(array|object $order_prescription): string
|
||
{
|
||
// 验证处方状态
|
||
if ($order_prescription['prescription_status'] == 2) {
|
||
// 处方状态(1:待审核 2:待使用 3:已失效 4:已使用)
|
||
return "处方已审核";
|
||
}
|
||
|
||
if ($order_prescription['prescription_status'] == 3) {
|
||
// 处方状态(1:待审核 2:待使用 3:已失效 4:已使用)
|
||
return "处方已审核并失效";
|
||
}
|
||
|
||
if ($order_prescription['prescription_status'] == 4) {
|
||
// 处方状态(1:待审核 2:待使用 3:已失效 4:已使用)
|
||
return "处方已审核并使用";
|
||
}
|
||
|
||
// 验证处方审核状态
|
||
if ($order_prescription['pharmacist_audit_status'] != 0) {
|
||
// 药师审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
return "处方已审核";
|
||
}
|
||
|
||
// 处方平台审核状态
|
||
if ($order_prescription['platform_audit_status'] != 0) {
|
||
// 处方平台审核状态(0:审核中 1:审核成功 2:审核驳回)
|
||
return "处方已审核";
|
||
}
|
||
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* 检测问诊订单数据
|
||
* @param array|object $order_inquiry
|
||
* @return string
|
||
*/
|
||
protected function checkOrderInquiryData(array|object $order_inquiry): string
|
||
{
|
||
if (in_array($order_inquiry['inquiry_status'], [1, 2, 3, 7])) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
return "问诊订单状态错误,无法继续审核";
|
||
}
|
||
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* 检测处方文件数据
|
||
* @param array|object $order_prescription_file
|
||
* @return string
|
||
*/
|
||
protected function checkPrescriptionFileData(array|object $order_prescription_file): string
|
||
{
|
||
if (empty($order_prescription_file['doctor_ca_file_id'])) {
|
||
return "缺少医生签章pdf文件id";
|
||
}
|
||
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* 检测执行次数
|
||
* @param string $prescription_file_id
|
||
* @return bool
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
protected function checkHandleNumber(string $prescription_file_id): bool
|
||
{
|
||
try {
|
||
$redis = $this->container->get(Redis::class);
|
||
|
||
$redis_key = "AutoPharmacistCaVerify" . $prescription_file_id;
|
||
$redis_value = $redis->get($redis_key);
|
||
if (empty($redis_value)) {
|
||
$redis->set($redis_key, 1, 60 * 60 * 24 * 5);
|
||
return true;
|
||
}
|
||
|
||
// 问诊订单执行退款次数过多
|
||
if ($redis_value > 3) {
|
||
// 加入短信队列,通知管理员
|
||
|
||
return false;
|
||
}
|
||
|
||
$redis->incr($redis_key);
|
||
} catch (\Exception $e) {
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 下载签章pdf并上传至oss
|
||
* @param array|object $order_prescription
|
||
* @param string $user_id 用户id
|
||
* @param string|int $user_type 用户类型 1:患者 2:医生 3:药师
|
||
* @param string $file_id
|
||
* @return string|bool oss地址|false(存在异常时)
|
||
*/
|
||
protected function downCaPdfToOss(array|object $order_prescription, string $user_id, string|int $user_type, string $file_id): string|bool
|
||
{
|
||
try {
|
||
$user = array();
|
||
$user['user_id'] = $user_id;
|
||
$user['user_type'] = $user_type;
|
||
$CaService = new CaService($order_prescription, $user);
|
||
|
||
// 下载签章pdf并上传至oss
|
||
if ($user_type == 2){
|
||
return $CaService->downCaPdfToOss($file_id,1);
|
||
}else{
|
||
return $CaService->downCaPdfToOss($file_id,2);
|
||
}
|
||
|
||
} catch (\Exception $e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改处方文件表
|
||
* @param string $prescription_file_id 主键id
|
||
* @param string $prescription_pdf_oss_path pdf的oss地址
|
||
* @param string $hospital_ca_file_id 医院签章文件id
|
||
* @return void
|
||
*/
|
||
protected function modifyOrderPrescriptionFile(string $prescription_file_id,string $prescription_pdf_oss_path,string $hospital_ca_file_id): void
|
||
{
|
||
try {
|
||
$params = array();
|
||
$params['prescription_file_id'] = $prescription_file_id;
|
||
|
||
$data = array();
|
||
$data['hospital_ca_file_id'] = $hospital_ca_file_id;
|
||
$data['prescription_pdf_oss_path'] = $prescription_pdf_oss_path;
|
||
|
||
OrderPrescriptionFile::edit($params,$data);
|
||
}catch(\Exception $e){
|
||
// 失败不做处理
|
||
Log::getInstance("queue-AutoPharmacistCaVerify")->error("错误:修改处方文件表失败" . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改处方表
|
||
* @param string $order_prescription_id 处方id
|
||
* @param int $status 审核状态 1:成功 2:驳回
|
||
* @param string $pharmacist_fail_reason 驳回理由
|
||
* @return void
|
||
*/
|
||
protected function modifyOrderPrescription(string $order_prescription_id,int $status,string $pharmacist_fail_reason = ""): void
|
||
{
|
||
// 修改处方审核状态
|
||
$data = array();
|
||
if ($status == 1){
|
||
// 审核成功
|
||
$data['prescription_status'] = 2;
|
||
$data['pharmacist_audit_status'] = 1;
|
||
$data['platform_audit_status'] = 1;
|
||
}else{
|
||
$data['pharmacist_audit_status'] = 2;
|
||
$data['pharmacist_fail_reason'] = $pharmacist_fail_reason;
|
||
}
|
||
$data['pharmacist_verify_time'] = date('Y-m-d H:i:s',time());
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription_id;
|
||
|
||
OrderPrescription::edit($params,$data);
|
||
}
|
||
|
||
/**
|
||
* 增加上报监管平台表
|
||
* @param string $order_inquiry_id
|
||
* @param string $order_prescription_id
|
||
* @return bool
|
||
*/
|
||
protected function addReportRegulatory(string $order_inquiry_id,string $order_prescription_id): bool
|
||
{
|
||
// 检测是否已添加
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$report_regulatory = ReportRegulatory::getOne($params);
|
||
if (!empty($report_regulatory)) {
|
||
return true;
|
||
}
|
||
|
||
$data = array();
|
||
$data['order_inquiry_id'] = $order_inquiry_id;
|
||
$data['order_prescription_id'] = $order_prescription_id;
|
||
$report_regulatory = ReportRegulatory::addReportRegulatory($data);
|
||
if (empty($report_regulatory)){
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|