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

96 lines
2.8 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\adminapi\logic\tcm;
use app\common\logic\BaseLogic;
use app\common\model\tcm\TrackingNote;
/**
* 诊单跟踪备注 Logic
*
* - 按 diagnosis_id + 当天 find-or-create
* - 多次追加 content(换行分隔,带 [HH:MM] 前缀)
* - 不涉及附件(与医生备注 DoctorNoteLogic 不同)
*/
class TrackingNoteLogic extends BaseLogic
{
/**
* 追加跟踪备注(按天合并)
*
* @param array $params diagnosis_id (int)、admin_id (int)、content (string)
* @return bool
*/
public static function addOrAppend(array $params): bool
{
try {
$diagnosisId = (int) ($params['diagnosis_id'] ?? 0);
$adminId = (int) ($params['admin_id'] ?? 0);
$newContent = trim((string) ($params['content'] ?? ''));
if ($diagnosisId <= 0) {
self::setError('诊单ID缺失');
return false;
}
if ($newContent === '') {
self::setError('备注内容不能为空');
return false;
}
$today = date('Y-m-d');
$time = date('H:i');
$line = "[{$time}] {$newContent}";
$existing = TrackingNote::where('diagnosis_id', $diagnosisId)
->where('note_date', $today)
->whereNull('delete_time')
->find();
if ($existing) {
$prev = trim((string) ($existing->content ?? ''));
$existing->content = $prev !== '' ? ($prev . "\n" . $line) : $line;
$existing->save();
} else {
TrackingNote::create([
'diagnosis_id' => $diagnosisId,
'admin_id' => $adminId,
'note_date' => $today,
'content' => $line,
]);
}
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* 按 diagnosis_id 获取跟踪备注列表(note_date DESC
*
* @param int $diagnosisId
* @param int $limit 最近 N 天
* @return array
*/
public static function getByDiagnosis(int $diagnosisId, int $limit = 60): array
{
try {
if ($diagnosisId <= 0) {
return [];
}
$records = TrackingNote::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->order('note_date', 'desc')
->limit($limit)
->select()
->toArray();
return $records;
} catch (\Exception $e) {
self::setError($e->getMessage());
return [];
}
}
}