1
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace app\adminapi\logic\doctor;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\doctor\DoctorNote;
|
||||
|
||||
class DoctorNoteLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* 按 diagnosis_id + 当天 find-or-create,追加 content / tongue_images
|
||||
*/
|
||||
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 = $params['tongue_images'] ?? [];
|
||||
if (is_string($newImages)) {
|
||||
$newImages = json_decode($newImages, true) ?: [];
|
||||
}
|
||||
|
||||
if ($existing) {
|
||||
$data = [];
|
||||
|
||||
if ($newContent !== '') {
|
||||
$prev = trim($existing->content ?? '');
|
||||
$line = "[{$time}] {$newContent}";
|
||||
$data['content'] = $prev !== '' ? ($prev . "\n" . $line) : $line;
|
||||
}
|
||||
|
||||
if (!empty($newImages)) {
|
||||
$prev = $existing->tongue_images;
|
||||
if (is_string($prev)) {
|
||||
$prev = json_decode($prev, true) ?: [];
|
||||
}
|
||||
if (!is_array($prev)) {
|
||||
$prev = [];
|
||||
}
|
||||
$merged = array_values(array_unique(array_merge($prev, $newImages)));
|
||||
if (count($merged) > 9) {
|
||||
$merged = array_slice($merged, 0, 9);
|
||||
}
|
||||
$data['tongue_images'] = 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,
|
||||
]);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
$domain = request()->domain();
|
||||
|
||||
foreach ($records as &$record) {
|
||||
$images = $record['tongue_images'] ?? [];
|
||||
if (is_string($images)) {
|
||||
$images = json_decode($images, true) ?: [];
|
||||
}
|
||||
if (!is_array($images)) {
|
||||
$images = [];
|
||||
}
|
||||
$record['tongue_images'] = array_map(function ($url) use ($domain) {
|
||||
if (empty($url)) return $url;
|
||||
if (stripos($url, 'http://') === 0 || stripos($url, 'https://') === 0) {
|
||||
return $url;
|
||||
}
|
||||
return $domain . '/' . $url;
|
||||
}, $images);
|
||||
}
|
||||
|
||||
return $records;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user