Files
zyt/server/app/adminapi/lists/tcm/DiagnosisLists.php
T
2026-03-24 16:32:56 +08:00

297 lines
12 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\lists\tcm;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\tcm\Diagnosis;
use app\common\model\DiagnosisViewRecord;
use app\common\model\doctor\Appointment;
use app\common\model\tcm\Prescription;
use app\common\model\tcm\CallRecord;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminRole;
use app\common\lists\ListsSearchInterface;
/**
* 中医辨房病因诊单列表
* Class DiagnosisLists
* @package app\adminapi\lists\tcm
*/
class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return array
*/
public function setSearch(): array
{
return [
'%like%' => ['patient_name'],
'=' => ['patient_id', 'gender', 'diagnosis_type', 'syndrome_type', 'assistant_id', 'status'],
'between_time' => ['diagnosis_date'],
];
}
/**
* @notes 获取列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function lists(): array
{
// 获取当前管理员的角色ID
$roleIds = AdminRole::where('admin_id', $this->adminId)->column('role_id');
// 构建查询
$query = Diagnosis::where($this->searchWhere);
// 如果是医助角色(role_id=2),只显示自己添加的患者
if (in_array(2, $roleIds)) {
$query->where('assistant_id', $this->adminId);
}
// 是否确认诊单:1=已确认 0=未确认
if (isset($this->params['diagnosis_confirmed']) && $this->params['diagnosis_confirmed'] !== '') {
$confirmed = (int)$this->params['diagnosis_confirmed'];
$dvrTbl = (new DiagnosisViewRecord())->getTable();
$diagTbl = (new Diagnosis())->getTable();
$subSql = "SELECT 1 FROM {$dvrTbl} dvr WHERE dvr.diagnosis_id = {$diagTbl}.id AND dvr.is_confirmed = 1 AND dvr.delete_time IS NULL";
if ($confirmed === 1) {
$query->whereExists($subSql);
} else {
$query->whereNotExists($subSql);
}
}
// 挂号日期筛选:当天/明天/后天
if (!empty($this->params['appointment_date'])) {
$aptDate = addslashes($this->params['appointment_date']);
$aptTbl = (new Appointment())->getTable();
$diagTbl = (new Diagnosis())->getTable();
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.appointment_date = '{$aptDate}' AND apt.status IN (1,3)");
}
// 挂号状态:1=已挂号 0=未挂号
if (isset($this->params['has_appointment']) && $this->params['has_appointment'] !== '') {
$aptTbl = (new Appointment())->getTable();
$diagTbl = (new Diagnosis())->getTable();
if ((int)$this->params['has_appointment'] === 1) {
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.status IN (1,3)");
} else {
$query->whereNotExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.status IN (1,3)");
}
}
$lists = $query
->with(['DiagnosisViewRecord'])
->field(['id', 'patient_id', 'patient_name', 'id_card', 'phone', 'gender', 'age', 'diagnosis_date', 'diagnosis_type', 'syndrome_type', 'assistant_id', 'status', 'create_time', 'update_time'])
->append(['gender_desc', 'status_desc', 'diagnosis_date_text'])
->order(['id' => 'desc'])
->limit($this->limitOffset, $this->limitLength)
->select()
->toArray();
// 关联挂号信息:获取患者最新有效挂号(已预约或已完成)
$patientIds = array_filter(array_unique(array_column($lists, 'patient_id')));
if (!empty($patientIds)) {
$appointmentMap = [];
$subQuery = Appointment::where('patient_id', 'in', $patientIds)
->where('status', 'in', [1, 3]) // 1=已预约 3=已完成
->field('id, patient_id, doctor_id, appointment_date, appointment_time, create_time')
->order('id', 'desc');
$appointments = $subQuery->select()->toArray();
foreach ($appointments as $apt) {
$pid = $apt['patient_id'];
if (!isset($appointmentMap[$pid])) {
$appointmentMap[$pid] = $apt;
}
}
// 获取医生名称
$doctorIds = array_unique(array_column($appointmentMap, 'doctor_id'));
$doctorNames = [];
if (!empty($doctorIds)) {
$doctors = Admin::whereIn('id', $doctorIds)->column('name', 'id');
$doctorNames = $doctors ?: [];
}
// 合并到诊单列表
foreach ($lists as &$item) {
$apt = $appointmentMap[$item['patient_id']] ?? null;
if ($apt) {
$item['has_appointment'] = 1;
$item['appointment_id'] = $apt['id'];
$item['appointment_doctor_id'] = $apt['doctor_id'];
$item['appointment_doctor_name'] = $doctorNames[$apt['doctor_id']] ?? '-';
$timePart = $apt['appointment_time'] ?? '';
if (strlen($timePart) > 5) {
$timePart = substr($timePart, 0, 5); // 09:00:00 -> 09:00
}
$item['appointment_time_text'] = trim(($apt['appointment_date'] ?? '') . ' ' . $timePart);
} else {
$item['has_appointment'] = 0;
$item['appointment_id'] = null;
$item['appointment_doctor_id'] = null;
$item['appointment_doctor_name'] = '';
$item['appointment_time_text'] = '';
}
}
} else {
foreach ($lists as &$item) {
$item['has_appointment'] = 0;
$item['appointment_id'] = null;
$item['appointment_doctor_id'] = null;
$item['appointment_doctor_name'] = '';
$item['appointment_time_text'] = '';
}
}
// 关联是否开方:诊单是否有处方记录
$diagnosisIds = array_column($lists, 'id');
$prescriptionMap = [];
if (!empty($diagnosisIds)) {
$prescriptionTbl = (new Prescription())->getTable();
$prescribedIds = Prescription::whereIn('diagnosis_id', $diagnosisIds)
->whereNull('delete_time')
->column('diagnosis_id');
$prescriptionMap = array_fill_keys($prescribedIds, 1);
}
foreach ($lists as &$item) {
$item['has_prescription'] = isset($prescriptionMap[$item['id']]) ? 1 : 0;
}
// 最近一条通话记录状态(列表行展示:通话中 / 已结束等)
$latestCallByDiag = [];
if (!empty($diagnosisIds)) {
$agg = CallRecord::whereIn('diagnosis_id', $diagnosisIds)
->field('diagnosis_id, MAX(id) AS max_id')
->group('diagnosis_id')
->select()
->toArray();
$maxIds = array_filter(array_column($agg, 'max_id'));
if (!empty($maxIds)) {
$recList = CallRecord::whereIn('id', $maxIds)->select()->toArray();
foreach ($recList as $r) {
$latestCallByDiag[(int)$r['diagnosis_id']] = $r;
}
}
}
foreach ($lists as &$item) {
$item['video_call_hint'] = $this->buildVideoCallHint($latestCallByDiag[$item['id']] ?? null);
}
return $lists;
}
/**
* @param array<string,mixed>|null $rec tcm_call_record 一行
* @return array{state:string,label:string,end_time?:int,start_time?:int}
*/
private function buildVideoCallHint(?array $rec): array
{
if (!$rec) {
return ['state' => 'none', 'label' => ''];
}
$st = (int)($rec['status'] ?? 0);
$roomOk = trim((string)($rec['room_id'] ?? '')) !== '';
if ($st === 1 && $roomOk) {
$t = (int)($rec['start_time'] ?? 0);
return [
'state' => 'live',
'label' => '视频通话进行中',
'start_time' => $t,
];
}
if ($st === 1 && !$roomOk) {
return [
'state' => 'pending_room',
'label' => '通话发起中,待同步房间',
];
}
if ($st === 2) {
$end = (int)($rec['end_time'] ?? 0);
$timePart = $end > 0 ? date('m-d H:i', $end) : '';
return [
'state' => 'ended',
'label' => $timePart !== '' ? ('视频已结束 · ' . $timePart) : '视频已结束',
'end_time' => $end,
];
}
if ($st === 3) {
return ['state' => 'missed', 'label' => '上次通话未接听'];
}
if ($st === 4) {
return ['state' => 'cancelled', 'label' => '上次通话已取消'];
}
return ['state' => 'none', 'label' => ''];
}
/**
* @notes 获取数量
* @return int
*/
public function count(): int
{
// 获取当前管理员的角色ID
$roleIds = AdminRole::where('admin_id', $this->adminId)->column('role_id');
// 构建查询
$query = Diagnosis::where($this->searchWhere);
// 如果是医助角色(role_id=2),只显示自己添加的患者
if (in_array(2, $roleIds)) {
$query->where('assistant_id', $this->adminId);
}
// 是否确认诊单
if (isset($this->params['diagnosis_confirmed']) && $this->params['diagnosis_confirmed'] !== '') {
$confirmed = (int)$this->params['diagnosis_confirmed'];
$dvrTbl = (new DiagnosisViewRecord())->getTable();
$diagTbl = (new Diagnosis())->getTable();
$subSql = "SELECT 1 FROM {$dvrTbl} dvr WHERE dvr.diagnosis_id = {$diagTbl}.id AND dvr.is_confirmed = 1 AND dvr.delete_time IS NULL";
if ($confirmed === 1) {
$query->whereExists($subSql);
} else {
$query->whereNotExists($subSql);
}
}
// 挂号日期筛选
if (!empty($this->params['appointment_date'])) {
$aptDate = addslashes($this->params['appointment_date']);
$aptTbl = (new Appointment())->getTable();
$diagTbl = (new Diagnosis())->getTable();
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.appointment_date = '{$aptDate}' AND apt.status IN (1,3)");
}
// 挂号状态
if (isset($this->params['has_appointment']) && $this->params['has_appointment'] !== '') {
$aptTbl = (new Appointment())->getTable();
$diagTbl = (new Diagnosis())->getTable();
if ((int)$this->params['has_appointment'] === 1) {
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.status IN (1,3)");
} else {
$query->whereNotExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.patient_id AND apt.status IN (1,3)");
}
}
return $query->count();
}
}