debt(D4): bzzoiro Team/League/Match 查找经 Repository 层,纠正空壳表述
- repositories.py 新增 MatchRepository.find_by_league_and_date_range / find_finished_with_stats(含 stats 预加载),TeamRepository.get_or_create 支持 name_zh - bzzoiro events/standings/stats 三条管线的联赛、球队、比赛查找与批量预载 全部改走 Repository;事务提交仍由调用方 UnitOfWork 控制 - Standing/RawEvent/Lineage 管线内私有读写保留在本模块(不强行 Repository 化) - 模块 docstring 纠正为准确表述,移除「已全面 Repository 化」的误导性声明
This commit is contained in:
+31
-3
@@ -64,6 +64,34 @@ class MatchRepository:
|
||||
)
|
||||
return (await self._session.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
async def find_by_league_and_date_range(
|
||||
self, league_id: int, start, end
|
||||
) -> list[Match]:
|
||||
"""批量预加载某联赛日期范围内的比赛(ingest 管线内存去重用)。"""
|
||||
stmt = (
|
||||
select(Match)
|
||||
.where(Match.league_id == league_id)
|
||||
.where(Match.match_date >= start)
|
||||
.where(Match.match_date <= end)
|
||||
)
|
||||
return (await self._session.execute(stmt)).scalars().all()
|
||||
|
||||
async def find_finished_with_stats(self, league_ids: list[int], *, limit: int) -> list[Match]:
|
||||
"""已完赛且有上游 event id 的比赛(按日期倒序),供统计回填逐场拉取。
|
||||
|
||||
预加载 stats:调用方需读取 existing.stats 判断是否跳过。
|
||||
"""
|
||||
stmt = (
|
||||
select(Match)
|
||||
.options(selectinload(Match.stats))
|
||||
.where(Match.match_status == "finished")
|
||||
.where(Match.source_event_id.is_not(None))
|
||||
.where(Match.league_id.in_(league_ids))
|
||||
.order_by(Match.match_date.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
return (await self._session.execute(stmt)).scalars().all()
|
||||
|
||||
async def add(self, match: Match) -> None:
|
||||
self._session.add(match)
|
||||
await self._session.flush()
|
||||
@@ -79,11 +107,11 @@ class TeamRepository:
|
||||
stmt = select(Team).where(Team.name == name)
|
||||
return (await self._session.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
async def get_or_create(self, name: str) -> Team:
|
||||
"""按名获取球队,不存在则创建。"""
|
||||
async def get_or_create(self, name: str, *, name_zh: str | None = None) -> Team:
|
||||
"""按名获取球队,不存在则创建(name_zh 供 bzzoiro 管线写中文名)。"""
|
||||
team = await self.get_by_name(name)
|
||||
if team is None:
|
||||
team = Team(name=name)
|
||||
team = Team(name=name, name_zh=name_zh)
|
||||
self._session.add(team)
|
||||
await self._session.flush()
|
||||
return team
|
||||
|
||||
Reference in New Issue
Block a user