110 lines
3.4 KiB
PHP
110 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Amqp\Consumer;
|
|
|
|
use App\Model\OrderPrescription;
|
|
use App\Utils\Log;
|
|
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
|
use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
|
use Hyperf\Amqp\Message\Type;
|
|
use Hyperf\Amqp\Result;
|
|
use Hyperf\Amqp\Annotation\Consumer;
|
|
use Hyperf\Amqp\Message\ConsumerMessage;
|
|
use Hyperf\DbConnection\Db;
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
|
|
|
/**
|
|
* 处方过期未使用
|
|
*/
|
|
#[Consumer(nums: 1)]
|
|
class PrescriptionExpiredDelayDirectConsumer extends ConsumerMessage
|
|
{
|
|
use ProducerDelayedMessageTrait;
|
|
use ConsumerDelayedMessageTrait;
|
|
|
|
protected string $exchange = 'amqp.delay.direct';
|
|
|
|
protected ?string $queue = 'prescription.expired.delay.queue';
|
|
|
|
protected string $type = Type::DIRECT; //Type::FANOUT;
|
|
|
|
protected string|array $routingKey = 'PrescriptionExpired';
|
|
|
|
public function consumeMessage($data, AMQPMessage $message): string
|
|
{
|
|
Log::getInstance()->error("开始执行 自动取消未使用处方 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
// 验证参数
|
|
if (!isset($data['order_prescription_id'])){
|
|
Db::rollBack();
|
|
Log::getInstance()->error("自动取消未使用处方队列执行失败:入参错误");
|
|
return Result::DROP;
|
|
}
|
|
|
|
// 获取处方数据
|
|
$params = array();
|
|
$params['order_prescription_id'] = $data['order_prescription_id'];
|
|
$order_prescription = OrderPrescription::getOne($params);
|
|
if (empty($order_prescription)){
|
|
Db::rollBack();
|
|
Log::getInstance()->error("自动取消未使用处方队列执行失败:获取处方数据为空");
|
|
return Result::DROP;
|
|
}
|
|
|
|
// 检测处方审核、使用状态
|
|
$res = $this->checkPrescriptionStatus($order_prescription);
|
|
if (!$res){
|
|
Db::rollBack();
|
|
return Result::ACK;
|
|
}
|
|
|
|
// 处理处方过期状态
|
|
|
|
|
|
|
|
Db::commit();
|
|
Log::getInstance()->info("自动取消未使用处方 队列执行成功");
|
|
return Result::ACK;
|
|
} catch (\Exception $e) {
|
|
Db::rollBack();
|
|
Log::getInstance()->error("自动取消未使用处方 执行失败:" . $e->getMessage());
|
|
return Result::ACK; // 重回队列
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检测处方审核状态
|
|
* @param array|object $order_prescription
|
|
* @return bool
|
|
*/
|
|
protected function checkPrescriptionStatus(array|object $order_prescription): bool
|
|
{
|
|
if ($order_prescription['prescription_status'] == 3){
|
|
Db::rollBack();
|
|
Log::getInstance()->info("自动取消未使用处方队列执行失败:处方已失效,无需处理");
|
|
return false;
|
|
}
|
|
|
|
if ($order_prescription['prescription_status'] == 4){
|
|
Db::rollBack();
|
|
Log::getInstance()->info("自动取消未使用处方队列执行失败:处方已使用,无需处理");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 处理处方
|
|
protected function handlePrescription(array|object $order_prescription){
|
|
$pamras = array();
|
|
$pamras['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
|
|
|
$data = array();
|
|
$data['prescription_status'] = 3;
|
|
|
|
}
|
|
}
|