sample_insurance.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """样本保险办理 CRUD。"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import Any, Dict, List, Optional
  5. from sqlalchemy.orm import Session
  6. from app.core.utils import generate_uuid
  7. from app.models.models import BizSampleInsurance
  8. from app.services.sample_info_progress import apply_handle_payload, insurance_handled
  9. def _fmt_dt(t: Optional[datetime]) -> str:
  10. return t.strftime("%Y-%m-%d %H:%M:%S") if t else ""
  11. def get_by_sample_code(
  12. db: Session, tenant_no: str, sample_code: str
  13. ) -> Optional[BizSampleInsurance]:
  14. code = (sample_code or "").strip()
  15. if not code:
  16. return None
  17. return (
  18. db.query(BizSampleInsurance)
  19. .filter(
  20. BizSampleInsurance.tenant_no == tenant_no,
  21. BizSampleInsurance.sample_code == code,
  22. BizSampleInsurance.del_flag == 0,
  23. )
  24. .first()
  25. )
  26. def get_map_by_sample_codes(
  27. db: Session, tenant_no: str, codes: List[str]
  28. ) -> Dict[str, BizSampleInsurance]:
  29. cleaned = list({(c or "").strip() for c in codes if (c or "").strip()})
  30. if not cleaned:
  31. return {}
  32. rows = (
  33. db.query(BizSampleInsurance)
  34. .filter(
  35. BizSampleInsurance.tenant_no == tenant_no,
  36. BizSampleInsurance.sample_code.in_(cleaned),
  37. BizSampleInsurance.del_flag == 0,
  38. )
  39. .all()
  40. )
  41. return {row.sample_code: row for row in rows}
  42. def upsert_handle(
  43. db: Session,
  44. *,
  45. tenant_no: str,
  46. sample_code: str,
  47. collection_protocol_id: Optional[str],
  48. user_id: Optional[str],
  49. handle_time: Optional[datetime],
  50. handler_user_id: Optional[str],
  51. insurance_attachments: str = "",
  52. handle_remark: str = "",
  53. policy_send_time: Optional[datetime] = None,
  54. update_policy_send_time: bool = False,
  55. ) -> BizSampleInsurance:
  56. code = (sample_code or "").strip()
  57. if not code:
  58. raise ValueError("sample_code is required")
  59. payload = apply_handle_payload(
  60. handle_time=handle_time,
  61. handler_user_id=handler_user_id,
  62. attachments=insurance_attachments or "",
  63. remark=handle_remark or "",
  64. )
  65. now = datetime.now()
  66. row = get_by_sample_code(db, tenant_no, code)
  67. if row is None:
  68. row = BizSampleInsurance(
  69. id=generate_uuid(),
  70. sample_code=code,
  71. collection_protocol_id=(collection_protocol_id or "").strip() or None,
  72. tenant_no=tenant_no,
  73. order_num=0,
  74. todo_status="0",
  75. create_by=user_id,
  76. create_dept_id="",
  77. create_time=now,
  78. update_by=user_id,
  79. update_time=now,
  80. del_flag=0,
  81. )
  82. db.add(row)
  83. else:
  84. if collection_protocol_id and not row.collection_protocol_id:
  85. row.collection_protocol_id = (collection_protocol_id or "").strip() or None
  86. row.update_by = user_id
  87. row.update_time = now
  88. row.handle_time = payload["handle_time"]
  89. row.handler_user_id = payload["handler_user_id"]
  90. row.todo_status = payload["make_or_todo_status"]
  91. row.handle_date = payload["legacy_date"] or None
  92. row.insurance_attachments = payload["attachments"] or None
  93. row.handle_remark = payload["remark"] or None
  94. if update_policy_send_time:
  95. row.policy_send_time = policy_send_time
  96. db.flush()
  97. return row
  98. def row_to_dict(row: BizSampleInsurance) -> Dict[str, Any]:
  99. return {
  100. "id": row.id,
  101. "sampleCode": row.sample_code or "",
  102. "collectionProtocolId": row.collection_protocol_id or "",
  103. "todoStatus": row.todo_status or "0",
  104. "handleDate": (row.handle_date or "").strip() if row.handle_date else "",
  105. "handleTime": _fmt_dt(row.handle_time) if getattr(row, "handle_time", None) else "",
  106. "handlerUserId": getattr(row, "handler_user_id", None) or "",
  107. "handleRemark": row.handle_remark or "",
  108. "insuranceAttachments": row.insurance_attachments or "",
  109. "policySendTime": _fmt_dt(row.policy_send_time)
  110. if getattr(row, "policy_send_time", None)
  111. else "",
  112. "insuranceHandled": insurance_handled(row),
  113. "tenantNo": row.tenant_no,
  114. "orderNum": row.order_num if row.order_num is not None else 0,
  115. "remark": row.remark or "",
  116. "createTime": _fmt_dt(row.create_time),
  117. "updateTime": _fmt_dt(row.update_time),
  118. }