832 lines
33 KiB
PHP
832 lines
33 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Amqp\Producer\AutoFinishInquiryDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\DoctorAccount;
|
||
use App\Model\DoctorAccountDay;
|
||
use App\Model\OrderEvaluation;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCase;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\PatientFamily;
|
||
use App\Model\PatientFamilyHealth;
|
||
use App\Model\PatientFamilyPersonal;
|
||
use App\Model\PatientPathography;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserPatient;
|
||
use App\Services\ImService;
|
||
use App\Services\MessagePush;
|
||
use App\Services\ReportRegulatoryService;
|
||
use App\Utils\Log;
|
||
use App\Utils\Mask;
|
||
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 AutoFinishInquiryDelayDirectConsumer extends ConsumerMessage
|
||
{
|
||
use ProducerDelayedMessageTrait;
|
||
use ConsumerDelayedMessageTrait;
|
||
|
||
protected string $exchange = 'amqp.delay.direct';
|
||
|
||
protected ?string $queue = 'auto.finish.inquiry.delay.queue';
|
||
|
||
protected string $type = Type::DIRECT; //Type::FANOUT;
|
||
|
||
protected string|array $routingKey = 'AutoFinishInquiry';
|
||
|
||
public function consumeMessage($data, AMQPMessage $message): string
|
||
{
|
||
Log::getInstance("queue-AutoFinishInquiry")->info("开始:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
// 检测入参参数
|
||
if (empty($data['order_inquiry_id'])) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->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-AutoFinishInquiry")->error("问诊订单数据为空");
|
||
return Result::DROP;
|
||
}
|
||
|
||
$order_inquiry = $order_inquiry->toArray();
|
||
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
if ($order_inquiry['inquiry_status'] != 5) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("问诊订单未完成,无法结束");
|
||
return Result::DROP;
|
||
}
|
||
|
||
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
|
||
if (!in_array($order_inquiry['inquiry_refund_status'], [0, 4, 5])) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("问诊订单正在申请退款");
|
||
return Result::DROP;
|
||
}
|
||
|
||
if (empty($order_inquiry['doctor_id'])) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("医生id为空");
|
||
return Result::DROP;
|
||
}
|
||
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("医生为空");
|
||
return Result::DROP;
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 检测执行次数
|
||
$result = $this->checkExecutionCount($data['order_inquiry_id']);
|
||
if (empty($result)) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("检测订单执行次数错误");
|
||
return Result::ACK;
|
||
}
|
||
|
||
// 超出执行次数
|
||
if ($result['code'] == 2) {
|
||
Db::rollBack();
|
||
return Result::ACK;
|
||
}
|
||
|
||
/**
|
||
* 处理统计问题。规则:
|
||
* 1、金额大于0
|
||
* 2、快速、购药类型下非坐班医生
|
||
*/
|
||
if ($order_inquiry['amount_total'] > 0) {
|
||
$is_handle_doctor_account = 1;
|
||
|
||
if ($user_doctor['is_platform_deep_cooperation'] == 1 && in_array($order_inquiry['inquiry_type'],[2,4])){
|
||
$is_handle_doctor_account = 0;
|
||
}
|
||
|
||
if ($is_handle_doctor_account == 1){
|
||
// 处理医生账户总表
|
||
$res = $this->handleDoctorAccount($order_inquiry);
|
||
if (!$res) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("处理医生账户总表失败");
|
||
return Result::REQUEUE;
|
||
}
|
||
|
||
// 处理医生账户表-日
|
||
$res = $this->handleDoctorAccountDay($order_inquiry);
|
||
if (!$res) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("处理医生账户表-日失败");
|
||
return Result::REQUEUE;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 处理问诊订单表
|
||
$this->handleOrderInquiry($order_inquiry);
|
||
|
||
// 处理订单评价
|
||
$this->handleOrderEvaluation($order_inquiry);
|
||
|
||
// 处理回写患者病例-回写失败不做处理
|
||
$this->handleOrderInquiryCase($order_inquiry);
|
||
|
||
// 处理回写患者病情记录-回写失败不做处理
|
||
$this->handlePatientPathography($order_inquiry);
|
||
|
||
// 处理医生服务患者数量
|
||
$this->handleDoctorServedPatientsNum($order_inquiry['doctor_id']);
|
||
|
||
// 计算医生好评率
|
||
$this->computeDoctorPraiseRate($order_inquiry['doctor_id']);
|
||
|
||
// 计算医生平均响应时间
|
||
$this->computeDoctorAvgPesponseTime($order_inquiry['doctor_id']);
|
||
|
||
// 新增上报监管平台数据,只上报 多点执业的医生
|
||
if ($user_doctor['multi_point_status'] == 1){//医生多点执业认证状态(0:未认证 1:认证通过 2:审核中 3:认证失败)
|
||
$reportRegulatoryService = new ReportRegulatoryService();
|
||
$res = $reportRegulatoryService->addReportRegulatory($order_inquiry['order_inquiry_id']);
|
||
if (!$res) {
|
||
// 新增上报失败
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("新增上报监管平台数据失败");
|
||
return Result::REQUEUE;
|
||
}
|
||
}else{
|
||
Log::getInstance("queue-AutoFinishInquiry")->info("非多点执业的医生不上报监管平台");
|
||
}
|
||
|
||
Db::commit();
|
||
Log::getInstance("queue-AutoFinishInquiry")->info("成功");
|
||
} catch (\Throwable $e) {
|
||
Db::rollBack();
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("错误:" . $e->getMessage());
|
||
return Result::REQUEUE; // 重回队列
|
||
}
|
||
|
||
// 推送消息
|
||
try {
|
||
// 推送问诊结束im消息
|
||
$this->pushImInquiryFinish($order_inquiry);
|
||
} catch (\Exception $e) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("发送消息异常错误:" . $e->getMessage());
|
||
}
|
||
|
||
return Result::ACK;
|
||
}
|
||
|
||
/**
|
||
* 处理医生账户总表
|
||
* 创建/修改医生账户总表中总金额、账户余额数据
|
||
* @param array|object $order_inquiry
|
||
* @return bool
|
||
*/
|
||
protected function handleDoctorAccount(array|object $order_inquiry): bool
|
||
{
|
||
$amount_total = bcmul((string)$order_inquiry['amount_total'],"0.75",8);
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$doctor_account = DoctorAccount::getOne($params);
|
||
if (empty($doctor_account)) {
|
||
// 首次
|
||
$data = array();
|
||
$data['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$data['total_amount'] = $amount_total;
|
||
$data['balance_account'] = $amount_total;
|
||
$data['applied_withdrawal_amount'] = 0;
|
||
$data['actual_withdrawal_amount'] = 0;
|
||
$data['income_tax'] = 0;
|
||
$doctor_account = DoctorAccount::addDoctorAccount($data);
|
||
if (empty($doctor_account)) {
|
||
return false;
|
||
}
|
||
} else {
|
||
// 非首次
|
||
// 总金额(已结束订单的总金额)
|
||
$params = array();
|
||
$params['account_id'] = $doctor_account['account_id'];
|
||
DoctorAccount::inc($params, 'total_amount', (float)$amount_total);
|
||
|
||
// 账户余额
|
||
DoctorAccount::inc($params, 'balance_account', (float)$amount_total);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 处理医生账户表-日
|
||
* @param array|object $order_inquiry
|
||
* @return bool
|
||
*/
|
||
protected function handleDoctorAccountDay(array|object $order_inquiry): bool
|
||
{
|
||
$amount_total = bcmul((string)$order_inquiry['amount_total'],"0.75",8);
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$params['date'] = date('Y-m-d', strtotime($order_inquiry['created_at']));
|
||
$doctor_account_day = DoctorAccountDay::getOne($params);
|
||
if (empty($doctor_account_day)) {
|
||
// 当日首次
|
||
$data = array();
|
||
$data['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$data['year'] = date('Y', strtotime($order_inquiry['created_at']));
|
||
$data['month'] = date('m', strtotime($order_inquiry['created_at']));
|
||
$data['day'] = date('d', strtotime($order_inquiry['created_at']));
|
||
$data['date'] = date('Y-m-d', strtotime($order_inquiry['created_at']));
|
||
$data['total_amount'] = $amount_total;
|
||
$doctor_account_day = DoctorAccountDay::addDoctorAccountDay($data);
|
||
if (empty($doctor_account_day)) {
|
||
return false;
|
||
}
|
||
} else {
|
||
// 非当日首次
|
||
$params = array();
|
||
$params['account_detail_id'] = $doctor_account_day['account_detail_id'];
|
||
DoctorAccountDay::inc($params, 'total_amount', (float)$amount_total);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 处理问诊订单表
|
||
* @param array|object $order_inquiry
|
||
* @return void
|
||
*/
|
||
protected function handleOrderInquiry(array|object $order_inquiry): void
|
||
{
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
|
||
$data = array();
|
||
$data['inquiry_status'] = 6; // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
$data['finish_time'] = date('Y-m-d H:i:s', time()); // 订单结束时间
|
||
$data['statistics_status'] = 1; // 订单统计状态(0:未统计 1:已统计 2:统计失败)
|
||
$data['statistics_time'] = date('Y-m-d H:i:s', time()); // 订单统计时间
|
||
$data['updated_at'] = date('Y-m-d H:i:s', time()); // 修改时间
|
||
OrderInquiry::edit($params, $data);
|
||
}
|
||
|
||
/**
|
||
* 处理订单评价
|
||
* @param array|object $order_inquiry
|
||
* @return bool
|
||
*/
|
||
protected function handleOrderEvaluation(array|object $order_inquiry): bool
|
||
{
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$order_evaluation = OrderEvaluation::getOne($params);
|
||
if (!empty($order_evaluation)) {
|
||
// 已评价
|
||
return true;
|
||
}
|
||
|
||
// 未评价
|
||
$data = array();
|
||
$data['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$data['patient_id'] = $order_inquiry['patient_id'];
|
||
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$data['name_mask'] = Mask::maskNameStr($order_inquiry['patient_name'], 2);
|
||
$data['reply_quality'] = 100;//回复质量(百分制)
|
||
$data['service_attitude'] = 100; // 服务态度(百分制)
|
||
$data['reply_progress'] = 100; // 回复速度(百分制)
|
||
$data['avg_score'] = 100; // 平均得分(百分制,回复质量占4、服务态度占3、回复速度占3,计算公式:每个得分 * 占比 相加)
|
||
$data['type'] = 1; // 类型(1:默认评价 2:主动评价)
|
||
$data['content'] = "感谢您的服务";
|
||
$order_evaluation = OrderEvaluation::addOrderEvaluation($data);
|
||
if (empty($order_evaluation)) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 处理回写患者病例
|
||
* 回写失败不做处理
|
||
* @param array|object $order_inquiry
|
||
* @return bool
|
||
*/
|
||
protected function handleOrderInquiryCase(array|object $order_inquiry): bool
|
||
{
|
||
try {
|
||
// 获取订单-问诊病例表
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$order_inquiry_case = OrderInquiryCase::getOne($params);
|
||
if (empty($order_inquiry_case)) {
|
||
return false;
|
||
}
|
||
|
||
// 获取患者家庭成员信息表-基本信息
|
||
$params = array();
|
||
$params['family_id'] = $order_inquiry['family_id'];
|
||
$params['patient_id'] = $order_inquiry['patient_id'];
|
||
$params['status'] = 1;
|
||
$patient_family = PatientFamily::getOne($params);
|
||
if (empty($patient_family)){
|
||
return false;
|
||
}
|
||
|
||
$data = array();
|
||
|
||
// 身高
|
||
if ($patient_family['height'] == null && $order_inquiry_case['height'] != null){
|
||
$data['height'] = $order_inquiry_case['height'];
|
||
}
|
||
|
||
// 体重
|
||
if ($patient_family['weight'] == null && $order_inquiry_case['weight'] != null){
|
||
$data['weight'] = $order_inquiry_case['weight'];
|
||
}
|
||
|
||
// 婚姻状况(0:未婚 1:已婚 2:离异)
|
||
if ($patient_family['marital_status'] == null && $order_inquiry_case['marital_status'] != null){
|
||
$data['marital_status'] = $order_inquiry_case['marital_status'];
|
||
}
|
||
|
||
// 民族
|
||
if ($patient_family['nation_id'] == null && $order_inquiry_case['nation_id'] != null){
|
||
$data['nation_id'] = $order_inquiry_case['nation_id'];
|
||
|
||
// 民族名称
|
||
if ($patient_family['nation_name'] == null && $order_inquiry_case['nation_name'] != null){
|
||
$data['nation_name'] = $order_inquiry_case['nation_name'];
|
||
}
|
||
}
|
||
|
||
// 职业
|
||
if ($patient_family['job_id'] == null && $order_inquiry_case['job_id'] != null){
|
||
$data['job_id'] = $order_inquiry_case['job_id'];
|
||
|
||
// 职业名称
|
||
if ($patient_family['job_name'] == null && $order_inquiry_case['job_name'] != null){
|
||
$data['job_name'] = $order_inquiry_case['job_name'];
|
||
}
|
||
}
|
||
|
||
if (!empty($data)) {
|
||
$params = array();
|
||
$params['family_id'] = $patient_family['family_id'];
|
||
PatientFamily::edit($params, $data);
|
||
}
|
||
|
||
// 获取患者家庭成员信息表-健康情况
|
||
$params = array();
|
||
$params['family_id'] = $order_inquiry['family_id'];
|
||
$params['patient_id'] = $order_inquiry['patient_id'];
|
||
$patient_family_health = PatientFamilyHealth::getOne($params);
|
||
if (empty($patient_family_health)) {
|
||
// 表数据为空
|
||
$data = array();
|
||
$data['family_id'] = $order_inquiry['family_id'];
|
||
$data['patient_id'] = $order_inquiry['patient_id'];
|
||
$data['disease_class_id'] = $order_inquiry_case['disease_class_id']; // 疾病分类id-系统
|
||
$data['disease_class_name'] = $order_inquiry_case['disease_class_name']; // 疾病名称-系统
|
||
$data['diagnosis_date'] = $order_inquiry_case['diagnosis_date']; // 确诊日期
|
||
$data['diagnosis_hospital'] = $order_inquiry_case['diagnosis_hospital']; // 确诊医院
|
||
$data['is_take_medicine'] = $order_inquiry_case['is_take_medicine']; // 正在服药(0:否 1:是)
|
||
$data['drugs_name'] = $order_inquiry_case['drugs_name']; // 正在服药名称
|
||
$patient_family_health = PatientFamilyHealth::addPatientFamilyHealth($data);
|
||
if (empty($patient_family_health)) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("回写患者家庭成员信息表-健康情况表失败");
|
||
}
|
||
} else {
|
||
$data = array();
|
||
|
||
// 疾病分类id-系统
|
||
if ($patient_family_health['disease_class_id'] == null && $order_inquiry_case['disease_class_id'] != null) {
|
||
$data['disease_class_id'] = $order_inquiry_case['disease_class_id'];
|
||
|
||
// 疾病名称-系统
|
||
if ($patient_family_health['disease_class_name'] == null && $order_inquiry_case['disease_class_name'] != null) {
|
||
$data['disease_class_name'] = $order_inquiry_case['disease_class_name'];
|
||
}
|
||
}
|
||
|
||
// 确诊日期
|
||
if ($patient_family_health['diagnosis_date'] == null && $order_inquiry_case['diagnosis_date'] != null) {
|
||
$data['diagnosis_date'] = $order_inquiry_case['diagnosis_date'];
|
||
}
|
||
|
||
// 确诊医院
|
||
if ($patient_family_health['diagnosis_hospital'] == null && $order_inquiry_case['diagnosis_hospital'] != null) {
|
||
$data['diagnosis_hospital'] = $order_inquiry_case['diagnosis_hospital'];
|
||
}
|
||
|
||
// 正在服药
|
||
if ($patient_family_health['is_take_medicine'] == null && $order_inquiry_case['is_take_medicine'] != null) {
|
||
$data['is_take_medicine'] = $order_inquiry_case['is_take_medicine'];
|
||
|
||
// 正在服药名称
|
||
if ($patient_family_health['drugs_name'] == null && $order_inquiry_case['drugs_name'] != null) {
|
||
$data['drugs_name'] = $order_inquiry_case['drugs_name'];
|
||
}
|
||
}
|
||
|
||
if (!empty($data)) {
|
||
$params = array();
|
||
$params['family_health_id'] = $patient_family_health['family_health_id'];
|
||
PatientFamilyHealth::edit($params, $data);
|
||
}
|
||
}
|
||
|
||
// 获取患者家庭成员信息表-个人情况
|
||
$params = array();
|
||
$params['family_id'] = $order_inquiry['family_id'];
|
||
$params['patient_id'] = $order_inquiry['patient_id'];
|
||
$patient_family_personal = PatientFamilyPersonal::getOne($params);
|
||
if (empty($patient_family_personal)) {
|
||
$data = array();
|
||
$data['family_id'] = $order_inquiry['family_id'];
|
||
$data['patient_id'] = $order_inquiry['patient_id'];
|
||
|
||
// 是否存在过敏史(0:否 1:是)
|
||
if ($order_inquiry_case['is_allergy_history'] !== null) {
|
||
$data['is_allergy_history'] = $order_inquiry_case['is_allergy_history'];
|
||
|
||
// 过敏史描述
|
||
if ($order_inquiry_case['allergy_history'] != null) {
|
||
$data['allergy_history'] = $order_inquiry_case['allergy_history'];
|
||
}
|
||
}
|
||
|
||
|
||
// 是否存在家族病史(0:否 1:是)
|
||
if ($order_inquiry_case['is_family_history'] !== null) {
|
||
$data['is_family_history'] = $order_inquiry_case['is_family_history'];
|
||
|
||
// 家族病史描述
|
||
if ($order_inquiry_case['family_history'] != null) {
|
||
$data['family_history'] = $order_inquiry_case['family_history'];
|
||
}
|
||
}
|
||
|
||
// 是否备孕、妊娠、哺乳期(0:否 1:是)
|
||
if ($order_inquiry_case['is_pregnant'] !== null) {
|
||
$data['is_pregnant'] = $order_inquiry_case['is_pregnant'];
|
||
|
||
// 是否备孕、妊娠、哺乳期(0:否 1:是)
|
||
if ($order_inquiry_case['pregnant'] != null) {
|
||
$data['pregnant'] = $order_inquiry_case['pregnant'];
|
||
}
|
||
}
|
||
|
||
$patient_family_personal = PatientFamilyPersonal::addPatientFamilyPersonal($data);
|
||
if (empty($patient_family_personal)) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("回写患者家庭成员信息表-个人情况表失败");
|
||
}
|
||
} else {
|
||
$data = array();
|
||
|
||
// 是否存在过敏史(0:否 1:是)
|
||
if ($patient_family_personal['is_allergy_history'] == null && $order_inquiry_case['is_allergy_history'] != null){
|
||
$data['is_allergy_history'] = $order_inquiry_case['is_allergy_history'];
|
||
|
||
// 过敏史描述
|
||
if ($patient_family_personal['allergy_history'] == null && $order_inquiry_case['allergy_history'] != null){
|
||
$data['allergy_history'] = $order_inquiry_case['allergy_history'];
|
||
}
|
||
}
|
||
|
||
// 是否存在家族病史(0:否 1:是)
|
||
if ($patient_family_personal['is_family_history'] == null && $order_inquiry_case['is_family_history'] != null){
|
||
$data['is_family_history'] = $order_inquiry_case['is_family_history'];
|
||
|
||
// 家族病史描述
|
||
if ($patient_family_personal['family_history'] == null && $order_inquiry_case['family_history'] != null){
|
||
$data['family_history'] = $order_inquiry_case['family_history'];
|
||
}
|
||
}
|
||
|
||
// 是否备孕、妊娠、哺乳期(0:否 1:是)
|
||
if ($patient_family_personal['is_pregnant'] == null && $order_inquiry_case['is_pregnant'] != null){
|
||
$data['is_pregnant'] = $order_inquiry_case['is_pregnant'];
|
||
|
||
// 备孕、妊娠、哺乳期描述
|
||
if ($patient_family_personal['pregnant'] == null && $order_inquiry_case['pregnant'] != null){
|
||
$data['pregnant'] = $order_inquiry_case['pregnant'];
|
||
}
|
||
}
|
||
|
||
// 是否存在手术(0:否 1:是)
|
||
if ($patient_family_personal['is_operation'] == null && $order_inquiry_case['is_operation'] != null){
|
||
$data['is_operation'] = $order_inquiry_case['is_operation'];
|
||
|
||
// 手术描述
|
||
if ($patient_family_personal['operation'] == null && $order_inquiry_case['operation'] != null){
|
||
$data['operation'] = $order_inquiry_case['operation'];
|
||
}
|
||
}
|
||
|
||
// 饮酒状态(1:从不 2:偶尔 3:经常 4:每天 5:已戒酒)
|
||
if ($patient_family_personal['drink_wine_status'] == null && $order_inquiry_case['drink_wine_status'] != null){
|
||
$data['drink_wine_status'] = $order_inquiry_case['drink_wine_status'];
|
||
}
|
||
|
||
// 吸烟状态(1:从不 2:偶尔 3:经常 4:每天 5:已戒烟)
|
||
if ($patient_family_personal['smoke_status'] == null && $order_inquiry_case['smoke_status'] != null){
|
||
$data['smoke_status'] = $order_inquiry_case['smoke_status'];
|
||
}
|
||
|
||
// 化合物状态(1:从不 2:偶尔 3:经常 4:每天)
|
||
if ($patient_family_personal['chemical_compound_status'] == null && $order_inquiry_case['chemical_compound_status'] != null){
|
||
$data['chemical_compound_status'] = $order_inquiry_case['chemical_compound_status'];
|
||
|
||
// 化合物描述
|
||
if ($patient_family_personal['chemical_compound_describe'] == null && $order_inquiry_case['chemical_compound_describe'] != null){
|
||
$data['chemical_compound_describe'] = $order_inquiry_case['chemical_compound_describe'];
|
||
}
|
||
}
|
||
|
||
if (!empty($data)) {
|
||
$params = array();
|
||
$params['family_personal_id'] = $patient_family_personal['family_personal_id'];
|
||
PatientFamilyPersonal::edit($params, $data);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}catch (\Throwable $e){
|
||
Log::getInstance("queue-AutoFinishInquiry")->error($e->getMessage());
|
||
return true;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理医生服务患者数量
|
||
* @param string $doctor_id
|
||
* @return void
|
||
*/
|
||
protected function handleDoctorServedPatientsNum(string $doctor_id): void
|
||
{
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
UserDoctor::inc($params, 'served_patients_num');
|
||
}
|
||
|
||
/**
|
||
* 处理医生好评率
|
||
* @param string $doctor_id
|
||
* @return int|void
|
||
*/
|
||
protected function computeDoctorPraiseRate(string $doctor_id)
|
||
{
|
||
// 获取该医生全部问诊评价总数
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
$order_evaluation_count = OrderEvaluation::getCount($params);
|
||
if ($order_evaluation_count == 0) {
|
||
return 0;
|
||
}
|
||
|
||
// 获取该医生好评的评价总数
|
||
$avg_score_params = [80, 100];
|
||
$good_quantity = OrderEvaluation::getScoreCount($params, $avg_score_params);
|
||
|
||
$praise_rate = floor(($good_quantity / $order_evaluation_count) * 100);
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
|
||
$data = array();
|
||
$data['praise_rate'] = $praise_rate;
|
||
UserDoctor::editUserDoctor($params, $data);
|
||
}
|
||
|
||
/**
|
||
* 处理医生平均响应时间
|
||
* @param string $doctor_id
|
||
* @return void
|
||
*/
|
||
protected function computeDoctorAvgPesponseTime(string $doctor_id): void
|
||
{
|
||
// 统计数组
|
||
$avg_response_time = [];
|
||
|
||
// 获取该医生全部已结束问诊订单
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
$params['inquiry_status'] = 6;
|
||
$order_inquiry = OrderInquiry::getList($params);
|
||
if (!empty($order_inquiry)) {
|
||
$order_inquiry = $order_inquiry->toArray();
|
||
foreach ($order_inquiry as $item) {
|
||
if (empty($item['reception_time'])) {
|
||
continue;
|
||
}
|
||
|
||
$response_time = strtotime($item['reception_time']) - strtotime($item['created_at']);
|
||
if ($response_time < 0) {
|
||
// 小于0的数据不统计
|
||
continue;
|
||
}
|
||
$avg_response_time[] = $response_time;
|
||
}
|
||
|
||
if (!empty($avg_response_time)) {
|
||
$result = floor((array_sum($avg_response_time) / count($avg_response_time)) / 60 * 100) / 100;
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
|
||
$data = array();
|
||
$data['avg_response_time'] = $result;
|
||
UserDoctor::editUserDoctor($params, $data);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 推送问诊结束im消息
|
||
* @param array|object $order_inquiry
|
||
* @return void
|
||
*/
|
||
protected function pushImInquiryFinish(array|object $order_inquiry): void
|
||
{
|
||
// 当前用户是否在这三天内再次咨询了该医生
|
||
$is_inquiry = false;
|
||
|
||
$params = array();
|
||
$params[] = ['user_id', '=', $order_inquiry['user_id']];
|
||
$params[] = ['doctor_id', '=', $order_inquiry['doctor_id']];
|
||
$params[] = ['created_at', '>', $order_inquiry['created_at']];
|
||
$order_inquirys = OrderInquiry::getList($params);
|
||
if (!empty($order_inquirys)) {
|
||
foreach ($order_inquirys as $item) {
|
||
if ($item['order_inquiry_id'] == $order_inquiry['order_inquiry_id']) {
|
||
continue;
|
||
}
|
||
$is_inquiry = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!$is_inquiry) {
|
||
// 获取医生数据
|
||
$params = array();
|
||
$params['doctor_id'] = $order_inquiry['doctor_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (!empty($user_doctor)) {
|
||
// 发送IM消息-问诊已结束
|
||
$imService = new ImService();
|
||
$imService->inquiryFinish($order_inquiry, $user_doctor['user_id'], $order_inquiry['user_id']);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理回写患者病情记录
|
||
* 回写失败不做处理
|
||
* @param array|object $order_inquiry
|
||
* @return bool
|
||
*/
|
||
protected function handlePatientPathography(array|object $order_inquiry): bool
|
||
{
|
||
if ($order_inquiry['inquiry_type'] != 1 && $order_inquiry['inquiry_type'] != 2 && $order_inquiry['inquiry_type'] != 3 && $order_inquiry['inquiry_type'] != 4) {
|
||
// 非问诊订单,不进行处理
|
||
return true;
|
||
}
|
||
|
||
// 获取患者家庭成员病情记录
|
||
$params = array();
|
||
$params['user_id'] = $order_inquiry['user_id'];
|
||
$params['patient_id'] = $order_inquiry['patient_id'];
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$patient_pathography = PatientPathography::getOne($params);
|
||
if (!empty($patient_pathography)) {
|
||
return false;
|
||
}
|
||
|
||
// 获取订单-问诊病例表
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry["order_inquiry_id"];
|
||
$order_inquiry_case = OrderInquiryCase::getOne($params);
|
||
if (empty($order_inquiry_case)) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("错误:缺少问诊病例表");
|
||
return false;
|
||
}
|
||
|
||
$order_inquiry_case = $order_inquiry_case->toArray();
|
||
|
||
$data = array();
|
||
foreach ($order_inquiry_case as $key => $value) {
|
||
// 主键跳过
|
||
if ($key == "inquiry_case_id") {
|
||
continue;
|
||
}
|
||
|
||
if ($key == "created_at") {
|
||
continue;
|
||
}
|
||
|
||
if ($key == "updated_at") {
|
||
continue;
|
||
}
|
||
|
||
if ($value == null) {
|
||
continue;
|
||
}
|
||
|
||
$data[$key] = $value;
|
||
}
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (!empty($order_prescription)) {
|
||
$data['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
}
|
||
|
||
// 新增病情记录
|
||
$patient_pathography = PatientPathography::addPatientPathography($data);
|
||
if (empty($patient_pathography)) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error("错误:回写病情记录失败");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 检测执行次数
|
||
* @param string $redis_key
|
||
* @return array
|
||
*/
|
||
protected function checkExecutionCount(string $redis_key): array
|
||
{
|
||
// code:0:异常 1:正常 2:超出最大执行次数
|
||
$result = array();
|
||
$result['code'] = 0;
|
||
$result['message'] = "";
|
||
|
||
try {
|
||
$redis = $this->container->get(Redis::class);
|
||
} catch (\Throwable $e) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error($e->getMessage());
|
||
$result['message'] = $e->getMessage();
|
||
return $result;
|
||
}
|
||
|
||
try {
|
||
$redis_value = $redis->get($redis_key);
|
||
if (empty($redis_value)) {
|
||
$redis->set($redis_key, 1, 60 * 60 * 24 * 1);
|
||
|
||
$result['code'] = 1;
|
||
return $result;
|
||
}
|
||
|
||
// 执行次数超出4次
|
||
if ($redis_value > 4) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->info("超出最大执行次数");
|
||
// 加入短信队列,通知管理员
|
||
|
||
$result['code'] = 2;
|
||
$result['message'] = "超出最大执行次数";
|
||
return $result;
|
||
}
|
||
|
||
$redis->incr($redis_key);
|
||
} catch (\Throwable $e) {
|
||
Log::getInstance("queue-AutoFinishInquiry")->error($e->getMessage());
|
||
$result['message'] = $e->getMessage();
|
||
return $result;
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
}
|