更新
This commit is contained in:
@@ -21,7 +21,7 @@ class DeptPerformanceTargetController extends BaseAdminController
|
||||
$params = (new DeptPerformanceTargetValidate())->goCheck('monthMatrix');
|
||||
$ym = (string) $params['year_month'];
|
||||
|
||||
return $this->data(DeptPerformanceTargetLogic::monthMatrix($ym));
|
||||
return $this->data(DeptPerformanceTargetLogic::monthMatrix($ym, $this->adminId, $this->adminInfo));
|
||||
}
|
||||
|
||||
public function batchSave()
|
||||
@@ -37,12 +37,18 @@ class DeptPerformanceTargetController extends BaseAdminController
|
||||
$ym,
|
||||
$items,
|
||||
$this->adminId,
|
||||
$this->adminInfo,
|
||||
(string) ($this->adminInfo['name'] ?? '')
|
||||
);
|
||||
if ($result === false) {
|
||||
return $this->fail(DeptPerformanceTargetLogic::getError());
|
||||
}
|
||||
|
||||
return $this->success('保存成功', DeptPerformanceTargetLogic::monthMatrix($ym), 1, 1);
|
||||
return $this->success(
|
||||
'保存成功',
|
||||
DeptPerformanceTargetLogic::monthMatrix($ym, $this->adminId, $this->adminInfo),
|
||||
1,
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -170,6 +170,43 @@ class DataScopeService
|
||||
return self::getEffectiveScope($adminInfo) === self::SCOPE_ALL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据范围下:可见成员所在部门及其下级部门 id(与业绩看板 deptOptions、部门类下拉收窄一致)。
|
||||
*
|
||||
* @return array<int, true>|null null 表示不限制;[] 表示无可选部门
|
||||
*/
|
||||
public static function getAllowedDeptIdSet(int $adminId, array $adminInfo): ?array
|
||||
{
|
||||
if ($adminId <= 0 || !self::isEnabled()) {
|
||||
return null;
|
||||
}
|
||||
$visibleIds = self::getVisibleAdminIds($adminId, $adminInfo);
|
||||
if ($visibleIds === null) {
|
||||
return null;
|
||||
}
|
||||
if ($visibleIds === []) {
|
||||
return [];
|
||||
}
|
||||
$set = [];
|
||||
foreach ($visibleIds as $aid) {
|
||||
$deptRows = AdminDept::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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文字描述(日志 / 接口返回可选使用)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user