更改任务动作执行调用

usertest
嵇文龙 3 months ago
parent e8defab35c
commit b887912827

@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Http;
namespace DS.Module.Core
namespace DS.Module.Core
{
/// <summary>
/// 维护一些常用的,用于任务台数据上下文读写参数的参数名
@ -85,5 +83,11 @@ namespace DS.Module.Core
/// <remarks>类型Param{T}</remarks>
public const string TaskPKId = "TaskPKId";
/// <summary>
/// 执行动作的程序集类型名称
/// </summary>
/// <remarks>类型string</remarks>
public const string TypeName = "TypeName";
}
}

@ -104,7 +104,7 @@
/// <returns></returns>
public bool TryGetValue(string key, out object value)
{
return TryGetValue(key, out value);
return dataContext.TryGetValue(key, out value);
}
/// <summary>

@ -60,7 +60,7 @@ namespace DS.WMS.Core.Op.Interface.TaskInteraction
/// <param name="includeOrder">是否包含订单数据</param>
/// <param name="additionalData">附加参数</param>
/// <returns></returns>
Task TriggerActionAsync(BusinessTask businessTask, bool includeOrder = false, IDictionary<string, object>? additionalData = null);
Task<DataResult> TriggerActionAsync(BusinessTask businessTask, bool includeOrder = false, IDictionary<string, object>? additionalData = null);
/// <summary>
/// 执行放舱任务(任务台使用)

@ -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);
}
}
}
}

@ -42,11 +42,18 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
if (request == null || !request.Any())
return;
var first = request.First();
var taskTypes = request.Select(x => x.TaskType).Distinct().ToArray();
var existTypes = await GetQuery(first.BusinessId, first.BusinessType, taskTypes).Select(x => x.TaskType).ToListAsync();
//移除已存在的任务类型
request = request.Where(x => !existTypes.Contains(x.TaskType));
if (!request.Any())
return;
long userId = long.Parse(User.UserId);
long tenatId = long.Parse(User.TenantId);
string tenatName = Db.Queryable<SysTenant>().Where(x => x.Id == tenatId).Select(x => x.Name).First();
var dt = DateTime.Now;
var first = request.First();
var defaultUsers = await GetRecvUsersAsync(first.RecvUserIdList);
var biz = await TenantDb.Queryable<SeaExport>().Select(x => new
{
@ -58,13 +65,6 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
x.ETD,
}).FirstAsync(x => x.Id == first.BusinessId);
var taskTypes = request.Select(x => x.TaskType).Distinct().ToArray();
var existTypes = await GetQuery(first.BusinessId, first.BusinessType, taskTypes).Select(x => x.TaskType).ToListAsync();
//移除已存在的任务类型
request = request.Where(x => !existTypes.Contains(x.TaskType));
if (!request.Any())
return;
var taskList = new List<BusinessTask>();
var taskLogList = new List<BusinessTaskLog>();
//获取任务接收人

@ -5,6 +5,7 @@ using DS.WMS.Core.Op.Dtos.TaskInteraction;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Entity.TaskInteraction;
using DS.WMS.Core.Op.Interface.TaskInteraction;
using Mapster;
using Microsoft.AspNetCore.Mvc;
namespace DS.WMS.OpApi.Controllers
@ -72,6 +73,49 @@ namespace DS.WMS.OpApi.Controllers
return await actionService.AutomaticBooking(businessId, businessType);
}
/// <summary>
/// 运行任务动作
/// </summary>
/// <param name="actionService"></param>
/// <param name="request">任务信息</param>
/// <returns></returns>
[HttpPost, Route("RunTaskAction")]
public async Task<DataResult> RunTaskActionAsync([FromServices] IActionManagerService actionService, [FromBody] TaskRequest request)
{
var result = await taskService.GetTasks(request.BusinessId, request.BusinessType, request.TaskType);
if (result.Data?.Count > 0)
return await actionService.TriggerActionAsync(result.Data[0].Adapt<BusinessTask>(), false);
return DataResult.FailedWithDesc(MultiLanguageConst.EmptyData);
}
/// <summary>
/// 创建服务项目任务
/// </summary>
/// <param name="businessId">业务ID</param>
/// <param name="businessType">业务类型(可空,默认=海运出口)</param>
/// <returns></returns>
[HttpPost, Route("CreateSubTask")]
public async Task<IActionResult> CreateSubTaskAsync([FromQuery] long businessId,
[FromQuery] BusinessType businessType = BusinessType.OceanShippingExport)
{
HttpStatusCode statusCode = HttpStatusCode.NoContent;
try
{
await taskService.CreateSubTaskAsync(new BusinessTask
{
BusinessId = businessId,
BusinessType = businessType
});
}
catch
{
statusCode = HttpStatusCode.InternalServerError;
}
return new StatusCodeResult((int)statusCode);
}
/// <summary>
/// 创建关联任务
/// </summary>

@ -70,10 +70,10 @@
"LocalPrintUrl": "http://localhost:2999/printApi/OpenPrint/GetOpenJsonPrintInfoAsync"
},
"DjyService": {
"IP": "60.209.125.238",
"Port": "35100",
"AccessKey": "6b050e757c1e405489e19602dd81ebbe",
"AccessSecret": "51d564741c761e0760f62d904f13c193009e74d0af9f72e2dc67f9320b8ae42580e2339745aee1bf",
"IP": "118.190.144.189",
"Port": "5001",
"AccessKey": "8f96908b74d944e0b1dedfb2714a941f",
"AccessSecret": "a5689efa45367b591668b0b3031d9b68e9f3bb04dc6cee26e556cc272de78841e01f78f6f35a313b",
"SaveServiceProjectUrl": "/EmbedProjectGoodsStatus/SaveServiceProject",
"CancelServiceProjectUrl": "/EmbedProjectGoodsStatus/CancelServiceProject",
"GetServiceProjectListUrl": "/EmbedProjectGoodsStatus/GetServiceProjectList",

Loading…
Cancel
Save