548 lines
26 KiB
PHP
548 lines
26 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\adminapi\logic\dept\DeptLogic;
|
||
use app\common\model\tcm\Diagnosis;
|
||
use app\common\model\tcm\BloodRecord;
|
||
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\AdminDept;
|
||
use app\common\model\auth\AdminRole;
|
||
use app\common\lists\ListsSearchInterface;
|
||
use app\common\lists\Traits\HasDataScopeFilter;
|
||
|
||
/**
|
||
* 中医辨房病因诊单列表
|
||
* Class DiagnosisLists
|
||
* @package app\adminapi\lists\tcm
|
||
*/
|
||
class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
|
||
{
|
||
use HasDataScopeFilter;
|
||
/**
|
||
* @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');
|
||
|
||
// 待分配医助筛选:assistant_id 为空或 0(放在最前面判断,便于跳过医助角色的自我过滤)
|
||
$pendingAssign = isset($this->params['pending_assign']) && (string) $this->params['pending_assign'] === '1';
|
||
|
||
// 构建查询
|
||
$query = Diagnosis::where($this->searchWhere);
|
||
|
||
// 如果是医助角色(role_id=2),只显示自己添加的患者;
|
||
// 但当前在查"待分配医助"时应放开,否则 NULL 记录永远不会匹配 assistant_id=adminId
|
||
if (in_array(2, $roleIds) && !$pendingAssign) {
|
||
$query->where('assistant_id', $this->adminId);
|
||
}
|
||
|
||
if (!$pendingAssign) {
|
||
$this->applyDataScopeByOwner($query, 'assistant_id');
|
||
}
|
||
|
||
// 关键字搜索:支持患者姓名或手机号(模糊匹配)
|
||
if (isset($this->params['keyword']) && trim((string) $this->params['keyword']) !== '') {
|
||
$keyword = trim((string) $this->params['keyword']);
|
||
$query->where(function ($q) use ($keyword) {
|
||
$q->whereLike('patient_name', '%' . $keyword . '%')
|
||
->whereOr('phone', 'like', '%' . $keyword . '%');
|
||
});
|
||
}
|
||
|
||
// 待分配医助:assistant_id 为 NULL 或 0(使用 whereRaw 避免闭包 whereOr 歧义)
|
||
if ($pendingAssign) {
|
||
$query->whereRaw('(assistant_id IS NULL OR assistant_id = 0)');
|
||
}
|
||
|
||
// 是否确认诊单: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);
|
||
}
|
||
}
|
||
|
||
// 挂号日期筛选:当天/明天/后天(1=已预约 3=已完成 4=已过号,需展示以便取消等操作)
|
||
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}.id AND apt.appointment_date = '{$aptDate}' AND apt.status IN (1,3,4)");
|
||
}
|
||
|
||
// 挂号状态: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}.id AND apt.status IN (1,3,4)");
|
||
} else {
|
||
$query->whereNotExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.id AND apt.status IN (1,3,4)");
|
||
}
|
||
}
|
||
|
||
// 至少有一条挂号为「已完成」(status=3),用于列表顶部「已完成」Tab
|
||
if (isset($this->params['completed_appointment']) && (string) $this->params['completed_appointment'] === '1') {
|
||
$aptTbl = (new Appointment())->getTable();
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.id AND apt.status = 3");
|
||
}
|
||
|
||
// 仅已开方(有处方)的诊单:随访列表传 only_has_prescription=1;问诊等页面不传则不过滤
|
||
if (isset($this->params['only_has_prescription']) && (string) $this->params['only_has_prescription'] === '1') {
|
||
$rxTbl = (new Prescription())->getTable();
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$rxTbl} rx WHERE rx.diagnosis_id = {$diagTbl}.id AND rx.delete_time IS NULL");
|
||
}
|
||
|
||
// 医助所属部门(随访等页面传 assistant_dept_id):选父级时包含全部子级部门下医助
|
||
if (isset($this->params['assistant_dept_id']) && $this->params['assistant_dept_id'] !== '' && (int) $this->params['assistant_dept_id'] > 0) {
|
||
$rootDeptId = (int) $this->params['assistant_dept_id'];
|
||
$deptIds = DeptLogic::getSelfAndDescendantIds($rootDeptId);
|
||
$deptIds = array_values(array_filter(array_map('intval', $deptIds), static function (int $id): bool {
|
||
return $id > 0;
|
||
}));
|
||
if ($deptIds === []) {
|
||
$query->whereRaw('0 = 1');
|
||
} else {
|
||
$inList = implode(',', $deptIds);
|
||
$adTbl = (new AdminDept())->getTable();
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$adTbl} ad WHERE ad.admin_id = {$diagTbl}.assistant_id AND ad.dept_id IN ({$inList})");
|
||
}
|
||
}
|
||
|
||
// 按挂号状态优先级排序:已过号(4) > 已预约(1) > 已完成(3),然后按挂号日期+时间升序
|
||
// 若传了 appointment_date(当天/明天等筛选),只按「该日」的挂号排序与展示
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$aptTbl = (new Appointment())->getTable();
|
||
$minAptDateCond = '';
|
||
if (!empty($this->params['appointment_date'])) {
|
||
$sortAptDate = addslashes((string) $this->params['appointment_date']);
|
||
$minAptDateCond = " AND apt.appointment_date = '{$sortAptDate}'";
|
||
}
|
||
|
||
// 获取最早的挂号状态(用于排序优先级)
|
||
$minAptStatusExpr = '(SELECT apt.status FROM ' . $aptTbl . ' apt WHERE apt.patient_id = ' . $diagTbl . '.id AND apt.status IN (1,3,4)' . $minAptDateCond . ' ORDER BY CASE apt.status WHEN 4 THEN 1 WHEN 1 THEN 2 WHEN 3 THEN 3 ELSE 4 END, apt.appointment_date ASC, apt.appointment_time ASC LIMIT 1)';
|
||
|
||
// 获取最早的挂号时间(用于同状态内排序)
|
||
$minAptExpr = '(SELECT MIN(CONCAT(apt.appointment_date, \' \', IFNULL(NULLIF(TRIM(apt.appointment_time), \'\'), \'00:00:00\'))) FROM ' . $aptTbl . ' apt WHERE apt.patient_id = ' . $diagTbl . '.id AND apt.status IN (1,3,4)' . $minAptDateCond . ')';
|
||
|
||
// 「已完成」Tab:按最近一条「已完成」(status=3) 挂号日期+时间降序,最新在最前;有 appointment_date 时仍只在该日内比
|
||
$isCompletedTab = isset($this->params['completed_appointment']) && (string) $this->params['completed_appointment'] === '1';
|
||
if ($isCompletedTab) {
|
||
$maxCompletedAptExpr = '(SELECT MAX(CONCAT(apt.appointment_date, \' \', IFNULL(NULLIF(TRIM(apt.appointment_time), \'\'), \'00:00:00\'))) FROM ' . $aptTbl . ' apt WHERE apt.patient_id = ' . $diagTbl . '.id AND apt.status = 3' . $minAptDateCond . ')';
|
||
$orderRaw = 'IFNULL(' . $maxCompletedAptExpr . ", '1970-01-01 00:00:00') DESC, {$diagTbl}.id DESC";
|
||
} else {
|
||
$orderRaw = 'CASE IFNULL(' . $minAptStatusExpr . ', 999) WHEN 4 THEN 1 WHEN 1 THEN 2 WHEN 3 THEN 3 ELSE 4 END ASC, IFNULL(' . $minAptExpr . ", '9999-12-31 23:59:59') ASC, {$diagTbl}.id DESC";
|
||
}
|
||
|
||
$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'])
|
||
->orderRaw($orderRaw)
|
||
->limit($this->limitOffset, $this->limitLength)
|
||
->select()
|
||
->toArray();
|
||
|
||
// 关联挂号:doctor_appointment.patient_id 存诊单主键 id(与 Appointment 列表 join 一致)
|
||
$diagnosisIds = array_filter(array_unique(array_column($lists, 'id')));
|
||
|
||
if (!empty($diagnosisIds)) {
|
||
$appointmentMap = [];
|
||
$subQuery = Appointment::where('patient_id', 'in', $diagnosisIds)
|
||
->where('status', 'in', [1, 3, 4]); // 1=已预约 3=已完成 4=已过号
|
||
if (!empty($this->params['appointment_date'])) {
|
||
$subQuery->where('appointment_date', (string) $this->params['appointment_date']);
|
||
}
|
||
|
||
$subQuery
|
||
->field('id, patient_id, doctor_id, appointment_date, appointment_time, status, create_time')
|
||
->order('appointment_date', 'asc')
|
||
->order('appointment_time', 'asc')
|
||
->order('id', 'asc');
|
||
$appointments = $subQuery->select()->toArray();
|
||
|
||
foreach ($appointments as $apt) {
|
||
$did = (int) ($apt['patient_id'] ?? 0);
|
||
if ($did <= 0) {
|
||
continue;
|
||
}
|
||
if (!isset($appointmentMap[$did])) {
|
||
$appointmentMap[$did] = [];
|
||
}
|
||
$appointmentMap[$did][] = $apt;
|
||
}
|
||
|
||
// 获取医生名称
|
||
$doctorIds = [];
|
||
foreach ($appointmentMap as $aptList) {
|
||
foreach ($aptList as $apt) {
|
||
$docId = (int) ($apt['doctor_id'] ?? 0);
|
||
if ($docId > 0) {
|
||
$doctorIds[] = $docId;
|
||
}
|
||
}
|
||
}
|
||
$doctorIds = array_values(array_unique($doctorIds));
|
||
$doctorNames = [];
|
||
if (!empty($doctorIds)) {
|
||
$doctors = Admin::whereIn('id', $doctorIds)->column('name', 'id');
|
||
$doctorNames = $doctors ?: [];
|
||
}
|
||
// 合并到诊单列表
|
||
foreach ($lists as &$item) {
|
||
$aptList = $appointmentMap[(int) $item['id']] ?? [];
|
||
if (!empty($aptList)) {
|
||
$apt = $aptList[0];
|
||
$item['has_appointment'] = 1;
|
||
$item['appointment_id'] = $apt['id'];
|
||
$item['appointment_status'] = (int) ($apt['status'] ?? 0);
|
||
$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);
|
||
$item['appointments'] = array_map(function ($a) use ($doctorNames) {
|
||
$timePart = $a['appointment_time'] ?? '';
|
||
if (strlen((string) $timePart) > 5) {
|
||
$timePart = substr((string) $timePart, 0, 5);
|
||
}
|
||
$doctorId = (int) ($a['doctor_id'] ?? 0);
|
||
|
||
return [
|
||
'id' => (int) ($a['id'] ?? 0),
|
||
'status' => (int) ($a['status'] ?? 0),
|
||
'doctor_id' => $doctorId,
|
||
'doctor_name' => (string) ($doctorNames[$doctorId] ?? '-'),
|
||
'time_text' => trim((string) ($a['appointment_date'] ?? '') . ' ' . (string) $timePart),
|
||
];
|
||
}, $aptList);
|
||
} else {
|
||
$item['has_appointment'] = 0;
|
||
$item['appointment_id'] = null;
|
||
$item['appointment_status'] = 0;
|
||
$item['appointment_doctor_id'] = null;
|
||
$item['appointment_doctor_name'] = '';
|
||
$item['appointment_time_text'] = '';
|
||
$item['appointments'] = [];
|
||
}
|
||
}
|
||
} else {
|
||
foreach ($lists as &$item) {
|
||
$item['has_appointment'] = 0;
|
||
$item['appointment_id'] = null;
|
||
$item['appointment_status'] = 0;
|
||
$item['appointment_doctor_id'] = null;
|
||
$item['appointment_doctor_name'] = '';
|
||
$item['appointment_time_text'] = '';
|
||
$item['appointments'] = [];
|
||
}
|
||
}
|
||
|
||
// 关联是否开方:诊单是否有处方记录
|
||
$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;
|
||
}
|
||
|
||
// 复诊展示:业务上「已开方」即算有复诊,取该诊单下最新一条处方的时间与医师(优先未作废)
|
||
$followupByDiag = [];
|
||
$rxCountByDiag = [];
|
||
if ($diagnosisIds !== []) {
|
||
$rxAll = Prescription::whereIn('diagnosis_id', $diagnosisIds)
|
||
->whereNull('delete_time')
|
||
->field(['id', 'diagnosis_id', 'doctor_name', 'creator_id', 'prescription_date', 'create_time', 'void_status', 'audit_status'])
|
||
->order('id', 'desc')
|
||
->select()
|
||
->toArray();
|
||
foreach ($rxAll as $rx) {
|
||
$dCount = (int) ($rx['diagnosis_id'] ?? 0);
|
||
if ($dCount > 0) {
|
||
$rxCountByDiag[$dCount] = ($rxCountByDiag[$dCount] ?? 0) + 1;
|
||
}
|
||
}
|
||
foreach ($rxAll as $rx) {
|
||
$d = (int) ($rx['diagnosis_id'] ?? 0);
|
||
if ($d <= 0 || isset($followupByDiag[$d])) {
|
||
continue;
|
||
}
|
||
if ((int) ($rx['void_status'] ?? 0) === 0) {
|
||
$followupByDiag[$d] = $rx;
|
||
}
|
||
}
|
||
foreach ($rxAll as $rx) {
|
||
$d = (int) ($rx['diagnosis_id'] ?? 0);
|
||
if ($d <= 0 || isset($followupByDiag[$d])) {
|
||
continue;
|
||
}
|
||
$followupByDiag[$d] = $rx;
|
||
}
|
||
$creatorIds = array_values(array_unique(array_filter(array_column($followupByDiag, 'creator_id'))));
|
||
$creatorNames = [];
|
||
if ($creatorIds !== []) {
|
||
$creatorNames = Admin::whereIn('id', $creatorIds)->whereNull('delete_time')->column('name', 'id');
|
||
}
|
||
foreach ($followupByDiag as $d => $rx) {
|
||
$doctorName = trim((string) ($rx['doctor_name'] ?? ''));
|
||
$cid = (int) ($rx['creator_id'] ?? 0);
|
||
if ($doctorName === '' && $cid > 0) {
|
||
$doctorName = (string) ($creatorNames[$cid] ?? '');
|
||
}
|
||
$followupByDiag[$d] = [
|
||
'time_text' => $this->formatPrescriptionFollowupTime($rx),
|
||
'doctor_name' => $doctorName !== '' ? $doctorName : '—',
|
||
'voided' => (int) ($rx['void_status'] ?? 0) !== 0,
|
||
'void_status' => (int) ($rx['void_status'] ?? 0),
|
||
'audit_status' => (int) ($rx['audit_status'] ?? -1),
|
||
];
|
||
}
|
||
}
|
||
foreach ($lists as &$item) {
|
||
$did = (int) $item['id'];
|
||
$fu = $followupByDiag[$did] ?? null;
|
||
$item['followup_time_text'] = ($item['has_prescription'] && $fu) ? ($fu['time_text'] ?? '') : '';
|
||
$item['followup_doctor_name'] = ($item['has_prescription'] && $fu) ? ($fu['doctor_name'] ?? '—') : '';
|
||
$item['followup_rx_voided'] = ($item['has_prescription'] && $fu && !empty($fu['voided'])) ? 1 : 0;
|
||
$item['prescription_audit_status'] = ($item['has_prescription'] && $fu) ? (int) ($fu['audit_status'] ?? -1) : -1;
|
||
$item['prescription_void_status'] = ($item['has_prescription'] && $fu) ? (int) ($fu['void_status'] ?? 0) : 0;
|
||
// 开方次数即复诊次数:1 张处方 = 第 1 次复诊,以此类推
|
||
$item['followup_prescription_count'] = (int) ($rxCountByDiag[$did] ?? 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);
|
||
}
|
||
unset($item);
|
||
|
||
// 未服务天数:以血糖记录最近一条 record_date 为锚点(口径见 prd T3)
|
||
// 单次 GROUP BY 聚合,不会成 N+1;走 idx_diagnosis_id 索引。
|
||
$diagIdsForUnserved = array_values(array_filter(array_unique(array_column($lists, 'id'))));
|
||
$maxBloodByDiag = [];
|
||
if (!empty($diagIdsForUnserved)) {
|
||
$maxBloodByDiag = BloodRecord::whereIn('diagnosis_id', $diagIdsForUnserved)
|
||
->whereNull('delete_time')
|
||
->group('diagnosis_id')
|
||
->column('MAX(record_date)', 'diagnosis_id');
|
||
}
|
||
$nowTs = time();
|
||
foreach ($lists as &$item) {
|
||
$last = (int) ($maxBloodByDiag[(int) $item['id']] ?? 0);
|
||
$item['last_blood_record_at'] = $last > 0 ? date('Y-m-d', $last) : null;
|
||
$item['unserved_days'] = $last > 0 ? max(0, (int) floor(($nowTs - $last) / 86400)) : null;
|
||
}
|
||
unset($item);
|
||
|
||
return $lists;
|
||
}
|
||
|
||
/**
|
||
* @param array<string,mixed> $rx 处方一行
|
||
*/
|
||
private function formatPrescriptionFollowupTime(array $rx): string
|
||
{
|
||
$ct = (int) ($rx['create_time'] ?? 0);
|
||
$pd = trim((string) ($rx['prescription_date'] ?? ''));
|
||
if ($pd !== '') {
|
||
return $pd . ($ct > 0 ? ' ' . date('H:i', $ct) : '');
|
||
}
|
||
|
||
return $ct > 0 ? date('Y-m-d H:i', $ct) : '—';
|
||
}
|
||
|
||
/**
|
||
* @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');
|
||
|
||
$pendingAssign = isset($this->params['pending_assign']) && (string) $this->params['pending_assign'] === '1';
|
||
|
||
// 构建查询
|
||
$query = Diagnosis::where($this->searchWhere);
|
||
|
||
// 如果是医助角色(role_id=2),只显示自己添加的患者;查"待分配医助"时放开
|
||
if (in_array(2, $roleIds) && !$pendingAssign) {
|
||
$query->where('assistant_id', $this->adminId);
|
||
}
|
||
|
||
if (!$pendingAssign) {
|
||
$this->applyDataScopeByOwner($query, 'assistant_id');
|
||
}
|
||
|
||
// 关键字搜索:支持患者姓名或手机号(模糊匹配)
|
||
if (isset($this->params['keyword']) && trim((string) $this->params['keyword']) !== '') {
|
||
$keyword = trim((string) $this->params['keyword']);
|
||
$query->where(function ($q) use ($keyword) {
|
||
$q->whereLike('patient_name', '%' . $keyword . '%')
|
||
->whereOr('phone', 'like', '%' . $keyword . '%');
|
||
});
|
||
}
|
||
|
||
// 待分配医助:assistant_id 为 NULL 或 0
|
||
if ($pendingAssign) {
|
||
$query->whereRaw('(assistant_id IS NULL OR assistant_id = 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}.id AND apt.appointment_date = '{$aptDate}' AND apt.status IN (1,3,4)");
|
||
}
|
||
|
||
// 挂号状态
|
||
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}.id AND apt.status IN (1,3,4)");
|
||
} else {
|
||
$query->whereNotExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.id AND apt.status IN (1,3,4)");
|
||
}
|
||
}
|
||
|
||
// 至少有一条挂号为「已完成」(status=3)
|
||
if (isset($this->params['completed_appointment']) && (string) $this->params['completed_appointment'] === '1') {
|
||
$aptTbl = (new Appointment())->getTable();
|
||
$diagTbl = (new Diagnosis())->getTable();
|
||
$query->whereExists("SELECT 1 FROM {$aptTbl} apt WHERE apt.patient_id = {$diagTbl}.id AND apt.status = 3");
|
||
}
|
||
|
||
return $query->count();
|
||
}
|
||
}
|