crud.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from collections.abc import Sequence
  2. from typing import Any, List
  3. from app.api.v1.module_system.auth.schema import AuthSchema
  4. from app.core.base_crud import CRUDBase
  5. from app.core.exceptions import CustomException
  6. from .model import AlipayNotifyLogModel, PayBillModel, PayBillOrderModel, PayBillVoucherModel
  7. class AlipayNotifyLogCRUD(CRUDBase[AlipayNotifyLogModel, Any, Any]):
  8. """支付宝通知日志 CRUD 操作"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. self.auth = auth
  11. super().__init__(model=AlipayNotifyLogModel, auth=auth)
  12. async def get_by_notify_id(
  13. self, notify_id: str
  14. ) -> AlipayNotifyLogModel | None:
  15. return await self.get(notify_id=notify_id)
  16. async def update_by_notify_id(
  17. self, notify_id: str, data: dict
  18. ) -> AlipayNotifyLogModel | None:
  19. obj = await self.get(notify_id=notify_id, preload=[])
  20. if not obj:
  21. raise CustomException(msg="通知日志不存在")
  22. if self.auth.user and hasattr(obj, "updated_id"):
  23. setattr(obj, "updated_id", self.auth.user.id)
  24. for key, value in data.items():
  25. if hasattr(obj, key):
  26. setattr(obj, key, value)
  27. await self.auth.db.flush()
  28. await self.auth.db.refresh(obj)
  29. return obj
  30. class BillCRUD(CRUDBase[PayBillModel, Any, Any]):
  31. """账单 CRUD 操作"""
  32. def __init__(self, auth: AuthSchema) -> None:
  33. self.auth = auth
  34. super().__init__(model=PayBillModel, auth=auth)
  35. async def get_by_pay_no(
  36. self, pay_no: str
  37. ) -> PayBillModel | None:
  38. """根据账单号查询账单"""
  39. return await self.get(pay_no=pay_no)
  40. async def get_by_enterprise_id(
  41. self, enterprise_id: str
  42. ) -> Sequence[PayBillModel]:
  43. """根据企业ID查询账单列表"""
  44. return await self.list({"enterprise_id": enterprise_id})
  45. async def update_by_pay_no(
  46. self, pay_no: str, data: dict
  47. ) -> PayBillModel | None:
  48. """根据账单号更新账单"""
  49. obj = await self.get(pay_no=pay_no, preload=[])
  50. if not obj:
  51. raise CustomException(msg="账单不存在")
  52. if self.auth.user and hasattr(obj, "updated_id"):
  53. setattr(obj, "updated_id", self.auth.user.id)
  54. for key, value in data.items():
  55. if hasattr(obj, key):
  56. setattr(obj, key, value)
  57. await self.auth.db.flush()
  58. await self.auth.db.refresh(obj)
  59. return obj
  60. async def create_or_update(
  61. self, pay_no: str, data: dict
  62. ) -> PayBillModel:
  63. """创建或更新账单"""
  64. obj = await self.get(pay_no=pay_no, preload=[])
  65. if obj:
  66. if self.auth.user and hasattr(obj, "updated_id"):
  67. setattr(obj, "updated_id", self.auth.user.id)
  68. for key, value in data.items():
  69. if hasattr(obj, key):
  70. setattr(obj, key, value)
  71. await self.auth.db.flush()
  72. await self.auth.db.refresh(obj)
  73. return obj
  74. else:
  75. data["pay_no"] = pay_no
  76. if "tenant_id" not in data:
  77. data["tenant_id"] = self.auth.tenant_id
  78. return await self.create(data=data, skip_tenant_id=True)
  79. class BillOrderCRUD(CRUDBase[PayBillOrderModel, Any, Any]):
  80. """订单 CRUD 操作"""
  81. def __init__(self, auth: AuthSchema) -> None:
  82. self.auth = auth
  83. super().__init__(model=PayBillOrderModel, auth=auth)
  84. async def get_by_order_no(
  85. self, order_no: str
  86. ) -> PayBillOrderModel | None:
  87. """根据订单号查询订单"""
  88. return await self.get(order_no=order_no)
  89. async def get_by_pay_no(
  90. self, pay_no: str
  91. ) -> Sequence[PayBillOrderModel]:
  92. """根据账单号查询订单列表"""
  93. return await self.list({"pay_no": pay_no})
  94. async def update_by_order_no(
  95. self, order_no: str, data: dict
  96. ) -> PayBillOrderModel | None:
  97. """根据订单号更新订单"""
  98. obj = await self.get(order_no=order_no, preload=[])
  99. if not obj:
  100. raise CustomException(msg="订单不存在")
  101. if self.auth.user and hasattr(obj, "updated_id"):
  102. setattr(obj, "updated_id", self.auth.user.id)
  103. for key, value in data.items():
  104. if hasattr(obj, key):
  105. setattr(obj, key, value)
  106. await self.auth.db.flush()
  107. await self.auth.db.refresh(obj)
  108. return obj
  109. async def create_or_update(
  110. self, order_no: str, data: dict
  111. ) -> PayBillOrderModel:
  112. """创建或更新订单"""
  113. obj = await self.get(order_no=order_no, preload=[])
  114. if obj:
  115. if self.auth.user and hasattr(obj, "updated_id"):
  116. setattr(obj, "updated_id", self.auth.user.id)
  117. for key, value in data.items():
  118. if hasattr(obj, key):
  119. setattr(obj, key, value)
  120. await self.auth.db.flush()
  121. await self.auth.db.refresh(obj)
  122. return obj
  123. else:
  124. data["order_no"] = order_no
  125. if "tenant_id" not in data:
  126. data["tenant_id"] = self.auth.tenant_id
  127. return await self.create(data=data, skip_tenant_id=True)
  128. class BillVoucherCRUD(CRUDBase[PayBillVoucherModel, Any, Any]):
  129. """凭证 CRUD 操作"""
  130. def __init__(self, auth: AuthSchema) -> None:
  131. self.auth = auth
  132. super().__init__(model=PayBillVoucherModel, auth=auth)
  133. async def get_by_voucher_id(
  134. self, voucher_id: str
  135. ) -> PayBillVoucherModel | None:
  136. """根据凭证ID查询凭证"""
  137. return await self.get(voucher_id=voucher_id)
  138. async def get_by_pay_no(
  139. self, pay_no: str
  140. ) -> Sequence[PayBillVoucherModel]:
  141. """根据账单号查询凭证列表"""
  142. return await self.list({"pay_no": pay_no})
  143. async def update_by_voucher_id(
  144. self, voucher_id: str, data: dict
  145. ) -> PayBillVoucherModel | None:
  146. """根据凭证ID更新凭证"""
  147. obj = await self.get(voucher_id=voucher_id, preload=[])
  148. if not obj:
  149. raise CustomException(msg="凭证不存在")
  150. if self.auth.user and hasattr(obj, "updated_id"):
  151. setattr(obj, "updated_id", self.auth.user.id)
  152. for key, value in data.items():
  153. if hasattr(obj, key):
  154. setattr(obj, key, value)
  155. await self.auth.db.flush()
  156. await self.auth.db.refresh(obj)
  157. return obj
  158. async def create_or_update(
  159. self, voucher_id: str, data: dict
  160. ) -> PayBillVoucherModel:
  161. """创建或更新凭证"""
  162. obj = await self.get(voucher_id=voucher_id, preload=[])
  163. if obj:
  164. if self.auth.user and hasattr(obj, "updated_id"):
  165. setattr(obj, "updated_id", self.auth.user.id)
  166. for key, value in data.items():
  167. if hasattr(obj, key):
  168. setattr(obj, key, value)
  169. await self.auth.db.flush()
  170. await self.auth.db.refresh(obj)
  171. return obj
  172. else:
  173. data["voucher_id"] = voucher_id
  174. if "tenant_id" not in data:
  175. data["tenant_id"] = self.auth.tenant_id
  176. return await self.create(data=data, skip_tenant_id=True)