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.Entity;
using Fasterflect;
using LanguageExt.Pipes;
using Masuit.Tools;
using Masuit.Tools.Systems;
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 bool IsMatch(object source, ConditionContent condition)
{
if (source == null || condition == null)
return false;
TaskFlowDataContext dataContext = new((TaskFlowDataNameConst.Business, source));
if (condition.SourceName.IsNullOrEmpty())
condition.SourceName = TaskFlowDataNameConst.Business;
return ConditionHelper.IsPass(condition, dataContext);
}
///
/// 根据指定条件返回是否匹配的结果
///
/// 业务ID
/// 业务类型
/// 匹配条件
///
public async Task IsMatchAsync(long bsId, BusinessType businessType, IEnumerable conditions)
{
ArgumentNullException.ThrowIfNull(conditions, nameof(conditions));
var biz = await GetBusinessDataAsync(bsId, businessType, conditions);
if (biz != null)
{
TaskFlowDataContext dataContext = new((TaskFlowDataNameConst.Business, biz));
//循环匹配
foreach (var item in conditions)
{
if (ConditionHelper.IsPass(item, dataContext))
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