新增功能
This commit is contained in:
@@ -16,6 +16,7 @@ namespace app\adminapi\logic\tcm;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
use app\common\model\tcm\DiagnosisAssignLog;
|
||||
use app\common\model\tcm\ImChatMessage;
|
||||
use app\common\model\DiagnosisViewRecord;
|
||||
use think\facade\Db;
|
||||
@@ -91,11 +92,11 @@ class DiagnosisLogic extends BaseLogic
|
||||
if (isset($params['diagnosis_date'])) {
|
||||
$params['diagnosis_date'] = strtotime($params['diagnosis_date']);
|
||||
}
|
||||
|
||||
|
||||
$model = Diagnosis::create($params);
|
||||
|
||||
// 自动为患者创建 TRTC 账号
|
||||
self::createPatientTrtcAccount($model->patient_id);
|
||||
self::createPatientTrtcAccount($model->id);
|
||||
|
||||
return $model->id;
|
||||
} catch (\Exception $e) {
|
||||
@@ -450,23 +451,102 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 指派医助
|
||||
* @notes 指派医助(每次成功更新写入一条操作记录,批量指派为多次接口调用各记一条)
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public static function assign(array $params): bool
|
||||
{
|
||||
try {
|
||||
Diagnosis::where('id', $params['id'])->update([
|
||||
'assistant_id' => $params['assistant_id']
|
||||
$id = (int) ($params['id'] ?? 0);
|
||||
$toAssistantId = (int) ($params['assistant_id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
self::setError('诊单不存在');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$diagnosis = Diagnosis::where('id', $id)->whereNull('delete_time')->find();
|
||||
if (!$diagnosis) {
|
||||
self::setError('诊单不存在');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$fromAssistantId = (int) ($diagnosis->getAttr('assistant_id') ?? 0);
|
||||
|
||||
Db::startTrans();
|
||||
Diagnosis::where('id', $id)->whereNull('delete_time')->update([
|
||||
'assistant_id' => $toAssistantId,
|
||||
]);
|
||||
|
||||
$req = request();
|
||||
$admin = $req->adminInfo ?? [];
|
||||
DiagnosisAssignLog::create([
|
||||
'diagnosis_id' => $id,
|
||||
'from_assistant_id' => $fromAssistantId,
|
||||
'to_assistant_id' => $toAssistantId,
|
||||
'operator_admin_id' => (int) ($admin['admin_id'] ?? 0),
|
||||
'operator_name' => (string) ($admin['name'] ?? ''),
|
||||
'operator_account' => (string) ($admin['account'] ?? ''),
|
||||
'ip' => (string) ($req->ip() ?? ''),
|
||||
'create_time' => time(),
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 诊单指派医助操作记录列表
|
||||
*/
|
||||
public static function assignLogList(int $diagnosisId): array
|
||||
{
|
||||
$rows = DiagnosisAssignLog::where('diagnosis_id', $diagnosisId)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$adminIds = [];
|
||||
foreach ($rows as $r) {
|
||||
if (!empty($r['from_assistant_id'])) {
|
||||
$adminIds[] = (int) $r['from_assistant_id'];
|
||||
}
|
||||
if (!empty($r['to_assistant_id'])) {
|
||||
$adminIds[] = (int) $r['to_assistant_id'];
|
||||
}
|
||||
}
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds)));
|
||||
$nameMap = [];
|
||||
if ($adminIds !== []) {
|
||||
$nameMap = \app\common\model\auth\Admin::whereIn('id', $adminIds)->column('name', 'id');
|
||||
}
|
||||
|
||||
foreach ($rows as &$r) {
|
||||
$fromId = (int) ($r['from_assistant_id'] ?? 0);
|
||||
$toId = (int) ($r['to_assistant_id'] ?? 0);
|
||||
$r['from_assistant_name'] = $fromId > 0
|
||||
? (string) ($nameMap[$fromId] ?? ('ID:' . $fromId))
|
||||
: '—';
|
||||
$r['to_assistant_name'] = $toId > 0
|
||||
? (string) ($nameMap[$toId] ?? ('ID:' . $toId))
|
||||
: '—';
|
||||
$r['create_time_text'] = !empty($r['create_time'])
|
||||
? date('Y-m-d H:i:s', (int) $r['create_time'])
|
||||
: '';
|
||||
}
|
||||
unset($r);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取通话签名
|
||||
* @param array $params
|
||||
@@ -1152,47 +1232,195 @@ class DiagnosisLogic extends BaseLogic
|
||||
{
|
||||
try {
|
||||
// 获取当前管理员ID(从参数中获取)
|
||||
$adminId = $params['admin_id'] ?? 0;
|
||||
|
||||
if (!$adminId) {
|
||||
$adminId = (int)($params['admin_id'] ?? 0);
|
||||
$diagnosisId = (int)($params['diagnosis_id'] ?? 0);
|
||||
|
||||
if ($adminId <= 0) {
|
||||
self::setError('获取管理员信息失败');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 查找最近的通话记录
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $params['diagnosis_id'])
|
||||
if ($diagnosisId <= 0) {
|
||||
self::setError('诊单ID无效');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 优先匹配「当前医生 + 进行中」,与 startCloudRecording / bindCallRoom 一致
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('caller_id', $adminId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
|
||||
if ($record) {
|
||||
$taskId = trim((string)($record['cloud_recording_task_id'] ?? ''));
|
||||
if ($taskId !== '') {
|
||||
\app\common\service\TrtcCloudRecordingService::stopRecording(
|
||||
\app\common\service\TrtcCloudRecordingService::trtcSdkAppId(),
|
||||
$taskId
|
||||
);
|
||||
if (!$record) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if ($record) {
|
||||
\think\facade\Log::warning('endCall: 未匹配 caller_id,已回退到该诊单最新进行中记录', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'admin_id' => $adminId,
|
||||
'record_caller_id' => $record['caller_id'] ?? null,
|
||||
'call_record_id' => $record['id'] ?? null,
|
||||
]);
|
||||
}
|
||||
$endTime = time();
|
||||
$duration = $endTime - $record->start_time;
|
||||
|
||||
$record->save([
|
||||
'status' => 2, // 2-已结束
|
||||
'end_time' => $endTime,
|
||||
'duration' => $duration,
|
||||
'cloud_recording_task_id' => '',
|
||||
'update_time' => time(),
|
||||
}
|
||||
|
||||
if (!$record) {
|
||||
// 前端常重复回调 endCall(afterCalling + Store idle),第一条已结束则不再告警
|
||||
$latest = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
$justEnded = $latest
|
||||
&& (int)($latest['status'] ?? 0) === 2
|
||||
&& (int)($latest['end_time'] ?? 0) > 0
|
||||
&& (time() - (int)$latest['end_time']) < 120;
|
||||
if (!$justEnded) {
|
||||
\think\facade\Log::warning('endCall: 无进行中通话记录,未调用 DeleteCloudRecording', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'admin_id' => $adminId,
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$taskId = trim((string)($record['cloud_recording_task_id'] ?? ''));
|
||||
if ($taskId !== '') {
|
||||
$stopped = \app\common\service\TrtcCloudRecordingService::stopRecording(
|
||||
\app\common\service\TrtcCloudRecordingService::trtcSdkAppId(),
|
||||
$taskId
|
||||
);
|
||||
if (empty($stopped['ok'])) {
|
||||
\think\facade\Log::warning('endCall: DeleteCloudRecording 失败', [
|
||||
'message' => (string)($stopped['message'] ?? ''),
|
||||
'task_id' => $taskId,
|
||||
'call_record_id' => $record['id'] ?? null,
|
||||
'room_id' => $record['room_id'] ?? '',
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
\think\facade\Log::warning('endCall: cloud_recording_task_id 为空,未调用 DeleteCloudRecording;控制台「房间尚未结束」常见于录制机器人仍在房或未走 API 合流录制', [
|
||||
'call_record_id' => $record['id'] ?? null,
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'room_id' => $record['room_id'] ?? '',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
$endTime = time();
|
||||
$duration = $endTime - (int)$record->start_time;
|
||||
|
||||
// 保留 cloud_recording_task_id:VOD 311 回调常在挂断之后到达,需按 TaskId 关联写入 recording_urls
|
||||
$record->save([
|
||||
'status' => 2, // 2-已结束
|
||||
'end_time' => $endTime,
|
||||
'duration' => $duration,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 患者端挂断:停止云端录制并结束该诊单下对应通话记录(不依赖管理端浏览器是否触发 endCall)
|
||||
* @param array{diagnosis_id?:int,patient_id:int,room_id?:string} $params patient_id 须与诊单 tcm_diagnosis.patient_id 一致;room_id 与 bindCallRoom 一致时可精准命中(小程序通话页独立时 chat 页可能已卸载,仅靠 room_id + patient_id 即可)
|
||||
*/
|
||||
public static function patientHangupVideoCall(array $params): bool
|
||||
{
|
||||
try {
|
||||
$diagnosisId = (int)($params['diagnosis_id'] ?? 0);
|
||||
$patientId = (int)($params['patient_id'] ?? 0);
|
||||
$roomIdRaw = trim((string)($params['room_id'] ?? ''));
|
||||
if ($patientId <= 0) {
|
||||
self::setError('参数错误');
|
||||
|
||||
return false;
|
||||
}
|
||||
if ($diagnosisId <= 0 && $roomIdRaw === '') {
|
||||
self::setError('诊单ID或房间号须至少传一项');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$record = null;
|
||||
if ($roomIdRaw !== '') {
|
||||
$roomCandidates = array_values(array_unique(array_filter([
|
||||
$roomIdRaw,
|
||||
ctype_digit($roomIdRaw) ? (string)((int)$roomIdRaw) : '',
|
||||
])));
|
||||
$record = \app\common\model\tcm\CallRecord::where('status', 1)
|
||||
->whereIn('room_id', $roomCandidates)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if ($record) {
|
||||
$diagByRoom = Diagnosis::find((int)$record['diagnosis_id']);
|
||||
if (!$diagByRoom || (int)($diagByRoom['patient_id'] ?? 0) !== $patientId) {
|
||||
$record = null;
|
||||
} else {
|
||||
$diagnosisId = (int)$record['diagnosis_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$record && $diagnosisId > 0) {
|
||||
$diag = Diagnosis::find($diagnosisId);
|
||||
if (!$diag || (int)($diag['patient_id'] ?? 0) !== $patientId) {
|
||||
self::setError('无权操作该诊单');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('callee_id', $patientId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if (!$record) {
|
||||
$cnt = (int)\app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)->where('status', 1)->count();
|
||||
if ($cnt === 1) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$record) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$taskId = trim((string)($record['cloud_recording_task_id'] ?? ''));
|
||||
if ($taskId !== '') {
|
||||
\app\common\service\TrtcCloudRecordingService::stopRecording(
|
||||
\app\common\service\TrtcCloudRecordingService::trtcSdkAppId(),
|
||||
$taskId
|
||||
);
|
||||
}
|
||||
$endTime = time();
|
||||
$duration = $endTime - (int)$record->start_time;
|
||||
$record->save([
|
||||
'status' => 2,
|
||||
'end_time' => $endTime,
|
||||
'duration' => $duration,
|
||||
'update_time' => $endTime,
|
||||
]);
|
||||
\think\facade\Log::info('patientHangupVideoCall: 已停录并结束通话记录', [
|
||||
'call_record_id' => $record->id,
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
\think\facade\Log::warning('patientHangupVideoCall: ' . $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取通话记录
|
||||
* @param array $params
|
||||
@@ -1229,9 +1457,10 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 将 TRTC 房间号写入当前诊单最近一次通话记录,便于云端录制回调按 room_id 关联
|
||||
* @notes 将 TRTC 房间号写入当前诊单通话记录,并尝试 API 合流云端录制
|
||||
* @return array{cloud_recording?:array}|false 成功返回 data 数组(供接口带给前端);失败 false
|
||||
*/
|
||||
public static function bindCallRoom(array $params): bool
|
||||
public static function bindCallRoom(array $params)
|
||||
{
|
||||
try {
|
||||
$diagnosisId = (int)($params['diagnosis_id'] ?? 0);
|
||||
@@ -1241,10 +1470,23 @@ class DiagnosisLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
$adminId = (int)($params['admin_id'] ?? 0);
|
||||
|
||||
// 必须与 startCloudRecording 使用同一条「进行中 + 当前管理员」记录写 room_id,否则会写到别的记录上,合流 API 读到 room_id 仍为空 → 关闭全局录制后无任何文件
|
||||
$record = null;
|
||||
if ($adminId > 0) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->where('caller_id', $adminId)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
}
|
||||
if (!$record) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
}
|
||||
if (!$record) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('room_id', '')
|
||||
@@ -1261,15 +1503,49 @@ class DiagnosisLogic extends BaseLogic
|
||||
'update_time' => time(),
|
||||
]);
|
||||
|
||||
$adminId = (int)($params['admin_id'] ?? 0);
|
||||
$cloudPayload = [
|
||||
'started' => false,
|
||||
'task_id' => '',
|
||||
'message' => '未尝试合流录制(admin_id 为空)',
|
||||
];
|
||||
if ($adminId > 0) {
|
||||
self::startCloudRecording([
|
||||
$cloudRec = self::startCloudRecording([
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'admin_id' => $adminId,
|
||||
], true);
|
||||
if ($cloudRec === false) {
|
||||
\think\facade\Log::warning('bindCallRoom: startCloudRecording 未执行或异常', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'room_id' => $roomId,
|
||||
]);
|
||||
$cloudPayload = [
|
||||
'started' => false,
|
||||
'task_id' => '',
|
||||
'message' => '合流录制未启动(无匹配通话记录或异常,见服务端日志)',
|
||||
];
|
||||
} elseif (is_array($cloudRec)) {
|
||||
$cloudPayload = [
|
||||
'started' => !empty($cloudRec['started']),
|
||||
'task_id' => (string)($cloudRec['task_id'] ?? ''),
|
||||
'message' => (string)($cloudRec['message'] ?? ''),
|
||||
];
|
||||
if (empty($cloudRec['started'])) {
|
||||
\think\facade\Log::warning('bindCallRoom: CreateCloudRecording 合流未启动', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'room_id' => $roomId,
|
||||
'message' => $cloudPayload['message'],
|
||||
]);
|
||||
} else {
|
||||
\think\facade\Log::info('bindCallRoom: 合流云端录制已发起', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'room_id' => $roomId,
|
||||
'task_id' => $cloudPayload['task_id'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return ['cloud_recording' => $cloudPayload];
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
@@ -1302,6 +1578,19 @@ class DiagnosisLogic extends BaseLogic
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if (!$record) {
|
||||
$record = \app\common\model\tcm\CallRecord::where('diagnosis_id', $diagnosisId)
|
||||
->where('status', 1)
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
if ($record) {
|
||||
\think\facade\Log::warning('startCloudRecording: 未找到 caller_id 匹配的进行中记录,已回退到该诊单最新进行中记录', [
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'admin_id' => $adminId,
|
||||
'record_caller_id' => $record['caller_id'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
if (!$record) {
|
||||
if (!$silent) {
|
||||
self::setError('没有进行中的通话记录');
|
||||
@@ -1337,11 +1626,13 @@ class DiagnosisLogic extends BaseLogic
|
||||
$botSig = \app\common\service\TrtcCloudRecordingService::makeBotUserSig($botUserId);
|
||||
$sdkAppId = \app\common\service\TrtcCloudRecordingService::trtcSdkAppId();
|
||||
|
||||
$vodMixPrefix = 'mix_' . $diagnosisId . '_' . (int)$record['id'];
|
||||
$r = \app\common\service\TrtcCloudRecordingService::startMixRecording(
|
||||
$sdkAppId,
|
||||
$roomId,
|
||||
$botUserId,
|
||||
$botSig
|
||||
$botSig,
|
||||
$vodMixPrefix
|
||||
);
|
||||
if (!$r['ok']) {
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user