controller.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from fastapi import APIRouter, Depends, Query, Body
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.common.response import ResponseSchema, SuccessResponse
  4. from app.core.logger import log
  5. from app.core.dependencies import AuthPermission
  6. from app.core.router_class import OperationLogRoute
  7. from .schema import (
  8. PointsCreateSchema,
  9. PointsUpdateSchema,
  10. PointsOutSchema,
  11. PointsChangeSchema,
  12. PointsRecordOutSchema,
  13. )
  14. from .service import PointsService
  15. PointsRouter = APIRouter(
  16. route_class=OperationLogRoute,
  17. prefix="/points",
  18. tags=["积分管理"]
  19. )
  20. @PointsRouter.post(
  21. "",
  22. summary="创建积分账户",
  23. description="创建租户积分账户",
  24. response_model=ResponseSchema[PointsOutSchema],
  25. )
  26. async def create_controller(
  27. data: PointsCreateSchema = Body(...),
  28. auth: AuthSchema = Depends(AuthPermission(["module_payment:points:create"])),
  29. ):
  30. """
  31. 创建积分账户
  32. """
  33. result = await PointsService.create_points_service(auth=auth, data=data)
  34. log.info(f"创建积分账户成功: 租户={data.tenant_id}")
  35. return SuccessResponse(data=result, msg="创建积分账户成功")
  36. @PointsRouter.get(
  37. "",
  38. summary="查询租户积分",
  39. description="查询当前租户的积分余额",
  40. response_model=ResponseSchema[PointsOutSchema],
  41. )
  42. async def get_controller(auth: AuthSchema = Depends(AuthPermission(["module_payment:points:detail"]))):
  43. """
  44. 查询租户积分
  45. """
  46. result = await PointsService.get_points_service(auth=auth)
  47. log.info(f"查询租户积分成功: 租户={auth.tenant_id}, 积分={result.points}")
  48. return SuccessResponse(data=result, msg="查询租户积分成功")
  49. @PointsRouter.post(
  50. "/add",
  51. summary="增加积分",
  52. description="增加租户积分",
  53. response_model=ResponseSchema[PointsOutSchema],
  54. )
  55. async def add_controller(
  56. data: PointsChangeSchema = Body(...),
  57. auth: AuthSchema = Depends(AuthPermission(["module_payment:points:add"])),
  58. ):
  59. """
  60. 增加积分
  61. """
  62. data.change_type = "ADD"
  63. result = await PointsService.add_points_service(auth=auth, data=data)
  64. log.info(f"增加积分成功: 租户={auth.tenant_id}, 增加={data.points}")
  65. return SuccessResponse(data=result, msg="增加积分成功")
  66. @PointsRouter.post(
  67. "/deduct",
  68. summary="扣除积分",
  69. description="扣除租户积分",
  70. response_model=ResponseSchema[PointsOutSchema],
  71. )
  72. async def deduct_controller(
  73. data: PointsChangeSchema = Body(...),
  74. auth: AuthSchema = Depends(AuthPermission(["module_payment:points:deduct"])),
  75. ):
  76. """
  77. 扣除积分
  78. """
  79. data.change_type = "DEDUCT"
  80. result = await PointsService.deduct_points_service(auth=auth, data=data)
  81. log.info(f"扣除积分成功: 租户={auth.tenant_id}, 扣除={data.points}")
  82. return SuccessResponse(data=result, msg="扣除积分成功")
  83. @PointsRouter.get(
  84. "/record",
  85. summary="查询积分记录",
  86. description="查询积分变动记录",
  87. response_model=ResponseSchema[dict],
  88. )
  89. async def list_record_controller(
  90. page_no: int = Query(1, ge=1, description="页码"),
  91. page_size: int = Query(20, ge=1, le=100, description="每页数量"),
  92. enterprise_id: str = Query(None, description="企业ID"),
  93. employee_id: str = Query(None, description="员工ID"),
  94. auth: AuthSchema = Depends(AuthPermission(["module_payment:points:record:list"])),
  95. ):
  96. """
  97. 查询积分记录
  98. """
  99. search = {}
  100. if enterprise_id:
  101. search["enterprise_id"] = enterprise_id
  102. if employee_id:
  103. search["employee_id"] = employee_id
  104. result = await PointsService.list_record_service(
  105. auth=auth,
  106. page_no=page_no,
  107. page_size=page_size,
  108. search=search,
  109. )
  110. log.info(f"查询积分记录成功: 租户={auth.tenant_id}, 页码={page_no}")
  111. return SuccessResponse(data=result, msg="查询积分记录成功")