protocol_refund_record.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """采集协议单 — 协议级退费登记 CRUD(不改转账笔 pay_status)"""
  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, parse_datetime
  7. from app.crud.collection_protocol import urls_to_attachment_list
  8. from app.models.models import BizProtocolRefundRecord
  9. from app.services.protocol_refund_record import validate_refund_amount
  10. def _now() -> datetime:
  11. return datetime.now()
  12. def _record_to_dict(
  13. db: Session, r: BizProtocolRefundRecord, tenant_no: str
  14. ) -> Dict[str, Any]:
  15. return {
  16. "id": r.id,
  17. "collectionProtocolId": r.collection_protocol_id,
  18. "refundAmount": float(r.refund_amount) if r.refund_amount is not None else None,
  19. "refundTime": r.refund_time.strftime("%Y-%m-%d %H:%M:%S") if r.refund_time else "",
  20. "refundRemark": r.refund_remark or "",
  21. "refundAttachments": r.refund_attachments or "",
  22. "refundAttachmentFiles": urls_to_attachment_list(
  23. r.refund_attachments, db, tenant_no
  24. ),
  25. "workflowInstanceId": r.workflow_instance_id or "",
  26. "remark": r.remark or "",
  27. "createTime": r.create_time.strftime("%Y-%m-%d %H:%M:%S") if r.create_time else "",
  28. "updateTime": r.update_time.strftime("%Y-%m-%d %H:%M:%S") if r.update_time else "",
  29. }
  30. def list_refund_records(
  31. db: Session, *, collection_protocol_id: str, tenant_no: str
  32. ) -> List[Dict[str, Any]]:
  33. rows = (
  34. db.query(BizProtocolRefundRecord)
  35. .filter(
  36. BizProtocolRefundRecord.collection_protocol_id == collection_protocol_id,
  37. BizProtocolRefundRecord.tenant_no == tenant_no,
  38. BizProtocolRefundRecord.del_flag == 0,
  39. )
  40. .order_by(BizProtocolRefundRecord.create_time.desc())
  41. .all()
  42. )
  43. return [_record_to_dict(db, r, tenant_no) for r in rows]
  44. def create_refund_record(
  45. db: Session,
  46. *,
  47. collection_protocol_id: str,
  48. tenant_no: str,
  49. user_id: Optional[str],
  50. refund_amount: Any,
  51. refund_time: Any = None,
  52. refund_remark: Optional[str] = None,
  53. refund_attachments: Optional[str] = None,
  54. workflow_instance_id: Optional[str] = None,
  55. remark: Optional[str] = None,
  56. ) -> BizProtocolRefundRecord:
  57. amt = validate_refund_amount(refund_amount)
  58. now = _now()
  59. rt = None
  60. if refund_time not in (None, ""):
  61. rt = (
  62. refund_time
  63. if isinstance(refund_time, datetime)
  64. else parse_datetime(str(refund_time))
  65. )
  66. obj = BizProtocolRefundRecord(
  67. id=generate_uuid(),
  68. collection_protocol_id=collection_protocol_id,
  69. refund_amount=amt,
  70. refund_time=rt or now,
  71. refund_remark=(refund_remark or "").strip() or None,
  72. refund_attachments=(refund_attachments or "").strip() or None,
  73. workflow_instance_id=(workflow_instance_id or "").strip() or None,
  74. tenant_no=tenant_no,
  75. order_num=0,
  76. remark=(remark or "").strip() or None,
  77. create_by=user_id,
  78. create_dept_id="",
  79. create_time=now,
  80. update_by=user_id,
  81. update_time=now,
  82. del_flag=0,
  83. )
  84. db.add(obj)
  85. db.commit()
  86. db.refresh(obj)
  87. return obj
  88. def update_refund_record(
  89. db: Session,
  90. *,
  91. record_id: str,
  92. tenant_no: str,
  93. user_id: Optional[str],
  94. refund_amount: Any = None,
  95. refund_time: Any = None,
  96. refund_remark: Optional[str] = None,
  97. refund_attachments: Optional[str] = None,
  98. workflow_instance_id: Optional[str] = None,
  99. remark: Optional[str] = None,
  100. fields_set: Optional[set] = None,
  101. ) -> BizProtocolRefundRecord:
  102. obj = (
  103. db.query(BizProtocolRefundRecord)
  104. .filter(
  105. BizProtocolRefundRecord.id == record_id,
  106. BizProtocolRefundRecord.tenant_no == tenant_no,
  107. BizProtocolRefundRecord.del_flag == 0,
  108. )
  109. .first()
  110. )
  111. if not obj:
  112. raise ValueError("退费登记不存在")
  113. fs = fields_set or {
  114. "refund_amount",
  115. "refund_time",
  116. "refund_remark",
  117. "refund_attachments",
  118. "workflow_instance_id",
  119. "remark",
  120. }
  121. if "refund_amount" in fs and refund_amount is not None:
  122. obj.refund_amount = validate_refund_amount(refund_amount)
  123. if "refund_time" in fs:
  124. if refund_time in (None, ""):
  125. obj.refund_time = None
  126. elif isinstance(refund_time, datetime):
  127. obj.refund_time = refund_time
  128. else:
  129. obj.refund_time = parse_datetime(str(refund_time))
  130. if "refund_remark" in fs:
  131. obj.refund_remark = (refund_remark or "").strip() or None
  132. if "refund_attachments" in fs:
  133. obj.refund_attachments = (refund_attachments or "").strip() or None
  134. if "workflow_instance_id" in fs:
  135. obj.workflow_instance_id = (workflow_instance_id or "").strip() or None
  136. if "remark" in fs:
  137. obj.remark = (remark or "").strip() or None
  138. obj.update_by = user_id
  139. obj.update_time = _now()
  140. db.add(obj)
  141. db.commit()
  142. db.refresh(obj)
  143. return obj
  144. def soft_delete_refund_record(
  145. db: Session,
  146. *,
  147. record_id: str,
  148. tenant_no: str,
  149. user_id: Optional[str],
  150. ) -> None:
  151. obj = (
  152. db.query(BizProtocolRefundRecord)
  153. .filter(
  154. BizProtocolRefundRecord.id == record_id,
  155. BizProtocolRefundRecord.tenant_no == tenant_no,
  156. BizProtocolRefundRecord.del_flag == 0,
  157. )
  158. .first()
  159. )
  160. if not obj:
  161. raise ValueError("退费登记不存在")
  162. obj.del_flag = 1
  163. obj.update_by = user_id
  164. obj.update_time = _now()
  165. db.add(obj)
  166. db.commit()