ethnic.py 961 B

123456789101112131415161718192021222324252627282930313233
  1. """民族码表 CRUD(全局 sys_ethnic)"""
  2. from typing import Any, Dict, List, Optional
  3. from sqlalchemy.orm import Session
  4. from app.models.models import SysEthnic
  5. def _row_to_dict(row: SysEthnic) -> Dict[str, Any]:
  6. return {
  7. "id": row.id,
  8. "ethnicName": row.ethnic_name or "",
  9. "ethnicCode": row.ethnic_code or "",
  10. "orderNum": row.order_num,
  11. }
  12. def list_ethnics(
  13. db: Session,
  14. *,
  15. keyword: Optional[str] = None,
  16. limit: int = 200,
  17. user_info=None,
  18. ) -> List[Dict[str, Any]]:
  19. q = db.query(SysEthnic).filter(SysEthnic.del_flag == 0)
  20. kw = (keyword or "").strip()
  21. if kw:
  22. like = f"%{kw}%"
  23. q = q.filter(SysEthnic.ethnic_name.like(like))
  24. # 民族码表为全局主数据,不做数据范围过滤
  25. _ = user_info
  26. rows = q.order_by(SysEthnic.order_num.asc(), SysEthnic.ethnic_code.asc()).limit(max(1, min(limit, 500))).all()
  27. return [_row_to_dict(x) for x in rows]