340 lines
14 KiB
PHP
340 lines
14 KiB
PHP
<?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;
|
||
}
|
||
|
||
// 诊单数:统计期内该医生挂号对应的 distinct 诊单(patient_id 存的是诊单 id)
|
||
$diagnosisCountQuery = \think\facade\Db::name('doctor_appointment')
|
||
->field('doctor_id, COUNT(DISTINCT patient_id) as diagnosis_count')
|
||
->whereIn('doctor_id', $doctorAdminIds)
|
||
->where('appointment_date', '>=', $startDate)
|
||
->where('appointment_date', '<=', $endDate);
|
||
if ($doctorId) {
|
||
$diagnosisCountQuery->where('doctor_id', $doctorId);
|
||
}
|
||
$diagnosisCountRows = $diagnosisCountQuery->group('doctor_id')->select()->toArray();
|
||
$diagnosisCountMap = [];
|
||
foreach ($diagnosisCountRows as $row) {
|
||
$diagnosisCountMap[(int) $row['doctor_id']] = (int) $row['diagnosis_count'];
|
||
}
|
||
|
||
// 成交单:上述诊单中存在有效处方(未删除、未作废)的数量
|
||
$dealCountQuery = \think\facade\Db::name('doctor_appointment')->alias('apt')
|
||
->join('tcm_prescription rx', 'rx.diagnosis_id = apt.patient_id')
|
||
->field('apt.doctor_id, COUNT(DISTINCT apt.patient_id) as deal_count')
|
||
->whereIn('apt.doctor_id', $doctorAdminIds)
|
||
->where('apt.appointment_date', '>=', $startDate)
|
||
->where('apt.appointment_date', '<=', $endDate)
|
||
->whereNull('rx.delete_time')
|
||
->whereRaw('IFNULL(rx.void_status, 0) <> 1');
|
||
if ($doctorId) {
|
||
$dealCountQuery->where('apt.doctor_id', $doctorId);
|
||
}
|
||
$dealCountRows = $dealCountQuery->group('apt.doctor_id')->select()->toArray();
|
||
$dealCountMap = [];
|
||
foreach ($dealCountRows as $row) {
|
||
$dealCountMap[(int) $row['doctor_id']] = (int) $row['deal_count'];
|
||
}
|
||
|
||
// 获取医生信息
|
||
$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'] . ')';
|
||
}
|
||
|
||
// 获取渠道字典(dict_data.value => name),与挂号创建时 channel_source 存字典 value 一致
|
||
$channelDict = \think\facade\Db::name('dict_data')
|
||
->where('type_value', 'channels')
|
||
->column('name', 'value');
|
||
|
||
// 渠道:业务保存的是 channel_source(字典 value 字符串,见 AppointmentLogic::create);旧库可能仅有 channels(tinyint)
|
||
$channelBuckets = [];
|
||
|
||
try {
|
||
$channelStats = \think\facade\Db::name('doctor_appointment')
|
||
->field('doctor_id, channel_source, COUNT(*) as channel_count')
|
||
->whereIn('doctor_id', $doctorIdsWithAppointments)
|
||
->where('appointment_date', '>=', $startDate)
|
||
->where('appointment_date', '<=', $endDate)
|
||
->where('channel_source', '<>', '')
|
||
->group('doctor_id, channel_source')
|
||
->select()
|
||
->toArray();
|
||
foreach ($channelStats as $stat) {
|
||
$did = (int) $stat['doctor_id'];
|
||
$src = trim((string) ($stat['channel_source'] ?? ''));
|
||
$cnt = (int) ($stat['channel_count'] ?? 0);
|
||
if ($src === '' || $cnt < 1) {
|
||
continue;
|
||
}
|
||
if (!isset($channelBuckets[$did])) {
|
||
$channelBuckets[$did] = [];
|
||
}
|
||
if (!isset($channelBuckets[$did][$src])) {
|
||
$channelBuckets[$did][$src] = 0;
|
||
}
|
||
$channelBuckets[$did][$src] += $cnt;
|
||
}
|
||
} catch (\Throwable) {
|
||
// 无 channel_source 字段等
|
||
}
|
||
|
||
$mergeLegacyChannels = static function (array &$buckets, array $rows): void {
|
||
foreach ($rows as $stat) {
|
||
$did = (int) $stat['doctor_id'];
|
||
$key = (string) (int) ($stat['channels'] ?? 0);
|
||
$cnt = (int) ($stat['channel_count'] ?? 0);
|
||
if ($key === '0' || $cnt < 1) {
|
||
continue;
|
||
}
|
||
if (!isset($buckets[$did])) {
|
||
$buckets[$did] = [];
|
||
}
|
||
if (!isset($buckets[$did][$key])) {
|
||
$buckets[$did][$key] = 0;
|
||
}
|
||
$buckets[$did][$key] += $cnt;
|
||
}
|
||
};
|
||
|
||
try {
|
||
$legacyStats = \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)
|
||
->where('channels', '>', 0)
|
||
->where(function ($q) {
|
||
$q->whereNull('channel_source')->whereOr('channel_source', '=', '');
|
||
})
|
||
->group('doctor_id, channels')
|
||
->select()
|
||
->toArray();
|
||
$mergeLegacyChannels($channelBuckets, $legacyStats);
|
||
} catch (\Throwable) {
|
||
try {
|
||
$legacyStats = \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)
|
||
->where('channels', '>', 0)
|
||
->group('doctor_id, channels')
|
||
->select()
|
||
->toArray();
|
||
$mergeLegacyChannels($channelBuckets, $legacyStats);
|
||
} catch (\Throwable) {
|
||
// 无 channels 字段
|
||
}
|
||
}
|
||
|
||
foreach ($channelBuckets as $did => $byKey) {
|
||
$parts = [];
|
||
foreach ($byKey as $key => $cnt) {
|
||
$label = $channelDict[$key] ?? $channelDict[(string) $key] ?? $key;
|
||
$parts[] = $label . '(' . $cnt . ')';
|
||
}
|
||
$channelStatsMap[$did] = $parts;
|
||
}
|
||
}
|
||
|
||
// 组装结果
|
||
$result = [];
|
||
foreach ($doctors as $doctor) {
|
||
$stat = $statsMap[$doctor['id']] ?? [
|
||
'total_count' => 0,
|
||
'registered_count' => 0,
|
||
'completed_count' => 0,
|
||
'missed_count' => 0,
|
||
'cancelled_count' => 0
|
||
];
|
||
|
||
$did = (int) $doctor['id'];
|
||
$diagnosisCount = $diagnosisCountMap[$did] ?? 0;
|
||
$dealCount = $dealCountMap[$did] ?? 0;
|
||
$dealRate = $diagnosisCount > 0
|
||
? round(($dealCount / $diagnosisCount) * 100, 2)
|
||
: 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'],
|
||
'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']]) : '未设置',
|
||
'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());
|
||
}
|
||
}
|