Update ConversionLogic.php

This commit is contained in:
2026-05-07 12:44:43 +08:00
parent 79203d948e
commit 755491c263
@@ -69,8 +69,6 @@ class ConversionLogic
$entities = self::loadEntities($dimension, $params, $visibleAdminIds); $entities = self::loadEntities($dimension, $params, $visibleAdminIds);
$entityIds = array_keys($entities); $entityIds = array_keys($entities);
$allocationEntities = self::loadEntities($dimension, $params, $visibleAdminIds);
$allocationEntityIds = array_keys($allocationEntities);
if ($entityIds === []) { if ($entityIds === []) {
$result = [ $result = [
@@ -97,8 +95,9 @@ class ConversionLogic
} }
$adminToDeptIds = self::loadAdminDeptMap(); $adminToDeptIds = self::loadAdminDeptMap();
self::hydrateFanStats($allocationEntities, $dimension, $allocationEntityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $mediaChannel, $visibleAdminIds);
self::hydrateFanStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $mediaChannel, $visibleAdminIds); self::hydrateFanStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $mediaChannel, $visibleAdminIds);
$allocationEntities = $entities;
$allocationEntityIds = $entityIds;
self::hydrateAppointmentStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $startDate, $endDate, $mediaChannel, $visibleAdminIds); self::hydrateAppointmentStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $startDate, $endDate, $mediaChannel, $visibleAdminIds);
self::hydrateOrderAndAmountStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $mediaChannel, $visibleAdminIds); self::hydrateOrderAndAmountStats($entities, $dimension, $entityIds, $adminToDeptIds, $startTimestamp, $endTimestamp, $mediaChannel, $visibleAdminIds);
// 数据隔离:可见部门 = 可见 admin 所属部门并集;用于 account_cost 与下游 cost 分摊。 // 数据隔离:可见部门 = 可见 admin 所属部门并集;用于 account_cost 与下游 cost 分摊。
@@ -258,11 +257,13 @@ class ConversionLogic
?array $adminInfo = null ?array $adminInfo = null
): array ): array
{ {
$allowedDeptIds = self::resolveFilterAllowedDeptIds($visibleAdminIds, $eligibleDeptIds);
return [ return [
'departments' => self::buildDepartmentOptions($visibleAdminIds, $eligibleDeptIds), 'departments' => self::buildDepartmentOptions($visibleAdminIds, $eligibleDeptIds, $allowedDeptIds),
'assistants' => DiagnosisLogic::getAssistants($adminId, $adminInfo), 'assistants' => DiagnosisLogic::getAssistants($adminId, $adminInfo),
'doctors' => self::buildDoctorOptions($visibleAdminIds), 'doctors' => self::buildDoctorOptions($visibleAdminIds),
'media_channels' => self::buildMediaChannelOptions($visibleAdminIds, $eligibleDeptIds), 'media_channels' => self::buildMediaChannelOptions($visibleAdminIds, $eligibleDeptIds, $allowedDeptIds),
]; ];
} }
@@ -273,36 +274,19 @@ class ConversionLogic
* *
* @return array<int, array<string, mixed>> * @return array<int, array<string, mixed>>
*/ */
private static function buildDepartmentOptions(?array $visibleAdminIds, array $eligibleDeptIds): array private static function buildDepartmentOptions(?array $visibleAdminIds, array $eligibleDeptIds, ?array $allowedDeptIds = null): array
{ {
$allTree = DeptLogic::getAllData(); $allTree = DeptLogic::getAllData();
if ($visibleAdminIds === null) { if ($visibleAdminIds === null) {
return $allTree; return $allTree;
} }
$allowedDeptIds = []; $allowedDeptIds ??= self::resolveFilterAllowedDeptIds($visibleAdminIds, $eligibleDeptIds);
if ($visibleAdminIds !== []) {
$deptIds = Db::name('admin_dept')
->whereIn('admin_id', $visibleAdminIds)
->column('dept_id');
foreach ($deptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = true;
}
}
}
foreach ($eligibleDeptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = true;
}
}
if ($allowedDeptIds === []) { if ($allowedDeptIds === []) {
return []; return [];
} }
return self::filterDeptTreeByIds($allTree, array_keys($allowedDeptIds)); return self::filterDeptTreeByIds($allTree, $allowedDeptIds);
} }
/** /**
@@ -341,7 +325,7 @@ class ConversionLogic
* *
* @return array<int, array<string, mixed>> * @return array<int, array<string, mixed>>
*/ */
private static function buildMediaChannelOptions(?array $visibleAdminIds, array $eligibleDeptIds): array private static function buildMediaChannelOptions(?array $visibleAdminIds, array $eligibleDeptIds, ?array $allowedDeptIds = null): array
{ {
$allOptions = MediaChannelService::getOptions(); $allOptions = MediaChannelService::getOptions();
if ($visibleAdminIds === null) { if ($visibleAdminIds === null) {
@@ -351,30 +335,13 @@ class ConversionLogic
return $allOptions; return $allOptions;
} }
$allowedDeptIds = []; $allowedDeptIds ??= self::resolveFilterAllowedDeptIds($visibleAdminIds, $eligibleDeptIds);
if ($visibleAdminIds !== []) {
$deptIds = Db::name('admin_dept')
->whereIn('admin_id', $visibleAdminIds)
->column('dept_id');
foreach ($deptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = true;
}
}
}
foreach ($eligibleDeptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = true;
}
}
if ($allowedDeptIds === []) { if ($allowedDeptIds === []) {
return []; return [];
} }
$allowedCodes = Db::name('account_cost') $allowedCodes = Db::name('account_cost')
->whereIn('dept_id', array_keys($allowedDeptIds)) ->whereIn('dept_id', $allowedDeptIds)
->where('media_channel_code', '<>', '') ->where('media_channel_code', '<>', '')
->distinct(true) ->distinct(true)
->column('media_channel_code'); ->column('media_channel_code');
@@ -832,8 +799,14 @@ class ConversionLogic
->leftJoin('admin a', 'a.work_wechat_userid = e.user_id AND a.delete_time IS NULL') ->leftJoin('admin a', 'a.work_wechat_userid = e.user_id AND a.delete_time IS NULL')
->where('e.change_type', 'add_external_contact') ->where('e.change_type', 'add_external_contact')
->where('e.event_time', 'between', [$startTimestamp, $endTimestamp]) ->where('e.event_time', 'between', [$startTimestamp, $endTimestamp])
->fieldRaw('e.user_id, a.id AS admin_id, COUNT(*) AS add_fans_count') ->fieldRaw('a.id AS admin_id, COUNT(*) AS add_fans_count')
->group('e.user_id, a.id'); ->group('a.id');
if ($visibleAdminIds !== null) {
$query->whereIn('a.id', $visibleAdminIds);
} elseif ($dimension !== 'dept' && $entityIds !== []) {
$query->whereIn('a.id', $entityIds);
}
if ($mediaChannel !== null) { if ($mediaChannel !== null) {
$query->leftJoin('qywx_external_contact q', 'q.external_userid = e.external_userid'); $query->leftJoin('qywx_external_contact q', 'q.external_userid = e.external_userid');
@@ -2396,4 +2369,37 @@ class ConversionLogic
return round($numerator / $denominator, 2); return round($numerator / $denominator, 2);
} }
/**
* @param int[]|null $visibleAdminIds
* @param int[] $eligibleDeptIds
* @return int[]
*/
private static function resolveFilterAllowedDeptIds(?array $visibleAdminIds, array $eligibleDeptIds): array
{
if ($visibleAdminIds === null) {
return array_values(array_unique(array_filter(array_map('intval', $eligibleDeptIds), static fn (int $deptId): bool => $deptId > 0)));
}
$allowedDeptIds = [];
if ($visibleAdminIds !== []) {
$deptIds = Db::name('admin_dept')
->whereIn('admin_id', $visibleAdminIds)
->column('dept_id');
foreach ($deptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = $deptId;
}
}
}
foreach ($eligibleDeptIds as $deptId) {
$deptId = (int)$deptId;
if ($deptId > 0) {
$allowedDeptIds[$deptId] = $deptId;
}
}
return array_values($allowedDeptIds);
}
} }