using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using Mapster;
using Newtonsoft.Json;
using SqlSugar;
namespace DS.WMS.Core.Flow.Method;
///
///
///
public class FlowRuntime
{
readonly ISqlSugarClient db;
readonly ISqlSugarClient? tenantDb;
///
/// 构造函数
///
///
///
public FlowRuntime(FlowInstance instance, ISqlSugarClient _db) : this(instance, _db, null)
{
}
///
/// 构造函数
///
public FlowRuntime(FlowInstance instance, ISqlSugarClient _db, ISqlSugarClient? tenantDb)
{
ArgumentNullException.ThrowIfNull(instance, nameof(instance));
db = _db;
this.tenantDb = tenantDb;
InstanceId = instance.Id;
BusinessId = instance.BusinessId;
ColumnView = instance.ColumnView;
ReadJson(instance.Content); //获取工作流模板内容的json对象;
CurrentNodeId = instance.ActivityId == "" ? StartNodeId : instance.ActivityId;
CurrentNodeType = GetNodeType(CurrentNodeId);
PreviousId = instance.PreviousId;
//会签开始节点和流程结束节点没有下一步
if (CurrentNodeType == 0 || CurrentNodeType == 4)
{
NextNodeId = "-1";
NextNodeType = -1;
}
else if (CurrentNodeType == 5)
{
NextNodeId = GetNextConditionNodeId(CurrentNode); //下一个节点
NextNodeType = GetNodeType(NextNodeId);
}
else
{
NextNodeId = GetNextNodeId(CurrentNodeId); //下一个节点
if (string.IsNullOrEmpty(NextNodeId) && CurrentNodeType == 3)
{
//当前节点类型为开始节点,且没有下一级节点,则认为应跳出工作流
ShouldSkip = true;
}
else
{
NextNodeType = GetNodeType(NextNodeId);
}
}
}
///
/// 当前实例ID
///
public long InstanceId { get; set; }
///
/// 业务ID
///
public long BusinessId { get; set; }
///
/// 查询对象名
///
public string? ColumnView { get; set; }
///
/// 是否应跳过工作流执行
///
public bool ShouldSkip { get; private set; }
///
/// 开始节点的ID
///
public string StartNodeId { get; set; }
///
/// 当前节点的ID
///
public string CurrentNodeId { get; set; }
///
/// 当前节点类型 0会签开始,1会签结束,2一般节点,开始节点,4流程运行结束
///
public int CurrentNodeType { get; set; }
///
/// 当前节点的对象
///
public FlowChild CurrentNode => ChildNodes.First(x => x.Id == CurrentNodeId);
///
/// 下一个节点
///
public string? NextNodeId { get; set; }
///
/// 下一个节点类型 -1无法运行,0会签开始,1会签结束,2一般节点,4流程运行结束,5同级审批
///
public int NextNodeType { get; set; }
///
/// 下一个节点对象
///
public FlowChild? NextNode => NextNodeId != "-1" ? ChildNodes.First(x => x.Id == NextNodeId) : null;
///
/// 上一个节点
///
public string PreviousId { get; set; }
///
/// 实例节点集合
///
public List ChildNodes { get; set; }
///
/// 备注
///
public string? Note { get; set; }
///
/// 获取工作流节点的字典列表:key节点id
///
///
///
private void ReadJson(string schemeContentJson)
{
ChildNodes = [];
var root = JsonConvert.DeserializeObject(schemeContentJson);
if (root.Type == FlowChild.START)
{
StartNodeId = root.Id;
}
if (root.Child.IsNotNull())
{
var parent = root.Adapt();
ChildNodes.Add(parent);
GetFlowChildsByParent(parent.Id, parent);
}
}
///
/// 撤销流程,清空所有节点
///
public void Cancel()
{
foreach (var item in ChildNodes)
{
item.SetInfo = null;
}
}
private void GetFlowChildsByParent(string nodeId, FlowChild parent)
{
if (parent.Type == FlowChild.Exclusive && parent.Children.IsNotNull() && parent.Children.Count > 0)
{
foreach (var item in parent.Children)
{
ChildNodes.Add(item);
GetFlowChildsByParent(nodeId, item);
}
}
if (parent.Child.IsNotNull())
{
ChildNodes.Add(parent.Child);
GetFlowChildsByParent(parent.Child.Id, parent.Child);
}
// var childs = new List();
}
private static List GetFlowConditions(FlowChild parent)
{
var conditionNodes = new List();
if (parent.Children != null)
{
foreach (var item in parent.Children)
{
conditionNodes.Add(item);
}
}
return conditionNodes;
}
///
/// 获取下一个节点
///
///
private string? GetNextNodeId(string? nodeId = null)
{
if (nodeId == null)
{
return ChildNodes.Where(x => x.Pid == "root").First()?.Id;
}
else
{
if (ChildNodes.Exists(x => x.Pid == nodeId))
{
var nextChild = ChildNodes.Where(x => x.Pid == nodeId).First();
if (nextChild.Type == FlowChild.Exclusive) //互斥条件
{
return GetNextConditionNodeId(nextChild); //下一个节点
}
else
{
return nextChild.Id;
}
}
else
{
return ChildNodes.Where(x => x.Id == "end").First()?.Id;
}
}
}
///
/// 获取下一个条件节点
///
///
///
public string? GetNextConditionNodeId(FlowChild parent)
{
var conditionNodes = GetFlowConditions(parent);
var conditionId = string.Empty;
ISqlSugarClient sugarClient = tenantDb ?? db;
for (int i = 0; i < conditionNodes.Count; i++)
{
var conditionNode = conditionNodes[i];
if (i == conditionNodes.Count - 1)
{
conditionId = conditionNode.Id;
}
else if (conditionNode.UseSQL)
{
var scalar = sugarClient.Ado.GetScalar(conditionNode.SQLText,
new SugarParameter($"@{nameof(BaseModel.Id)}", BusinessId));
if (scalar != null && (scalar is int count && count > 0 || scalar is bool boolValue && boolValue))
{
conditionId = conditionNode.Id;
break;
}
}
else
{
var nodeSection = conditionNode.Conditions;
var list = new List();
var conditionList = new List>(nodeSection.Conditions.Count);
var whereType = Enum.Parse(nodeSection.LogicalOperator, true);
foreach (var item in nodeSection.Conditions)
{
conditionList.Add(new KeyValuePair
(whereType,
new ConditionalModel
{
FieldName = item.Field,
ConditionalType = GetConditionalType(item.Operator),
FieldValue = item.Value
})
);
}
if (conditionList.Count > 0)
{
list.Add(new ConditionalCollections
{
ConditionalList = conditionList
});
}
var groupList = new List>();
foreach (var group in nodeSection.Groups)
{
WhereType whereType2 = Enum.Parse(group.LogicalOperator, true);
for (int j = 0; j < group.Conditions.Count; j++)
{
var item1 = group.Conditions[j];
groupList.Add(new KeyValuePair
(j == 0 ? whereType : whereType2,
new ConditionalModel
{
FieldName = item1.Field,
ConditionalType = GetConditionalType(item1.Operator),
FieldValue = item1.Value
})
);
}
}
if (groupList.Count > 0)
{
list.Add(new ConditionalCollections
{
ConditionalList = groupList
});
}
if (list.Count == 0) //跳过默认条件
continue;
List conditionalModels = [.. list];
var exists = sugarClient.Queryable