protocol_refund_record.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """协议级退费登记 API"""
  2. from __future__ import annotations
  3. from typing import Optional
  4. from fastapi import APIRouter, Depends
  5. from pydantic import BaseModel, ConfigDict, Field
  6. from sqlalchemy.orm import Session
  7. from app.core.database import get_db
  8. from app.core.menu_guard import require_admin_or_super_admin
  9. from app.core.response import error_response, success_response
  10. from app.crud import protocol_refund_record as rr_svc
  11. from app.models.models import (
  12. BizCollectionProtocol,
  13. BizProtocolRefundRecord,
  14. SysUser,
  15. )
  16. router = APIRouter(prefix="/web/admin", tags=["细胞存储-退费登记"])
  17. def _get_tenant_no(db: Session, user: dict) -> str:
  18. user_id = user.get("user_id")
  19. if not user_id:
  20. raise PermissionError("用户身份信息缺失,无法确定租户")
  21. sys_user = db.query(SysUser).filter(SysUser.id == user_id).first()
  22. if not sys_user or not sys_user.tenant_no:
  23. raise PermissionError("用户租户信息缺失")
  24. return sys_user.tenant_no
  25. def _get_current_user_id(user: dict) -> str:
  26. user_id = user.get("user_id")
  27. if not user_id:
  28. raise PermissionError("用户身份信息缺失")
  29. return str(user_id)
  30. def _protocol_accessible(db: Session, protocol_id: str, tenant_no: str, user: dict) -> bool:
  31. from app.core.data_scope import apply_data_scope
  32. q = db.query(BizCollectionProtocol).filter(
  33. BizCollectionProtocol.id == protocol_id,
  34. BizCollectionProtocol.del_flag == 0,
  35. BizCollectionProtocol.tenant_no == tenant_no,
  36. )
  37. q = apply_data_scope(q, BizCollectionProtocol, user, db, tenant_no)
  38. return q.first() is not None
  39. class RefundRecordBody(BaseModel):
  40. model_config = ConfigDict(extra="ignore", populate_by_name=True)
  41. refund_amount: float = Field(..., alias="refundAmount")
  42. refund_time: Optional[str] = Field(None, alias="refundTime")
  43. refund_remark: Optional[str] = Field(None, alias="refundRemark")
  44. refund_attachments: Optional[str] = Field(None, alias="refundAttachments")
  45. workflow_instance_id: Optional[str] = Field(None, alias="workflowInstanceId")
  46. remark: Optional[str] = None
  47. class RefundRecordUpdateBody(BaseModel):
  48. model_config = ConfigDict(extra="ignore", populate_by_name=True)
  49. refund_amount: Optional[float] = Field(None, alias="refundAmount")
  50. refund_time: Optional[str] = Field(None, alias="refundTime")
  51. refund_remark: Optional[str] = Field(None, alias="refundRemark")
  52. refund_attachments: Optional[str] = Field(None, alias="refundAttachments")
  53. workflow_instance_id: Optional[str] = Field(None, alias="workflowInstanceId")
  54. remark: Optional[str] = None
  55. @router.get("/collection-protocols/{protocol_id}/refund-records")
  56. async def list_protocol_refund_records(
  57. protocol_id: str,
  58. user: dict = Depends(require_admin_or_super_admin),
  59. db: Session = Depends(get_db),
  60. ):
  61. try:
  62. tenant_no = _get_tenant_no(db, user)
  63. if not _protocol_accessible(db, protocol_id, tenant_no, user):
  64. return error_response(404, "协议单不存在或无权查看")
  65. data = rr_svc.list_refund_records(
  66. db, collection_protocol_id=protocol_id, tenant_no=tenant_no
  67. )
  68. return success_response(data)
  69. except PermissionError as e:
  70. return error_response(403, str(e))
  71. @router.post("/collection-protocols/{protocol_id}/refund-records")
  72. async def create_protocol_refund_record(
  73. protocol_id: str,
  74. body: RefundRecordBody,
  75. user: dict = Depends(require_admin_or_super_admin),
  76. db: Session = Depends(get_db),
  77. ):
  78. try:
  79. tenant_no = _get_tenant_no(db, user)
  80. user_id = _get_current_user_id(user)
  81. if not _protocol_accessible(db, protocol_id, tenant_no, user):
  82. return error_response(404, "协议单不存在或无权操作")
  83. obj = rr_svc.create_refund_record(
  84. db,
  85. collection_protocol_id=protocol_id,
  86. tenant_no=tenant_no,
  87. user_id=user_id,
  88. refund_amount=body.refund_amount,
  89. refund_time=body.refund_time,
  90. refund_remark=body.refund_remark,
  91. refund_attachments=body.refund_attachments,
  92. workflow_instance_id=body.workflow_instance_id,
  93. remark=body.remark,
  94. )
  95. return success_response(
  96. rr_svc._record_to_dict(db, obj, tenant_no), message="登记成功"
  97. )
  98. except PermissionError as e:
  99. return error_response(403, str(e))
  100. except ValueError as e:
  101. return error_response(400, str(e))
  102. @router.put("/protocol-refund-records/{record_id}")
  103. async def update_protocol_refund_record(
  104. record_id: str,
  105. body: RefundRecordUpdateBody,
  106. user: dict = Depends(require_admin_or_super_admin),
  107. db: Session = Depends(get_db),
  108. ):
  109. try:
  110. tenant_no = _get_tenant_no(db, user)
  111. user_id = _get_current_user_id(user)
  112. existing = (
  113. db.query(BizProtocolRefundRecord)
  114. .filter(
  115. BizProtocolRefundRecord.id == record_id,
  116. BizProtocolRefundRecord.tenant_no == tenant_no,
  117. BizProtocolRefundRecord.del_flag == 0,
  118. )
  119. .first()
  120. )
  121. if not existing:
  122. return error_response(404, "退费登记不存在")
  123. if not _protocol_accessible(
  124. db, existing.collection_protocol_id, tenant_no, user
  125. ):
  126. return error_response(403, "无权操作该协议")
  127. dumped = body.model_dump(by_alias=False, exclude_unset=True)
  128. obj = rr_svc.update_refund_record(
  129. db,
  130. record_id=record_id,
  131. tenant_no=tenant_no,
  132. user_id=user_id,
  133. refund_amount=dumped.get("refund_amount"),
  134. refund_time=dumped.get("refund_time"),
  135. refund_remark=dumped.get("refund_remark"),
  136. refund_attachments=dumped.get("refund_attachments"),
  137. workflow_instance_id=dumped.get("workflow_instance_id"),
  138. remark=dumped.get("remark"),
  139. fields_set=set(dumped.keys()),
  140. )
  141. return success_response(
  142. rr_svc._record_to_dict(db, obj, tenant_no), message="保存成功"
  143. )
  144. except PermissionError as e:
  145. return error_response(403, str(e))
  146. except ValueError as e:
  147. return error_response(400, str(e))
  148. @router.delete("/protocol-refund-records/{record_id}")
  149. async def delete_protocol_refund_record(
  150. record_id: str,
  151. user: dict = Depends(require_admin_or_super_admin),
  152. db: Session = Depends(get_db),
  153. ):
  154. try:
  155. tenant_no = _get_tenant_no(db, user)
  156. user_id = _get_current_user_id(user)
  157. existing = (
  158. db.query(BizProtocolRefundRecord)
  159. .filter(
  160. BizProtocolRefundRecord.id == record_id,
  161. BizProtocolRefundRecord.tenant_no == tenant_no,
  162. BizProtocolRefundRecord.del_flag == 0,
  163. )
  164. .first()
  165. )
  166. if not existing:
  167. return error_response(404, "退费登记不存在")
  168. if not _protocol_accessible(
  169. db, existing.collection_protocol_id, tenant_no, user
  170. ):
  171. return error_response(403, "无权操作该协议")
  172. rr_svc.soft_delete_refund_record(
  173. db, record_id=record_id, tenant_no=tenant_no, user_id=user_id
  174. )
  175. return success_response(None, message="删除成功")
  176. except PermissionError as e:
  177. return error_response(403, str(e))
  178. except ValueError as e:
  179. return error_response(400, str(e))