新增功能
This commit is contained in:
@@ -1066,7 +1066,6 @@ class DiagnosisLogic extends BaseLogic
|
||||
|
||||
// 获取access_token(如果是重试,强制刷新)
|
||||
$forceRefresh = $retryCount > 0;
|
||||
|
||||
$accessToken = self::getWxAccessToken($config['app_id'], $config['app_secret'], $forceRefresh);
|
||||
|
||||
if (!$accessToken) {
|
||||
@@ -1723,4 +1722,168 @@ class DiagnosisLogic extends BaseLogic
|
||||
curl_close($ch);
|
||||
return $result ? json_decode($result, true) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 医助理诊单统计(按部门维度,按人统计创建数量)
|
||||
* @param array $params start_time, end_time, days(可选,默认7)
|
||||
* @return array
|
||||
*/
|
||||
public static function assistantDiagnosisStats(array $params = [])
|
||||
{
|
||||
$days = isset($params['days']) ? (int)$params['days'] : 7;
|
||||
|
||||
$endTime = !empty($params['end_time']) ? strtotime($params['end_time']) : time();
|
||||
if ($days === 0) {
|
||||
$startTime = strtotime(date('Y-m-d'));
|
||||
} else {
|
||||
$days = $days > 0 ? min($days, 90) : 7;
|
||||
$startTime = !empty($params['start_time']) ? strtotime($params['start_time']) : strtotime("-{$days} days", $endTime);
|
||||
if ($startTime > $endTime) {
|
||||
$startTime = strtotime("-{$days} days", $endTime);
|
||||
}
|
||||
}
|
||||
|
||||
$depts = \app\common\model\dept\Dept::where('status', 1)
|
||||
->whereNull('delete_time')
|
||||
->order('sort desc, id asc')
|
||||
->column('name', 'id');
|
||||
|
||||
$assistantIds = \app\common\model\auth\AdminRole::where('role_id', 2)
|
||||
->column('admin_id');
|
||||
if (empty($assistantIds)) {
|
||||
return [
|
||||
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
|
||||
'today_total' => 0,
|
||||
'today_ranking' => [],
|
||||
'departments' => [],
|
||||
'ranking' => [],
|
||||
'chart' => ['xAxis' => [], 'series' => []],
|
||||
];
|
||||
}
|
||||
|
||||
$adminDepts = \app\common\model\auth\AdminDept::whereIn('admin_id', $assistantIds)
|
||||
->select()
|
||||
->toArray();
|
||||
$adminToDepts = [];
|
||||
foreach ($adminDepts as $ad) {
|
||||
$adminId = $ad['admin_id'];
|
||||
if (!isset($adminToDepts[$adminId])) {
|
||||
$adminToDepts[$adminId] = [];
|
||||
}
|
||||
$adminToDepts[$adminId][] = (int)$ad['dept_id'];
|
||||
}
|
||||
|
||||
$admins = \app\common\model\auth\Admin::whereIn('id', $assistantIds)
|
||||
->whereNull('delete_time')
|
||||
->column('name', 'id');
|
||||
|
||||
$countRows = Diagnosis::where('assistant_id', 'in', $assistantIds)
|
||||
->where('create_time', 'between', [$startTime, $endTime])
|
||||
->whereNull('delete_time')
|
||||
->field('assistant_id, count(*) as cnt')
|
||||
->group('assistant_id')
|
||||
->select()
|
||||
->toArray();
|
||||
$counts = [];
|
||||
foreach ($countRows as $row) {
|
||||
$counts[$row['assistant_id']] = (int)$row['cnt'];
|
||||
}
|
||||
|
||||
$todayStart = strtotime(date('Y-m-d'));
|
||||
$todayEnd = time();
|
||||
$todayCountRows = Diagnosis::where('assistant_id', 'in', $assistantIds)
|
||||
->where('create_time', 'between', [$todayStart, $todayEnd])
|
||||
->whereNull('delete_time')
|
||||
->field('assistant_id, count(*) as cnt')
|
||||
->group('assistant_id')
|
||||
->select()
|
||||
->toArray();
|
||||
$todayCounts = [];
|
||||
foreach ($todayCountRows as $row) {
|
||||
$todayCounts[$row['assistant_id']] = (int)$row['cnt'];
|
||||
}
|
||||
$todayTotal = array_sum($todayCounts);
|
||||
$todayRanking = [];
|
||||
foreach ($assistantIds as $adminId) {
|
||||
$cnt = (int)($todayCounts[$adminId] ?? 0);
|
||||
$todayRanking[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
usort($todayRanking, fn($a, $b) => $b['count'] <=> $a['count']);
|
||||
$todayRanking = array_values(array_filter($todayRanking, fn($a) => $a['count'] > 0));
|
||||
|
||||
$deptData = [];
|
||||
$assignedAdminIds = [];
|
||||
foreach ($depts as $deptId => $deptName) {
|
||||
$assistants = [];
|
||||
foreach ($adminToDepts as $adminId => $deptIds) {
|
||||
if (!in_array((int)$deptId, $deptIds)) {
|
||||
continue;
|
||||
}
|
||||
$assignedAdminIds[] = $adminId;
|
||||
$cnt = (int)($counts[$adminId] ?? 0);
|
||||
$assistants[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
$deptData[] = [
|
||||
'dept_id' => (int)$deptId,
|
||||
'dept_name' => $deptName,
|
||||
'assistants' => $assistants,
|
||||
'total' => array_sum(array_column($assistants, 'count')),
|
||||
];
|
||||
}
|
||||
$unassignedIds = array_diff($assistantIds, array_unique($assignedAdminIds));
|
||||
if (!empty($unassignedIds)) {
|
||||
$assistants = [];
|
||||
foreach ($unassignedIds as $adminId) {
|
||||
$cnt = (int)($counts[$adminId] ?? 0);
|
||||
$assistants[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
$deptData[] = [
|
||||
'dept_id' => 0,
|
||||
'dept_name' => '未分配部门',
|
||||
'assistants' => $assistants,
|
||||
'total' => array_sum(array_column($assistants, 'count')),
|
||||
];
|
||||
}
|
||||
|
||||
$ranking = [];
|
||||
foreach ($assistantIds as $adminId) {
|
||||
$ranking[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => (int)($counts[$adminId] ?? 0),
|
||||
];
|
||||
}
|
||||
usort($ranking, fn($a, $b) => $b['count'] <=> $a['count']);
|
||||
|
||||
$allAssistantNames = [];
|
||||
$allAssistantData = [];
|
||||
foreach ($ranking as $i => $a) {
|
||||
$allAssistantNames[] = ($i + 1) . '.' . $a['name'];
|
||||
$allAssistantData[] = $a['count'];
|
||||
}
|
||||
|
||||
return [
|
||||
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
|
||||
'today_total' => $todayTotal,
|
||||
'today_ranking' => $todayRanking,
|
||||
'departments' => $deptData,
|
||||
'ranking' => $ranking,
|
||||
'chart' => [
|
||||
'xAxis' => $allAssistantNames,
|
||||
'series' => [['name' => '诊单数', 'data' => $allAssistantData]],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user