This commit is contained in:
Your Name
2026-06-16 15:23:59 +08:00
parent e61e7f97fa
commit c9c2b4608f
5 changed files with 678 additions and 2 deletions
@@ -17,6 +17,7 @@ use app\adminapi\logic\stats\YejiStatsLogic;
* - GET stats.yejiStats/leadLines 进线数据明细(add_external_contact 逐条)
* - GET stats.yejiStats/appointmentLines 医助排行榜「预约诊单」挂号逐条明细
* - GET stats.yejiStats/revisitBreakdown 二中心复诊下钻:医助 × 业务订单笔数
* - GET stats.yejiStats/assignLines 被指派数明细(医助×诊单去重,与看板同口径)
*/
class YejiStatsController extends BaseAdminController
{
@@ -141,4 +142,13 @@ class YejiStatsController extends BaseAdminController
return $this->data(YejiStatsLogic::revisitDeptAssistantBreakdown($params, $this->adminId, $this->adminInfo));
}
/** 被指派数明细:与看板「被指派数」同口径(区间内非继承指派,医助×诊单去重) */
public function assignLines()
{
@set_time_limit(120);
$params = $this->request->get();
return $this->data(YejiStatsLogic::assignLines($params, $this->adminId, $this->adminInfo));
}
}
@@ -2616,6 +2616,420 @@ class YejiStatsLogic
];
}
/**
* 被指派明细:与看板「被指派数」同口径(区间内非继承指派,医助×诊单去重)。
*
* @param array{
* start_date?:string,end_date?:string,dept_ids?:int[]|string,channel_code?:string,tag_id?:string,
* assistant_id?:int|string,admin_id?:int|string,dept_id?:int|string,
* page?:int|string,page_size?:int|string
* } $params
*
* @return array<string, mixed>
*/
public static function assignLines(array $params, int $viewerAdminId = 0, array $viewerAdminInfo = []): array
{
$c = self::resolveYejiContext($params);
if ($viewerAdminId > 0) {
self::applyYejiDataScope($c, $viewerAdminId, $viewerAdminInfo);
}
$assistantId = (int) ($params['assistant_id'] ?? $params['admin_id'] ?? 0);
if ($assistantId <= 0 && array_key_exists('dept_id', $params)) {
return self::yejiDeptAssignLinesInner($params, $c);
}
return self::yejiAssistantAssignLinesInner($params, $c, $assistantId);
}
/**
* @param array<string, mixed> $params
* @param array<string, mixed> $c
*
* @return array<string, mixed>
*/
private static function yejiDeptAssignLinesInner(array $params, array $c): array
{
$deptIdParam = (int) ($params['dept_id'] ?? 0);
$channelCode = (string) $c['channelCode'];
$deptName = $deptIdParam === 0
? '未归属中心'
: self::formatYejiDeptRowDisplayName($deptIdParam, $c['deptById']);
$empty = static function (string $note) use ($c, $channelCode, $deptIdParam, $deptName): array {
return self::yejiAssignLinesEmptyPayload($c, $note, 0, '', $deptIdParam, $deptName, $channelCode);
};
if ($deptIdParam <= 0) {
return $empty('未归属中心无被指派明细(看板该行为 0)。');
}
if (!in_array($deptIdParam, $c['tableRowDeptIds'], true)) {
return $empty('该部门不在当前展示部门筛选或数据权限范围内。');
}
$erCenterDeptSet = DeptLogic::getErCenterSubtreeDeptIdSet();
if ($channelCode !== '' && isset($erCenterDeptSet[$deptIdParam])) {
return $empty('与看板一致:二中心及其组织下级在选定渠道下被指派数计 0,无明细。');
}
$targetAssistantIds = [];
foreach ($c['adminToPrimary'] as $aid => $primary) {
if ((int) $primary === $deptIdParam) {
$targetAssistantIds[] = (int) $aid;
}
}
if ($targetAssistantIds === []) {
return $empty('当前部门下没有可映射的被指派医助,无明细。');
}
return self::yejiAssignLinesFetch($c, $params, $targetAssistantIds, 0, '', $deptIdParam, $deptName);
}
/**
* @param array<string, mixed> $params
* @param array<string, mixed> $c
*
* @return array<string, mixed>
*/
private static function yejiAssistantAssignLinesInner(array $params, array $c, int $assistantId): array
{
$channelCode = (string) $c['channelCode'];
$assistantName = '';
if ($assistantId > 0) {
$assistantName = (string) (Db::name('admin')->where('id', $assistantId)->whereNull('delete_time')->value('name') ?: '');
}
$empty = static function (string $note) use ($c, $assistantId, $assistantName, $channelCode): array {
return self::yejiAssignLinesEmptyPayload($c, $note, $assistantId, $assistantName, 0, '', $channelCode);
};
if ($assistantId <= 0) {
return $empty('请指定有效医助,或传入 dept_id 查看部门汇总明细。');
}
$dataScopeRestricted = !empty($c['dataScopeRestricted']);
$dataScopeAdminIds = $dataScopeRestricted && isset($c['dataScopeVisibleAdminIds'])
? $c['dataScopeVisibleAdminIds']
: null;
if ($dataScopeRestricted && $dataScopeAdminIds !== null) {
$flip = array_flip($dataScopeAdminIds);
if (!isset($flip[$assistantId])) {
return $empty('当前账号数据权限下不可查看该医助被指派明细。');
}
}
if (!isset($c['adminToPrimary'][$assistantId])) {
return $empty('该医助不在当前业绩看板展示范围内。');
}
$tagDiagIds = $c['tagDiagIds'];
$tagAssistantIds = $c['tagAssistantIds'];
$tagFallback = $c['tagFallback'];
$scopedAssistants = $tagFallback ? $tagAssistantIds : null;
if ($tagDiagIds !== null && $tagDiagIds === []) {
return $empty('当前渠道标签下无关联诊单,无被指派明细。');
}
if ($scopedAssistants !== null && $scopedAssistants === []) {
return $empty('当前渠道标签下无关联医助,无被指派明细。');
}
if ($scopedAssistants !== null && !isset($scopedAssistants[$assistantId])) {
return $empty('当前渠道标签口径下该医助无被指派记录(与排行榜「被指派数」一致)。');
}
$erCenterDeptSet = DeptLogic::getErCenterSubtreeDeptIdSet();
$primaryDept = (int) ($c['adminToPrimary'][$assistantId] ?? 0);
if ($channelCode !== '' && $primaryDept > 0 && isset($erCenterDeptSet[$primaryDept])) {
return $empty('与看板一致:二中心及其组织下级医助在选定渠道下被指派数计 0,无明细。');
}
return self::yejiAssignLinesFetch(
$c,
$params,
[$assistantId],
$assistantId,
$assistantName,
0,
''
);
}
/**
* @param array<string, mixed> $c
* @param array<string, mixed> $params
* @param list<int> $targetAssistantIds
*
* @return array<string, mixed>
*/
private static function yejiAssignLinesFetch(
array $c,
array $params,
array $targetAssistantIds,
int $assistantIdOut,
string $assistantNameOut,
int $deptIdOut,
string $deptNameOut
): array {
$channelCode = (string) $c['channelCode'];
$startTs = (int) $c['startTs'];
$endTs = (int) $c['endTs'];
$page = max(1, (int) ($params['page'] ?? 1));
$limit = min(100, max(1, (int) ($params['page_size'] ?? 20)));
$offset = ($page - 1) * $limit;
$dataScopeRestricted = !empty($c['dataScopeRestricted']);
$dataScopeAdminIds = $dataScopeRestricted && isset($c['dataScopeVisibleAdminIds'])
? $c['dataScopeVisibleAdminIds']
: null;
if ($dataScopeAdminIds !== null && $dataScopeAdminIds !== []) {
$visFlip = array_flip($dataScopeAdminIds);
$targetAssistantIds = array_values(array_filter(
$targetAssistantIds,
static fn (int $aid): bool => isset($visFlip[$aid])
));
}
if ($targetAssistantIds === []) {
return self::yejiAssignLinesEmptyPayload(
$c,
'当前数据权限下无可查看的被指派医助。',
$assistantIdOut,
$assistantNameOut,
$deptIdOut,
$deptNameOut,
$channelCode
);
}
$tagDiagIds = $c['tagDiagIds'];
$tagAssistantIds = $c['tagAssistantIds'];
$tagFallback = $c['tagFallback'];
$scopedAssistants = $tagFallback ? $tagAssistantIds : null;
if ($tagDiagIds !== null && $tagDiagIds === []) {
return self::yejiAssignLinesEmptyPayload(
$c,
'当前渠道标签下无关联诊单,无被指派明细。',
$assistantIdOut,
$assistantNameOut,
$deptIdOut,
$deptNameOut,
$channelCode
);
}
if ($scopedAssistants !== null && $scopedAssistants === []) {
return self::yejiAssignLinesEmptyPayload(
$c,
'当前渠道标签下无关联医助,无被指派明细。',
$assistantIdOut,
$assistantNameOut,
$deptIdOut,
$deptNameOut,
$channelCode
);
}
$buildBaseQuery = static function () use (
$c,
$targetAssistantIds,
$startTs,
$endTs,
$tagDiagIds,
$scopedAssistants
) {
$diagTable = self::tableWithPrefix('tcm_diagnosis');
$q = Db::name('tcm_diagnosis_assign_log')
->alias('lg')
->join("{$diagTable} dg", 'dg.id = lg.diagnosis_id AND dg.delete_time IS NULL', 'INNER')
->where('lg.create_time', 'between', [$startTs, $endTs])
->where('lg.to_assistant_id', '>', 0)
->where('lg.is_inherit', 0)
->whereIn('lg.to_assistant_id', $targetAssistantIds);
self::applyYejiAssignLogChannelFilters(
$q,
$tagDiagIds,
$scopedAssistants,
$c['appointmentChannelValues'],
$c['channelFilterActive'],
$c['channelInfo']
);
return $q;
};
$countRows = $buildBaseQuery()
->field(['lg.to_assistant_id', 'lg.diagnosis_id'])
->group('lg.to_assistant_id,lg.diagnosis_id')
->select()
->toArray();
$count = \count($countRows);
$rawList = $buildBaseQuery()
->field([
'lg.to_assistant_id',
'lg.diagnosis_id',
Db::raw('COUNT(*) AS assign_count'),
Db::raw('MAX(lg.create_time) AS last_assign_time'),
])
->group('lg.to_assistant_id,lg.diagnosis_id')
->order('last_assign_time', 'desc')
->order('lg.diagnosis_id', 'desc')
->limit($offset, $limit)
->select()
->toArray();
$diagIds = [];
$assistantIds = [];
foreach ($rawList as $rw) {
$diagIds[(int) ($rw['diagnosis_id'] ?? 0)] = true;
$assistantIds[(int) ($rw['to_assistant_id'] ?? 0)] = true;
}
$diagInfo = self::fetchYejiAssignDiagnosisInfo(array_keys($diagIds));
$nameMap = $assistantIds !== []
? Db::name('admin')->whereIn('id', array_keys($assistantIds))->column('name', 'id')
: [];
$lists = [];
foreach ($rawList as $rw) {
$did = (int) ($rw['diagnosis_id'] ?? 0);
$aid = (int) ($rw['to_assistant_id'] ?? 0);
$lastTs = (int) ($rw['last_assign_time'] ?? 0);
$lists[] = [
'diagnosis_id' => $did,
'patient_name' => (string) ($diagInfo[$did]['patient_name'] ?? ''),
'patient_phone' => (string) ($diagInfo[$did]['phone'] ?? ''),
'assistant_id' => $aid,
'assistant_name' => $aid > 0 ? (string) ($nameMap[$aid] ?? ('#' . $aid)) : '—',
'assign_count' => (int) ($rw['assign_count'] ?? 0),
'last_assign_time' => $lastTs,
'last_assign_time_text' => $lastTs > 0 ? date('Y-m-d H:i:s', $lastTs) : '',
];
}
$note = '与看板「被指派数」同口径:区间内 `tcm_diagnosis_assign_log` 成功指派(to_assistant_id>0,剔除勾选「继承」),'
. '按指派操作时间落区间,「医助 × 诊单」去重、剔除已删诊单。';
return [
'start_date' => (string) $c['startDate'],
'end_date' => (string) $c['endDate'],
'assistant_id' => $assistantIdOut,
'assistant_name' => $assistantNameOut,
'dept_id' => $deptIdOut,
'dept_name' => $deptNameOut,
'channel_code' => $channelCode,
'count' => $count,
'lists' => $lists,
'note' => $note,
];
}
/**
* @param array<string, mixed> $c
*
* @return array<string, mixed>
*/
private static function yejiAssignLinesEmptyPayload(
array $c,
string $note,
int $assistantId,
string $assistantName,
int $deptId,
string $deptName,
string $channelCode
): array {
return [
'start_date' => (string) $c['startDate'],
'end_date' => (string) $c['endDate'],
'assistant_id' => $assistantId,
'assistant_name' => $assistantName,
'dept_id' => $deptId,
'dept_name' => $deptName,
'channel_code' => $channelCode,
'count' => 0,
'lists' => [],
'note' => $note,
];
}
/**
* @param \think\db\Query $query
*/
private static function applyYejiAssignLogChannelFilters(
$query,
?array $tagDiagIds,
?array $tagAssistantIds,
array $appointmentChannelValues,
bool $channelFilterActive,
?array $channelInfo
): void {
if (!$channelFilterActive) {
return;
}
if ($appointmentChannelValues !== []) {
$norm = self::normalizeAppointmentChannelValues($appointmentChannelValues);
if ($norm === []) {
$query->whereRaw('0 = 1');
return;
}
$pack = self::buildChannelScopedAppointmentExistsSqlAndBindings('dg.id', $norm, $channelInfo);
if ($pack === null) {
$query->whereRaw('0 = 1');
return;
}
$query->whereRaw($pack[0], $pack[1]);
} elseif ($tagDiagIds !== null || $tagAssistantIds !== null) {
if ($tagDiagIds !== null && $tagDiagIds === []) {
$query->whereRaw('0 = 1');
return;
}
if ($tagAssistantIds !== null && $tagAssistantIds === []) {
$query->whereRaw('0 = 1');
return;
}
if ($tagDiagIds !== null) {
$query->whereIn('lg.diagnosis_id', $tagDiagIds);
}
if ($tagAssistantIds !== null) {
$query->whereIn('lg.to_assistant_id', array_map('intval', array_keys($tagAssistantIds)));
}
} else {
$query->whereRaw('0 = 1');
}
}
/**
* @param list<int> $diagIds
*
* @return array<int, array{patient_name:string,phone:string}>
*/
private static function fetchYejiAssignDiagnosisInfo(array $diagIds): array
{
if ($diagIds === []) {
return [];
}
$rows = Db::name('tcm_diagnosis')
->whereIn('id', $diagIds)
->field(['id', 'patient_name', 'phone'])
->select()
->toArray();
$out = [];
foreach ($rows as $r) {
$out[(int) $r['id']] = [
'patient_name' => trim((string) ($r['patient_name'] ?? '')),
'phone' => trim((string) ($r['phone'] ?? '')),
];
}
return $out;
}
/**
* admin 展示「中心」映射:用于挂号明细按部门筛选(与 aggregateConsults 映射同源)。
*/