This commit is contained in:
Your Name
2026-05-05 14:34:39 +08:00
parent c03e7051d7
commit 40eec808ef
7 changed files with 291 additions and 352 deletions
@@ -8,6 +8,7 @@ use app\adminapi\logic\dept\DeptLogic;
use app\common\logic\BaseLogic;
use app\common\model\dept\Dept;
use app\common\model\finance\DeptPerformanceTarget;
use app\common\service\DataScope\DataScopeService;
use think\facade\Db;
class DeptPerformanceTargetLogic extends BaseLogic
@@ -15,11 +16,16 @@ class DeptPerformanceTargetLogic extends BaseLogic
/**
* 指定月份:保留部门树层级 + 合并当月已有目标
*
* @return array{year_month: string, rows: array<int, array<string, mixed>>, total_target: float}
* @return array{year_month: string, rows: array<int, array<string, mixed>>, total_target: float, data_scope_limited: bool}
*/
public static function monthMatrix(string $yearMonth): array
public static function monthMatrix(string $yearMonth, int $adminId = 0, array $adminInfo = []): array
{
$tree = DeptLogic::getAllData();
$allowed = DataScopeService::getAllowedDeptIdSet($adminId, $adminInfo);
$scoped = $allowed !== null;
if ($scoped) {
$tree = self::filterDeptTreeAllowedForest(is_array($tree) ? $tree : [], $allowed);
}
$rowsDb = DeptPerformanceTarget::where('year_month', $yearMonth)
->select()
@@ -33,19 +39,22 @@ class DeptPerformanceTargetLogic extends BaseLogic
$total = self::sumTargetsInTree($rows);
return [
'year_month' => $yearMonth,
'rows' => $rows,
'total_target' => round($total, 2),
'year_month' => $yearMonth,
'rows' => $rows,
'total_target' => round($total, 2),
'data_scope_limited' => $scoped,
];
}
/**
* @param array<int, array{dept_id?:int|float|string, target_amount?:int|float|string, remark?:string}> $items
*/
public static function batchSave(string $yearMonth, array $items, int $adminId, string $adminName): bool
public static function batchSave(string $yearMonth, array $items, int $adminId, array $adminInfo, string $adminName): bool
{
$allowed = DataScopeService::getAllowedDeptIdSet($adminId, $adminInfo);
try {
Db::transaction(function () use ($yearMonth, $items, $adminId, $adminName): void {
Db::transaction(function () use ($yearMonth, $items, $adminId, $adminName, $allowed): void {
foreach ($items as $item) {
if (!is_array($item)) {
continue;
@@ -54,6 +63,10 @@ class DeptPerformanceTargetLogic extends BaseLogic
if ($deptId <= 0) {
continue;
}
// 仅处理 data scope 内的 dept_id(界面树与 monthMatrix 一致,不再含纯展示用父级)
if ($allowed !== null && !isset($allowed[$deptId])) {
continue;
}
$dept = Dept::where('id', $deptId)->whereNull('delete_time')->find();
if (!$dept) {
continue;
@@ -162,4 +175,63 @@ class DeptPerformanceTargetLogic extends BaseLogic
return $s;
}
/**
* 本部门及以下:仅展示 allowed 内节点;无权父级不渲染,从子树中接续(不露出公司/上级中心)。
*
* @param array<int, array<string, mixed>> $nodes
* @param array<int, true> $allowedMap
*
* @return array<int, array<string, mixed>>
*/
private static function filterDeptTreeAllowedForest(array $nodes, array $allowedMap): array
{
if ($allowedMap === []) {
return [];
}
$out = [];
foreach ($nodes as $n) {
if (!is_array($n)) {
continue;
}
$id = (int) ($n['id'] ?? 0);
if ($id <= 0) {
continue;
}
$rawChildren = $n['children'] ?? [];
$kids = is_array($rawChildren) ? $rawChildren : [];
if (isset($allowedMap[$id])) {
$childList = [];
foreach ($kids as $c) {
if (!is_array($c)) {
continue;
}
foreach (self::filterDeptTreeAllowedForest([$c], $allowedMap) as $item) {
$childList[] = $item;
}
}
$node = $n;
if ($childList !== []) {
$node['children'] = $childList;
} else {
unset($node['children']);
}
$out[] = $node;
continue;
}
foreach ($kids as $c) {
if (!is_array($c)) {
continue;
}
foreach (self::filterDeptTreeAllowedForest([$c], $allowedMap) as $item) {
$out[] = $item;
}
}
}
return $out;
}
}
@@ -672,34 +672,7 @@ class YejiStatsLogic
*/
private static function buildYejiDataScopeAllowedDeptIdMap(int $adminId, array $adminInfo): ?array
{
if ($adminId <= 0 || !DataScopeService::isEnabled()) {
return null;
}
$visibleIds = DataScopeService::getVisibleAdminIds($adminId, $adminInfo);
if ($visibleIds === null) {
return null;
}
if ($visibleIds === []) {
return [];
}
$set = [];
foreach ($visibleIds as $aid) {
$deptRows = Db::name('admin_dept')->where('admin_id', (int) $aid)->column('dept_id');
foreach ($deptRows as $d) {
$d = (int) $d;
if ($d <= 0) {
continue;
}
foreach (DeptLogic::getSelfAndDescendantIds($d) as $x) {
$x = (int) $x;
if ($x > 0) {
$set[$x] = true;
}
}
}
}
return $set;
return DataScopeService::getAllowedDeptIdSet($adminId, $adminInfo);
}
/**