mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
**架构重构** - 拆分为 Service、Repository、Adapter 三层架构,提升模块化 - 统一 APScheduler Job ID 生成方式,优化 ScheduleTargeter 逻辑 **新增功能** - 支持定时任务时区配置 - 新增"运行中"任务状态显示 - 为"所有群组"任务增加随机延迟,分散并发压力 **用户体验优化** - 重构操作反馈消息,提供详细的成功提示卡片 - 优化任务查看命令的筛选逻辑 - 统一删除、暂停、恢复、执行、更新操作的响应格式 Co-authored-by: webjoin111 <455457521@qq.com>
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
"""
|
||
数据持久层 (Repository)
|
||
|
||
封装所有对 ScheduleInfo 模型的数据库操作,将数据访问逻辑与业务逻辑分离。
|
||
"""
|
||
|
||
from typing import Any
|
||
|
||
from tortoise.queryset import QuerySet
|
||
|
||
from zhenxun.models.schedule_info import ScheduleInfo
|
||
|
||
|
||
class ScheduleRepository:
|
||
"""封装 ScheduleInfo 模型的数据库操作"""
|
||
|
||
@staticmethod
|
||
async def get_by_id(schedule_id: int) -> ScheduleInfo | None:
|
||
"""通过ID获取任务"""
|
||
return await ScheduleInfo.get_or_none(id=schedule_id)
|
||
|
||
@staticmethod
|
||
async def get_all_enabled() -> list[ScheduleInfo]:
|
||
"""获取所有启用的任务"""
|
||
return await ScheduleInfo.filter(is_enabled=True).all()
|
||
|
||
@staticmethod
|
||
async def get_all(plugin_name: str | None = None) -> list[ScheduleInfo]:
|
||
"""获取所有任务,可按插件名过滤"""
|
||
if plugin_name:
|
||
return await ScheduleInfo.filter(plugin_name=plugin_name).all()
|
||
return await ScheduleInfo.all()
|
||
|
||
@staticmethod
|
||
async def save(schedule: ScheduleInfo, update_fields: list[str] | None = None):
|
||
"""保存任务"""
|
||
await schedule.save(update_fields=update_fields)
|
||
|
||
@staticmethod
|
||
async def exists(**kwargs: Any) -> bool:
|
||
"""检查任务是否存在"""
|
||
return await ScheduleInfo.exists(**kwargs)
|
||
|
||
@staticmethod
|
||
async def get_by_plugin_and_group(
|
||
plugin_name: str, group_ids: list[str]
|
||
) -> list[ScheduleInfo]:
|
||
"""根据插件和群组ID列表获取任务"""
|
||
return await ScheduleInfo.filter(
|
||
plugin_name=plugin_name, group_id__in=group_ids
|
||
).all()
|
||
|
||
@staticmethod
|
||
async def update_or_create(
|
||
defaults: dict, **kwargs: Any
|
||
) -> tuple[ScheduleInfo, bool]:
|
||
"""更新或创建任务"""
|
||
return await ScheduleInfo.update_or_create(defaults=defaults, **kwargs)
|
||
|
||
@staticmethod
|
||
async def query_schedules(**filters: Any) -> list[ScheduleInfo]:
|
||
"""
|
||
根据任意条件查询任务列表
|
||
|
||
参数:
|
||
**filters: 过滤条件,如 group_id="123", plugin_name="abc"
|
||
|
||
返回:
|
||
list[ScheduleInfo]: 任务列表
|
||
"""
|
||
cleaned_filters = {k: v for k, v in filters.items() if v is not None}
|
||
if not cleaned_filters:
|
||
return await ScheduleInfo.all()
|
||
return await ScheduleInfo.filter(**cleaned_filters).all()
|
||
|
||
@staticmethod
|
||
def filter(**kwargs: Any) -> QuerySet[ScheduleInfo]:
|
||
"""提供一个通用的过滤查询接口,供Targeter使用"""
|
||
return ScheduleInfo.filter(**kwargs)
|