protocol_status_display.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """协议状态展示(含作废类型后缀)。"""
  2. from __future__ import annotations
  3. from typing import Dict, Optional
  4. from sqlalchemy.orm import Session
  5. from app.models.models import SysDictData
  6. VOID_TYPE_DICT = "xyzflx"
  7. # 协议/捐赠单无效态(与样本接收、生产工单列表过滤口径一致)
  8. INACTIVE_PROTOCOL_STATUSES = ("yzf", "sdfq")
  9. _PROTOCOL_STATUS_LABELS = {
  10. "dsx": "待生效",
  11. "sxz": "生效中",
  12. "ygq": "已过期",
  13. "yzf": "已作废",
  14. "sdfq": "属地废弃",
  15. "zky": "转科研",
  16. }
  17. _DONATION_STATUS_LABELS = {
  18. "dsx": "待生效",
  19. "sxz": "生效中",
  20. "ygq": "已过期",
  21. "yzf": "已作废",
  22. "sdfq": "属地废弃",
  23. "zckc": "转细胞存储",
  24. }
  25. _VOID_TYPE_FALLBACK_LABELS = {
  26. "khhc": "客户毁存",
  27. "jcbhg": "检测不合格",
  28. }
  29. def resolve_void_type_label(
  30. db: Optional[Session],
  31. tenant_no: Optional[str],
  32. void_type: Optional[str],
  33. ) -> str:
  34. code = (void_type or "").strip()
  35. if not code:
  36. return ""
  37. if db is not None and tenant_no:
  38. row = (
  39. db.query(SysDictData.dict_label)
  40. .filter(
  41. SysDictData.dict_type == VOID_TYPE_DICT,
  42. SysDictData.dict_value == code,
  43. SysDictData.tenant_no == tenant_no,
  44. SysDictData.del_flag == 0,
  45. )
  46. .first()
  47. )
  48. if row and (row.dict_label or "").strip():
  49. return (row.dict_label or "").strip()
  50. return _VOID_TYPE_FALLBACK_LABELS.get(code, code)
  51. def format_protocol_status_label(
  52. status: str,
  53. void_type: Optional[str] = None,
  54. *,
  55. kind: str = "protocol",
  56. db: Optional[Session] = None,
  57. tenant_no: Optional[str] = None,
  58. ) -> str:
  59. st = (status or "").strip()
  60. labels = _DONATION_STATUS_LABELS if kind == "donation" else _PROTOCOL_STATUS_LABELS
  61. base = labels.get(st, st)
  62. if st != "yzf":
  63. return base
  64. vt_label = resolve_void_type_label(db, tenant_no, void_type)
  65. if vt_label:
  66. return f"{base}({vt_label})"
  67. return base
  68. def build_void_type_label_map(
  69. db: Session,
  70. tenant_no: str,
  71. codes: list[str],
  72. ) -> Dict[str, str]:
  73. normalized = list({(c or "").strip() for c in codes if (c or "").strip()})
  74. if not normalized:
  75. return {}
  76. rows = (
  77. db.query(SysDictData.dict_value, SysDictData.dict_label)
  78. .filter(
  79. SysDictData.dict_type == VOID_TYPE_DICT,
  80. SysDictData.dict_value.in_(normalized),
  81. SysDictData.tenant_no == tenant_no,
  82. SysDictData.del_flag == 0,
  83. )
  84. .all()
  85. )
  86. out = {r.dict_value: (r.dict_label or "").strip() for r in rows if (r.dict_value or "").strip()}
  87. for code in normalized:
  88. out.setdefault(code, _VOID_TYPE_FALLBACK_LABELS.get(code, code))
  89. return out