-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a31e304
commit 8a2d7bc
Showing
4 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using Aix.ScheduleTask.Model; | ||
using Aix.ScheduleTask.Repository; | ||
using Aix.ScheduleTask.Utils; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Aix.ScheduleTask | ||
{ | ||
/// <summary> | ||
/// 任务管理接口 | ||
/// </summary> | ||
public interface IScheduleTaskAdminService | ||
{ | ||
/// <summary> | ||
/// 保存任务 有id是修改,否则新增 | ||
/// </summary> | ||
/// <param name="model"></param> | ||
/// <returns></returns> | ||
Task<int> SaveTask(ScheduleTaskDTO model); | ||
|
||
Task DeleteTask(int taskId); | ||
|
||
Task EnableTask(int taskId); | ||
|
||
Task DisableTask(int taskId); | ||
} | ||
public class ScheduleTaskAdminService : IScheduleTaskAdminService | ||
{ | ||
ILogger<ScheduleTaskAdminService> _logger; | ||
private readonly IAixScheduleTaskRepository _aixScheduleTaskRepository; | ||
public ScheduleTaskAdminService(ILogger<ScheduleTaskAdminService> logger, | ||
IAixScheduleTaskRepository aixScheduleTaskRepository | ||
) | ||
{ | ||
_logger = logger; | ||
_aixScheduleTaskRepository = aixScheduleTaskRepository; | ||
} | ||
public async Task<int> SaveTask(ScheduleTaskDTO req) | ||
{ | ||
AssertUtils.IsNotEmpty(req.TaskName, $"{nameof(AixScheduleTaskInfo.TaskName)}为空"); | ||
AssertUtils.IsNotEmpty(req.Cron, $"{nameof(AixScheduleTaskInfo.Cron)}为空"); | ||
AssertUtils.IsNotEmpty(req.TaskContent, $"{nameof(AixScheduleTaskInfo.TaskContent)}为空"); | ||
AssertUtils.IsTrue(req.MaxRetryCount >= 0, $"{nameof(AixScheduleTaskInfo.MaxRetryCount)} 为空"); | ||
|
||
var model = await _aixScheduleTaskRepository.GetById(req.TaskId); | ||
if (model == null) | ||
{ | ||
model = new AixScheduleTaskInfo | ||
{ | ||
TaskGroup = req.TaskGroup ?? "", | ||
TaskStatus = 1, | ||
TaskName = req.TaskName, | ||
TaskDesc = req.TaskDesc ?? "", | ||
Cron = req.Cron, | ||
TaskContent = req.TaskContent ?? "", | ||
LastExecuteTime = 0, | ||
NextExecuteTime = 0, | ||
MaxRetryCount = req.MaxRetryCount, | ||
CreatorId = req.UserId ?? "", | ||
CreateTime = DateTime.Now, | ||
ModifierId = req.UserId ?? "", | ||
ModifyTime = DateTime.Now | ||
}; | ||
|
||
var newId = await _aixScheduleTaskRepository.InsertAsync(model); | ||
model.Id = (int)newId; | ||
} | ||
|
||
else | ||
{ | ||
model = new AixScheduleTaskInfo | ||
{ | ||
Id = req.TaskId, | ||
TaskGroup = req.TaskGroup ?? "", | ||
//TaskStatus = 1, | ||
TaskName = req.TaskName, | ||
TaskDesc = req.TaskDesc ?? "", | ||
Cron = req.Cron, | ||
TaskContent = req.TaskContent ?? "", | ||
// LastExecuteTime = 0, | ||
// NextExecuteTime = 0, | ||
MaxRetryCount = req.MaxRetryCount, | ||
// CreatorId = req.UserId ?? "", | ||
// CreateTime = DateTime.Now, | ||
ModifierId = req.UserId ?? "", | ||
ModifyTime = DateTime.Now | ||
}; | ||
await _aixScheduleTaskRepository.UpdateAsync(model); | ||
} | ||
return model.Id; | ||
} | ||
|
||
public async Task DeleteTask(int taskId) | ||
{ | ||
await _aixScheduleTaskRepository.DeleteByPkAsync(new AixScheduleTaskInfo { Id = taskId }); | ||
} | ||
|
||
public async Task EnableTask(int taskId) | ||
{ | ||
await _aixScheduleTaskRepository.UpdateAsync(new AixScheduleTaskInfo { Id = taskId, TaskStatus = 1 }); | ||
} | ||
|
||
public async Task DisableTask(int taskId) | ||
{ | ||
await _aixScheduleTaskRepository.UpdateAsync(new AixScheduleTaskInfo { Id = taskId, TaskStatus = 0 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Aix.ScheduleTask.Utils | ||
{ | ||
/// <summary> | ||
/// 业务异常 | ||
/// </summary> | ||
public class AixScheduleTaskException : Exception | ||
{ | ||
public int Code { get; set; } | ||
|
||
public AixScheduleTaskException(int code, string message) : base(message) | ||
{ | ||
this.Code = code; | ||
} | ||
} | ||
|
||
internal static class AssertUtils | ||
{ | ||
public static void IsTrue(bool condition, string errorText) | ||
=> IsTrue(condition, 500, errorText); | ||
|
||
public static void IsTrue(bool condition, int code, string errorText) | ||
{ | ||
if (!condition) | ||
{ | ||
throw new AixScheduleTaskException(code, errorText ?? "异常"); | ||
} | ||
} | ||
|
||
public static void IsNotNull(object obj, string errorText) | ||
{ | ||
IsTrue(obj != null, errorText); | ||
} | ||
|
||
public static void IsNotNull(object obj, int code, string errorText) | ||
{ | ||
IsTrue(obj != null, code, errorText); | ||
} | ||
|
||
public static void IsNotEmpty(string obj, string errorText) | ||
{ | ||
IsTrue(!string.IsNullOrEmpty(obj), errorText); | ||
} | ||
public static void IsNotEmpty(string obj, int code, string errorText) | ||
{ | ||
IsTrue(!string.IsNullOrEmpty(obj), code, errorText); | ||
} | ||
|
||
public static void ThrowException(int code, string errorText) | ||
{ | ||
IsTrue(false, errorText); | ||
} | ||
} | ||
} |