"""民族码表 CRUD(全局 sys_ethnic)""" from typing import Any, Dict, List, Optional from sqlalchemy.orm import Session from app.models.models import SysEthnic def _row_to_dict(row: SysEthnic) -> Dict[str, Any]: return { "id": row.id, "ethnicName": row.ethnic_name or "", "ethnicCode": row.ethnic_code or "", "orderNum": row.order_num, } def list_ethnics( db: Session, *, keyword: Optional[str] = None, limit: int = 200, user_info=None, ) -> List[Dict[str, Any]]: q = db.query(SysEthnic).filter(SysEthnic.del_flag == 0) kw = (keyword or "").strip() if kw: like = f"%{kw}%" q = q.filter(SysEthnic.ethnic_name.like(like)) if user_info: from app.core.data_scope import apply_data_scope q = apply_data_scope(q, SysEthnic, user_info, db) rows = q.order_by(SysEthnic.order_num.asc(), SysEthnic.ethnic_code.asc()).limit(max(1, min(limit, 500))).all() return [_row_to_dict(x) for x in rows]