from app.api.v1.module_system.auth.schema import AuthSchema from app.core.base_crud import CRUDBase from app.core.exceptions import CustomException from .model import ExpenseInstitutionModel from .schema import InstitutionCreateSchema, InstitutionUpdateSchema class InstitutionCRUD( CRUDBase[ExpenseInstitutionModel, InstitutionCreateSchema, InstitutionUpdateSchema] ): """费控制度 CRUD 操作""" def __init__(self, auth: AuthSchema) -> None: self.auth = auth super().__init__(model=ExpenseInstitutionModel, auth=auth) async def get_by_institution_id( self, institution_id: str ) -> ExpenseInstitutionModel | None: """根据费控规则ID查询费控制度""" return await self.get(institution_id=institution_id) async def update_by_institution_id( self, institution_id: str, data: dict ) -> ExpenseInstitutionModel | None: """根据费控规则ID更新费控制度""" obj = await self.get(institution_id=institution_id, preload=[]) if not obj: raise CustomException(msg="更新失败!对象不存在") if self.auth.user and hasattr(obj, "updated_id"): setattr(obj, "updated_id", self.auth.user.id) for key, value in data.items(): if hasattr(obj, key): setattr(obj, key, value) await self.auth.db.flush() await self.auth.db.refresh(obj) verify_obj = await self.get(institution_id=institution_id, preload=[]) if not verify_obj: raise CustomException(msg="更新失败!对象不存在或无权限访问") return obj