This commit is contained in:
Your Name
2026-03-27 18:06:12 +08:00
parent 9160c36735
commit 099bc1dd22
645 changed files with 276473 additions and 957 deletions
@@ -7,6 +7,7 @@ use app\common\model\DiagnosisViewRecord;
use app\common\model\doctor\Appointment;
use app\common\model\tcm\Prescription;
use app\common\model\auth\AdminRole;
use app\common\lists\ListsExtendInterface;
use app\common\lists\ListsSearchInterface;
/**
@@ -14,7 +15,7 @@ use app\common\lists\ListsSearchInterface;
* Class AppointmentLists
* @package app\adminapi\lists\doctor
*/
class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterface
class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
{
/**
* @notes 设置搜索条件
@@ -147,45 +148,31 @@ class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterfac
}
/**
* @notes 获取数量
* @return int
* 列表 count / Tab 角标统计共用的过滤条件(不依赖 lists() 对 searchWhere 的副作用)
* @param mixed $query
* @param bool $applyStatusFilter 为 false 时按状态分组统计各 Tab 数量
*/
public function count(): int
private function applyAppointmentCountFilters($query, bool $applyStatusFilter): void
{
// 获取当前管理员的角色ID
$roleIds = AdminRole::where('admin_id', $this->adminId)->column('role_id');
// 处理患者姓名搜索
if (!empty($this->params['patient_name'])) {
$this->searchWhere[] = ['u.patient_name', 'like', '%' . $this->params['patient_name'] . '%'];
$query->where('u.patient_name', 'like', '%' . $this->params['patient_name'] . '%');
}
// 处理医生姓名搜索
if (!empty($this->params['doctor_name'])) {
$this->searchWhere[] = ['ad.name', 'like', '%' . $this->params['doctor_name'] . '%'];
$query->where('ad.name', 'like', '%' . $this->params['doctor_name'] . '%');
}
// 处理状态搜索
if (isset($this->params['status']) && $this->params['status'] !== '') {
$this->searchWhere[] = ['a.status', '=', $this->params['status']];
if ($applyStatusFilter && isset($this->params['status']) && $this->params['status'] !== '') {
$query->where('a.status', '=', $this->params['status']);
}
// 处理日期范围搜索
if (!empty($this->params['start_date']) && !empty($this->params['end_date'])) {
$this->searchWhere[] = ['a.appointment_date', 'between', [$this->params['start_date'], $this->params['end_date']]];
$query->whereBetween('a.appointment_date', [$this->params['start_date'], $this->params['end_date']]);
} elseif (!empty($this->params['start_date'])) {
$this->searchWhere[] = ['a.appointment_date', '>=', $this->params['start_date']];
$query->where('a.appointment_date', '>=', $this->params['start_date']);
} elseif (!empty($this->params['end_date'])) {
$this->searchWhere[] = ['a.appointment_date', '<=', $this->params['end_date']];
$query->where('a.appointment_date', '<=', $this->params['end_date']);
}
// 构建查询
$query = Appointment::alias('a')
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
->leftJoin('admin ad', 'a.doctor_id = ad.id')
->where($this->searchWhere);
// 是否确认诊单
if (isset($this->params['diagnosis_confirmed']) && $this->params['diagnosis_confirmed'] !== '') {
$confirmed = (int)$this->params['diagnosis_confirmed'];
$tbl = (new DiagnosisViewRecord())->getTable();
@@ -196,17 +183,57 @@ class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterfac
$query->whereNotExists($subSql);
}
}
// 如果是医生角色(role_id=1),只显示挂自己号的预约
if (in_array(1, $roleIds)) {
$query->where('a.doctor_id', $this->adminId);
}
// 如果是医助角色(role_id=2),只显示自己添加的患者的预约
if (in_array(2, $roleIds)) {
$query->where('u.assistant_id', $this->adminId);
}
}
return $query->count();
/**
* @notes 获取数量
* @return int
*/
public function count(): int
{
$query = Appointment::alias('a')
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
->leftJoin('admin ad', 'a.doctor_id = ad.id');
$this->applyAppointmentCountFilters($query, true);
return (int)$query->count();
}
/**
* @notes 扩展字段:按需返回各状态数量(一次 GROUP BY,替代前端 4 次列表请求)
* @return array
*/
public function extend(): array
{
if (empty($this->params['include_status_counts'])) {
return [];
}
$query = Appointment::alias('a')
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
->leftJoin('admin ad', 'a.doctor_id = ad.id');
$this->applyAppointmentCountFilters($query, false);
$rows = $query->field('a.status, COUNT(*) AS cnt')
->group('a.status')
->select()
->toArray();
$out = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
foreach ($rows as $r) {
$s = (int)($r['status'] ?? 0);
if (array_key_exists($s, $out)) {
$out[$s] = (int)($r['cnt'] ?? 0);
}
}
return ['status_count' => $out];
}
}
@@ -0,0 +1,76 @@
<?php
namespace app\adminapi\lists\doctor;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\doctor\Medicine;
use app\common\lists\ListsSearchInterface;
/**
* 药品库列表
*/
class MedicineLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* 设置搜索条件
*/
public function setSearch(): array
{
return [
'%like%' => ['supplier'],
'=' => ['status'],
];
}
/**
* name 参数:中文等按名称模糊;纯英文字母按拼音首字母字段 + 名称模糊(OR)
*/
private function appendMedicineNameSearch($query): void
{
$keyword = trim((string) ($this->params['name'] ?? ''));
if ($keyword === '') {
return;
}
if (preg_match('/^[a-zA-Z]+$/', $keyword)) {
$kw = strtolower($keyword);
$query->where(function ($q) use ($kw, $keyword) {
$q->where('name_pinyin_abbr', 'like', '%' . $kw . '%')
->whereOr('name', 'like', '%' . $keyword . '%');
});
} else {
$query->where('name', 'like', '%' . $keyword . '%');
}
}
/**
* 列表字段
*/
public function lists(): array
{
$query = Medicine::where($this->searchWhere);
$this->appendMedicineNameSearch($query);
return $query
->field([
'id', 'name', 'name_pinyin_abbr', 'supplier', 'unit',
'settlement_price', 'retail_price', 'stock',
'image', 'status', 'remark',
'create_time', 'update_time',
])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* 列表数量
*/
public function count(): int
{
$query = Medicine::where($this->searchWhere);
$this->appendMedicineNameSearch($query);
return $query->count();
}
}
@@ -41,9 +41,11 @@ class RosterLists extends BaseAdminDataLists implements ListsSearchInterface
}
$lists = Roster::where($where)
->field(['id', 'doctor_id', 'date', 'period', 'status', 'quota', 'max_patients', 'booked_count', 'remark', 'create_time', 'update_time'])
->order(['date' => 'asc', 'period' => 'asc'])
->limit($this->limitOffset, $this->limitLength)
->field([
'id', 'doctor_id', 'date', 'period', 'start_time', 'end_time', 'shift_type', 'slot_minutes',
'status', 'quota', 'max_patients', 'booked_count', 'remark', 'create_time', 'update_time',
])
->order(['date' => 'asc', 'start_time' => 'asc', 'id' => 'asc'])
->select()
->toArray();
@@ -0,0 +1,227 @@
<?php
namespace app\adminapi\lists\doctor;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\doctor\Appointment;
use app\common\model\auth\Admin;
/**
* 医生统计列表
* Class StatisticsLists
* @package app\adminapi\lists\doctor
*/
class StatisticsLists extends BaseAdminDataLists
{
/**
* @notes 搜索条件
* @return array
*/
public function setSearch(): array
{
return [];
}
/**
* @notes 获取列表
* @return array
*/
public function lists(): array
{
$doctorId = $this->params['doctor_id'] ?? '';
$timeType = $this->params['time_type'] ?? 'today';
$startDate = $this->params['start_date'] ?? '';
$endDate = $this->params['end_date'] ?? '';
// 计算时间范围
list($startDate, $endDate, $timeRangeText) = $this->getTimeRange($timeType, $startDate, $endDate);
// 获取医生列表(role_id=1 表示医生角色)
$doctorAdminIds = \app\common\model\auth\AdminRole::where('role_id', 1)->column('admin_id');
if (empty($doctorAdminIds)) {
return [];
}
// 查询1:获取预约统计数据(快速查询,只查appointment表)
$statisticsData = \think\facade\Db::name('doctor_appointment')
->field([
'doctor_id',
'COUNT(*) as total_count',
'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as registered_count',
'SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) as completed_count',
'SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) as missed_count',
'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as cancelled_count'
])
->whereIn('doctor_id', $doctorAdminIds)
->where('appointment_date', '>=', $startDate)
->where('appointment_date', '<=', $endDate);
if ($doctorId) {
$statisticsData->where('doctor_id', $doctorId);
}
$statisticsData = $statisticsData->group('doctor_id')->select()->toArray();
// 将统计数据按 doctor_id 索引
$statsMap = [];
foreach ($statisticsData as $stat) {
$statsMap[$stat['doctor_id']] = $stat;
}
// 获取医生信息
$doctorQuery = Admin::field('id,name')->whereIn('id', $doctorAdminIds);
if ($doctorId) {
$doctorQuery->where('id', $doctorId);
}
$doctors = $doctorQuery->select()->toArray();
// 查询2:获取部门统计信息(通过assistant_id直接关联,按部门分组统计数量)
$doctorIdsWithAppointments = array_keys($statsMap);
$deptStatsMap = [];
$channelStatsMap = [];
if (!empty($doctorIdsWithAppointments)) {
// 通过 assistant_id → admin_dept → dept 获取部门统计
$deptStats = \think\facade\Db::name('doctor_appointment')
->alias('apt')
->leftJoin('admin_dept ad', 'apt.assistant_id = ad.admin_id')
->leftJoin('dept dept', 'ad.dept_id = dept.id')
->field('apt.doctor_id, dept.name as dept_name, COUNT(*) as dept_count')
->whereIn('apt.doctor_id', $doctorIdsWithAppointments)
->where('apt.appointment_date', '>=', $startDate)
->where('apt.appointment_date', '<=', $endDate)
->whereNotNull('dept.id')
->group('apt.doctor_id, dept.id')
->select()
->toArray();
// 组装部门统计数据:医生ID => "部门1(数量1) 部门2(数量2)"
foreach ($deptStats as $stat) {
if (!isset($deptStatsMap[$stat['doctor_id']])) {
$deptStatsMap[$stat['doctor_id']] = [];
}
$deptStatsMap[$stat['doctor_id']][] = $stat['dept_name'] . '(' . $stat['dept_count'] . ')';
}
// 获取渠道字典数据
$channelDict = \think\facade\Db::name('dict_data')
->where('type_value', 'channels')
->column('name', 'value');
// 获取渠道统计信息(channels字段,按渠道分组统计数量)
$channelStats = \think\facade\Db::name('doctor_appointment')
->field('doctor_id, channels, COUNT(*) as channel_count')
->whereIn('doctor_id', $doctorIdsWithAppointments)
->where('appointment_date', '>=', $startDate)
->where('appointment_date', '<=', $endDate)
->whereNotNull('channels')
->where('channels', '<>', '')
->group('doctor_id, channels')
->select()
->toArray();
// 组装渠道统计数据:医生ID => "渠道名称1(数量1) 渠道名称2(数量2)"
foreach ($channelStats as $stat) {
if (!isset($channelStatsMap[$stat['doctor_id']])) {
$channelStatsMap[$stat['doctor_id']] = [];
}
$channelName = $channelDict[$stat['channels']] ?? $stat['channels'];
$channelStatsMap[$stat['doctor_id']][] = $channelName . '(' . $stat['channel_count'] . ')';
}
}
// 组装结果
$result = [];
foreach ($doctors as $doctor) {
$stat = $statsMap[$doctor['id']] ?? [
'total_count' => 0,
'registered_count' => 0,
'completed_count' => 0,
'missed_count' => 0,
'cancelled_count' => 0
];
// 计算完成率
$completionRate = $stat['total_count'] > 0
? round(($stat['completed_count'] / $stat['total_count']) * 100, 2)
: 0;
$result[] = [
'doctor_id' => $doctor['id'],
'doctor_name' => $doctor['name'],
'total_count' => (int)$stat['total_count'],
'registered_count' => (int)$stat['registered_count'],
'completed_count' => (int)$stat['completed_count'],
'missed_count' => (int)$stat['missed_count'],
'cancelled_count' => (int)$stat['cancelled_count'],
'completion_rate' => $completionRate,
'dept_stats' => isset($deptStatsMap[$doctor['id']]) ? implode(' ', $deptStatsMap[$doctor['id']]) : '未分配',
'channel_stats' => isset($channelStatsMap[$doctor['id']]) ? implode(' ', $channelStatsMap[$doctor['id']]) : '未设置',
'time_range' => $timeRangeText
];
}
return $result;
}
/**
* @notes 获取时间范围
* @param string $timeType
* @param string $customStartDate
* @param string $customEndDate
* @return array
*/
private function getTimeRange($timeType, $customStartDate, $customEndDate)
{
$today = date('Y-m-d');
switch ($timeType) {
case 'today':
$startDate = $today;
$endDate = $today;
$timeRangeText = '今天 (' . $today . ')';
break;
case 'week':
$startDate = date('Y-m-d', strtotime('-6 days'));
$endDate = $today;
$timeRangeText = '最近7天 (' . $startDate . ' 至 ' . $endDate . ')';
break;
case 'month':
$startDate = date('Y-m-d', strtotime('-29 days'));
$endDate = $today;
$timeRangeText = '最近30天 (' . $startDate . ' 至 ' . $endDate . ')';
break;
case 'custom':
if ($customStartDate && $customEndDate) {
$startDate = $customStartDate;
$endDate = $customEndDate;
$timeRangeText = '自定义 (' . $startDate . ' 至 ' . $endDate . ')';
} else {
$startDate = $today;
$endDate = $today;
$timeRangeText = '今天 (' . $today . ')';
}
break;
default:
$startDate = $today;
$endDate = $today;
$timeRangeText = '今天 (' . $today . ')';
}
return [$startDate, $endDate, $timeRangeText];
}
/**
* @notes 获取数量
* @return int
*/
public function count(): int
{
return count($this->lists());
}
}
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\adminapi\lists\tcm;
use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\tcm\PrescriptionLibraryLogic;
use app\common\model\tcm\PrescriptionLibrary;
use app\common\lists\ListsSearchInterface;
@@ -24,6 +25,21 @@ class PrescriptionLibraryLists extends BaseAdminDataLists implements ListsSearch
];
}
/**
* @notes 列表数据范围:超管/管理员角色看全部;普通账号仅看自己创建 + 公开处方
*/
private function applyDataScope($query)
{
if (PrescriptionLibraryLogic::canManageAllPrescriptions($this->adminId, $this->adminInfo)) {
return $query;
}
return $query->where(function ($q) {
$q->where('creator_id', $this->adminId)
->whereOr('is_public', 1);
});
}
/**
* @notes 获取列表
*/
@@ -34,12 +50,10 @@ class PrescriptionLibraryLists extends BaseAdminDataLists implements ListsSearch
'creator_id', 'creator_name', 'create_time', 'update_time'
];
$lists = PrescriptionLibrary::where($this->searchWhere)
->where(function ($query) {
// 只显示自己创建的或公开的处方
$query->where('creator_id', $this->adminId)
->whereOr('is_public', 1);
})
$query = PrescriptionLibrary::where($this->searchWhere);
$this->applyDataScope($query);
$lists = $query
->field($field)
->limit($this->limitOffset, $this->limitLength)
->order('id', 'desc')
@@ -63,12 +77,9 @@ class PrescriptionLibraryLists extends BaseAdminDataLists implements ListsSearch
*/
public function count(): int
{
return PrescriptionLibrary::where($this->searchWhere)
->where(function ($query) {
// 只统计自己创建的或公开的处方
$query->where('creator_id', $this->adminId)
->whereOr('is_public', 1);
})
->count();
$query = PrescriptionLibrary::where($this->searchWhere);
$this->applyDataScope($query);
return $query->count();
}
}