You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Admin.Core.Common;
using Admin.Core.Common.Resource;
using Admin.Core.IService.ISys;
using Admin.Core.Model;
using Admin.Core.Model.Sys;
using Admin.Core.Repository;
using Admin.Core.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Quartz;
namespace Admin.Core.Api
{
/// <summary>
/// 任务调度
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysJobLogController : BaseApiUserController
{
private readonly ISysJobLogService _sysJobLogService;
private readonly ISysTasksQzService _tasksQzService;
private readonly IMapper _mapper;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sysJobLogService"></param>
/// <param name="tasksQzService"></param>
/// <param name="mapper"></param>
/// <param name="sysUserService"></param>
public SysJobLogController(ISysJobLogService sysJobLogService, ISysTasksQzService tasksQzService, IMapper mapper, ISysUserService sysUserService) : base(sysUserService)
{
_sysJobLogService = sysJobLogService;
_sysUserService = sysUserService;
_mapper = mapper;
_tasksQzService = tasksQzService;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<PageModel<JobLogView>>> GetByPage([FromBody] PageQuery<JobLogView> pageQuery)
{
Expression<Func<SysTasksQz, bool>> whereExpression = x => x.DelFlag == false;
if (pageQuery.Query.TasksQz.ID != 0)
{
whereExpression = whereExpression.And(x => x.ID == pageQuery.Query.TasksQz.ID);
}
if (pageQuery.Query.TasksQz.Name.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.Name.Contains(pageQuery.Query.TasksQz.Name));
}
if (pageQuery.Query.TasksQz.JobGroup.IsNotEmptyOrNull())
{
whereExpression = whereExpression.And(x => x.JobGroup == pageQuery.Query.TasksQz.JobGroup);
}
var taskQzs = await _tasksQzService.QueryAsync(whereExpression);
var taskIds = taskQzs.Select(x => x.ID).ToList();
Expression<Func<SysJobLog, bool>> whereExpressionLog = x => true;
whereExpressionLog = whereExpressionLog.And(x => taskIds.Contains((int)x.JobID));
if (pageQuery.Query.Status.IsNotEmptyOrNull())
{
whereExpressionLog = whereExpressionLog.And(x => x.Status == pageQuery.Query.Status);
}
if (pageQuery.DateRange.IsNotEmptyOrNull() && pageQuery.DateRange.Count > 0)
{
if (pageQuery.DateRange[0].IsNotEmptyOrNull())
{
whereExpressionLog = whereExpressionLog.And(x => x.RunTimeStart >= pageQuery.DateRange[0]);
}
if (pageQuery.DateRange.Count > 1 && pageQuery.DateRange[1].IsNotEmptyOrNull())
{
whereExpressionLog = whereExpressionLog.And(x => x.RunTimeEnd <= pageQuery.DateRange[1]);
}
}
var orderBy = (pageQuery.OrderBy ?? "ID") + " " + (pageQuery.IsAsc ? "asc" : "desc");
var log = await _sysJobLogService.QueryPageAsync(whereExpressionLog, pageQuery.Page, pageQuery.PageSize, orderBy);
var data = _mapper.Map<PageModel<JobLogView>>(log);
data.data.ForEach(x => x.TasksQz = taskQzs.Find(m => m.ID == x.JobID));
return Success(data);
}
/// <summary>
/// 删除日志
/// </summary>
/// <param name="jobId"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(List<int> jobId)
{
return Success(await _sysJobLogService.DeleteByIdAsync(jobId));
}
/// <summary>
/// 清空日志
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<bool>> Clear()
{
return Success(await _sysJobLogService.Clear());
}
}
}