fix: 回退Grid布局恢复flex并列,保留二级分组映射,空大类隐藏
This commit is contained in:
@@ -35,13 +35,13 @@ class KnowledgeService:
|
||||
"manual", # 其他
|
||||
]
|
||||
|
||||
# 二级分组维度映射(与前端 useKnowledgeGrouping.js 的 SECONDARY_GROUP_FIELD 保持一致)
|
||||
# 二级分组维度映射(与前端 useKnowledgeGrouping.js 的 SECONDARY_GROUP_FIELD 一致)
|
||||
# key = source_type, value = 用来做二级分组的字段名,None = 不建二级节点
|
||||
SECONDARY_GROUP_FIELD = {
|
||||
"manuscript": "author", # 节目文稿 → 按作者(编导)
|
||||
"military_report": "source_detail", # 杂志文章 → 按出处
|
||||
"baoti": None, # 报题单 → 不分组
|
||||
"manual": None, # 其他 → 不分组
|
||||
"manuscript": "author", # 节目文稿 → 按作者(编导)
|
||||
"military_report": "source_detail", # 杂志文章 → 按出处
|
||||
"baoti": None, # 报题单 → 不分组
|
||||
"manual": None, # 其他 → 不分组
|
||||
}
|
||||
|
||||
SOURCE_TYPE_LABEL = {
|
||||
@@ -268,20 +268,21 @@ class KnowledgeService:
|
||||
def get_grouped_items(self) -> list[dict]:
|
||||
"""
|
||||
按 source_type → 二级字段(author / source_detail)两层聚合,返回树形结构数据。
|
||||
按 SOURCE_TYPE_ORDER 固定顺序排列。
|
||||
按 SOURCE_TYPE_ORDER 固定顺序排列,仅显示有数据的大类(count > 0)。
|
||||
|
||||
二级节点 key 格式:`{source_type}|{二级字段名}|{字段值}`
|
||||
例:manuscript|author|左鑫
|
||||
military_report|source_detail|航空知识 2026年第1期
|
||||
|
||||
二级字段值为 null / 空字串 → 归入对应大类,不造空节点。
|
||||
空大类(0条)不渲染。
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
items = session.exec(select(KnowledgeItem)).all()
|
||||
|
||||
total_count = len(items)
|
||||
|
||||
# 按 source_type 分组,初始化所有已知类别(保证空类别也按顺序出现)
|
||||
# 按 source_type 分组,初始化所有已知类别
|
||||
type_groups: dict = {st: [] for st in self.SOURCE_TYPE_ORDER}
|
||||
for item in items:
|
||||
st = item.source_type or "manual"
|
||||
@@ -292,14 +293,8 @@ class KnowledgeService:
|
||||
children = []
|
||||
for st in self.SOURCE_TYPE_ORDER:
|
||||
st_items = type_groups.get(st, [])
|
||||
# 空类别(0条)不渲染
|
||||
if not st_items:
|
||||
# 空类别也按顺序出现(0条)
|
||||
children.append({
|
||||
"key": st,
|
||||
"label": f"{self.SOURCE_TYPE_LABEL.get(st, st)}(0条)",
|
||||
"count": 0,
|
||||
"children": [],
|
||||
})
|
||||
continue
|
||||
|
||||
secondary_field = self.SECONDARY_GROUP_FIELD.get(st)
|
||||
@@ -309,7 +304,6 @@ class KnowledgeService:
|
||||
# 按二级字段分组
|
||||
detail_groups: dict = {}
|
||||
for item in st_items:
|
||||
# 取二级字段值:author 直接取,source_detail 从 tags(JSONB) 取
|
||||
if secondary_field == "source_detail":
|
||||
tags = item.tags or {}
|
||||
sd = tags.get("source_detail") if isinstance(tags, dict) else None
|
||||
|
||||
Reference in New Issue
Block a user