From c3b7aa9b0076470b5bc48e80ad6f71e95b0c0684 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Thu, 1 Dec 2022 17:01:41 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=96=B0=E5=A2=9E=E6=89=A7?=
=?UTF-8?q?=E8=A1=8CSQL=E8=84=9A=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/System/TasksController.cs | 19 ++-
ZR.Model/System/Dto/TasksDto.cs | 126 ++----------------
ZR.Model/System/SysTasks.cs | 7 +-
ZR.Service/System/SysTasksQzService.cs | 1 +
ZR.Tasks/TaskScheduler/Job_SqlExecute.cs | 45 +++++++
document/admin-mysql.sql | 1 +
document/admin-sqlserver.sql | 5 +-
7 files changed, 86 insertions(+), 118 deletions(-)
create mode 100644 ZR.Tasks/TaskScheduler/Job_SqlExecute.cs
diff --git a/ZR.Admin.WebApi/Controllers/System/TasksController.cs b/ZR.Admin.WebApi/Controllers/System/TasksController.cs
index 5bb2319..83a9e01 100644
--- a/ZR.Admin.WebApi/Controllers/System/TasksController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/TasksController.cs
@@ -42,7 +42,7 @@ namespace ZR.Admin.WebApi.Controllers
///
[HttpGet("list")]
[ActionPermissionFilter(Permission = "monitor:job:list")]
- public IActionResult Query([FromQuery] TasksQueryDto parm, [FromQuery] PagerInfo pager)
+ public IActionResult ListTask([FromQuery] TasksQueryDto parm, [FromQuery] PagerInfo pager)
{
//开始拼装查询条件
var predicate = Expressionable.Create();
@@ -51,6 +51,7 @@ namespace ZR.Admin.WebApi.Controllers
m => m.Name.Contains(parm.QueryText) ||
m.JobGroup.Contains(parm.QueryText) ||
m.AssemblyName.Contains(parm.QueryText));
+ predicate.AndIF(parm.TaskType != null, m => m.TaskType == parm.TaskType);
var response = _tasksQzService.GetPages(predicate.ToExpression(), pager, f => f.IsStart, OrderByType.Desc);
@@ -94,6 +95,15 @@ namespace ZR.Admin.WebApi.Controllers
{
throw new CustomException($"地址不能为空");
}
+ if (string.IsNullOrEmpty(parm.SqlText) && parm.TaskType == 3)
+ {
+ throw new CustomException($"sql语句不能为空");
+ }
+ if (parm.SqlText.IfNotEmpty() && parm.TaskType == 3)
+ {
+ parm.AssemblyName = "ZR.Tasks";
+ parm.ClassName = "TaskScheduler.Job_SqlExecute";
+ }
if (parm.TaskType == 1 && (parm.AssemblyName.IsEmpty() || parm.ClassName.IsEmpty()))
{
throw new CustomException($"程序集或者类名不能为空");
@@ -113,7 +123,7 @@ namespace ZR.Admin.WebApi.Controllers
[HttpPost("update")]
[ActionPermissionFilter(Permission = "monitor:job:edit")]
[Log(Title = "修改任务", BusinessType = BusinessType.UPDATE)]
- public async Task Update([FromBody] TasksUpdateDto parm)
+ public async Task Update([FromBody] TasksCreateDto parm)
{
//判断是否已经存在
if (_tasksQzService.Any(m => m.Name == parm.Name && m.ID != parm.ID))
@@ -138,6 +148,11 @@ namespace ZR.Admin.WebApi.Controllers
parm.AssemblyName = "ZR.Tasks";
parm.ClassName = "TaskScheduler.Job_HttpRequest";
}
+ if (parm.SqlText.IfNotEmpty() && parm.TaskType == 3)
+ {
+ parm.AssemblyName = "ZR.Tasks";
+ parm.ClassName = "TaskScheduler.Job_SqlExecute";
+ }
if (tasksQz.IsStart)
{
throw new CustomException($"该任务正在运行中,请先停止在更新");
diff --git a/ZR.Model/System/Dto/TasksDto.cs b/ZR.Model/System/Dto/TasksDto.cs
index bffb547..65a0871 100644
--- a/ZR.Model/System/Dto/TasksDto.cs
+++ b/ZR.Model/System/Dto/TasksDto.cs
@@ -14,6 +14,8 @@ namespace ZR.Model.System.Dto
///
[Display(Name = "查询字符串")]
public string QueryText { get; set; }
+ public int? TaskType { get; set; }
+ public int? TriggerType { get; set; }
}
///
@@ -21,6 +23,15 @@ namespace ZR.Model.System.Dto
///
public class TasksCreateDto
{
+ ///
+ /// 描述 : 任务id
+ /// 空值 : False
+ /// 默认 :
+ ///
+ [Display(Name = "任务id")]
+ //[Required(ErrorMessage = "任务不能为空")]
+ public string ID { get; set; }
+
///
/// 描述 : 任务名称
/// 空值 : False
@@ -114,120 +125,9 @@ namespace ZR.Model.System.Dto
public string JobParams { get; set; }
public string ApiUrl { get; set; }
///
- /// 1、程序集任务 2、apiUrl任务
- ///
- public int TaskType { get; set; }
- }
-
- ///
- /// 更新任务
- ///
- public class TasksUpdateDto
- {
- ///
- /// 描述 : 任务id
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "任务id")]
- [Required(ErrorMessage = "任务不能为空")]
- public string ID { get; set; }
-
- ///
- /// 描述 : 任务名称
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "任务名称")]
- [Required(ErrorMessage = "任务名称不能为空")]
- public string Name { get; set; }
-
- ///
- /// 描述 : 任务分组
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "任务分组")]
- [Required(ErrorMessage = "任务分组不能为空")]
- public string JobGroup { get; set; }
-
- ///
- /// 描述 : 运行时间表达式
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "运行时间表达式")]
- public string Cron { get; set; }
-
- ///
- /// 描述 : 程序集名称
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "程序集名称")]
- [Required(ErrorMessage = "程序集名称不能为空")]
- public string AssemblyName { get; set; }
-
- ///
- /// 描述 : 任务所在类
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "任务所在类")]
- [Required(ErrorMessage = "任务所在类不能为空")]
- public string ClassName { get; set; }
-
- ///
- /// 描述 : 任务描述
- /// 空值 : True
- /// 默认 :
- ///
- [Display(Name = "任务描述")]
- public string Remark { get; set; }
-
- ///
- /// 描述 : 开始时间
- /// 空值 : True
- /// 默认 :
- ///
- [Display(Name = "开始时间")]
- public DateTime? BeginTime { get; set; }
-
- ///
- /// 描述 : 结束时间
- /// 空值 : True
- /// 默认 :
- ///
- [Display(Name = "结束时间")]
- public DateTime? EndTime { get; set; }
-
- ///
- /// 描述 : 触发器类型(0、simple 1、cron)
- /// 空值 : False
- /// 默认 :
- ///
- [Display(Name = "触发器类型(0、simple 1、cron)")]
- public int TriggerType { get; set; }
-
- ///
- /// 描述 : 执行间隔时间(单位:秒)
- /// 空值 : False
- /// 默认 : 0
- ///
- [Display(Name = "执行间隔时间(单位:秒)")]
- public int IntervalSecond { get; set; }
-
- ///
- /// 描述 : 传入参数
- /// 空值 : True
- /// 默认 :
- ///
- [Display(Name = "传入参数")]
- public string JobParams { get; set; }
- public string ApiUrl { get; set; }
- ///
- /// 1、程序集任务 2、apiUrl任务
+ /// 1、程序集任务 2、apiUrl任务 3、SQL语句
///
public int TaskType { get; set; }
+ public string SqlText { get; set; }
}
}
diff --git a/ZR.Model/System/SysTasks.cs b/ZR.Model/System/SysTasks.cs
index 971ae41..263307f 100644
--- a/ZR.Model/System/SysTasks.cs
+++ b/ZR.Model/System/SysTasks.cs
@@ -161,8 +161,13 @@ namespace ZR.Model.System
///
public string ApiUrl { get; set; }
///
- /// 任务类型 1程序集2网络请求
+ /// 任务类型 1、程序集 2、网络请求 3、SQL语句
///
public int TaskType { get; set; }
+
+ ///
+ /// SQL语句
+ ///
+ public string SqlText { get; set; }
}
}
diff --git a/ZR.Service/System/SysTasksQzService.cs b/ZR.Service/System/SysTasksQzService.cs
index 8c4e1c6..69c01a7 100644
--- a/ZR.Service/System/SysTasksQzService.cs
+++ b/ZR.Service/System/SysTasksQzService.cs
@@ -53,6 +53,7 @@ namespace ZR.Service.System
EndTime = parm.EndTime,
TaskType = parm.TaskType,
ApiUrl = parm.ApiUrl,
+ SqlText = parm.SqlText,
});
}
}
diff --git a/ZR.Tasks/TaskScheduler/Job_SqlExecute.cs b/ZR.Tasks/TaskScheduler/Job_SqlExecute.cs
new file mode 100644
index 0000000..4296aef
--- /dev/null
+++ b/ZR.Tasks/TaskScheduler/Job_SqlExecute.cs
@@ -0,0 +1,45 @@
+using Infrastructure;
+using Infrastructure.Attribute;
+using Infrastructure.Extensions;
+using Quartz;
+using Quartz.Impl;
+using Quartz.Impl.Triggers;
+using SqlSugar.IOC;
+using System;
+using System.Threading.Tasks;
+using ZR.Service.System.IService;
+
+namespace ZR.Tasks.TaskScheduler
+{
+ [AppService(ServiceType = typeof(Job_SqlExecute), ServiceLifetime = LifeTime.Scoped)]
+ public class Job_SqlExecute : JobBase, IJob
+ {
+ private readonly ISysTasksQzService tasksQzService;
+ private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
+
+ public Job_SqlExecute(ISysTasksQzService tasksQzService)
+ {
+ this.tasksQzService = tasksQzService;
+ }
+ public async Task Execute(IJobExecutionContext context)
+ {
+ await ExecuteJob(context, async () => await Run(context));
+ }
+ public async Task Run(IJobExecutionContext context)
+ {
+ AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
+
+ var info = await tasksQzService.GetByIdAsync(trigger.JobName);
+
+ if (info != null && info.SqlText.IsNotEmpty())
+ {
+ var result = DbScoped.SugarScope.Ado.ExecuteCommandWithGo(info.SqlText);
+ logger.Info($"任务【{info.Name}】sql请求执行结果=" + result);
+ }
+ else
+ {
+ throw new CustomException($"任务{trigger?.JobName}执行失败,任务不存在");
+ }
+ }
+ }
+}
diff --git a/document/admin-mysql.sql b/document/admin-mysql.sql
index 2cfb08e..552ff20 100644
--- a/document/admin-mysql.sql
+++ b/document/admin-mysql.sql
@@ -28,6 +28,7 @@ CREATE TABLE `sys_tasks` (
`lastRunTime` datetime(0) NULL DEFAULT NULL COMMENT '最后执行时间',
`apiUrl` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'api执行地址',
`taskType` int(4) NULL DEFAULT 1 COMMENT '任务类型1程序集任务 2网络请求',
+ `sqlText` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'SQL语句',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '计划任务' ROW_FORMAT = Dynamic;
diff --git a/document/admin-sqlserver.sql b/document/admin-sqlserver.sql
index 9ca30e0..f3077f4 100644
--- a/document/admin-sqlserver.sql
+++ b/document/admin-sqlserver.sql
@@ -26,10 +26,11 @@ CREATE TABLE sys_tasks
update_by varchar(50) NULL DEFAULT NULL , --'更新人编码',
lastRunTime datetime , --最后执行时间
taskType int null , --任务类型 1程序集 2网络请求
- apiUrl varchar(200) --网络请求地址
+ apiUrl varchar(200), --网络请求地址
+ sqlText VARCHAR(1000) --sql语句
)
GO
-INSERT INTO sys_tasks VALUES ('1410905433996136448', '测试任务', 'SYSTEM', '0 0/10 * * * ? ', 'ZR.Tasks', 'TaskScheduler.Job_SyncTest', NULL, 0, '2021-07-02 18:17:31', '9999-12-31 00:00:00', 1, 1, 1, NULL, '2021-07-02 18:17:23', '2021-07-02 18:17:31', 'admin', NULL, NULL, 1, '');
+INSERT INTO sys_tasks VALUES ('1410905433996136448', '测试任务', 'SYSTEM', '0 0/10 * * * ? ', 'ZR.Tasks', 'TaskScheduler.Job_SyncTest', NULL, 0, '2021-07-02 18:17:31', '9999-12-31 00:00:00', 1, 1, 1, NULL, '2021-07-02 18:17:23', '2021-07-02 18:17:31', 'admin', NULL, NULL, 1, '', '');
GO
if OBJECT_ID(N'sys_tasks_log',N'U') is not NULL DROP TABLE sys_tasks_log
GO