from fastapi import APIRouter, Depends, Query, Body from app.api.v1.module_system.auth.schema import AuthSchema from app.common.response import ResponseSchema, SuccessResponse from app.core.logger import log from app.core.dependencies import AuthPermission from app.core.router_class import OperationLogRoute from .schema import ( PointsCreateSchema, PointsUpdateSchema, PointsOutSchema, PointsChangeSchema, PointsRecordOutSchema, ) from .service import PointsService PointsRouter = APIRouter( route_class=OperationLogRoute, prefix="/points", tags=["积分管理"] ) @PointsRouter.post( "", summary="创建积分账户", description="创建租户积分账户", response_model=ResponseSchema[PointsOutSchema], ) async def create_controller( data: PointsCreateSchema = Body(...), auth: AuthSchema = Depends(AuthPermission(["module_payment:points:create"])), ): """ 创建积分账户 """ result = await PointsService.create_points_service(auth=auth, data=data) log.info(f"创建积分账户成功: 租户={data.tenant_id}") return SuccessResponse(data=result, msg="创建积分账户成功") @PointsRouter.get( "", summary="查询租户积分", description="查询当前租户的积分余额", response_model=ResponseSchema[PointsOutSchema], ) async def get_controller(auth: AuthSchema = Depends(AuthPermission(["module_payment:points:detail"]))): """ 查询租户积分 """ result = await PointsService.get_points_service(auth=auth) log.info(f"查询租户积分成功: 租户={auth.tenant_id}, 积分={result.points}") return SuccessResponse(data=result, msg="查询租户积分成功") @PointsRouter.post( "/add", summary="增加积分", description="增加租户积分", response_model=ResponseSchema[PointsOutSchema], ) async def add_controller( data: PointsChangeSchema = Body(...), auth: AuthSchema = Depends(AuthPermission(["module_payment:points:add"])), ): """ 增加积分 """ data.change_type = "ADD" result = await PointsService.add_points_service(auth=auth, data=data) log.info(f"增加积分成功: 租户={auth.tenant_id}, 增加={data.points}") return SuccessResponse(data=result, msg="增加积分成功") @PointsRouter.post( "/deduct", summary="扣除积分", description="扣除租户积分", response_model=ResponseSchema[PointsOutSchema], ) async def deduct_controller( data: PointsChangeSchema = Body(...), auth: AuthSchema = Depends(AuthPermission(["module_payment:points:deduct"])), ): """ 扣除积分 """ data.change_type = "DEDUCT" result = await PointsService.deduct_points_service(auth=auth, data=data) log.info(f"扣除积分成功: 租户={auth.tenant_id}, 扣除={data.points}") return SuccessResponse(data=result, msg="扣除积分成功") @PointsRouter.get( "/record", summary="查询积分记录", description="查询积分变动记录", response_model=ResponseSchema[dict], ) async def list_record_controller( page_no: int = Query(1, ge=1, description="页码"), page_size: int = Query(20, ge=1, le=100, description="每页数量"), enterprise_id: str = Query(None, description="企业ID"), employee_id: str = Query(None, description="员工ID"), auth: AuthSchema = Depends(AuthPermission(["module_payment:points:record:list"])), ): """ 查询积分记录 """ search = {} if enterprise_id: search["enterprise_id"] = enterprise_id if employee_id: search["employee_id"] = employee_id result = await PointsService.list_record_service( auth=auth, page_no=page_no, page_size=page_size, search=search, ) log.info(f"查询积分记录成功: 租户={auth.tenant_id}, 页码={page_no}") return SuccessResponse(data=result, msg="查询积分记录成功")