|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, Path, Query |
| 6 | + |
| 7 | +from backend.app.admin.schema.data_scope import ( |
| 8 | + CreateDataScopeParam, |
| 9 | + GetDataScopeDetail, |
| 10 | + GetDataScopeWithRelationDetail, |
| 11 | + UpdateDataScopeParam, |
| 12 | + UpdateDataScopeRuleParam, |
| 13 | +) |
| 14 | +from backend.app.admin.service.data_scope_service import data_scope_service |
| 15 | +from backend.common.pagination import DependsPagination, PageData, paging_data |
| 16 | +from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base |
| 17 | +from backend.common.security.jwt import DependsJwtAuth |
| 18 | +from backend.common.security.permission import RequestPermission |
| 19 | +from backend.common.security.rbac import DependsRBAC |
| 20 | +from backend.database.db import CurrentSession |
| 21 | + |
| 22 | +router = APIRouter() |
| 23 | + |
| 24 | + |
| 25 | +@router.get('/{pk}', summary='获取数据范围详情', dependencies=[DependsJwtAuth]) |
| 26 | +async def get_data_scope( |
| 27 | + pk: Annotated[int, Path(description='数据范围 ID')], |
| 28 | +) -> ResponseSchemaModel[GetDataScopeDetail]: |
| 29 | + data = await data_scope_service.get(pk=pk) |
| 30 | + return response_base.success(data=data) |
| 31 | + |
| 32 | + |
| 33 | +@router.get('/{pk}/rules', summary='获取数据范围所有规则', dependencies=[DependsJwtAuth]) |
| 34 | +async def get_data_scope_rules( |
| 35 | + pk: Annotated[int, Path(description='数据范围 ID')], |
| 36 | +) -> ResponseSchemaModel[GetDataScopeWithRelationDetail]: |
| 37 | + data = await data_scope_service.get_rules(pk=pk) |
| 38 | + return response_base.success(data=data) |
| 39 | + |
| 40 | + |
| 41 | +@router.get( |
| 42 | + '', |
| 43 | + summary='分页获取所有数据范围', |
| 44 | + dependencies=[ |
| 45 | + DependsJwtAuth, |
| 46 | + DependsPagination, |
| 47 | + ], |
| 48 | +) |
| 49 | +async def get_pagination_data_scopes( |
| 50 | + db: CurrentSession, |
| 51 | + name: Annotated[str | None, Query(description='范围名称')] = None, |
| 52 | + status: Annotated[int | None, Query(description='状态')] = None, |
| 53 | +) -> ResponseSchemaModel[PageData[GetDataScopeDetail]]: |
| 54 | + data_scope_select = await data_scope_service.get_select(name=name, status=status) |
| 55 | + page_data = await paging_data(db, data_scope_select) |
| 56 | + return response_base.success(data=page_data) |
| 57 | + |
| 58 | + |
| 59 | +@router.post( |
| 60 | + '', |
| 61 | + summary='创建数据范围', |
| 62 | + dependencies=[ |
| 63 | + Depends(RequestPermission('data:scope:add')), |
| 64 | + DependsRBAC, |
| 65 | + ], |
| 66 | +) |
| 67 | +async def create_data_scope(obj: CreateDataScopeParam) -> ResponseModel: |
| 68 | + await data_scope_service.create(obj=obj) |
| 69 | + return response_base.success() |
| 70 | + |
| 71 | + |
| 72 | +@router.put( |
| 73 | + '/{pk}', |
| 74 | + summary='更新数据范围', |
| 75 | + dependencies=[ |
| 76 | + Depends(RequestPermission('data:scope:edit')), |
| 77 | + DependsRBAC, |
| 78 | + ], |
| 79 | +) |
| 80 | +async def update_data_scope( |
| 81 | + pk: Annotated[int, Path(description='数据范围 ID')], obj: UpdateDataScopeParam |
| 82 | +) -> ResponseModel: |
| 83 | + count = await data_scope_service.update(pk=pk, obj=obj) |
| 84 | + if count > 0: |
| 85 | + return response_base.success() |
| 86 | + return response_base.fail() |
| 87 | + |
| 88 | + |
| 89 | +@router.put( |
| 90 | + '/{pk}/rules', |
| 91 | + summary='更新数据范围规则', |
| 92 | + dependencies=[ |
| 93 | + Depends(RequestPermission('data:scope:rule:edit')), |
| 94 | + DependsRBAC, |
| 95 | + ], |
| 96 | +) |
| 97 | +async def update_data_scope_rules( |
| 98 | + pk: Annotated[int, Path(description='数据范围 ID')], rule_ids: UpdateDataScopeRuleParam |
| 99 | +): |
| 100 | + count = await data_scope_service.update_data_scope_rule(pk=pk, rule_ids=rule_ids) |
| 101 | + if count > 0: |
| 102 | + return response_base.success() |
| 103 | + return response_base.fail() |
| 104 | + |
| 105 | + |
| 106 | +@router.delete( |
| 107 | + '', |
| 108 | + summary='批量删除数据范围', |
| 109 | + dependencies=[ |
| 110 | + Depends(RequestPermission('data:scope:del')), |
| 111 | + DependsRBAC, |
| 112 | + ], |
| 113 | +) |
| 114 | +async def delete_data_scope(pk: Annotated[list[int], Query(description='数据范围 ID 列表')]) -> ResponseModel: |
| 115 | + count = await data_scope_service.delete(pk=pk) |
| 116 | + if count > 0: |
| 117 | + return response_base.success() |
| 118 | + return response_base.fail() |
0 commit comments