using DS.Module.Core;
using DS.Module.Core.Condition;
using DS.Module.Core.Data;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Op.Dtos;
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 DS.WMS.Core.Op.Method.TaskInteraction.ActionSelector;
using DS.WMS.Core.TaskPlat;
using Fasterflect;
using Masuit.Tools;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Op.Method.TaskInteraction
{
///
/// 动作执行管理
///
public class ActionManagerService : ServiceBase, IActionManagerService
{
ITaskLogService logService;
///
/// 初始化
///
public ActionManagerService(IServiceProvider serviceProvider) : base(serviceProvider)
{
logService = serviceProvider.GetRequiredService();
TenantDb.QueryFilter.Clear();
}
///
/// 根据指定条件返回是否匹配的结果
///
/// 任务请求
/// 匹配条件
///
public async Task IsMatchAsync(TaskRequest request, IEnumerable conditions)
{
ArgumentNullException.ThrowIfNull(request, nameof(request));
ArgumentNullException.ThrowIfNull(conditions, nameof(conditions));
var biz = await GetBusinessDataAsync(request.BusinessId, request.BusinessType, conditions);
if (biz != null)
{
TaskFlowDataContext dataContext = new(
(TaskFlowDataNameConst.Business, biz)
);
//循环匹配
foreach (var item in conditions)
{
if (ConditionHelper.IsPass(item, dataContext))
{
//var logEntity = new BusinessTaskLog
//{
// ActionType = ActionType.Create,
// BusinessId = request.BusinessId,
// BusinessType = request.BusinessType,
// TaskType = request.TaskType,
// CreateBy = long.Parse(User.UserId),
// CreateTime = DateTime.Now,
// Remark = $"符合设定条件,已跳过任务的创建"
//};
//await LogService.WriteLogAsync(logEntity);
return true;
}
}
}
return false;
}
///
/// 获取所需业务数据
///
/// 业务ID
/// 业务类型
/// 条件字段
///
public async Task GetBusinessDataAsync(long businessId, BusinessType businessType, IEnumerable conditions)
{
ArgumentNullException.ThrowIfNull(conditions, nameof(conditions));
HashSet fields = [];
foreach (var item in conditions)
{
if (item.Conditions != null)
fields.AddRange(item.Conditions.Select(x => x.Field));
if (item.Groups != null)
fields.AddRange(item.Groups.SelectMany(x => x.Conditions.Select(x => x.Field)));
}
return await GetBusinessDataAsync(businessId, businessType, fields.ToArray());
}
///
/// 获取所需业务数据
///
/// 业务ID
/// 业务类型
/// 返回字段列表
///
public async Task GetBusinessDataAsync(long businessId, BusinessType businessType, params string[] fields)
{
object? biz = null;
switch (businessType)
{
case BusinessType.OceanShippingExport:
//未设置查询字段
if (fields == null || fields.Length == 0)
{
biz = await TenantDb.Queryable().Where(a => a.Id == businessId)
.Select().Mapper(it =>
{
it.DischargePortCountry = TenantDb.Queryable().Where(x => x.Id == it.DischargePortId).Select(x => x.CountryName).First();
}).FirstAsync();
}
else
{
var selectors = fields.Select(x => new SelectModel { FieldName = x }).ToList();
biz = await TenantDb.Queryable