|
| 1 | +import datetime |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | +from sqlalchemy import and_, or_, select, func, delete, update |
| 5 | +from sqlalchemy.dialects.postgresql import JSONB |
| 6 | +from sqlalchemy.orm import aliased |
| 7 | + |
| 8 | +from apps.terminology.models.terminology_model import Terminology, TerminologyInfo |
| 9 | +from common.core.deps import SessionDep |
| 10 | + |
| 11 | + |
| 12 | +def page_terminology(session: SessionDep, current_page: int = 1, page_size: int = 10, name: Optional[str] = None): |
| 13 | + _list: List[TerminologyInfo] = [] |
| 14 | + |
| 15 | + child = aliased(Terminology) |
| 16 | + |
| 17 | + current_page = max(1, current_page) |
| 18 | + page_size = max(10, page_size) |
| 19 | + |
| 20 | + total_count = 0 |
| 21 | + total_pages = 0 |
| 22 | + |
| 23 | + if name and name.strip() != "": |
| 24 | + keyword_pattern = f"%{name.strip()}%" |
| 25 | + # 步骤1:先找到所有匹配的节点ID(无论是父节点还是子节点) |
| 26 | + matched_ids_subquery = ( |
| 27 | + select(Terminology.id) |
| 28 | + .where(Terminology.name.like(keyword_pattern)) # LIKE查询条件 |
| 29 | + .subquery() |
| 30 | + ) |
| 31 | + |
| 32 | + # 步骤2:找到这些匹配节点的所有父节点(包括自身如果是父节点) |
| 33 | + parent_ids_subquery = ( |
| 34 | + select(Terminology.id) |
| 35 | + .where( |
| 36 | + (Terminology.id.in_(matched_ids_subquery)) | |
| 37 | + (Terminology.id.in_( |
| 38 | + select(Terminology.pid) |
| 39 | + .where(Terminology.id.in_(matched_ids_subquery)) |
| 40 | + .where(Terminology.pid.isnot(None)) |
| 41 | + )) |
| 42 | + ) |
| 43 | + .where(Terminology.pid.is_(None)) # 只取父节点 |
| 44 | + ) |
| 45 | + |
| 46 | + count_stmt = select(func.count()).select_from(parent_ids_subquery.subquery()) |
| 47 | + total_count = session.execute(count_stmt).scalar() |
| 48 | + total_pages = (total_count + page_size - 1) // page_size |
| 49 | + |
| 50 | + # 步骤3:获取分页后的父节点ID |
| 51 | + paginated_parent_ids = ( |
| 52 | + parent_ids_subquery |
| 53 | + .order_by(Terminology.create_time.desc()) |
| 54 | + .offset((current_page - 1) * page_size) |
| 55 | + .limit(page_size) |
| 56 | + .subquery() |
| 57 | + ) |
| 58 | + |
| 59 | + # 步骤4:获取这些父节点的childrenNames |
| 60 | + children_subquery = ( |
| 61 | + select( |
| 62 | + child.pid, |
| 63 | + func.jsonb_agg(child.name).order_by(child.create_time.desc()).label('children_names') |
| 64 | + ) |
| 65 | + .where(child.pid.isnot(None)) |
| 66 | + .group_by(child.pid) |
| 67 | + .subquery() |
| 68 | + ) |
| 69 | + |
| 70 | + # 主查询 |
| 71 | + stmt = ( |
| 72 | + select( |
| 73 | + Terminology.id, |
| 74 | + Terminology.word, |
| 75 | + Terminology.create_time, |
| 76 | + Terminology.description, |
| 77 | + func.coalesce( |
| 78 | + children_subquery.c.children_names, |
| 79 | + func.cast('[]', JSONB) |
| 80 | + ).label('other_words') |
| 81 | + ) |
| 82 | + .outerjoin( |
| 83 | + children_subquery, |
| 84 | + Terminology.id == children_subquery.c.pid |
| 85 | + ) |
| 86 | + .where(Terminology.id.in_(paginated_parent_ids)) |
| 87 | + .order_by(Terminology.create_time.desc()) |
| 88 | + ) |
| 89 | + print(str(stmt)) |
| 90 | + else: |
| 91 | + parent_ids_subquery = ( |
| 92 | + select(Terminology.id) |
| 93 | + .where(Terminology.pid.is_(None)) # 只取父节点 |
| 94 | + ) |
| 95 | + count_stmt = select(func.count()).select_from(parent_ids_subquery.subquery()) |
| 96 | + total_count = session.execute(count_stmt).scalar() |
| 97 | + total_pages = (total_count + page_size - 1) // page_size |
| 98 | + |
| 99 | + paginated_parent_ids = ( |
| 100 | + parent_ids_subquery |
| 101 | + .order_by(Terminology.create_time.desc()) |
| 102 | + .offset((current_page - 1) * page_size) |
| 103 | + .limit(page_size) |
| 104 | + .subquery() |
| 105 | + ) |
| 106 | + |
| 107 | + stmt = ( |
| 108 | + select( |
| 109 | + Terminology.id, |
| 110 | + Terminology.word, |
| 111 | + Terminology.create_time, |
| 112 | + Terminology.description, |
| 113 | + func.coalesce( |
| 114 | + func.jsonb_agg(child.word), |
| 115 | + func.cast('[]', JSONB) |
| 116 | + ).label('other_words') |
| 117 | + ) |
| 118 | + .outerjoin(child, and_(Terminology.id == child.pid)) |
| 119 | + .where(Terminology.id.in_(paginated_parent_ids)) |
| 120 | + .group_by(Terminology.id, Terminology.word) |
| 121 | + .order_by(Terminology.create_time.desc()) |
| 122 | + ) |
| 123 | + print(str(stmt)) |
| 124 | + |
| 125 | + result = session.execute(stmt) |
| 126 | + |
| 127 | + for row in result: |
| 128 | + _list.append(TerminologyInfo( |
| 129 | + id=row.id, |
| 130 | + word=row.word, |
| 131 | + create_time=row.create_time, |
| 132 | + description=row.description, |
| 133 | + other_words=row.other_words, |
| 134 | + )) |
| 135 | + |
| 136 | + return current_page, page_size, total_count, total_pages, _list |
| 137 | + |
| 138 | + |
| 139 | +def create_terminology(session: SessionDep, info: TerminologyInfo): |
| 140 | + create_time = datetime.datetime.now() |
| 141 | + parent = Terminology(word=info.word, create_time=create_time, description=info.description) |
| 142 | + |
| 143 | + result = Terminology(**parent.model_dump()) |
| 144 | + |
| 145 | + session.add(parent) |
| 146 | + session.flush() |
| 147 | + session.refresh(parent) |
| 148 | + |
| 149 | + result.id = parent.id |
| 150 | + session.commit() |
| 151 | + |
| 152 | + _list: List[Terminology] = [] |
| 153 | + if info.other_words: |
| 154 | + for other_word in info.other_words: |
| 155 | + _list.append( |
| 156 | + Terminology(pid=result.id, word=other_word, create_time=create_time, description=info.description)) |
| 157 | + session.bulk_save_objects(_list) |
| 158 | + session.flush() |
| 159 | + session.commit() |
| 160 | + |
| 161 | + # todo embedding |
| 162 | + |
| 163 | + return result.id |
| 164 | + |
| 165 | + |
| 166 | +def update_terminology(session: SessionDep, info: TerminologyInfo): |
| 167 | + stmt = update(Terminology).where(and_(Terminology.id == info.id)).values( |
| 168 | + word=info.word, |
| 169 | + description=info.description, |
| 170 | + ) |
| 171 | + session.execute(stmt) |
| 172 | + session.commit() |
| 173 | + |
| 174 | + stmt = delete(Terminology).where(and_(Terminology.pid == info.id)) |
| 175 | + session.execute(stmt) |
| 176 | + session.commit() |
| 177 | + |
| 178 | + create_time = datetime.datetime.now() |
| 179 | + _list: List[Terminology] = [] |
| 180 | + if info.other_words: |
| 181 | + for other_word in info.other_words: |
| 182 | + _list.append( |
| 183 | + Terminology(pid=info.id, word=other_word, create_time=create_time, description=info.description)) |
| 184 | + session.bulk_save_objects(_list) |
| 185 | + session.flush() |
| 186 | + session.commit() |
| 187 | + |
| 188 | + # todo embedding |
| 189 | + |
| 190 | + return info.id |
| 191 | + |
| 192 | + |
| 193 | +def delete_terminology(session: SessionDep, ids: list[int]): |
| 194 | + stmt = delete(Terminology).where(or_(Terminology.id.in_(ids), Terminology.pid.in_(ids))) |
| 195 | + session.execute(stmt) |
| 196 | + session.commit() |
0 commit comments