controller.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. from typing import Annotated
  2. import uuid
  3. from fastapi import APIRouter, Depends, Path, Query
  4. from fastapi.responses import JSONResponse
  5. from app.api.v1.module_system.auth.schema import AuthSchema
  6. from app.common.response import ResponseSchema, SuccessResponse
  7. from app.core.dependencies import AuthPermission
  8. from app.core.logger import log
  9. from app.core.router_class import OperationLogRoute
  10. from app.plugin.module_payment.expense.institution.schema import InstitutionListOutSchema
  11. from .service import InstitutionService, InstitutionScopeService, IssueruleService
  12. from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionCreateModel import (
  13. AlipayEbppInvoiceInstitutionCreateModel,
  14. )
  15. from alipay.aop.api.response.AlipayEbppInvoiceInstitutionCreateResponse import (
  16. AlipayEbppInvoiceInstitutionCreateResponse,
  17. )
  18. from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionDeleteModel import (
  19. AlipayEbppInvoiceInstitutionDeleteModel,
  20. )
  21. from alipay.aop.api.response.AlipayEbppInvoiceInstitutionDeleteResponse import (
  22. AlipayEbppInvoiceInstitutionDeleteResponse,
  23. )
  24. from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionModifyModel import (
  25. AlipayEbppInvoiceInstitutionModifyModel,
  26. )
  27. from alipay.aop.api.response.AlipayEbppInvoiceInstitutionModifyResponse import (
  28. AlipayEbppInvoiceInstitutionModifyResponse,
  29. )
  30. InstitutionRouter = APIRouter(
  31. route_class=OperationLogRoute,
  32. prefix="/institution",
  33. tags=["费控制度"],
  34. )
  35. @InstitutionRouter.post(
  36. "",
  37. summary="创建费控制度",
  38. description="创建费控制度。支持串联调用:创建制度→设置成员→创建发放规则",
  39. )
  40. async def create_institution_controller(
  41. data: dict,
  42. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:create"]))],
  43. ) -> JSONResponse:
  44. """创建费控制度(含完整串联流程)"""
  45. institution_create_model = AlipayEbppInvoiceInstitutionCreateModel.from_alipay_dict(data)
  46. enterprise_id = data.get("enterprise_id", "")
  47. # 解析适用成员数据
  48. scope_data = None
  49. adapter_type = data.get("applicable_scope")
  50. if adapter_type and adapter_type != "NONE":
  51. scope_data = {
  52. "adapter_type": adapter_type,
  53. "owner_type": data.get("scope_owner_type", "EMPLOYEE"),
  54. "add_owner_id_list": data.get("scope_owner_id_list"),
  55. }
  56. # 解析发放规则数据
  57. issuerule_data = None
  58. if data.get("grant_mode") == "period":
  59. period_type_raw = data.get("period_type", "monthly")
  60. # 映射前端period_type到支付宝枚举
  61. ISSUE_TYPE_MAP = {
  62. "daily": "ISSUE_DAY",
  63. "weekly": "ISSUE_WEEK",
  64. "monthly": "ISSUE_MONTH",
  65. "quarterly": "ISSUE_QUARTER",
  66. "yearly": "ISSUE_YEAR",
  67. }
  68. issue_type = ISSUE_TYPE_MAP.get(period_type_raw, "ISSUE_MONTH")
  69. amount = data.get("amount", 0)
  70. # 有效时间配置
  71. effective_time_type = data.get("effective_time_type", "unlimited")
  72. if effective_time_type == "unlimited":
  73. effective_period = '{"all": true}'
  74. elif effective_time_type == "workday":
  75. workday_start = data.get("workday_start_time", "00:00")
  76. workday_end = data.get("workday_end_time", "23:59")
  77. effective_period = f'{{"regular":{{"workday":[["{workday_start}","{workday_end}"]]}}}}'
  78. else:
  79. effective_period = '{"all": true}'
  80. issuerule_data = {
  81. "quota_type": "CAP",
  82. "issue_type": issue_type,
  83. "issue_amount_value": str(amount),
  84. "issue_rule_name": data.get("name", "") + "-发放规则",
  85. "effective_period": effective_period,
  86. "invalid_mode": 1 if data.get("effective_time_type") == "unlimited" else 0,
  87. "share_mode": 0,
  88. "outer_source_id": data.get("outer_source_id") or str(uuid.uuid4()),
  89. }
  90. result = await InstitutionService.create_institution_full_flow(
  91. auth=auth,
  92. institution_model=institution_create_model,
  93. enterprise_id=enterprise_id,
  94. scope_data=scope_data,
  95. issuerule_data=issuerule_data,
  96. )
  97. log.info(f"创建费控制度成功: institution_id={result.get('institution_id')}")
  98. return SuccessResponse(data=result, msg="创建费控制度成功")
  99. @InstitutionRouter.get(
  100. "",
  101. summary="查询费控制度列表",
  102. description="分页查询费控制度列表",
  103. response_model=ResponseSchema[InstitutionListOutSchema],
  104. )
  105. async def list_institution_controller(
  106. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:list"]))],
  107. page_no: Annotated[int, Query(description="页码")] = 1,
  108. page_size: Annotated[int, Query(description="每页数量")] = 20,
  109. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  110. name: Annotated[str | None, Query(description="制度名称")] = None,
  111. expense_type: Annotated[str | None, Query(description="费用类型")] = None,
  112. status: Annotated[str | None, Query(description="状态")] = None,
  113. ) -> JSONResponse:
  114. """查询费控制度列表"""
  115. search = {}
  116. if enterprise_id:
  117. search["enterprise_id"] = enterprise_id
  118. if name:
  119. search["name"] = name
  120. if expense_type:
  121. search["expense_type"] = expense_type
  122. if status:
  123. search["status"] = status
  124. result = await InstitutionService.list_service(
  125. auth=auth, page_no=page_no, page_size=page_size, search=search
  126. )
  127. return SuccessResponse(data=result, msg="查询费控制度列表成功")
  128. @InstitutionRouter.get(
  129. "/{institution_id}",
  130. summary="查询费控制度详情",
  131. description="查询费控制度详情 (alipay.ebpp.invoice.institution.detailinfo.query),失败时降级到本地DB",
  132. )
  133. async def detail_institution_controller(
  134. institution_id: Annotated[str, Path(description="制度ID")],
  135. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:detail"]))],
  136. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  137. ) -> JSONResponse:
  138. """查询费控制度详情"""
  139. if not enterprise_id:
  140. return SuccessResponse(data=None, msg="企业ID不能为空")
  141. result = await InstitutionService.detailinfo_query_service(
  142. auth=auth,
  143. institution_id=institution_id,
  144. enterprise_id=enterprise_id,
  145. )
  146. if result is None:
  147. return SuccessResponse(data=None, msg="制度不存在")
  148. return SuccessResponse(data=result, msg="查询费控制度详情成功")
  149. @InstitutionRouter.delete(
  150. "",
  151. summary="删除费控制度",
  152. description="删除费控制度 (alipay.ebpp.invoice.institution.delete)",
  153. )
  154. async def delete_institution_controller(
  155. data: dict,
  156. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:delete"]))],
  157. ) -> JSONResponse:
  158. """删除费控制度"""
  159. institution_delete_model = AlipayEbppInvoiceInstitutionDeleteModel(**data)
  160. result = await InstitutionService.delete_institution_service(auth=auth, data=institution_delete_model)
  161. log.info(f"删除费控制度成功: institution_id={institution_delete_model.institution_id}, enterprise_id={institution_delete_model.enterprise_id}")
  162. return SuccessResponse(data=result, msg="删除费控制度成功")
  163. @InstitutionRouter.post(
  164. "/modify",
  165. summary="编辑费控制度",
  166. description="编辑费控制度 (alipay.ebpp.invoice.institution.modify)",
  167. )
  168. async def modify_institution_controller(
  169. data: dict,
  170. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:modify"]))],
  171. ) -> JSONResponse:
  172. """编辑费控制度"""
  173. institution_modify_model = AlipayEbppInvoiceInstitutionModifyModel(**data)
  174. result = await InstitutionService.modify_institution_service(auth=auth, data=institution_modify_model)
  175. log.info(f"编辑费控制度成功: institution_id={institution_modify_model.institution_id}")
  176. return SuccessResponse(data=result, msg="编辑费控制度成功")
  177. # ========== 制度成员范围管理 ==========
  178. @InstitutionRouter.get(
  179. "/{institution_id}/scope",
  180. summary="查询制度成员范围",
  181. description="查询制度下成员范围 (alipay.ebpp.invoice.institution.scopepageinfo.query)",
  182. )
  183. async def list_scope_controller(
  184. institution_id: Annotated[str, Path(description="制度ID")],
  185. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:scope:list"]))],
  186. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  187. owner_type: Annotated[str | None, Query(description="适配ID类型")] = None,
  188. page_num: Annotated[int, Query(description="页码")] = 1,
  189. page_size: Annotated[int, Query(description="每页条数")] = 20,
  190. ) -> JSONResponse:
  191. """查询制度成员"""
  192. result = await InstitutionScopeService.scopepageinfo_query_service(
  193. auth=auth,
  194. institution_id=institution_id,
  195. enterprise_id=enterprise_id,
  196. page_num=page_num,
  197. page_size=page_size,
  198. owner_type=owner_type,
  199. )
  200. return SuccessResponse(data=result, msg="查询成功")
  201. @InstitutionRouter.post(
  202. "/{institution_id}/scope",
  203. summary="设置制度成员范围",
  204. description="设置/修改制度成员范围 (alipay.ebpp.invoice.institution.scope.modify)",
  205. )
  206. async def modify_scope_controller(
  207. institution_id: Annotated[str, Path(description="制度ID")],
  208. data: dict,
  209. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:scope:modify"]))],
  210. ) -> JSONResponse:
  211. """设置制度成员"""
  212. result = await InstitutionScopeService.scope_modify_service(
  213. auth=auth,
  214. institution_id=institution_id,
  215. data=data,
  216. )
  217. log.info(f"设置制度成员成功: institution_id={institution_id}, adapter_type={data.get('adapter_type')}")
  218. return SuccessResponse(data=result, msg="设置成功")
  219. # ========== 自动额度发放规则管理 ==========
  220. @InstitutionRouter.post(
  221. "/{institution_id}/issuerule",
  222. summary="创建自动发放规则",
  223. description="创建自动额度发放规则 (alipay.ebpp.invoice.issuerule.create)",
  224. )
  225. async def create_issuerule_controller(
  226. institution_id: Annotated[str, Path(description="制度ID")],
  227. data: dict,
  228. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:create"]))],
  229. ) -> JSONResponse:
  230. """创建自动发放规则"""
  231. result = await IssueruleService.create_issuerule_service(
  232. auth=auth,
  233. institution_id=institution_id,
  234. enterprise_id=data.get("enterprise_id", ""),
  235. quota_type=data.get("quota_type", "CAP"),
  236. issue_type=data.get("issue_type", "ISSUE_MONTH"),
  237. issue_amount_value=data.get("issue_amount_value", "0"),
  238. outer_source_id=data.get("outer_source_id"),
  239. issue_rule_name=data.get("issue_rule_name"),
  240. effective_period=data.get("effective_period"),
  241. invalid_mode=data.get("invalid_mode"),
  242. share_mode=data.get("share_mode"),
  243. )
  244. log.info(f"创建自动发放规则成功: institution_id={institution_id}")
  245. return SuccessResponse(data=result, msg="创建自动发放规则成功")
  246. @InstitutionRouter.put(
  247. "/{institution_id}/issuerule/{issue_rule_id}",
  248. summary="编辑自动发放规则",
  249. description="编辑自动额度发放规则 (alipay.ebpp.invoice.issuerule.modify)",
  250. )
  251. async def modify_issuerule_controller(
  252. institution_id: Annotated[str, Path(description="制度ID")],
  253. issue_rule_id: Annotated[str, Path(description="发放规则ID")],
  254. data: dict,
  255. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:modify"]))],
  256. ) -> JSONResponse:
  257. result = await IssueruleService.modify_issuerule_service(
  258. auth=auth,
  259. institution_id=institution_id,
  260. issue_rule_id=issue_rule_id,
  261. enterprise_id=data.get("enterprise_id", ""),
  262. quota_type=data.get("quota_type"),
  263. issue_type=data.get("issue_type"),
  264. issue_amount_value=data.get("issue_amount_value"),
  265. issue_rule_name=data.get("issue_rule_name"),
  266. effective=data.get("effective"),
  267. effective_period=data.get("effective_period"),
  268. invalid_mode=data.get("invalid_mode"),
  269. share_mode=data.get("share_mode"),
  270. )
  271. log.info(f"编辑自动发放规则成功: issue_rule_id={issue_rule_id}")
  272. return SuccessResponse(data=result, msg="编辑自动发放规则成功")
  273. @InstitutionRouter.delete(
  274. "/{institution_id}/issuerule",
  275. summary="删除自动发放规则",
  276. description="删除自动额度发放规则 (alipay.ebpp.invoice.issuerule.delete)",
  277. )
  278. async def delete_issuerule_controller(
  279. institution_id: Annotated[str, Path(description="制度ID")],
  280. data: dict,
  281. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:delete"]))],
  282. ) -> JSONResponse:
  283. result = await IssueruleService.delete_issuerule_service(
  284. auth=auth,
  285. institution_id=institution_id,
  286. issue_rule_id_list=data.get("issue_rule_id_list", []),
  287. enterprise_id=data.get("enterprise_id", ""),
  288. )
  289. log.info(f"删除自动发放规则成功: institution_id={institution_id}")
  290. return SuccessResponse(data=result, msg="删除自动发放规则成功")