ethnic.py 1012 B

12345678910111213141516171819202122232425262728293031323334
  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. if user_info:
  25. from app.core.data_scope import apply_data_scope
  26. q = apply_data_scope(q, SysEthnic, user_info, db)
  27. rows = q.order_by(SysEthnic.order_num.asc(), SysEthnic.ethnic_code.asc()).limit(max(1, min(limit, 500))).all()
  28. return [_row_to_dict(x) for x in rows]