You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					127 lines
				
				3.9 KiB
			
		
		
			
		
	
	
					127 lines
				
				3.9 KiB
			| 
											1 year ago
										 | from sqlalchemy import delete, select, update | ||
| 
											1 year ago
										 | from sqlalchemy.ext.asyncio import AsyncSession | ||
| 
											2 years ago
										 | from module_admin.entity.do.job_do import SysJob | ||
| 
											1 year ago
										 | from module_admin.entity.vo.job_vo import JobModel, JobPageQueryModel | ||
| 
											2 years ago
										 | from utils.page_util import PageUtil | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class JobDao: | ||
|  |     """
 | ||
|  |     定时任务管理模块数据库操作层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_job_detail_by_id(cls, db: AsyncSession, job_id: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         根据定时任务id获取定时任务详细信息 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param job_id: 定时任务id | ||
|  |         :return: 定时任务信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         job_info = (await db.execute(select(SysJob).where(SysJob.job_id == job_id))).scalars().first() | ||
| 
											2 years ago
										 | 
 | ||
|  |         return job_info | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_job_detail_by_info(cls, db: AsyncSession, job: JobModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         根据定时任务参数获取定时任务信息 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param job: 定时任务参数对象 | ||
|  |         :return: 定时任务信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         job_info = ( | ||
|  |             ( | ||
|  |                 await db.execute( | ||
|  |                     select(SysJob).where( | ||
| 
											1 year ago
										 |                         SysJob.job_name == job.job_name, | ||
|  |                         SysJob.job_group == job.job_group, | ||
|  |                         SysJob.job_executor == job.job_executor, | ||
|  |                         SysJob.invoke_target == job.invoke_target, | ||
|  |                         SysJob.job_args == job.job_args, | ||
|  |                         SysJob.job_kwargs == job.job_kwargs, | ||
|  |                         SysJob.cron_expression == job.cron_expression, | ||
| 
											1 year ago
										 |                     ) | ||
|  |                 ) | ||
|  |             ) | ||
|  |             .scalars() | ||
|  |             .first() | ||
|  |         ) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return job_info | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_job_list(cls, db: AsyncSession, query_object: JobPageQueryModel, is_page: bool = False): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         根据查询参数获取定时任务列表信息 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param query_object: 查询参数对象 | ||
| 
											2 years ago
										 |         :param is_page: 是否开启分页 | ||
| 
											2 years ago
										 |         :return: 定时任务列表信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         query = ( | ||
|  |             select(SysJob) | ||
|  |             .where( | ||
|  |                 SysJob.job_name.like(f'%{query_object.job_name}%') if query_object.job_name else True, | ||
|  |                 SysJob.job_group == query_object.job_group if query_object.job_group else True, | ||
|  |                 SysJob.status == query_object.status if query_object.status else True, | ||
|  |             ) | ||
| 
											1 year ago
										 |             .order_by(SysJob.job_id) | ||
| 
											2 years ago
										 |             .distinct() | ||
| 
											1 year ago
										 |         ) | ||
| 
											1 year ago
										 |         job_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return job_list | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_job_list_for_scheduler(cls, db: AsyncSession): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取定时任务列表信息 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :return: 定时任务列表信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         job_list = (await db.execute(select(SysJob).where(SysJob.status == '0').distinct())).scalars().all() | ||
| 
											2 years ago
										 | 
 | ||
|  |         return job_list | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def add_job_dao(cls, db: AsyncSession, job: JobModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         新增定时任务数据库操作 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param job: 定时任务对象 | ||
|  |         :return: | ||
|  |         """
 | ||
|  |         db_job = SysJob(**job.model_dump()) | ||
|  |         db.add(db_job) | ||
| 
											1 year ago
										 |         await db.flush() | ||
| 
											2 years ago
										 | 
 | ||
|  |         return db_job | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def edit_job_dao(cls, db: AsyncSession, job: dict): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑定时任务数据库操作 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param job: 需要更新的定时任务字典 | ||
|  |         :return: | ||
|  |         """
 | ||
| 
											1 year ago
										 |         await db.execute(update(SysJob), [job]) | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def delete_job_dao(cls, db: AsyncSession, job: JobModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         删除定时任务数据库操作 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param db: orm对象 | ||
|  |         :param job: 定时任务对象 | ||
|  |         :return: | ||
|  |         """
 | ||
| 
											1 year ago
										 |         await db.execute(delete(SysJob).where(SysJob.job_id.in_([job.job_id]))) |