"""advisor_todo 投顾待办仓储:按唯一键幂等建待办 + 列表查询。""" from __future__ import annotations from sqlalchemy import func, select from model.advisor_todo import AdvisorTodo from repositories.base import BaseRepository class AdvisorTodoRepo(BaseRepository): model = AdvisorTodo async def get_by_unique( self, todo_type: str, customer_id: int | None, biz_id: str | None ) -> AdvisorTodo | None: """按 DDL 唯一键 uk_todo(todo_type, customer_id, biz_id) 查重(幂等建待办)。 注:customer_id/biz_id 为 None 时 SQLAlchemy 生成 IS NULL 条件,语义与 DDL 一致。 """ return await self.db.scalar( select(AdvisorTodo).where( AdvisorTodo.todo_type == todo_type, AdvisorTodo.customer_id == customer_id, AdvisorTodo.biz_id == biz_id, ) ) async def list_by_advisor( self, *, advisor_id: int, status: str | None = None, todo_type: str | None = None, limit: int = 100, offset: int = 0, ) -> list[AdvisorTodo]: conds = [AdvisorTodo.advisor_id == advisor_id] if status: conds.append(AdvisorTodo.status == status) if todo_type: conds.append(AdvisorTodo.todo_type == todo_type) stmt = ( select(AdvisorTodo) .where(*conds) .order_by(AdvisorTodo.id.desc()) .limit(limit) .offset(offset) ) return list((await self.db.scalars(stmt)).all()) async def count_by_advisor( self, *, advisor_id: int, status: str | None = None, todo_type: str | None = None, ) -> int: conds = [AdvisorTodo.advisor_id == advisor_id] if status: conds.append(AdvisorTodo.status == status) if todo_type: conds.append(AdvisorTodo.todo_type == todo_type) stmt = select(func.count()).select_from(AdvisorTodo).where(*conds) return (await self.db.scalar(stmt)) or 0