This commit is contained in:
Your Name
2026-04-07 18:13:03 +08:00
parent a780356908
commit fdf714f833
397 changed files with 15086 additions and 1043 deletions
@@ -21,6 +21,7 @@ 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;
@@ -96,6 +97,21 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
}
}
// 仅已开方(有处方)的诊单:随访列表传 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) {
$deptId = (int) $this->params['assistant_dept_id'];
$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 = {$deptId}");
}
// 按挂号日期+时间升序。若传了 appointment_date(当天/明天等筛选),只按「该日」的挂号排序与展示,避免仍按历史最早一天把昨天号顶到最前
$diagTbl = (new Diagnosis())->getTable();
$aptTbl = (new Appointment())->getTable();
@@ -114,9 +130,10 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
->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)
@@ -124,20 +141,36 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
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 = $apt['patient_id'];
$did = (int) ($apt['patient_id'] ?? 0);
if ($did <= 0) {
continue;
}
if (!isset($appointmentMap[$did])) {
$appointmentMap[$did] = $apt;
$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_unique(array_column($appointmentMap, 'doctor_id'));
$doctorIds = array_values(array_unique($doctorIds));
$doctorNames = [];
if (!empty($doctorIds)) {
$doctors = Admin::whereIn('id', $doctorIds)->column('name', 'id');
@@ -145,8 +178,9 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
}
// 合并到诊单列表
foreach ($lists as &$item) {
$apt = $appointmentMap[$item['id']] ?? null;
if ($apt) {
$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);
@@ -157,6 +191,21 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$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;
@@ -164,6 +213,7 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$item['appointment_doctor_id'] = null;
$item['appointment_doctor_name'] = '';
$item['appointment_time_text'] = '';
$item['appointments'] = [];
}
}
} else {
@@ -174,6 +224,7 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$item['appointment_doctor_id'] = null;
$item['appointment_doctor_name'] = '';
$item['appointment_time_text'] = '';
$item['appointments'] = [];
}
}
@@ -191,6 +242,66 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$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'])
->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,
];
}
}
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;
// 开方次数即复诊次数:1 张处方 = 第 1 次复诊,以此类推
$item['followup_prescription_count'] = (int) ($rxCountByDiag[$did] ?? 0);
}
// 最近一条通话记录状态(列表行展示:通话中 / 已结束等)
$latestCallByDiag = [];
if (!empty($diagnosisIds)) {
@@ -214,6 +325,20 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
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}
@@ -8,6 +8,7 @@ use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\adminapi\logic\tcm\PrescriptionLogic;
use app\common\model\tcm\Prescription;
use app\common\model\tcm\PrescriptionOrder;
/**
* 处方列表
@@ -80,6 +81,7 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
$query->where(function ($q) use ($adminId, $roleIds) {
$q->whereOr('is_shared', '=', 1);
$q->whereOr('creator_id', '=', $adminId);
$q->whereOr('assistant_id', '=', $adminId);
foreach ($roleIds as $rid) {
if ($rid > 0) {
$q->whereOrRaw('FIND_IN_SET(?, `visible_role_ids`)', [(string) $rid]);
@@ -105,7 +107,42 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
->limit($this->limitOffset, $this->limitLength)
->select()
->toArray();
$rxIds = array_column($lists, 'id');
$rxIds = array_map('intval', $rxIds);
$rejectedRx = [];
$bizRejectRemark = [];
$hasBizOrderRx = [];
if ($rxIds !== []) {
$bizRejectRows = PrescriptionOrder::whereIn('prescription_id', $rxIds)
->where('prescription_audit_status', 2)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->field(['prescription_id', 'prescription_audit_remark', 'id'])
->order('id', 'desc')
->select()
->toArray();
foreach ($bizRejectRows as $br) {
$pid = (int) ($br['prescription_id'] ?? 0);
if ($pid <= 0) {
continue;
}
$rejectedRx[$pid] = true;
if (!isset($bizRejectRemark[$pid])) {
$bizRejectRemark[$pid] = (string) ($br['prescription_audit_remark'] ?? '');
}
}
$orderRxIds = array_unique(array_map(
'intval',
PrescriptionOrder::whereIn('prescription_id', $rxIds)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->column('prescription_id')
));
$hasBizOrderRx = array_fill_keys($orderRxIds, true);
}
// 处理字段格式
foreach ($lists as &$item) {
// 设置默认值
@@ -117,6 +154,10 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
$item['audit_status'] = (int) ($item['audit_status'] ?? 1);
$item['void_status'] = (int) ($item['void_status'] ?? 0);
$item['visible_role_ids'] = PrescriptionLogic::visibleRoleIdsToArray((string) ($item['visible_role_ids'] ?? ''));
$rid = (int) ($item['id'] ?? 0);
$item['business_prescription_audit_rejected'] = !empty($rejectedRx[$rid]) ? 1 : 0;
$item['business_prescription_audit_remark'] = (string) ($bizRejectRemark[$rid] ?? '');
$item['has_prescription_order'] = !empty($hasBizOrderRx[$rid]) ? 1 : 0;
// 将 dietary_taboo 从逗号分隔的字符串转换为数组(前端需要数组格式)
if (!empty($item['dietary_taboo']) && is_string($item['dietary_taboo'])) {
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace app\adminapi\lists\tcm;
use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\tcm\PrescriptionOrderLogic;
use app\common\lists\ListsSearchInterface;
use app\common\model\tcm\Diagnosis;
use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\PrescriptionOrderPayOrder;
class PrescriptionOrderLists extends BaseAdminDataLists implements ListsSearchInterface
{
public function setSearch(): array
{
return [
'=' => ['prescription_id', 'fulfillment_status'],
'%like%' => ['order_no'],
];
}
public function lists(): array
{
$query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time');
if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) {
$diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id');
$query->where(function ($q) use ($diagIds) {
$q->where('creator_id', $this->adminId);
if ($diagIds !== []) {
$q->whereOr('diagnosis_id', 'in', $diagIds);
}
});
}
$lists = $query
->order('id', 'desc')
->limit($this->limitOffset, $this->limitLength)
->select()
->toArray();
foreach ($lists as &$item) {
PrescriptionOrderLogic::maskInternalCostIfNeeded($item, $this->adminInfo);
$item['linked_pay_order_count'] = (int) PrescriptionOrderPayOrder::where(
'prescription_order_id',
(int) ($item['id'] ?? 0)
)->count();
}
unset($item);
return $lists;
}
public function count(): int
{
$query = PrescriptionOrder::where($this->searchWhere)->whereNull('delete_time');
if (!PrescriptionOrderLogic::canSeeAllPrescriptionOrders($this->adminInfo)) {
$diagIds = Diagnosis::where('assistant_id', $this->adminId)->whereNull('delete_time')->column('id');
$query->where(function ($q) use ($diagIds) {
$q->where('creator_id', $this->adminId);
if ($diagIds !== []) {
$q->whereOr('diagnosis_id', 'in', $diagIds);
}
});
}
return (int) $query->count();
}
}