查询接口修改

This commit is contained in:
lhx
2025-09-27 09:35:54 +08:00
parent 0b1e9851dd
commit 2cdf5c97e1
2 changed files with 33 additions and 6 deletions

View File

@@ -28,16 +28,23 @@ def get_accounts(request: AccountListRequest, db: Session = Depends(get_db)):
"""获取账号列表""" """获取账号列表"""
return AccountService.get_accounts(db, skip=request.skip, limit=request.limit) return AccountService.get_accounts(db, skip=request.skip, limit=request.limit)
@router.post("/get", response_model=AccountResponse) @router.post("/get", response_model=List[AccountResponse])
def get_account(request: AccountGetRequest, db: Session = Depends(get_db)): def get_account(request: AccountGetRequest, db: Session = Depends(get_db)):
"""根据ID获取账号""" """根据多种条件查询账号"""
account = AccountService.get_account(db, request.account_id) accounts = AccountService.search_accounts(
if not account: db,
account_id=request.account_id,
account=request.account,
section=request.section,
status=request.status,
today_updated=request.today_updated
)
if not accounts:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
detail="账号不存在" detail="未找到符合条件的账号"
) )
return account return accounts
@router.post("/update", response_model=AccountResponse) @router.post("/update", response_model=AccountResponse)
def update_account(request: AccountUpdateRequest, db: Session = Depends(get_db)): def update_account(request: AccountUpdateRequest, db: Session = Depends(get_db)):

View File

@@ -13,6 +13,26 @@ class AccountService:
db.refresh(db_account) db.refresh(db_account)
return db_account return db_account
@staticmethod
def search_accounts(db: Session, account_id: Optional[int] = None,
account: Optional[str] = None, section: Optional[str] = None,
status: Optional[int] = None, today_updated: Optional[int] = None) -> List[Account]:
"""根据多种条件搜索账号"""
query = db.query(Account)
if account_id is not None:
query = query.filter(Account.id == account_id)
if account is not None:
query = query.filter(Account.account.like(f"%{account}%"))
if section is not None:
query = query.filter(Account.section.like(f"%{section}%"))
if status is not None:
query = query.filter(Account.status == status)
if today_updated is not None:
query = query.filter(Account.today_updated == today_updated)
return query.all()
@staticmethod @staticmethod
def get_account(db: Session, account_id: int) -> Optional[Account]: def get_account(db: Session, account_id: int) -> Optional[Account]:
"""根据ID获取账号""" """根据ID获取账号"""