This commit is contained in:
Your Name
2026-04-11 18:06:02 +08:00
parent 4909ec6daa
commit abcecf66e7
325 changed files with 4199 additions and 633 deletions
@@ -38,7 +38,7 @@ class StatisticsLists extends BaseAdminDataLists
// 获取医生列表(role_id=1 表示医生角色)
$doctorAdminIds = \app\common\model\auth\AdminRole::where('role_id', 1)->column('admin_id');
if (empty($doctorAdminIds)) {
return [];
}
@@ -56,11 +56,11 @@ class StatisticsLists extends BaseAdminDataLists
->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 索引
@@ -109,32 +109,75 @@ class StatisticsLists extends BaseAdminDataLists
}
$doctors = $doctorQuery->select()->toArray();
// 查询2:获取部门统计信息(通过assistant_id直接关联,按部门分组统计数量)
// 查询2:获取部门统计信息(通过assistant_id直接关联,按部门和状态分组统计数量)
$doctorIdsWithAppointments = array_keys($statsMap);
$deptStatsMap = [];
$deptStatusMap = [];
$channelStatsMap = [];
$channelStatusMap = [];
if (!empty($doctorIdsWithAppointments)) {
// 通过 assistant_id → admin_dept → dept 获取部门统计
// 通过 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')
->field([
'apt.doctor_id',
'dept.id as dept_id',
'dept.name as dept_name',
'apt.status',
'COUNT(*) as 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')
->group('apt.doctor_id, dept.id, apt.status')
->select()
->toArray();
// 组装部门统计数据:医生ID => "部门1(数量1) 部门2(数量2)"
// 同时组装部门状态数据:医生ID => 部门ID => 状态 => 数量
foreach ($deptStats as $stat) {
if (!isset($deptStatsMap[$stat['doctor_id']])) {
$deptStatsMap[$stat['doctor_id']] = [];
$doctorId = $stat['doctor_id'];
$deptId = $stat['dept_id'];
$deptName = $stat['dept_name'];
$status = $stat['status'];
$count = $stat['count'];
// 组装部门统计数据
if (!isset($deptStatsMap[$doctorId])) {
$deptStatsMap[$doctorId] = [];
}
$deptStatsMap[$stat['doctor_id']][] = $stat['dept_name'] . '(' . $stat['dept_count'] . ')';
if (!isset($deptStatsMap[$doctorId][$deptId])) {
$deptStatsMap[$doctorId][$deptId] = [
'dept_name' => $deptName,
'total_count' => 0
];
}
$deptStatsMap[$doctorId][$deptId]['total_count'] += $count;
// 组装部门状态数据
if (!isset($deptStatusMap[$doctorId])) {
$deptStatusMap[$doctorId] = [];
}
if (!isset($deptStatusMap[$doctorId][$deptId])) {
$deptStatusMap[$doctorId][$deptId] = [
'dept_name' => $deptName,
'statuses' => []
];
}
$deptStatusMap[$doctorId][$deptId]['statuses'][$status] = $count;
}
// 格式化部门统计数据
foreach ($deptStatsMap as $doctorId => &$depts) {
$formattedDepts = [];
foreach ($depts as $dept) {
$formattedDepts[] = $dept['dept_name'] . '(' . $dept['total_count'] . ')';
}
$deptStatsMap[$doctorId] = $formattedDepts;
}
// 获取渠道字典(dict_data.value => name),与挂号创建时 channel_source 存字典 value 一致
@@ -144,24 +187,28 @@ class StatisticsLists extends BaseAdminDataLists
// 渠道:业务保存的是 channel_source(字典 value 字符串,见 AppointmentLogic::create);旧库可能仅有 channels(tinyint)
$channelBuckets = [];
$channelStatusBuckets = [];
try {
$channelStats = \think\facade\Db::name('doctor_appointment')
->field('doctor_id, channel_source, COUNT(*) as channel_count')
->field('doctor_id, channel_source, status, COUNT(*) as channel_count')
->whereIn('doctor_id', $doctorIdsWithAppointments)
->where('appointment_date', '>=', $startDate)
->where('appointment_date', '<=', $endDate)
->where('channel_source', '<>', '')
->group('doctor_id, channel_source')
->group('doctor_id, channel_source, status')
->select()
->toArray();
foreach ($channelStats as $stat) {
$did = (int) $stat['doctor_id'];
$src = trim((string) ($stat['channel_source'] ?? ''));
$status = $stat['status'];
$cnt = (int) ($stat['channel_count'] ?? 0);
if ($src === '' || $cnt < 1) {
continue;
}
// 组装渠道统计数据
if (!isset($channelBuckets[$did])) {
$channelBuckets[$did] = [];
}
@@ -169,19 +216,31 @@ class StatisticsLists extends BaseAdminDataLists
$channelBuckets[$did][$src] = 0;
}
$channelBuckets[$did][$src] += $cnt;
// 组装渠道状态数据
if (!isset($channelStatusBuckets[$did])) {
$channelStatusBuckets[$did] = [];
}
if (!isset($channelStatusBuckets[$did][$src])) {
$channelStatusBuckets[$did][$src] = [];
}
$channelStatusBuckets[$did][$src][$status] = $cnt;
}
} catch (\Throwable) {
// 无 channel_source 字段等
}
$mergeLegacyChannels = static function (array &$buckets, array $rows): void {
$mergeLegacyChannels = static function (array &$buckets, array &$statusBuckets, array $rows): void {
foreach ($rows as $stat) {
$did = (int) $stat['doctor_id'];
$key = (string) (int) ($stat['channels'] ?? 0);
$status = $stat['status'];
$cnt = (int) ($stat['channel_count'] ?? 0);
if ($key === '0' || $cnt < 1) {
continue;
}
// 组装渠道统计数据
if (!isset($buckets[$did])) {
$buckets[$did] = [];
}
@@ -189,12 +248,21 @@ class StatisticsLists extends BaseAdminDataLists
$buckets[$did][$key] = 0;
}
$buckets[$did][$key] += $cnt;
// 组装渠道状态数据
if (!isset($statusBuckets[$did])) {
$statusBuckets[$did] = [];
}
if (!isset($statusBuckets[$did][$key])) {
$statusBuckets[$did][$key] = [];
}
$statusBuckets[$did][$key][$status] = $cnt;
}
};
try {
$legacyStats = \think\facade\Db::name('doctor_appointment')
->field('doctor_id, channels, COUNT(*) as channel_count')
->field('doctor_id, channels, status, COUNT(*) as channel_count')
->whereIn('doctor_id', $doctorIdsWithAppointments)
->where('appointment_date', '>=', $startDate)
->where('appointment_date', '<=', $endDate)
@@ -202,22 +270,22 @@ class StatisticsLists extends BaseAdminDataLists
->where(function ($q) {
$q->whereNull('channel_source')->whereOr('channel_source', '=', '');
})
->group('doctor_id, channels')
->group('doctor_id, channels, status')
->select()
->toArray();
$mergeLegacyChannels($channelBuckets, $legacyStats);
$mergeLegacyChannels($channelBuckets, $channelStatusBuckets, $legacyStats);
} catch (\Throwable) {
try {
$legacyStats = \think\facade\Db::name('doctor_appointment')
->field('doctor_id, channels, COUNT(*) as channel_count')
->field('doctor_id, channels, status, COUNT(*) as channel_count')
->whereIn('doctor_id', $doctorIdsWithAppointments)
->where('appointment_date', '>=', $startDate)
->where('appointment_date', '<=', $endDate)
->where('channels', '>', 0)
->group('doctor_id, channels')
->group('doctor_id, channels, status')
->select()
->toArray();
$mergeLegacyChannels($channelBuckets, $legacyStats);
$mergeLegacyChannels($channelBuckets, $channelStatusBuckets, $legacyStats);
} catch (\Throwable) {
// 无 channels 字段
}
@@ -231,6 +299,20 @@ class StatisticsLists extends BaseAdminDataLists
}
$channelStatsMap[$did] = $parts;
}
// 组装渠道状态明细数据
foreach ($channelStatusBuckets as $did => $byKey) {
foreach ($byKey as $key => $statuses) {
$label = $channelDict[$key] ?? $channelDict[(string) $key] ?? $key;
if (!isset($channelStatusMap[$did])) {
$channelStatusMap[$did] = [];
}
$channelStatusMap[$did][] = [
'channel_name' => $label,
'statuses' => $statuses
];
}
}
}
// 组装结果
@@ -252,24 +334,42 @@ class StatisticsLists extends BaseAdminDataLists
: 0;
// 计算完成率
$completionRate = $stat['total_count'] > 0
? round(($stat['completed_count'] / $stat['total_count']) * 100, 2)
$completionRate = $stat['total_count'] > 0
? round(($stat['completed_count'] / $stat['total_count']) * 100, 2)
: 0;
// 格式化部门状态明细
$deptStatusDetails = [];
if (isset($deptStatusMap[$doctor['id']])) {
foreach ($deptStatusMap[$doctor['id']] as $deptId => $deptInfo) {
$deptStatusDetails[] = [
'dept_id' => $deptId,
'dept_name' => $deptInfo['dept_name'],
'statuses' => $deptInfo['statuses']
];
}
}
// 格式化渠道状态明细
$channelStatusDetails = [];
if (isset($channelStatusMap[$doctor['id']])) {
$channelStatusDetails = $channelStatusMap[$doctor['id']];
}
$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'],
'diagnosis_count' => $diagnosisCount,
'deal_count' => $dealCount,
'deal_rate' => $dealRate,
'completion_rate' => $completionRate,
'dept_stats' => isset($deptStatsMap[$doctor['id']]) ? implode(' ', $deptStatsMap[$doctor['id']]) : '未分配',
'channel_stats' => isset($channelStatsMap[$doctor['id']]) ? implode(' ', $channelStatsMap[$doctor['id']]) : '未设置',
'total_count' => (int)$stat['total_count'] ?? 0,
'registered_count' => (int)$stat['registered_count'] ?? 0,
'completed_count' => (int)$stat['completed_count'] ?? 0,
'missed_count' => (int)$stat['missed_count'] ?? 0,
'cancelled_count' => (int)$stat['cancelled_count'] ?? 0,
'diagnosis_count' => $diagnosisCount ?? 0,
'deal_count' => $dealCount ?? 0,
'deal_rate' => $dealRate ?? 0,
'completion_rate' => $completionRate ?? 0,
'dept_status_details' => $deptStatusDetails,
'channel_status_details' => $channelStatusDetails,
'time_range' => $timeRangeText
];
}
@@ -287,26 +387,26 @@ class StatisticsLists extends BaseAdminDataLists
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;
@@ -318,7 +418,7 @@ class StatisticsLists extends BaseAdminDataLists
$timeRangeText = '今天 (' . $today . ')';
}
break;
default:
$startDate = $today;
$endDate = $today;
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace app\adminapi\lists\qywx;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\QywxExternalContact;
/**
* 企业微信客户列表
*/
class CustomerLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 搜索条件
*/
public function setSearch(): array
{
return [
'%like%' => ['name', 'follow_user'],
];
}
/**
* @notes 获取列表
*/
public function lists(): array
{
$lists = QywxExternalContact::where($this->searchWhere)
->whereNull('delete_time')
->order('id', 'desc')
->limit($this->limitOffset, $this->limitLength)
->select()
->toArray();
foreach ($lists as &$item) {
// 解析跟进人JSON
$followUsers = json_decode($item['follow_users'] ?? '[]', true);
$item['follow_users'] = is_array($followUsers) ? $followUsers : [];
}
return $lists;
}
/**
* @notes 获取数量
*/
public function count(): int
{
return QywxExternalContact::where($this->searchWhere)
->whereNull('delete_time')
->count();
}
}
@@ -112,7 +112,8 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$query->whereExists("SELECT 1 FROM {$adTbl} ad WHERE ad.admin_id = {$diagTbl}.assistant_id AND ad.dept_id = {$deptId}");
}
// 按挂号日期+时间升序。若传了 appointment_date(当天/明天等筛选),只按「该日」的挂号排序与展示,避免仍按历史最早一天把昨天号顶到最前
// 按挂号状态优先级排序:已过号(4) > 已预约(1) > 已完成(3),然后按挂号日期+时间升序
// 若传了 appointment_date(当天/明天等筛选),只按「该日」的挂号排序与展示
$diagTbl = (new Diagnosis())->getTable();
$aptTbl = (new Appointment())->getTable();
$minAptDateCond = '';
@@ -120,13 +121,18 @@ class DiagnosisLists extends BaseAdminDataLists implements ListsSearchInterface
$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 . ')';
$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('IFNULL(' . $minAptExpr . ", '9999-12-31 23:59:59') ASC, {$diagTbl}.id DESC")
->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")
->limit($this->limitOffset, $this->limitLength)
->select()
->toArray();
@@ -73,11 +73,29 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
private function applyVisibilityScope($query): void
{
$query->where(function ($query) {
// 超级管理员可查看全部
if (!empty($this->adminInfo['root']) && (int) $this->adminInfo['root'] === 1) {
return;
}
$adminId = $this->adminId;
// 检查是否属于可查看全部处方的角色
$manageAllRoles = config('project.prescription_library_manage_all_roles', [0, 3]);
$roleIds = array_values(array_unique(array_map('intval', $this->adminInfo['role_id'] ?? [])));
$canSeeAll = false;
foreach ($roleIds as $rid) {
if (in_array($rid, $manageAllRoles, true)) {
$canSeeAll = true;
break;
}
}
// 如果属于可查看全部的角色,不添加任何限制
if ($canSeeAll) {
return;
}
// 其他用户只能查看:共享的、自己创建的、自己是医助的、或指定给自己角色的
$adminId = $this->adminId;
$query->where(function ($q) use ($adminId, $roleIds) {
$q->whereOr('is_shared', '=', 1);
$q->whereOr('creator_id', '=', $adminId);
@@ -143,6 +161,14 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
$hasBizOrderRx = array_fill_keys($orderRxIds, true);
}
// 获取医助姓名
$assistantIds = array_unique(array_filter(array_map('intval', array_column($lists, 'assistant_id'))));
$assistantNames = [];
if (!empty($assistantIds)) {
$assistantNames = \app\common\model\auth\Admin::whereIn('id', $assistantIds)
->column('name', 'id');
}
// 处理字段格式
foreach ($lists as &$item) {
// 设置默认值
@@ -159,6 +185,10 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
$item['business_prescription_audit_remark'] = (string) ($bizRejectRemark[$rid] ?? '');
$item['has_prescription_order'] = !empty($hasBizOrderRx[$rid]) ? 1 : 0;
// 添加医助姓名
$assistantId = (int) ($item['assistant_id'] ?? 0);
$item['assistant_name'] = $assistantId > 0 ? ($assistantNames[$assistantId] ?? '') : '';
// 将 dietary_taboo 从逗号分隔的字符串转换为数组(前端需要数组格式)
if (!empty($item['dietary_taboo']) && is_string($item['dietary_taboo'])) {
$item['dietary_taboo'] = array_filter(explode(',', $item['dietary_taboo']));