using DS.Module.Core;
using DS.Module.Core.Data;
using DS.WMS.Core.Op.Dtos.TaskInteraction;
using DS.WMS.Core.Op.Entity.TaskInteraction;
using DS.WMS.Core.Op.Interface.TaskInteraction;
using DS.WMS.Core.TaskPlat;
using Masuit.Tools;
using SqlSugar;
using Fasterflect;
using Microsoft.Extensions.DependencyInjection;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
namespace DS.WMS.Core.Op.Method.TaskInteraction
{
///
/// 动作执行管理
///
public class ActionManagerService : ServiceBase, IActionManagerService
{
///
/// 初始化
///
public ActionManagerService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 获取所需业务数据
///
/// 业务ID
/// 业务类型
///
public dynamic? GetBusinessData(long businessId, BusinessType businessType)
{
object? biz = null;
switch (businessType)
{
case BusinessType.OceanShippingExport:
var service1 = ServiceProvider.GetRequiredService();
biz = service1.GetSeaExportInfo(businessId.ToString())?.Data;
break;
case BusinessType.OceanShippingImport:
break;
}
return biz;
}
///
/// 触发任务执行动作
///
/// 任务信息
///
public async Task TriggerAction(BusinessTask businessTask)
{
ArgumentNullException.ThrowIfNull(businessTask, nameof(businessTask));
//目前只限制任务完成才触发
if (businessTask.TaskStatus != TaskStatusEnum.Complete)
return;
var biz = GetBusinessData(businessTask.BusinessId, businessTask.BusinessType);
// 执行自动化操作
TaskFlowDataContext dataContext = new(
(TaskFlowDataNameConst.BusinessTask, businessTask),
(TaskFlowDataNameConst.Business, biz)
);
TaskFlowRuner taskFlow = new(TenantDb, ServiceProvider);
await taskFlow.RunWithBsno(businessTask.TaskType, businessTask.BusinessId, dataContext);
}
public async Task ExecuteAsync(TaskFlowDataContext dataContext)
{
ArgumentNullException.ThrowIfNull(dataContext, nameof(dataContext));
var context = new ActionExecutionContext
{
TaskInfo = dataContext.GetOrDefault(TaskFlowDataNameConst.BusinessTask),
ServiceProvider = ServiceProvider
};
foreach (var key in dataContext.Keys)
context.AdditionalData[key] = dataContext[key];
string selectorType = dataContext.GetOrDefault("SelectorType");
if (selectorType.IsNullOrEmpty())
return;
Type? t = Type.GetType(selectorType, true, false);
if (t == null)
{
await new ApplicationException($"未能获取类型【{selectorType}】的信息").LogAsync(Db);
return;
}
IActionSelector? selector = ConstructorExtensions.CreateInstance(t) as IActionSelector;
if (selector == null)
{
await new ApplicationException($"未能创建执行选择器【{t.AssemblyQualifiedName}】的实例").LogAsync(Db);
return;
}
var executor = await selector.GetActionExecutor(context);
if (executor != null)
{
executor.ExecuteAsync(context);
var logService = context.ServiceProvider.GetRequiredService();
await logService.WriteLogAsync(context.TaskInfo, $"开始运行后台任务({executor.GetType().FullName})...");
}
}
public async Task TriggerTest(TaskBaseTypeEnum taskType, long? id)
{
var task = await TenantDb.Queryable()
.Where(t => t.TaskType == taskType
//&& SqlFunc.Subqueryable().Where(s => t.BusinessId == s.Id && s.BillSubmitStatus == AuditStatusEnum.Approve).Any()
)
//.Where(x => x.TaskStatus == TaskStatusEnum.Complete)
.WhereIF(id.HasValue, x => x.BusinessId == id)
.OrderByDescending(t => SqlFunc.GetRandom()).Take(1).FirstAsync();
if (task != null)
{
TaskFlowDataContext dataContext = new(
(TaskFlowDataNameConst.BusinessTask, task)
);
TaskFlowRuner taskFlow = new(TenantDb, ServiceProvider);
await taskFlow.RunWithBsno(task.TaskType, task.BusinessId, dataContext);
return DataResult.Success;
}
var result = DataResult.Failed("找不到指定类型的任务");
result.Data = 404;
return result;
}
}
///
/// 任务执行类型
///
public enum TaskActionType
{
///
/// 后台任务
///
Background,
///
/// 邮件
///
Email,
///
/// EDI
///
EDI,
}
}