115 lines
3.6 KiB
PHP
115 lines
3.6 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("queue-PrescriptionExpired")->info(json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
// 验证参数
|
|
if (!isset($data['order_prescription_id'])){
|
|
Db::rollBack();
|
|
Log::getInstance("queue-PrescriptionExpired")->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("queue-PrescriptionExpired")->error("获取处方数据为空");
|
|
return Result::DROP;
|
|
}
|
|
|
|
// 检测处方审核、使用状态
|
|
$res = $this->checkPrescriptionStatus($order_prescription);
|
|
if (!$res){
|
|
Db::rollBack();
|
|
return Result::ACK;
|
|
}
|
|
|
|
// 处理处方过期状态
|
|
$this->handlePrescription($order_prescription);
|
|
|
|
Db::commit();
|
|
Log::getInstance("queue-PrescriptionExpired")->info("成功");
|
|
return Result::ACK;
|
|
} catch (\Exception $e) {
|
|
Db::rollBack();
|
|
Log::getInstance("queue-PrescriptionExpired")->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("queue-PrescriptionExpired")->info("处方已失效,无需处理");
|
|
return false;
|
|
}
|
|
|
|
if ($order_prescription['prescription_status'] == 4){
|
|
Db::rollBack();
|
|
Log::getInstance("queue-PrescriptionExpired")->info("处方已使用,无需处理");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 处理处方过期状态
|
|
* @param array|object $order_prescription
|
|
* @return void
|
|
*/
|
|
protected function handlePrescription(array|object $order_prescription): void
|
|
{
|
|
$params = array();
|
|
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
|
|
|
$data = array();
|
|
$data['prescription_status'] = 3;
|
|
$data['expired_time'] = date('Y-m-d H:i:s',time());
|
|
OrderPrescription::edit($params,$data);
|
|
}
|
|
}
|