using DS.Module.Core.Extensions;
using DS.Module.UserModule;
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
{
public FlowRuntime(FlowInstance instance, ISqlSugarClient _db) : this(instance, _db, null, null)
{
}
///
/// 构造函数
///
public FlowRuntime(FlowInstance instance, ISqlSugarClient _db, ISqlSugarClient tenantDb, IUser user)
{
db = _db;
BusinessId = instance.BusinessId;
ColumnView = instance.ColumnView;
this.tenantDb = tenantDb;
this.user = user;
InitNodes(instance.Content); //获取工作流模板内容的json对象;
CurrentNodeId = instance.ActivityId == "" ? StartNodeId : instance.ActivityId;
CurrentNodeType = GetNodeType(CurrentNodeId);
PreviousId = instance.PreviousId;
FlowInstanceId = instance.Id;
//会签开始节点和流程结束节点没有下一步
if (CurrentNodeType == 0 || CurrentNodeType == 4)
{
NextNodeId = "-1";
NextNodeType = -1;
}
else if (CurrentNodeType == 5)
{
NextNodeId = GetNextConditionNodeId(CurrentNode); //下一个节点
NextNodeType = GetNodeType(NextNodeId);
}
else
{
NextNodeId = GetNextNodeId(CurrentNodeId); //下一个节点
NextNodeType = GetNodeType(NextNodeId);
}
}
public long BusinessId { get; set; }
public string ColumnView { get; set; }
ISqlSugarClient db { get; set; }
ISqlSugarClient tenantDb { get; set; }
IUser user { get; set; }
///
/// 运行实例的Id
///
public long FlowInstanceId { get; 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 InitNodes(string schemeContentJson)
{
ChildNodes = new List();
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 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;
}
// return ChildNodes.Where(x => x.Pid == nodeId).First().Id;
}
}
///
/// 获取下一个条件节点
///
///
///
public string GetNextConditionNodeId(FlowChild parent)
{
var conditionNodes = GetFlowConditions(parent);
var conditionId = string.Empty;
for (int i = 0; i < conditionNodes.Count; i++)
{
if (i == conditionNodes.Count - 1)
{
// return ChildNodes.Where(x => x.Pid == conditionNodes[i].Id).First().Id;
conditionId = conditionNodes[i].Id;
}
else
{
var nodeSection = conditionNodes[i].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;
//string json = JsonConvert.SerializeObject(list);
//db.Utilities.JsonToConditionalModels(json);
List conditionalModels = [.. list];
ISqlSugarClient sugarClient = tenantDb ?? db;
var exists = sugarClient.Queryable