['label'], '=' => ['type', 'is_show'], ]; } public function lists(): array { $lists = League::where($this->searchWhere) ->whereIn('type', ['sport', 'league']) ->order('sort', 'desc') ->order('id', 'desc') ->select() ->toArray(); // 按 type 分组 $sports = []; $leagues = []; foreach ($lists as $item) { if ($item['type'] === 'sport') { $sports[] = $item; } else { $leagues[] = $item; } } // 按 sport_type 将联赛挂到对应体育类型下 $leagueMap = []; foreach ($leagues as $lg) { $st = $lg['sport_type']; if (!isset($leagueMap[$st])) { $leagueMap[$st] = []; } $leagueMap[$st][] = $lg; } // 构建树形:sport 为父节点,league 为子节点 $tree = []; foreach ($sports as $sport) { $st = $sport['sport_type']; $children = $leagueMap[$st] ?? []; $tree[] = [ 'id' => $sport['id'], 'label' => $sport['label'], 'type' => 'sport', 'sport_type' => $st, 'icon' => $sport['icon'] ?? '', 'sort' => $sport['sort'], 'is_show' => $sport['is_show'], 'is_group' => true, 'children' => $children, ]; } // 未归属任何 sport 的联赛(孤儿数据) $assignedIds = []; foreach ($leagueMap as $group) { foreach ($group as $lg) { $assignedIds[] = $lg['id']; } } $orphans = []; foreach ($leagues as $lg) { if (!in_array($lg['id'], $assignedIds)) { $orphans[] = $lg; } } if (!empty($orphans)) { $tree[] = [ 'id' => 'type_orphan', 'label' => '未分类', 'type' => 'league', 'sport_type' => -1, 'icon' => '', 'sort' => '', 'is_show' => '', 'is_group' => true, 'children' => $orphans, ]; } return $tree; } public function count(): int { return League::where($this->searchWhere)->whereIn('type', ['sport', 'league'])->count(); } }