Merge branch 'master' of https://gitee.com/v_cms/zyt
This commit is contained in:
@@ -5,6 +5,7 @@ namespace app\adminapi\controller\doctor;
|
||||
use app\adminapi\controller\BaseAdminController;
|
||||
use app\adminapi\lists\doctor\AppointmentLists;
|
||||
use app\adminapi\logic\doctor\AppointmentLogic;
|
||||
use app\adminapi\logic\doctor\DoctorNoteLogic;
|
||||
use app\adminapi\validate\doctor\AppointmentValidate;
|
||||
|
||||
/**
|
||||
@@ -104,4 +105,60 @@ class AppointmentController extends BaseAdminController
|
||||
}
|
||||
return $this->success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 接诊台聚合详情(患者信息 + 诊单病例 + 血糖血压记录)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function reception()
|
||||
{
|
||||
$params = (new AppointmentValidate())->goCheck('reception');
|
||||
$result = AppointmentLogic::reception($params);
|
||||
return $this->data($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 通知接诊医助(发企业微信)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function notifyAssistant()
|
||||
{
|
||||
$params = (new AppointmentValidate())->post()->goCheck('notifyAssistant');
|
||||
$result = AppointmentLogic::notifyAssistant((int) $params['id']);
|
||||
if (empty($result['ok'])) {
|
||||
return $this->fail($result['message'] ?? '通知失败');
|
||||
}
|
||||
return $this->success('通知已发送');
|
||||
}
|
||||
|
||||
public function addDoctorNote()
|
||||
{
|
||||
$params = (new AppointmentValidate())->post()->goCheck('addDoctorNote');
|
||||
$params['doctor_id'] = $this->adminId;
|
||||
$result = DoctorNoteLogic::addOrAppend($params);
|
||||
if ($result === false) {
|
||||
return $this->fail(DoctorNoteLogic::getError());
|
||||
}
|
||||
return $this->success('保存成功');
|
||||
}
|
||||
|
||||
public function doctorNotes()
|
||||
{
|
||||
$params = (new AppointmentValidate())->goCheck('doctorNotes');
|
||||
return $this->data(DoctorNoteLogic::getByDiagnosis((int) $params['diagnosis_id']));
|
||||
}
|
||||
|
||||
public function deleteDoctorNoteImage()
|
||||
{
|
||||
$params = (new AppointmentValidate())->post()->goCheck('deleteDoctorNoteImage');
|
||||
$result = DoctorNoteLogic::deleteImage(
|
||||
(int) $params['note_id'],
|
||||
$params['image_type'],
|
||||
$params['image_path']
|
||||
);
|
||||
if ($result === false) {
|
||||
return $this->fail(DoctorNoteLogic::getError());
|
||||
}
|
||||
return $this->success('删除成功');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,12 @@ use app\common\model\tcm\DiagnosisGuahaoLog;
|
||||
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;
|
||||
|
||||
@@ -489,6 +495,263 @@ class AppointmentLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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,205 @@
|
||||
<?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 = array_map([self::class, 'toRelativePath'], self::parseJsonArray($params['tongue_images'] ?? []));
|
||||
$newReports = array_map([self::class, 'toRelativePath'], 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 [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除备注中的单张图片
|
||||
*/
|
||||
public static function deleteImage(int $noteId, string $imageType, string $imagePath): bool
|
||||
{
|
||||
try {
|
||||
$note = DoctorNote::where('id', $noteId)->whereNull('delete_time')->find();
|
||||
if (!$note) {
|
||||
self::setError('记录不存在');
|
||||
return false;
|
||||
}
|
||||
if (!in_array($imageType, ['tongue_images', 'report_files'])) {
|
||||
self::setError('类型无效');
|
||||
return false;
|
||||
}
|
||||
$images = self::parseJsonArray($note->$imageType);
|
||||
// 统一转为相对路径再匹配
|
||||
$targetPath = self::toRelativePath($imagePath);
|
||||
$images = array_values(array_filter($images, fn($url) => self::toRelativePath($url) !== $targetPath));
|
||||
$note->$imageType = empty($images) ? null : json_encode($images, JSON_UNESCAPED_UNICODE);
|
||||
$note->save();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚合某诊单所有备注中的图片(供 DiagnosisLogic::detail 使用)
|
||||
*/
|
||||
public static function getAggregatedImages(int $diagnosisId): array
|
||||
{
|
||||
$records = DoctorNote::where('diagnosis_id', $diagnosisId)
|
||||
->whereNull('delete_time')
|
||||
->select();
|
||||
|
||||
$tongueImages = [];
|
||||
$reportFiles = [];
|
||||
foreach ($records as $r) {
|
||||
$tongueImages = array_merge($tongueImages, self::parseJsonArray($r->tongue_images));
|
||||
$reportFiles = array_merge($reportFiles, self::parseJsonArray($r->report_files));
|
||||
}
|
||||
|
||||
return [
|
||||
'tongue_images' => array_map(
|
||||
fn($u) => empty($u) ? $u : FileService::getFileUrl($u),
|
||||
array_values(array_unique($tongueImages))
|
||||
),
|
||||
'report_files' => array_map(
|
||||
fn($u) => empty($u) ? $u : FileService::getFileUrl($u),
|
||||
array_values(array_unique($reportFiles))
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果 URL 的域名是当前配置的存储域名则去掉,否则原样保留
|
||||
*/
|
||||
private static function toRelativePath(string $url): string
|
||||
{
|
||||
if (empty($url)) return $url;
|
||||
if (stripos($url, 'http://') !== 0 && stripos($url, 'https://') !== 0) {
|
||||
return $url;
|
||||
}
|
||||
// 获取当前存储域名
|
||||
$domain = self::getStorageDomain();
|
||||
if ($domain && stripos($url, rtrim($domain, '/')) === 0) {
|
||||
$relative = substr($url, strlen(rtrim($domain, '/')));
|
||||
return ltrim($relative, '/');
|
||||
}
|
||||
// 非当前存储域名,保留完整 URL
|
||||
return $url;
|
||||
}
|
||||
|
||||
private static function getStorageDomain(): string
|
||||
{
|
||||
$default = \app\common\service\ConfigService::get('storage', 'default', 'local');
|
||||
if ($default === 'local') {
|
||||
return request()->domain() . '/';
|
||||
}
|
||||
$storage = \app\common\service\ConfigService::get('storage', $default);
|
||||
return $storage ? ($storage['domain'] ?? '') : '';
|
||||
}
|
||||
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ use app\common\model\tcm\DiagnosisGuahaoLog;
|
||||
use app\common\model\tcm\DiagnosisAssignLog;
|
||||
use app\common\model\tcm\ImChatMessage;
|
||||
use app\common\model\DiagnosisViewRecord;
|
||||
use app\adminapi\logic\doctor\DoctorNoteLogic;
|
||||
use app\common\service\FileService;
|
||||
use think\facade\Db;
|
||||
/**
|
||||
* 中医辨房病因诊单逻辑
|
||||
@@ -79,23 +81,19 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
|
||||
// 处理舌苔照片(JSON数组)
|
||||
if (isset($params['tongue_images']) && is_array($params['tongue_images'])) {
|
||||
$params['tongue_images'] = json_encode($params['tongue_images'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 处理检查报告(JSON数组)
|
||||
if (isset($params['report_files']) && is_array($params['report_files'])) {
|
||||
$params['report_files'] = json_encode($params['report_files'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 图片字段迁移至 doctor_note,从 params 中提取后再写入备注表
|
||||
$newTongueImages = isset($params['tongue_images']) && is_array($params['tongue_images'])
|
||||
? $params['tongue_images'] : [];
|
||||
$newReportFiles = isset($params['report_files']) && is_array($params['report_files'])
|
||||
? $params['report_files'] : [];
|
||||
unset($params['tongue_images'], $params['report_files']);
|
||||
|
||||
// 处理诊断日期
|
||||
if (isset($params['diagnosis_date'])) {
|
||||
$params['diagnosis_date'] = strtotime($params['diagnosis_date']);
|
||||
}
|
||||
|
||||
// 表未增加 admin_id 列时勿写入,避免 SQL 报错(见 sql/1.9.20260421/add_tcm_diagnosis_admin_id.sql)
|
||||
// 注意:Model::getConnection() 在 think-orm 中返回连接名(string),须用 Db 查询对象取字段
|
||||
if (array_key_exists('admin_id', $params)) {
|
||||
$fieldNames = Db::name('tcm_diagnosis')->getTableFields();
|
||||
if (! is_array($fieldNames) || ! in_array('admin_id', $fieldNames, true)) {
|
||||
@@ -104,10 +102,19 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
|
||||
$model = Diagnosis::create($params);
|
||||
|
||||
|
||||
// 图片写入 doctor_note 表
|
||||
if (!empty($newTongueImages) || !empty($newReportFiles)) {
|
||||
DoctorNoteLogic::addOrAppend([
|
||||
'diagnosis_id' => (int) $model->id,
|
||||
'tongue_images' => $newTongueImages,
|
||||
'report_files' => $newReportFiles,
|
||||
]);
|
||||
}
|
||||
|
||||
// 自动为患者创建 TRTC 账号
|
||||
self::createPatientTrtcAccount($model->id);
|
||||
|
||||
|
||||
return $model->id;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
@@ -178,29 +185,22 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
|
||||
// 处理舌苔照片(JSON数组)
|
||||
if (isset($params['tongue_images']) && is_array($params['tongue_images'])) {
|
||||
$params['tongue_images'] = json_encode($params['tongue_images'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 处理检查报告(JSON数组)
|
||||
if (isset($params['report_files']) && is_array($params['report_files'])) {
|
||||
$params['report_files'] = json_encode($params['report_files'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 图片字段已迁移至 doctor_note 表管理,不再写入 tcm_diagnosis
|
||||
unset($params['tongue_images'], $params['report_files']);
|
||||
|
||||
// 处理诊断日期
|
||||
if (isset($params['diagnosis_date'])) {
|
||||
$params['diagnosis_date'] = strtotime($params['diagnosis_date']);
|
||||
}
|
||||
|
||||
Diagnosis::update($params);
|
||||
|
||||
|
||||
// 如果是编辑,也确保患者有 TRTC 账号
|
||||
$diagnosis = Diagnosis::find($params['id']);
|
||||
if ($diagnosis) {
|
||||
self::createPatientTrtcAccount($diagnosis->patient_id);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
@@ -256,60 +256,36 @@ class DiagnosisLogic extends BaseLogic
|
||||
}
|
||||
|
||||
|
||||
if (!empty($diagnosis['tongue_images'])) {
|
||||
// 先尝试 JSON 解析(兼容旧数据)
|
||||
$files = json_decode($diagnosis['tongue_images'], true);
|
||||
if (is_array($files)) {
|
||||
$diagnosis['tongue_images'] = $files;
|
||||
} else {
|
||||
// JSON 解析失败则按逗号分割
|
||||
$diagnosis['tongue_images'] = array_filter(explode(',', $diagnosis['tongue_images'])) ?: [];
|
||||
}
|
||||
|
||||
// 为没有 http 前缀的图片添加域名
|
||||
$domain = request()->domain();
|
||||
$diagnosis['tongue_images'] = array_map(function($url) use ($domain) {
|
||||
if (empty($url)) {
|
||||
return $url;
|
||||
// 从 doctor_note 聚合图片(已迁移至备注表统一管理)
|
||||
$noteImages = DoctorNoteLogic::getAggregatedImages((int) $diagnosis['id']);
|
||||
if (!empty($noteImages['tongue_images']) || !empty($noteImages['report_files'])) {
|
||||
$diagnosis['tongue_images'] = $noteImages['tongue_images'];
|
||||
$diagnosis['report_files'] = $noteImages['report_files'];
|
||||
} else {
|
||||
// Fallback: 未迁移的旧数据仍从 tcm_diagnosis 读取
|
||||
if (!empty($diagnosis['tongue_images'])) {
|
||||
$files = json_decode($diagnosis['tongue_images'], true);
|
||||
if (!is_array($files)) {
|
||||
$files = array_filter(explode(',', $diagnosis['tongue_images'])) ?: [];
|
||||
}
|
||||
$diagnosis['tongue_images'] = array_map(function($url) {
|
||||
return empty($url) ? $url : FileService::getFileUrl($url);
|
||||
}, $files);
|
||||
} else {
|
||||
$diagnosis['tongue_images'] = [];
|
||||
}
|
||||
if (!empty($diagnosis['report_files'])) {
|
||||
$files = json_decode($diagnosis['report_files'], true);
|
||||
if (!is_array($files)) {
|
||||
$files = array_filter(explode(',', $diagnosis['report_files'])) ?: [];
|
||||
}
|
||||
$diagnosis['report_files'] = array_map(function($url) {
|
||||
return empty($url) ? $url : FileService::getFileUrl($url);
|
||||
}, $files);
|
||||
} else {
|
||||
$diagnosis['report_files'] = [];
|
||||
}
|
||||
}
|
||||
// 如果已经包含 http:// 或 https://,则跳过
|
||||
if (stripos($url, 'http://') === 0 || stripos($url, 'https://') === 0) {
|
||||
return $url;
|
||||
}
|
||||
// 添加域名前缀
|
||||
return $domain .'/'. $url;
|
||||
}, $diagnosis['tongue_images']);
|
||||
} else {
|
||||
$diagnosis['tongue_images'] = [];
|
||||
}
|
||||
|
||||
// 处理检查报告(JSON转数组)
|
||||
if (!empty($diagnosis['report_files'])) {
|
||||
// 先尝试 JSON 解析(兼容旧数据)
|
||||
$files = json_decode($diagnosis['report_files'], true);
|
||||
if (is_array($files)) {
|
||||
$diagnosis['report_files'] = $files;
|
||||
} else {
|
||||
// JSON 解析失败则按逗号分割
|
||||
$diagnosis['report_files'] = array_filter(explode(',', $diagnosis['report_files'])) ?: [];
|
||||
}
|
||||
|
||||
// 为没有 http 前缀的文件添加域名
|
||||
$domain = request()->domain();
|
||||
$diagnosis['report_files'] = array_map(function($url) use ($domain) {
|
||||
if (empty($url)) {
|
||||
return $url;
|
||||
}
|
||||
// 如果已经包含 http:// 或 https://,则跳过
|
||||
if (stripos($url, 'http://') === 0 || stripos($url, 'https://') === 0) {
|
||||
return $url;
|
||||
}
|
||||
// 添加域名前缀
|
||||
return $domain . $url;
|
||||
}, $diagnosis['report_files']);
|
||||
} else {
|
||||
$diagnosis['report_files'] = [];
|
||||
}
|
||||
// 处理诊断日期格式
|
||||
if (!empty($diagnosis['diagnosis_date'])) {
|
||||
$diagnosis['diagnosis_date'] = date('Y-m-d', $diagnosis['diagnosis_date']);
|
||||
@@ -2791,12 +2767,13 @@ class DiagnosisLogic extends BaseLogic
|
||||
$params[$field] = implode(',', $params[$field]);
|
||||
}
|
||||
}
|
||||
if (isset($params['tongue_images']) && is_array($params['tongue_images'])) {
|
||||
$params['tongue_images'] = json_encode($params['tongue_images'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
if (isset($params['report_files']) && is_array($params['report_files'])) {
|
||||
$params['report_files'] = json_encode($params['report_files'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
// 图片字段迁移至 doctor_note 表
|
||||
$cardTongueImages = isset($params['tongue_images']) && is_array($params['tongue_images'])
|
||||
? $params['tongue_images'] : [];
|
||||
$cardReportFiles = isset($params['report_files']) && is_array($params['report_files'])
|
||||
? $params['report_files'] : [];
|
||||
unset($params['tongue_images'], $params['report_files']);
|
||||
|
||||
if (isset($params['diagnosis_date'])) {
|
||||
$params['diagnosis_date'] = is_numeric($params['diagnosis_date']) ? $params['diagnosis_date'] : strtotime($params['diagnosis_date']);
|
||||
}
|
||||
@@ -2804,6 +2781,15 @@ class DiagnosisLogic extends BaseLogic
|
||||
$model = Diagnosis::create($params);
|
||||
self::createPatientTrtcAccount($model->patient_id);
|
||||
|
||||
// 图片写入 doctor_note
|
||||
if (!empty($cardTongueImages) || !empty($cardReportFiles)) {
|
||||
DoctorNoteLogic::addOrAppend([
|
||||
'diagnosis_id' => (int) $model->id,
|
||||
'tongue_images' => $cardTongueImages,
|
||||
'report_files' => $cardReportFiles,
|
||||
]);
|
||||
}
|
||||
|
||||
// 关联 diagnosis_view_records,便于用户通过 User::with('diagnosis') 获取就诊卡
|
||||
if ($userId) {
|
||||
$now = time();
|
||||
@@ -2909,8 +2895,6 @@ class DiagnosisLogic extends BaseLogic
|
||||
// 临床信息
|
||||
'symptoms' => $params['symptoms'] ?? '',
|
||||
'tongue_coating' => $params['tongue_coating'] ?? '',
|
||||
'tongue_images' => $params['tongue_images'] ?? '',
|
||||
'report_files' => $params['report_files'] ?? '',
|
||||
'pulse' => $params['pulse'] ?? '',
|
||||
'treatment_principle' => $params['treatment_principle'] ?? '',
|
||||
'prescription' => $params['prescription'] ?? '',
|
||||
@@ -2923,14 +2907,25 @@ class DiagnosisLogic extends BaseLogic
|
||||
if (!empty($params['diagnosis_date'])) {
|
||||
$updateData['diagnosis_date'] = strtotime($params['diagnosis_date']);
|
||||
} elseif (!empty($params['local_hospital_visit_date'])) {
|
||||
// 如果没有diagnosis_date但有local_hospital_visit_date,则使用local_hospital_visit_date
|
||||
$updateData['diagnosis_date'] = strtotime($params['local_hospital_visit_date']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 执行更新
|
||||
$diagnosis->save($updateData);
|
||||
|
||||
// 图片写入 doctor_note 表
|
||||
$cardTongueImages = isset($params['tongue_images']) && is_array($params['tongue_images'])
|
||||
? $params['tongue_images'] : [];
|
||||
$cardReportFiles = isset($params['report_files']) && is_array($params['report_files'])
|
||||
? $params['report_files'] : [];
|
||||
if (!empty($cardTongueImages) || !empty($cardReportFiles)) {
|
||||
DoctorNoteLogic::addOrAppend([
|
||||
'diagnosis_id' => (int) $diagnosisId,
|
||||
'tongue_images' => $cardTongueImages,
|
||||
'report_files' => $cardReportFiles,
|
||||
]);
|
||||
}
|
||||
|
||||
\think\facade\Log::info('诊单更新成功 - diagnosis_id: ' . $diagnosisId . ', patient_id: ' . $patientId);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -19,11 +19,15 @@ class AppointmentValidate extends BaseValidate
|
||||
'id' => 'require',
|
||||
'patient_id' => 'require|integer',
|
||||
'doctor_id' => 'require|integer',
|
||||
'diagnosis_id' => 'require|integer',
|
||||
'appointment_date' => 'require|date',
|
||||
'period' => 'in:morning,afternoon,all',
|
||||
'appointment_time' => 'require',
|
||||
'appointment_type' => 'in:video,text,phone',
|
||||
'channel_source' => 'require',
|
||||
'note_id' => 'require|integer',
|
||||
'image_type' => 'require|in:tongue_images,report_files',
|
||||
'image_path' => 'require',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -34,6 +38,7 @@ class AppointmentValidate extends BaseValidate
|
||||
'id' => '预约ID',
|
||||
'patient_id' => '患者ID',
|
||||
'doctor_id' => '医生ID',
|
||||
'diagnosis_id' => '诊单ID',
|
||||
'appointment_date' => '预约日期',
|
||||
'period' => '时段',
|
||||
'appointment_time' => '预约时间',
|
||||
@@ -85,4 +90,37 @@ class AppointmentValidate extends BaseValidate
|
||||
{
|
||||
return $this->only(['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 接诊台聚合详情场景
|
||||
* @return AppointmentValidate
|
||||
*/
|
||||
public function sceneReception()
|
||||
{
|
||||
return $this->only(['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 通知接诊医助场景
|
||||
* @return AppointmentValidate
|
||||
*/
|
||||
public function sceneNotifyAssistant()
|
||||
{
|
||||
return $this->only(['id']);
|
||||
}
|
||||
|
||||
public function sceneAddDoctorNote()
|
||||
{
|
||||
return $this->only(['diagnosis_id']);
|
||||
}
|
||||
|
||||
public function sceneDoctorNotes()
|
||||
{
|
||||
return $this->only(['diagnosis_id']);
|
||||
}
|
||||
|
||||
public function sceneDeleteDoctorNoteImage()
|
||||
{
|
||||
return $this->only(['note_id', 'image_type', 'image_path']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class DiagnosisValidate extends BaseValidate
|
||||
|
||||
public function sceneEdit()
|
||||
{
|
||||
return $this->only(['id', 'patient_name', 'id_card', 'phone', 'gender', 'age', 'diagnosis_date', 'diagnosis_type', 'syndrome_type', 'marital_status', 'height', 'weight', 'region', 'systolic_pressure', 'diastolic_pressure', 'fasting_blood_sugar', 'diabetes_discovery_year', 'local_hospital_diagnosis', 'local_hospital_name', 'past_history', 'symptoms', 'tongue_coating', 'pulse', 'treatment_principle', 'prescription', 'doctor_advice', 'remark', 'current_medications', 'status', 'tongue_images', 'report_files']);
|
||||
return $this->only(['id', 'patient_name', 'id_card', 'phone', 'gender', 'age', 'diagnosis_date', 'diagnosis_type', 'syndrome_type', 'marital_status', 'height', 'weight', 'region', 'systolic_pressure', 'diastolic_pressure', 'fasting_blood_sugar', 'diabetes_discovery_year', 'local_hospital_diagnosis', 'local_hospital_name', 'past_history', 'symptoms', 'tongue_coating', 'pulse', 'treatment_principle', 'prescription', 'doctor_advice', 'remark', 'current_medications', 'status']);
|
||||
}
|
||||
|
||||
public function sceneId()
|
||||
|
||||
Reference in New Issue
Block a user