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 []; } }