controller.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. from typing import Annotated
  2. from fastapi import APIRouter, Depends, Path, Query
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, SuccessResponse
  6. from app.core.dependencies import AuthPermission
  7. from app.core.logger import log
  8. from app.core.router_class import OperationLogRoute
  9. from .schema import (
  10. AdjustQuotaSchema,
  11. ExpenseQuotaCreateSchema,
  12. ExpenseQuotaDeleteSchema,
  13. ExpenseQuotaModifySchema,
  14. ExpenseQuotaQuerySchema,
  15. IssueBatchCancelOutSchema,
  16. IssueBatchCancelSchema,
  17. IssueBatchCreateOutSchema,
  18. IssueBatchCreateSchema,
  19. IssueBatchListOutSchema,
  20. IssueBatchRecordsQueryOutSchema,
  21. IssueBatchRecordsQuerySchema,
  22. QuotaCreateSchema,
  23. QuotaListOutSchema,
  24. QuotaOperationOutSchema,
  25. QuotaOutSchema,
  26. QuotaUpdateSchema,
  27. )
  28. from .service import QuotaService
  29. from .outsource_schema import OutsourceNotifySchema, OutsourceNotifyOutSchema
  30. from .outsource_service import OutsourceNotifyService
  31. QuotaRouter = APIRouter(
  32. route_class=OperationLogRoute,
  33. prefix="/quota",
  34. tags=["额度管理"],
  35. )
  36. @QuotaRouter.post(
  37. "/expense/create",
  38. summary="创建余额/点券",
  39. description="创建余额或点券 (alipay.ebpp.invoice.expensecontrol.quota.create)",
  40. response_model=ResponseSchema[QuotaOperationOutSchema],
  41. )
  42. async def create_expense_quota_controller(
  43. data: ExpenseQuotaCreateSchema,
  44. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:expense:create"]))],
  45. ) -> JSONResponse:
  46. result = await QuotaService.create_expense_quota_service(auth=auth, data=data)
  47. log.info(f"创建余额/点券成功: out_biz_no={result.out_biz_no}")
  48. return SuccessResponse(data=result, msg="创建余额/点券成功")
  49. @QuotaRouter.post(
  50. "/expense/query",
  51. summary="查询余额/点券",
  52. description="查询余额或点券 (alipay.ebpp.invoice.expensecontrol.quota.query)",
  53. response_model=ResponseSchema[ExpenseQuotaQuerySchema],
  54. )
  55. async def query_expense_quota_controller(
  56. data: ExpenseQuotaQuerySchema,
  57. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:expense:query"]))],
  58. ) -> JSONResponse:
  59. result = await QuotaService.query_expense_quota_service(auth=auth, data=data)
  60. log.info(f"查询余额/点券成功")
  61. return SuccessResponse(data=result, msg="查询余额/点券成功")
  62. @QuotaRouter.put(
  63. "/expense/{out_biz_no}",
  64. summary="修改余额/点券",
  65. description="修改余额或点券 (alipay.ebpp.invoice.expensecontrol.quota.modify)",
  66. response_model=ResponseSchema[QuotaOperationOutSchema],
  67. )
  68. async def modify_expense_quota_controller(
  69. out_biz_no: Annotated[str, Path(description="外部业务编号")],
  70. data: ExpenseQuotaModifySchema,
  71. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:expense:modify"]))],
  72. ) -> JSONResponse:
  73. result = await QuotaService.modify_expense_quota_service(auth=auth, out_biz_no=out_biz_no, data=data)
  74. log.info(f"修改余额/点券成功: {out_biz_no}")
  75. return SuccessResponse(data=result, msg="修改余额/点券成功")
  76. @QuotaRouter.delete(
  77. "/expense/{out_biz_no}",
  78. summary="删除额度",
  79. description="删除额度 (alipay.ebpp.invoice.expensecontrol.quota.delete)",
  80. response_model=ResponseSchema[QuotaOperationOutSchema],
  81. )
  82. async def delete_expense_quota_controller(
  83. out_biz_no: Annotated[str, Path(description="外部业务编号")],
  84. data: ExpenseQuotaDeleteSchema,
  85. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:expense:delete"]))],
  86. ) -> JSONResponse:
  87. result = await QuotaService.delete_expense_quota_service(auth=auth, out_biz_no=out_biz_no, data=data)
  88. log.info(f"删除额度成功: {out_biz_no}")
  89. return SuccessResponse(data=result, msg="删除额度成功")
  90. @QuotaRouter.post(
  91. "",
  92. summary="创建额度",
  93. description="创建额度",
  94. response_model=ResponseSchema[QuotaOperationOutSchema],
  95. )
  96. async def create_quota_controller(
  97. data: QuotaCreateSchema,
  98. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:create"]))],
  99. ) -> JSONResponse:
  100. result = await QuotaService.create_quota_service(auth=auth, data=data)
  101. log.info(f"创建额度成功: out_biz_no={result.out_biz_no}")
  102. return SuccessResponse(data=result, msg="创建额度成功")
  103. @QuotaRouter.get(
  104. "",
  105. summary="查询额度列表",
  106. description="分页查询额度列表",
  107. response_model=ResponseSchema[QuotaListOutSchema],
  108. )
  109. async def list_quota_controller(
  110. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:list"]))],
  111. page_no: Annotated[int, Query(description="页码")] = 1,
  112. page_size: Annotated[int, Query(description="每页数量")] = 20,
  113. institution_id: Annotated[str | None, Query(description="制度ID")] = None,
  114. employee_id: Annotated[str | None, Query(description="员工ID")] = None,
  115. ) -> JSONResponse:
  116. search = {}
  117. if institution_id:
  118. search["institution_id"] = institution_id
  119. if employee_id:
  120. search["employee_id"] = employee_id
  121. result = await QuotaService.list_service(
  122. auth=auth, page_no=page_no, page_size=page_size, search=search
  123. )
  124. return SuccessResponse(data=result, msg="查询额度列表成功")
  125. @QuotaRouter.get(
  126. "/{quota_id}",
  127. summary="查询额度详情",
  128. description="根据额度ID查询额度详情",
  129. )
  130. async def detail_quota_controller(
  131. quota_id: Annotated[str, Path(description="额度ID")],
  132. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:detail"]))],
  133. ) -> JSONResponse:
  134. from .model import QuotaModel
  135. from sqlalchemy import select, update as sa_update
  136. stmt = select(QuotaModel).where(QuotaModel.quota_id == quota_id)
  137. result = await auth.db.execute(stmt)
  138. quota = result.scalar_one_or_none()
  139. # quota_id 未回填时按数据库 id 查找
  140. if not quota:
  141. try:
  142. local_id = int(quota_id)
  143. stmt = select(QuotaModel).where(QuotaModel.id == local_id)
  144. result = await auth.db.execute(stmt)
  145. quota = result.scalar_one_or_none()
  146. except (ValueError, TypeError):
  147. pass
  148. if quota:
  149. return SuccessResponse(data={
  150. "id": quota.id,
  151. "quota_id": quota.quota_id,
  152. "employee_id": quota.employee_id,
  153. "institution_id": quota.institution_id,
  154. "out_biz_no": quota.out_biz_no,
  155. "quota_type": quota.quota_type,
  156. "target_type": quota.target_type,
  157. "target_id": quota.target_id,
  158. "total_amount": float(quota.total_amount) if quota.total_amount else 0,
  159. "available_amount": float(quota.available_amount) if quota.available_amount else 0,
  160. "status": quota.status,
  161. "valid_from": quota.valid_from.strftime("%Y-%m-%d %H:%M:%S") if quota.valid_from else None,
  162. "valid_to": quota.valid_to.strftime("%Y-%m-%d %H:%M:%S") if quota.valid_to else None,
  163. "created_time": str(quota.created_time) if quota.created_time else None,
  164. "updated_time": str(quota.updated_time) if quota.updated_time else None,
  165. "enterprise_id": quota.enterprise_id,
  166. }, msg="查询额度详情成功")
  167. # 本地未找到时,回查支付宝
  168. try:
  169. from alipay.aop.api.request.AlipayEbppInvoiceExpensecontrolQuotaQueryRequest import (
  170. AlipayEbppInvoiceExpensecontrolQuotaQueryRequest,
  171. )
  172. from alipay.aop.api.domain.AlipayEbppInvoiceExpensecontrolQuotaQueryModel import (
  173. AlipayEbppInvoiceExpensecontrolQuotaQueryModel,
  174. )
  175. from alipay.aop.api.response.AlipayEbppInvoiceExpensecontrolQuotaQueryResponse import (
  176. AlipayEbppInvoiceExpensecontrolQuotaQueryResponse,
  177. )
  178. from app.core.alipay_client import AlipayClient
  179. alipay_model = AlipayEbppInvoiceExpensecontrolQuotaQueryModel()
  180. alipay_model.owner_type = "ENTERPRISE_PAY_UID"
  181. alipay_model.quota_id_list = [quota_id]
  182. alipay_model.page_size = 1
  183. alipay_model.page_num = 1
  184. request = AlipayEbppInvoiceExpensecontrolQuotaQueryRequest()
  185. request.biz_model = alipay_model
  186. client = AlipayClient.get_client()
  187. response = client.execute(request)
  188. if response:
  189. alipay_result = AlipayEbppInvoiceExpensecontrolQuotaQueryResponse()
  190. alipay_result.parse_response_content(response)
  191. if alipay_result.is_success() and hasattr(alipay_result, 'quota_detail_info_list') and alipay_result.quota_detail_info_list:
  192. q = alipay_result.quota_detail_info_list[0]
  193. return SuccessResponse(data={
  194. "quota_id": getattr(q, 'quota_id', None),
  195. "target_type": getattr(q, 'target_type', None),
  196. "target_id": getattr(q, 'target_id', None),
  197. "quota_type": getattr(q, 'quota_type', None),
  198. "total_amount": float(getattr(q, 'total_amount', 0)) if getattr(q, 'total_amount', None) else 0,
  199. "available_amount": float(getattr(q, 'available_amount', 0)) if getattr(q, 'available_amount', None) else 0,
  200. "frozen_amount": float(getattr(q, 'frozen_amount', 0)) if getattr(q, 'frozen_amount', None) else 0,
  201. "consumed_amount": float(getattr(q, 'consumed_amount', 0)) if getattr(q, 'consumed_amount', None) else 0,
  202. "status": getattr(q, 'status', "QUOTA_ACTIVE"),
  203. "valid_from": getattr(q, 'effective_start_date', None),
  204. "valid_to": getattr(q, 'effective_end_date', None),
  205. "employee_id": getattr(q, 'owner_id', None),
  206. "owner_id": getattr(q, 'owner_id', None),
  207. "owner_open_id": getattr(q, 'owner_open_id', None),
  208. "owner_type": getattr(q, 'owner_type', None),
  209. }, msg="查询额度详情成功(支付宝)")
  210. except Exception as e:
  211. log.warning(f"查询支付宝额度详情失败: {e}")
  212. return SuccessResponse(data=None, msg="额度不存在")
  213. # ========================
  214. # 手工批量发放额度
  215. # ========================
  216. @QuotaRouter.get(
  217. "/issuebatch/list",
  218. summary="查询手工发放批次列表",
  219. description="分页查询手工发放批次列表",
  220. )
  221. async def list_issue_batch_controller(
  222. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:issuebatch:list"]))],
  223. page_no: Annotated[int, Query(description="页码")] = 1,
  224. page_size: Annotated[int, Query(description="每页数量")] = 20,
  225. institution_id: Annotated[str | None, Query(description="制度ID")] = None,
  226. ) -> JSONResponse:
  227. search = {}
  228. if institution_id:
  229. search["institution_id"] = institution_id
  230. result = await QuotaService.list_batch_service(
  231. auth=auth, page_no=page_no, page_size=page_size, search=search
  232. )
  233. return SuccessResponse(data=result, msg="查询批次列表成功")
  234. @QuotaRouter.post(
  235. "/issuebatch/create",
  236. summary="手工批量发放额度",
  237. description="批量对企业下的员工进行额度发放 (alipay.ebpp.invoice.expensecontrol.issuebatch.create)",
  238. response_model=ResponseSchema[IssueBatchCreateOutSchema],
  239. )
  240. async def issue_batch_create_controller(
  241. data: IssueBatchCreateSchema,
  242. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:issuebatch:create"]))],
  243. ) -> JSONResponse:
  244. result = await QuotaService.issue_batch_create_service(auth=auth, data=data)
  245. log.info(f"手工批量发放额度成功: batch_no={data.batch_no}, issue_batch_id={result.issue_batch_id}")
  246. return SuccessResponse(data=result, msg="手工批量发放额度成功")
  247. @QuotaRouter.post(
  248. "/issuebatch/cancel",
  249. summary="作废手工发放批次",
  250. description="作废当前批次下发放的额度 (alipay.ebpp.invoice.expensecontrol.issuebatch.cancel)",
  251. response_model=ResponseSchema[IssueBatchCancelOutSchema],
  252. )
  253. async def issue_batch_cancel_controller(
  254. data: IssueBatchCancelSchema,
  255. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:issuebatch:cancel"]))],
  256. ) -> JSONResponse:
  257. result = await QuotaService.issue_batch_cancel_service(auth=auth, data=data)
  258. log.info(f"作废手工发放批次成功: issue_batch_id={data.issue_batch_id}")
  259. return SuccessResponse(data=result, msg="作废手工发放批次成功")
  260. @QuotaRouter.post(
  261. "/issuebatch/records",
  262. summary="查询手工发放发放明细",
  263. description="根据批次号分页查询手工发放的发放明细 (alipay.ebpp.invoice.issuebatch.issuerecords.batchquery)",
  264. response_model=ResponseSchema[IssueBatchRecordsQueryOutSchema],
  265. )
  266. async def issue_batch_records_query_controller(
  267. data: IssueBatchRecordsQuerySchema,
  268. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:issuebatch:records"]))],
  269. ) -> JSONResponse:
  270. result = await QuotaService.issue_batch_records_query_service(auth=auth, data=data)
  271. log.info(f"查询手工发放发放明细成功: issue_batch_id={data.issue_batch_id}")
  272. return SuccessResponse(data=result, msg="查询手工发放发放明细成功")
  273. @QuotaRouter.post(
  274. "/outsource/notify",
  275. summary="外部消费额度同步",
  276. description="将外部消费同步到支付宝额度系统 (alipay.ebpp.invoice.expensecomsue.outsource.notify)",
  277. response_model=ResponseSchema[OutsourceNotifyOutSchema],
  278. )
  279. async def outsource_notify_controller(
  280. data: OutsourceNotifySchema,
  281. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:outsource:notify"]))],
  282. ) -> JSONResponse:
  283. result = await OutsourceNotifyService.notify_service(auth=auth, data=data)
  284. log.info(f"外部消费额度同步: out_source_id={result.out_source_id}, success={result.success}")
  285. return SuccessResponse(data=result, msg="外部消费额度同步成功" if result.success else "外部消费额度同步失败")
  286. @QuotaRouter.get(
  287. "/employee/{employee_id}/records",
  288. summary="查询员工额度发放记录",
  289. description="查询该员工在指定/所有制度下的额度发放记录",
  290. )
  291. async def list_employee_quota_records_controller(
  292. employee_id: Annotated[str, Path(description="员工ID")],
  293. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:list"]))],
  294. institution_id: Annotated[str | None, Query(description="制度ID")] = None,
  295. ) -> JSONResponse:
  296. items = await QuotaService.list_employee_quota_records_service(
  297. auth=auth, employee_id=employee_id, institution_id=institution_id,
  298. )
  299. return SuccessResponse(data={"items": items, "total": len(items)}, msg="查询员工额度记录成功")
  300. @QuotaRouter.post(
  301. "/{quota_id}/adjust",
  302. summary="调整额度金额",
  303. description="调整额度可用金额 (alipay.ebpp.invoice.expensecontrol.quota.modify)",
  304. )
  305. async def adjust_quota_controller(
  306. quota_id: Annotated[str, Path(description="额度ID")],
  307. data: AdjustQuotaSchema,
  308. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:update"]))],
  309. ) -> JSONResponse:
  310. result = await QuotaService.adjust_quota_service(auth=auth, data=data)
  311. log.info(f"调整额度成功: quota_id={quota_id}, before={result['before_amount']}, after={result['after_amount']}")
  312. return SuccessResponse(data=result, msg="调整额度成功")
  313. @QuotaRouter.get(
  314. "/{quota_id}/changes",
  315. summary="查询额度变更记录",
  316. description="查询该额度的所有变更记录",
  317. )
  318. async def list_quota_changes_controller(
  319. quota_id: Annotated[str, Path(description="额度ID")],
  320. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:quota:detail"]))],
  321. ) -> JSONResponse:
  322. items = await QuotaService.list_quota_changes_service(auth=auth, quota_id=quota_id)
  323. return SuccessResponse(data={"items": items, "total": len(items)}, msg="查询变更记录成功")