196 lines
7.4 KiB
PHP
196 lines
7.4 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryVideoRecord;
|
||
use App\Model\OrderInquiryVideoReservation;
|
||
use App\Model\User;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\VideoRecord;
|
||
use App\Model\VideoReservation;
|
||
use App\Services\SendSmsService;
|
||
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 SendVideoNoticeDelayDirectConsumer extends ConsumerMessage
|
||
{
|
||
use ProducerDelayedMessageTrait;
|
||
use ConsumerDelayedMessageTrait;
|
||
|
||
protected string $exchange = 'amqp.delay.direct';
|
||
|
||
protected ?string $queue = 'send.video.notice.delay.queue';
|
||
|
||
protected string $type = Type::DIRECT; //Type::FANOUT;
|
||
|
||
protected string|array $routingKey = 'SendVideoNotice';
|
||
|
||
public function consumeMessage($data, AMQPMessage $message): string
|
||
{
|
||
Log::getInstance("queue-SendVideoNotice")->info("开始:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
// 检测参数
|
||
if (!isset($data['order_inquiry_id'])){
|
||
Log::getInstance("queue-SendVideoNotice")->error("入参错误");
|
||
return Result::DROP;
|
||
}
|
||
|
||
// 获取问诊订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $data['order_inquiry_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单错误" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 7) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单已取消" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 5) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单已完成" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 6) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单已结束" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 3) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单待接诊" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 1) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单待支付" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
if (!in_array($order_inquiry['inquiry_refund_status'], [0, 4, 5])) {
|
||
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
Log::getInstance("queue-SendVideoNotice")->error("订单存在退款" );
|
||
return Result::DROP;
|
||
}
|
||
|
||
// 获取视频预约状态
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $data['order_inquiry_id'];
|
||
$order_inquiry_video_reservation = OrderInquiryVideoReservation::getOne($params);
|
||
if (empty($order_inquiry_video_reservation)){
|
||
Log::getInstance("queue-SendVideoNotice")->error("缺少视频预约数据" );
|
||
return Result::DROP;
|
||
}
|
||
|
||
if ($order_inquiry_video_reservation['is_send_reservation_notice'] == 1){
|
||
Log::getInstance("queue-SendVideoNotice")->info("已发送过一次通知,无需发送" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测是否已开过视频
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry_video_reservation['order_inquiry_id'];
|
||
$order_inquiry_video_record = OrderInquiryVideoRecord::getOne($params);
|
||
if (!empty($order_inquiry_video_record)) {
|
||
Log::getInstance("queue-SendVideoNotice")->info("已开过视频,无需发送" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 检测通知时间
|
||
$reservation_time = strtotime($order_inquiry_video_reservation['reservation_time']);
|
||
if ($reservation_time < time()){
|
||
Log::getInstance("queue-SendVideoNotice")->info("当前时间已超过预约时间,无需发送" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 5分钟以内
|
||
$diff_time = $reservation_time - time();
|
||
if ($diff_time > 5* 60){
|
||
Log::getInstance("queue-SendVideoNotice")->info("不在发送时间内,无需发送" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取患者用户数据
|
||
$params = array();
|
||
$params['user_id'] = $order_inquiry['user_id'];
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
Log::getInstance("queue-SendVideoNotice")->error("患者用户数据错误" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
Log::getInstance("queue-SendVideoNotice")->error("医生数据错误" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 获取医生用户表数据
|
||
$params = array();
|
||
$params['user_id'] = $user_doctor['user_id'];
|
||
$doctor_user = User::getOne($params);
|
||
if (empty($doctor_user)){
|
||
Log::getInstance("queue-SendVideoNotice")->error("医生用户数据错误" );
|
||
return Result::ACK;
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 患者-通知患者视频
|
||
$sendSmsService = new SendSmsService($user['mobile']);
|
||
$sendSmsService->noticePatientVideo($user_doctor['user_name']);
|
||
|
||
// 医生-通知医生视频
|
||
$sendSmsService = new SendSmsService($doctor_user['mobile']);
|
||
$sendSmsService->noticeDoctorVideo($order_inquiry['patient_name']);
|
||
|
||
// 修改通知表
|
||
$data = array();
|
||
$data['is_send_reservation_notice'] = 1;
|
||
|
||
$params = array();
|
||
$params['inquiry_video_id'] = $order_inquiry_video_reservation['inquiry_video_id'];
|
||
$res = VideoReservation::edit($params,$data);
|
||
if (!$res){
|
||
Db::rollBack();
|
||
Log::getInstance("queue-SendVideoNotice")->error("修改失败");
|
||
return Result::ACK;
|
||
}
|
||
|
||
Db::commit();
|
||
}catch (\Throwable $e){
|
||
Db::rollBack();
|
||
Log::getInstance("queue-SendVideoNotice")->error($e->getMessage());
|
||
return Result::ACK;
|
||
}
|
||
|
||
return Result::ACK;
|
||
}
|
||
}
|