Files
zyt/server/app/api/logic/ChatNotifyLogic.php
T
2026-05-11 17:49:38 +08:00

119 lines
3.7 KiB
PHP
Executable File

<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use think\facade\Cache;
/**
* 聊天打开通知逻辑(患者打开会话时通知医生)
* 使用缓存存储,支持多消息队列
*/
class ChatNotifyLogic extends BaseLogic
{
const CACHE_PREFIX = 'chat_open_notify:';
const CACHE_TTL = 86400; // 24小时
const MAX_PER_DOCTOR = 50; // 每个医生最多保留条数
/**
* 添加患者打开会话通知
* @param int $doctorId 医生ID (admin_id)
* @param string $patientId 患者/诊单ID
* @param string $patientName 患者姓名
* @return bool
*/
public static function addNotify(int $doctorId, string $patientId, string $patientName): bool
{
$key = self::CACHE_PREFIX . $doctorId;
$item = [
'id' => uniqid('', true),
'type' => 'patient_opened_chat',
'doctor_id' => $doctorId,
'patient_id' => $patientId,
'patient_name' => $patientName,
'created_at' => time(),
];
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
array_unshift($list, $item);
$list = array_slice($list, 0, self::MAX_PER_DOCTOR);
Cache::set($key, $list, self::CACHE_TTL);
return true;
}
/**
* 患者离开会话页/诊室时通知医生端(管理后台轮询)
*/
public static function addPatientLeftNotify(int $doctorId, string $patientId, string $patientName): bool
{
$key = self::CACHE_PREFIX . $doctorId;
$item = [
'id' => uniqid('', true),
'type' => 'patient_left_chat',
'doctor_id' => $doctorId,
'patient_id' => $patientId,
'patient_name' => $patientName,
'created_at' => time(),
];
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
array_unshift($list, $item);
$list = array_slice($list, 0, self::MAX_PER_DOCTOR);
Cache::set($key, $list, self::CACHE_TTL);
return true;
}
/**
* 添加面诊结束通知(医生完成接诊后通知对应医助)
* @param int $assistantId 医助ID (admin_id)
* @param string $patientName 患者姓名
* @param string $doctorName 医生姓名
* @param int $diagnosisId 诊单ID
* @return bool
*/
public static function addConsultationCompleteNotify(int $assistantId, string $patientName, string $doctorName, int $diagnosisId = 0): bool
{
$key = self::CACHE_PREFIX . $assistantId;
$item = [
'id' => uniqid('', true),
'type' => 'consultation_complete',
'assistant_id' => $assistantId,
'patient_name' => $patientName,
'doctor_name' => $doctorName,
'diagnosis_id' => $diagnosisId,
'created_at' => time(),
];
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
array_unshift($list, $item);
$list = array_slice($list, 0, self::MAX_PER_DOCTOR);
Cache::set($key, $list, self::CACHE_TTL);
return true;
}
/**
* 获取未读通知(医生/医助通用,按 admin_id 获取)
* @param int $adminId 管理员ID (医生或医助)
* @param bool $consume 是否消费(移除)已获取的
* @return array
*/
public static function getNotifies(int $adminId, bool $consume = true): array
{
$key = self::CACHE_PREFIX . $adminId;
$list = Cache::get($key) ?: [];
if (!is_array($list)) {
$list = [];
}
if ($consume && !empty($list)) {
Cache::delete($key);
}
return $list;
}
}