using Quartz.Spi;
using SqlSugar;
using SqlSugar.IOC;
using ZR.Model.System;
using ZR.Tasks;
namespace ZR.Admin.WebApi.Extensions
{
///
/// 定时任务扩展方法
///
public static class TasksExtension
{
///
/// 注册任务
///
///
///
public static void AddTaskSchedulers(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//添加Quartz服务
services.AddSingleton();
services.AddTransient();
}
///
/// 程序启动后添加任务计划
///
///
///
public static IApplicationBuilder UseAddTaskSchedulers(this IApplicationBuilder app)
{
ITaskSchedulerServer _schedulerServer = app.ApplicationServices.GetRequiredService();
var tasks = DbScoped.SugarScope.Queryable()
.Where(m => m.IsStart == 1).ToListAsync();
//程序启动后注册所有定时任务
foreach (var task in tasks.Result)
{
var result = _schedulerServer.AddTaskScheduleAsync(task);
if (result.Result.IsSuccess())
{
Console.WriteLine($"注册任务[{task.Name}]ID:{task.ID}成功");
}
}
return app;
}
///
/// 初始化字典
///
///
///
public static IApplicationBuilder UseInit(this IApplicationBuilder app)
{
var mainDb = App.Configuration["mainDb"];
//var tenantId = App.GetCurrentTenantId();
//Console.WriteLine("初始化字典数据...");
var db = DbScoped.SugarScope.GetConnection(mainDb);
var types = db.Queryable()
.Where(it => it.Status == "0")
.Select(it => it.DictType)
.ToList();
//上面有耗时操作写在Any上面,保证程序启动后只执行一次
if (!db.ConfigQuery.Any())
{
foreach (var type in types)
{
db.ConfigQuery.SetTable(it => SqlFunc.ToString(it.DictValue), it => it.DictLabel, type, it => it.DictType == type);
}
}
return app;
}
}
}