Files
zyt/server/app/adminapi/logic/doctor/DoctorNoteLogic.php
T
2026-04-30 10:58:53 +08:00

121 lines
4.2 KiB
PHP
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\doctor;
use app\common\logic\BaseLogic;
use app\common\model\doctor\DoctorNote;
use app\common\service\FileService;
class DoctorNoteLogic extends BaseLogic
{
/**
* 按 diagnosis_id + 当天 find-or-create,追加 content / tongue_images / report_files
*/
public static function addOrAppend(array $params): bool
{
try {
$diagnosisId = (int) $params['diagnosis_id'];
$doctorId = (int) ($params['doctor_id'] ?? 0);
$today = date('Y-m-d');
$time = date('H:i');
$existing = DoctorNote::where('diagnosis_id', $diagnosisId)
->where('note_date', $today)
->whereNull('delete_time')
->find();
$newContent = trim($params['content'] ?? '');
$newImages = self::parseJsonArray($params['tongue_images'] ?? []);
$newReports = self::parseJsonArray($params['report_files'] ?? []);
if ($existing) {
$data = [];
if ($newContent !== '') {
$prev = trim($existing->content ?? '');
$line = "[{$time}] {$newContent}";
$data['content'] = $prev !== '' ? ($prev . "\n" . $line) : $line;
}
if (!empty($newImages)) {
$prev = self::parseJsonArray($existing->tongue_images);
$merged = array_values(array_unique(array_merge($prev, $newImages)));
$data['tongue_images'] = json_encode($merged, JSON_UNESCAPED_UNICODE);
}
if (!empty($newReports)) {
$prev = self::parseJsonArray($existing->report_files);
$merged = array_values(array_unique(array_merge($prev, $newReports)));
$data['report_files'] = json_encode($merged, JSON_UNESCAPED_UNICODE);
}
if (!empty($data)) {
$existing->save($data);
}
} else {
$content = $newContent !== '' ? "[{$time}] {$newContent}" : '';
DoctorNote::create([
'diagnosis_id' => $diagnosisId,
'doctor_id' => $doctorId,
'note_date' => $today,
'content' => $content,
'tongue_images' => !empty($newImages)
? json_encode($newImages, JSON_UNESCAPED_UNICODE)
: null,
'report_files' => !empty($newReports)
? json_encode($newReports, JSON_UNESCAPED_UNICODE)
: null,
]);
}
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* 按 diagnosis_id 获取备注列表(note_date DESC
*/
public static function getByDiagnosis(int $diagnosisId, int $limit = 30): array
{
try {
if ($diagnosisId <= 0) {
return [];
}
$records = DoctorNote::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->order('note_date', 'desc')
->limit($limit)
->select()
->toArray();
foreach ($records as &$record) {
$record['tongue_images'] = array_map(function ($url) {
return empty($url) ? $url : FileService::getFileUrl($url);
}, self::parseJsonArray($record['tongue_images'] ?? []));
$record['report_files'] = array_map(function ($url) {
return empty($url) ? $url : FileService::getFileUrl($url);
}, self::parseJsonArray($record['report_files'] ?? []));
}
return $records;
} catch (\Exception $e) {
self::setError($e->getMessage());
return [];
}
}
private static function parseJsonArray($value): array
{
if (is_array($value)) return $value;
if (is_string($value)) {
$decoded = json_decode($value, true);
return is_array($decoded) ? $decoded : [];
}
return [];
}
}