This commit is contained in:
2026-04-29 18:41:39 +08:00
parent dc899e4af4
commit 256771c384
103 changed files with 2942 additions and 17057 deletions
@@ -10,6 +10,12 @@ use app\common\model\tcm\Diagnosis;
use app\common\model\auth\Admin;
use app\api\logic\ChatNotifyLogic;
use app\adminapi\logic\tcm\PrescriptionLogic;
use app\adminapi\logic\tcm\DiagnosisLogic;
use app\adminapi\logic\tcm\BloodRecordLogic;
use app\adminapi\logic\doctor\DoctorNoteLogic;
use app\common\model\tcm\Prescription;
use app\common\service\wechat\WechatWorkAppMessageService;
use app\common\model\dict\DictData;
use think\facade\Db;
use think\facade\Log;
@@ -444,4 +450,261 @@ class AppointmentLogic extends BaseLogic
return false;
}
}
/**
* @notes 接诊台聚合:单次返回挂号信息 + 诊单病例 + 血糖血压记录 + 助理姓名
* 数据复用:detail() / DiagnosisLogic::detail() / BloodRecordLogic::getRecordsByPatient()
* 备注:appointment.patient_id 实际对应 tcm_diagnosis.id
* @param array $params
* @return array
*/
public static function reception(array $params): array
{
// 1) 挂号详情(已包含 patient_name / patient_phone / doctor_name / status_desc 等)
$appointment = self::detail($params);
if (empty($appointment)) {
return [];
}
// 2) 诊单(病例)详情:appointment.patient_id == tcm_diagnosis.id
$diagnosisId = (int) ($appointment['patient_id'] ?? 0);
$diagnosis = $diagnosisId > 0
? DiagnosisLogic::detail(['id' => $diagnosisId])
: [];
// 3) 血糖血压记录
$bloodRecords = $diagnosisId > 0
? BloodRecordLogic::getRecordsByPatient(['diagnosis_id' => $diagnosisId])
: [];
// 4) 补全医助姓名(detail() 未关联 admin 助理表)
$assistantName = '';
if (!empty($diagnosis['assistant_id'])) {
$assistant = Admin::where('id', (int) $diagnosis['assistant_id'])->find();
if ($assistant) {
$assistantName = $assistant->name ?? '';
}
}
$appointment['assistant_name'] = $assistantName;
$appointment['assistant_id'] = $diagnosis['assistant_id'] ?? 0;
// 5) 是否已开方(与列表口径一致:未删除 & 未作废)
$hasPrescription = 0;
if ($diagnosisId > 0) {
$rxExists = Prescription::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->where('void_status', 0)
->find();
$hasPrescription = $rxExists ? 1 : 0;
}
$appointment['has_prescription'] = $hasPrescription;
// 6) 对诊单字段进行字典/枚举翻译(产出 *_text 附加字段,不覆盖原 value)
$diagnosis = self::enrichDiagnosisLabels($diagnosis);
// 7) 医生备注
$doctorNotes = $diagnosisId > 0
? DoctorNoteLogic::getByDiagnosis($diagnosisId)
: [];
return [
'appointment' => $appointment,
'diagnosis' => $diagnosis,
'blood_records' => $bloodRecords,
'doctor_notes' => $doctorNotes,
];
}
/**
* 字典翻译:把诊单中以 code 存储的字段(字典/枚举)补一份中文 label 字段,
* 便于前端(接诊台等只读场景)直接渲染,无需加载字典。
* 原始 code 字段保留不动,edit 场景仍可使用。
*
* @param array $diagnosis
* @return array
*/
private static function enrichDiagnosisLabels(array $diagnosis): array
{
if (empty($diagnosis)) {
return $diagnosis;
}
// 单值字段:value 为字典 value,翻译成字典 name
$singleDictFields = [
'diagnosis_type' => 'diagnosis_type',
'syndrome_type' => 'syndrome_type',
'diabetes_type' => 'diabetes_type',
'water_intake' => 'water_intake',
'weight_change' => 'weight_change',
'fatty_liver_degree' => 'fatty_liver_degree',
];
// 多值字段:value 为数组或逗号分隔字符串
$multiDictFields = [
'past_history' => 'past_history',
'appetite' => 'appetite',
'diet_condition' => 'diet_condition',
'body_feeling' => 'body_feeling',
'sleep_condition' => 'sleep_condition',
'eye_condition' => 'eye_condition',
'head_feeling' => 'head_feeling',
'sweat_condition' => 'sweat_condition',
'skin_condition' => 'skin_condition',
'urine_condition' => 'urine_condition',
'stool_condition' => 'stool_condition',
'kidney_condition' => 'kidney_condition',
];
// 一次性查出用到的所有字典
$allTypes = array_values(array_unique(array_merge(array_values($singleDictFields), array_values($multiDictFields))));
$dictMap = self::buildDictMap($allTypes);
foreach ($singleDictFields as $field => $type) {
if (!array_key_exists($field, $diagnosis)) {
continue;
}
$diagnosis[$field . '_text'] = self::dictLabel($dictMap, $type, $diagnosis[$field]);
}
foreach ($multiDictFields as $field => $type) {
if (!array_key_exists($field, $diagnosis)) {
continue;
}
$v = $diagnosis[$field];
$arr = is_array($v)
? $v
: (is_string($v) && $v !== '' ? preg_split('/[,,、]/', $v) : []);
$labels = [];
foreach ($arr as $item) {
$item = is_string($item) ? trim($item) : $item;
if ($item === '' || $item === null) {
continue;
}
$labels[] = self::dictLabel($dictMap, $type, $item);
}
$diagnosis[$field . '_text'] = implode('、', array_filter($labels));
}
// 枚举字段
$diagnosis['gender_text'] = isset($diagnosis['gender']) ? self::genderText((int) $diagnosis['gender']) : '';
$diagnosis['marital_status_text'] = array_key_exists('marital_status', $diagnosis) ? self::maritalText($diagnosis['marital_status']) : '';
foreach (['trauma_history', 'surgery_history', 'allergy_history', 'family_history', 'pregnancy_history'] as $f) {
if (array_key_exists($f, $diagnosis)) {
$diagnosis[$f . '_text'] = self::yesNoText($diagnosis[$f]);
}
}
return $diagnosis;
}
/**
* 构建 type_value => [value => name] 的两级查询映射
*
* @param array $types
* @return array
*/
private static function buildDictMap(array $types): array
{
if (empty($types)) {
return [];
}
$rows = DictData::whereIn('type_value', $types)
->field(['type_value', 'value', 'name'])
->select()
->toArray();
$map = [];
foreach ($rows as $r) {
$tv = (string) ($r['type_value'] ?? '');
$v = (string) ($r['value'] ?? '');
$n = (string) ($r['name'] ?? '');
if ($tv === '' || $v === '') {
continue;
}
$map[$tv][$v] = $n;
}
return $map;
}
/**
* 找不到字典项时回显原值(避免空白)
*/
private static function dictLabel(array $dictMap, string $type, $value): string
{
if ($value === null || $value === '') {
return '';
}
$key = (string) $value;
return $dictMap[$type][$key] ?? $key;
}
private static function genderText(int $gender): string
{
if ($gender === 1) return '男';
if ($gender === 0) return '女';
return '';
}
private static function maritalText($v): string
{
if ($v === null || $v === '') return '';
$n = (int) $v;
if ($n === 0) return '未婚';
if ($n === 1) return '已婚';
if ($n === 2) return '离异';
return '';
}
private static function yesNoText($v): string
{
if ($v === null || $v === '') return '';
return ((int) $v) === 1 ? '有' : '无';
}
/**
* @notes 通知接诊医助(按诊单 assistant_id 查企微 userid 后发应用消息)
* 文案固定:马上面诊到你对应的患者了,请联系患者进入诊室等待。
* @param int $appointmentId
* @return array{ok: bool, message?: string}
*/
public static function notifyAssistant(int $appointmentId): array
{
try {
$appointment = Appointment::findOrEmpty($appointmentId);
if ($appointment->isEmpty()) {
return ['ok' => false, 'message' => '挂号记录不存在'];
}
$diagnosis = Diagnosis::where('id', (int) $appointment->patient_id)->find();
if (!$diagnosis) {
return ['ok' => false, 'message' => '未找到对应的诊单'];
}
$assistantId = (int) ($diagnosis->assistant_id ?? 0);
if ($assistantId <= 0) {
return ['ok' => false, 'message' => '该患者尚未指派医助'];
}
$assistant = Admin::whereNull('delete_time')->find($assistantId);
if (!$assistant) {
return ['ok' => false, 'message' => '医助账号不存在'];
}
$wxId = trim((string) ($assistant->work_wechat_userid ?? ''));
if ($wxId === '') {
Log::warning("接诊台通知医助跳过:医助 admin_id={$assistantId} 未绑定 work_wechat_userid");
return [
'ok' => false,
'message' => '该医助未绑定企业微信账号,无法推送消息(请其在后台「扫码绑定企业微信」后再试)',
];
}
$patientName = (string) ($diagnosis->patient_name ?? '患者');
$text = "【接诊通知】马上面诊到你对应的患者了,请联系患者进入诊室等待。\n患者:{$patientName}\n时间:" . (string) $appointment->appointment_date . ' ' . (string) $appointment->appointment_time;
$result = WechatWorkAppMessageService::sendTextToUser($wxId, $text);
if (empty($result['ok'])) {
Log::warning('接诊台通知医助失败:' . ($result['message'] ?? ''));
}
return $result;
} catch (\Throwable $e) {
Log::error('接诊台通知医助异常:' . $e->getMessage());
return ['ok' => false, 'message' => '企业微信通知异常:' . $e->getMessage()];
}
}
}
@@ -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 [];
}
}
}