From a8a70cac08e0fc0f6c135faf860228684b9938df Mon Sep 17 00:00:00 2001 From: Attente <19653207+wikrin@users.noreply.github.com> Date: Sat, 19 Apr 2025 20:22:37 +0800 Subject: [PATCH] refactor(db): optimize download history query logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用`TransferHistory.list_by`相同逻辑 --- app/db/downloadhistory_oper.py | 1 + app/db/models/downloadhistory.py | 65 ++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/app/db/downloadhistory_oper.py b/app/db/downloadhistory_oper.py index a67fbcaa..905c0bb5 100644 --- a/app/db/downloadhistory_oper.py +++ b/app/db/downloadhistory_oper.py @@ -113,6 +113,7 @@ class DownloadHistoryOper(DbOper): season: Optional[str] = None, episode: Optional[str] = None, tmdbid=None) -> List[DownloadHistory]: """ 按类型、标题、年份、季集查询下载记录 + tmdbid + mtype 或 title + year """ return DownloadHistory.get_last_by(db=self._db, mtype=mtype, diff --git a/app/db/models/downloadhistory.py b/app/db/models/downloadhistory.py index 5ddd3145..400a2eed 100644 --- a/app/db/models/downloadhistory.py +++ b/app/db/models/downloadhistory.py @@ -85,45 +85,54 @@ class DownloadHistory(Base): year: Optional[str] = None, season: Optional[str] = None, episode: Optional[str] = None, tmdbid: Optional[int] = None): """ - 据tmdbid、season、season_episode查询转移记录 + 据tmdbid、season、season_episode查询下载记录 + tmdbid + mtype 或 title + year """ result = None - if tmdbid and not season and not episode: - result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid).order_by( + # TMDBID + 类型 + if tmdbid and mtype: + # 电视剧某季某集 + if season and episode: + result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, + DownloadHistory.type == mtype, + DownloadHistory.seasons == season, + DownloadHistory.episodes == episode).order_by( DownloadHistory.id.desc()).all() - if tmdbid and season and not episode: - result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, - DownloadHistory.seasons == season).order_by( + # 电视剧某季 + elif season: + result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, + DownloadHistory.type == mtype, + DownloadHistory.seasons == season).order_by( DownloadHistory.id.desc()).all() - if tmdbid and season and episode: - result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, - DownloadHistory.seasons == season, - DownloadHistory.episodes == episode).order_by( + else: + # 电视剧所有季集/电影 + result = db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, + DownloadHistory.type == mtype).order_by( DownloadHistory.id.desc()).all() - # 电视剧所有季集|电影 - if not season and not episode: - result = db.query(DownloadHistory).filter(DownloadHistory.type == mtype, - DownloadHistory.title == title, - DownloadHistory.year == year).order_by( + # 标题 + 年份 + elif title and year: + # 电视剧某季某集 + if season and episode: + result = db.query(DownloadHistory).filter(DownloadHistory.title == title, + DownloadHistory.year == year, + DownloadHistory.seasons == season, + DownloadHistory.episodes == episode).order_by( DownloadHistory.id.desc()).all() - # 电视剧某季 - if season and not episode: - result = db.query(DownloadHistory).filter(DownloadHistory.type == mtype, - DownloadHistory.title == title, - DownloadHistory.year == year, - DownloadHistory.seasons == season).order_by( + # 电视剧某季 + elif season: + result = db.query(DownloadHistory).filter(DownloadHistory.title == title, + DownloadHistory.year == year, + DownloadHistory.seasons == season).order_by( DownloadHistory.id.desc()).all() - # 电视剧某季某集 - if season and episode: - result = db.query(DownloadHistory).filter(DownloadHistory.type == mtype, - DownloadHistory.title == title, - DownloadHistory.year == year, - DownloadHistory.seasons == season, - DownloadHistory.episodes == episode).order_by( + else: + # 电视剧所有季集/电影 + result = db.query(DownloadHistory).filter(DownloadHistory.title == title, + DownloadHistory.year == year).order_by( DownloadHistory.id.desc()).all() if result: return list(result) + return [] @staticmethod @db_query