Files
Xboard/app/Services/ServerParentVisibilityService.php
yinjianm e847252e12 fix(api): 修复节点流量限额共享统计与父子显隐联动
统一节点流量统计与限额展示口径,节点详情新增昨日流量,
并让今日、昨日和本月使用清晰的半开时间窗口聚合

同 machine_id 或同 host 的节点现在共享当前账期已用流量,
管理端优先使用后端 traffic_limit_snapshot 展示月额度状态,
mi-node 下发的 current_used 也改为共享账期统计

新增 parent_auto_hidden 标记与父节点显隐联动服务,父节点
因自动上线或流量限额变为不可展示时会隐藏当前显示的子节点,
恢复时只恢复这批自动隐藏的子节点,避免覆盖手动操作
2026-04-29 02:24:57 +08:00

114 lines
2.8 KiB
PHP

<?php
namespace App\Services;
use App\Models\Server;
use Illuminate\Database\Eloquent\Builder;
class ServerParentVisibilityService
{
public function syncChildrenForParent(Server $parent, bool $parentVisible): array
{
return $parentVisible
? $this->restoreChildrenForParent($parent)
: $this->hideChildrenForParent($parent);
}
public function hideChildrenForParent(Server $parent): array
{
if (!$this->isParentNode($parent)) {
return $this->emptyResult();
}
$result = $this->emptyResult();
$now = time();
$children = $this->childrenQuery($parent)
->where('show', true)
->get();
foreach ($children as $child) {
if (!$child instanceof Server) {
continue;
}
$child->forceFill([
'show' => false,
'parent_auto_hidden' => true,
'parent_auto_action_at' => $now,
])->save();
$result['hidden']++;
}
return $result;
}
public function restoreChildrenForParent(Server $parent): array
{
if (!$this->isParentNode($parent)) {
return $this->emptyResult();
}
$result = $this->emptyResult();
$now = time();
$children = $this->childrenQuery($parent)
->where('parent_auto_hidden', true)
->get();
foreach ($children as $child) {
if (!$child instanceof Server) {
continue;
}
if ($this->hasBlockingAutoHide($child)) {
$result['unchanged']++;
continue;
}
$child->forceFill([
'show' => true,
'parent_auto_hidden' => false,
'parent_auto_action_at' => $now,
])->save();
$result['restored']++;
}
return $result;
}
public function clearParentAutoHidden(Server $server): void
{
if (!(bool) $server->parent_auto_hidden && $server->parent_auto_action_at === null) {
return;
}
$server->parent_auto_hidden = false;
$server->parent_auto_action_at = null;
}
private function isParentNode(Server $server): bool
{
return (int) ($server->parent_id ?? 0) <= 0 && (int) ($server->id ?? 0) > 0;
}
private function childrenQuery(Server $parent): Builder
{
return Server::query()->where('parent_id', (int) $parent->id);
}
private function hasBlockingAutoHide(Server $server): bool
{
return (bool) $server->gfw_auto_hidden;
}
private function emptyResult(): array
{
return [
'hidden' => 0,
'restored' => 0,
'unchanged' => 0,
];
}
}