22from typing import List , Optional
33
44from sqlalchemy import and_ , or_ , select , func , delete , update
5- from sqlalchemy .dialects .postgresql import JSONB
65from sqlalchemy .orm import aliased
76
87from apps .terminology .models .terminology_model import Terminology , TerminologyInfo
@@ -25,7 +24,7 @@ def page_terminology(session: SessionDep, current_page: int = 1, page_size: int
2524 # 步骤1:先找到所有匹配的节点ID(无论是父节点还是子节点)
2625 matched_ids_subquery = (
2726 select (Terminology .id )
28- .where (Terminology .name .like (keyword_pattern )) # LIKE查询条件
27+ .where (Terminology .word .like (keyword_pattern )) # LIKE查询条件
2928 .subquery ()
3029 )
3130
@@ -60,7 +59,7 @@ def page_terminology(session: SessionDep, current_page: int = 1, page_size: int
6059 children_subquery = (
6160 select (
6261 child .pid ,
63- func .jsonb_agg (child .name ). order_by (child .create_time . desc ( )).label ('children_names ' )
62+ func .jsonb_agg (child .word ). filter (child .word . isnot ( None )).label ('other_words ' )
6463 )
6564 .where (child .pid .isnot (None ))
6665 .group_by (child .pid )
@@ -74,10 +73,7 @@ def page_terminology(session: SessionDep, current_page: int = 1, page_size: int
7473 Terminology .word ,
7574 Terminology .create_time ,
7675 Terminology .description ,
77- func .coalesce (
78- children_subquery .c .children_names ,
79- func .cast ('[]' , JSONB )
80- ).label ('other_words' )
76+ children_subquery .c .other_words
8177 )
8278 .outerjoin (
8379 children_subquery ,
@@ -110,10 +106,7 @@ def page_terminology(session: SessionDep, current_page: int = 1, page_size: int
110106 Terminology .word ,
111107 Terminology .create_time ,
112108 Terminology .description ,
113- func .coalesce (
114- func .jsonb_agg (child .word ),
115- func .cast ('[]' , JSONB )
116- ).label ('other_words' )
109+ func .jsonb_agg (child .word ).filter (child .word .isnot (None )).label ('other_words' )
117110 )
118111 .outerjoin (child , and_ (Terminology .id == child .pid ))
119112 .where (Terminology .id .in_ (paginated_parent_ids ))
@@ -130,7 +123,7 @@ def page_terminology(session: SessionDep, current_page: int = 1, page_size: int
130123 word = row .word ,
131124 create_time = row .create_time ,
132125 description = row .description ,
133- other_words = row .other_words ,
126+ other_words = row .other_words if row . other_words else [] ,
134127 ))
135128
136129 return current_page , page_size , total_count , total_pages , _list
@@ -153,7 +146,7 @@ def create_terminology(session: SessionDep, info: TerminologyInfo):
153146 if info .other_words :
154147 for other_word in info .other_words :
155148 _list .append (
156- Terminology (pid = result .id , word = other_word , create_time = create_time , description = info . description ))
149+ Terminology (pid = result .id , word = other_word , create_time = create_time ))
157150 session .bulk_save_objects (_list )
158151 session .flush ()
159152 session .commit ()
@@ -180,7 +173,7 @@ def update_terminology(session: SessionDep, info: TerminologyInfo):
180173 if info .other_words :
181174 for other_word in info .other_words :
182175 _list .append (
183- Terminology (pid = info .id , word = other_word , create_time = create_time , description = info . description ))
176+ Terminology (pid = info .id , word = other_word , create_time = create_time ))
184177 session .bulk_save_objects (_list )
185178 session .flush ()
186179 session .commit ()
0 commit comments