|
|
|
@ -8,11 +8,11 @@ using DS.WMS.Core.Op.Entity;
|
|
|
|
|
using DS.WMS.Core.Op.Entity.TaskInteraction;
|
|
|
|
|
using DS.WMS.Core.Op.Interface.TaskInteraction;
|
|
|
|
|
using DS.WMS.Core.Op.Method.TaskInteraction.ActionSelector;
|
|
|
|
|
using DS.WMS.Core.TaskPlat;
|
|
|
|
|
using DS.WMS.Core.TaskPlat.Entity;
|
|
|
|
|
using Fasterflect;
|
|
|
|
|
using LanguageExt.Pipes;
|
|
|
|
|
using Masuit.Tools;
|
|
|
|
|
using Masuit.Tools.Systems;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
@ -169,7 +169,7 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
/// <param name="includeOrder">是否包含订单数据</param>
|
|
|
|
|
/// <param name="additionalData">附加参数</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task TriggerActionAsync(BusinessTask businessTask, bool includeOrder = false, IDictionary<string, object>? additionalData = null)
|
|
|
|
|
public async Task<DataResult> TriggerActionAsync(BusinessTask businessTask, bool includeOrder = false, IDictionary<string, object>? additionalData = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(businessTask, nameof(businessTask));
|
|
|
|
|
|
|
|
|
@ -177,10 +177,25 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
(TaskFlowDataNameConst.BusinessTask, businessTask)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (includeOrder)
|
|
|
|
|
var paramList = await GetTaskParamsAsync(businessTask.TaskType);
|
|
|
|
|
var gpList = paramList.GroupBy(x => x.FieldName).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
var biz = GetBusinessDataAsync(businessTask.BusinessId, businessTask.BusinessType);
|
|
|
|
|
dataContext[TaskFlowDataNameConst.Business] = biz;
|
|
|
|
|
FieldName = x.Key,
|
|
|
|
|
Values = x.Select(y => y.FieldValue)
|
|
|
|
|
}).ToList();
|
|
|
|
|
foreach (var g in gpList)
|
|
|
|
|
{
|
|
|
|
|
if (g.FieldName == TaskFlowDataNameConst.TypeName)
|
|
|
|
|
{
|
|
|
|
|
dataContext[g.FieldName] = g.Values;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (g.Values.Count() == 1)
|
|
|
|
|
dataContext[g.FieldName] = g.Values.FirstOrDefault();
|
|
|
|
|
else
|
|
|
|
|
dataContext[g.FieldName] = g.Values;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (additionalData != null)
|
|
|
|
@ -189,8 +204,34 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
dataContext[item.Key] = item.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TaskFlowRuner taskFlow = new(TenantDb, ServiceProvider);
|
|
|
|
|
await taskFlow.RunWithBsno(businessTask.TaskType, businessTask.BusinessId, dataContext);
|
|
|
|
|
if (!dataContext.TryGetValue(TaskFlowDataNameConst.TypeName, out object value) || value is not IEnumerable<string>)
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
|
|
|
|
|
if (includeOrder)
|
|
|
|
|
{
|
|
|
|
|
var biz = GetBusinessDataAsync(businessTask.BusinessId, businessTask.BusinessType);
|
|
|
|
|
dataContext[TaskFlowDataNameConst.Business] = biz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await logService.WriteLogAsync(businessTask,
|
|
|
|
|
$"开始运行任务动作【{businessTask.TaskType.GetDescription()}】:{string.Join(",", (IEnumerable<string>)value)} ...");
|
|
|
|
|
await ExecuteAsync(dataContext);
|
|
|
|
|
await logService.WriteLogAsync(businessTask,
|
|
|
|
|
$"任务动作【{businessTask.TaskType.GetDescription()}】:{dataContext[TaskFlowDataNameConst.TypeName]} 运行结束");
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await logService.WriteLogAsync(businessTask, $"执行后台任务时出错:{ex.Message}"
|
|
|
|
|
+ Environment.NewLine + "调用堆栈:" + ex.StackTrace);
|
|
|
|
|
return DataResult.FailedWithDesc(MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TaskFlowRuner taskFlow = new(TenantDb, ServiceProvider);
|
|
|
|
|
//await taskFlow.RunWithBsno(businessTask.TaskType, businessTask.BusinessId, dataContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -201,37 +242,20 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> AutomaticBooking(long businessId, BusinessType businessType)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var businessTask = await TenantDb.Queryable<BusinessTask>().Where(
|
|
|
|
|
var businessTask = await TenantDb.Queryable<BusinessTask>().Where(
|
|
|
|
|
x => x.BusinessId == businessId && x.BusinessType == businessType && x.TaskType == TaskBaseTypeEnum.WAIT_BOOKING).FirstAsync();
|
|
|
|
|
if (businessTask == null)
|
|
|
|
|
return DataResult.FailedWithDesc(MultiLanguageConst.BookingTaskNotFound);
|
|
|
|
|
|
|
|
|
|
TaskFlowDataContext dataContext = new
|
|
|
|
|
(
|
|
|
|
|
(TaskFlowDataNameConst.BusinessTask, businessTask),
|
|
|
|
|
("TypeName", typeof(BookingSelector).AssemblyQualifiedName)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var paramList = await GetTaskParamsAsync(TaskBaseTypeEnum.WAIT_BOOKING);
|
|
|
|
|
foreach (var item in paramList)
|
|
|
|
|
dataContext[item.FieldName] = item.FieldValue;
|
|
|
|
|
if (businessTask == null)
|
|
|
|
|
return DataResult.FailedWithDesc(MultiLanguageConst.BookingTaskNotFound);
|
|
|
|
|
|
|
|
|
|
await ExecuteAsync(dataContext);
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await ex.LogAsync(Db);
|
|
|
|
|
return DataResult.FailedWithDesc(MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
var dic = new Dictionary<string, object>();
|
|
|
|
|
dic[TaskFlowDataNameConst.TypeName] = typeof(BookingSelector).AssemblyQualifiedName;
|
|
|
|
|
return await TriggerActionAsync(businessTask, false, dic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取指定任务类型的参数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="taskType"></param>
|
|
|
|
|
/// <param name="taskType">任务类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal async Task<List<TaskFlowParam>> GetTaskParamsAsync(TaskBaseTypeEnum taskType)
|
|
|
|
|
{
|
|
|
|
@ -260,37 +284,42 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
foreach (var key in dataContext.Keys)
|
|
|
|
|
context.AdditionalData[key] = dataContext[key];
|
|
|
|
|
|
|
|
|
|
var typeName = dataContext.GetOrDefault<string>("TypeName");
|
|
|
|
|
if (typeName.IsNullOrEmpty())
|
|
|
|
|
if (!dataContext.TryGetValue(TaskFlowDataNameConst.TypeName, out object typeNameObj) || typeNameObj == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Type? t = Type.GetType(typeName, true, false);
|
|
|
|
|
if (t == null)
|
|
|
|
|
{
|
|
|
|
|
await new ApplicationException($"未能获取类型【{typeName}】的信息,请检查配置").LogAsync(Db);
|
|
|
|
|
IEnumerable<string> typeNames = typeNameObj as IEnumerable<string>;
|
|
|
|
|
typeNames ??= [typeNameObj.ToString()];
|
|
|
|
|
if (typeNames.IsNullOrEmpty())
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IActionExecutor? currentExecutor = null;
|
|
|
|
|
object instance = ConstructorExtensions.CreateInstance(t);
|
|
|
|
|
if (instance is IActionSelector selector)
|
|
|
|
|
{
|
|
|
|
|
currentExecutor = await selector.GetActionExecutor(context);
|
|
|
|
|
}
|
|
|
|
|
else if (instance is IActionExecutor executor)
|
|
|
|
|
foreach (var typeName in typeNames)
|
|
|
|
|
{
|
|
|
|
|
currentExecutor = executor;
|
|
|
|
|
}
|
|
|
|
|
Type? t = Type.GetType(typeName, true, false);
|
|
|
|
|
if (t == null)
|
|
|
|
|
{
|
|
|
|
|
await new ApplicationException($"未能获取类型【{typeName}】的信息,请检查配置").LogAsync(Db);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentExecutor == null)
|
|
|
|
|
{
|
|
|
|
|
await new ApplicationException($"未能创建类型【{t.AssemblyQualifiedName}】的实例").LogAsync(Db);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
IActionExecutor? currentExecutor = null;
|
|
|
|
|
object instance = ConstructorExtensions.CreateInstance(t);
|
|
|
|
|
if (instance is IActionSelector selector)
|
|
|
|
|
{
|
|
|
|
|
currentExecutor = await selector.GetActionExecutor(context);
|
|
|
|
|
}
|
|
|
|
|
else if (instance is IActionExecutor executor)
|
|
|
|
|
{
|
|
|
|
|
currentExecutor = executor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var logService = context.ServiceProvider.GetRequiredService<ITaskLogService>();
|
|
|
|
|
await logService.WriteLogAsync(context.TaskInfo, $"开始运行后台任务({currentExecutor.GetType().FullName})...");
|
|
|
|
|
await currentExecutor.ExecuteAsync(context);
|
|
|
|
|
if (currentExecutor == null)
|
|
|
|
|
{
|
|
|
|
|
await new ApplicationException($"未能创建类型【{t.AssemblyQualifiedName}】的实例").LogAsync(Db);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await currentExecutor.ExecuteAsync(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|