using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
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);
// ti = schemeContentJson.title;
// initNum = schemeContentJson.initNum?? 0;
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; }
public ISqlSugarClient db { get; set; }
public ISqlSugarClient tenantDb { get; set; }
public IUser user { get; set; }
///
///
///
public string Name { get; set; }
public int initNum { 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; }
///
/// 获取工作流节点的字典列表:key节点id
///
///
///
private void InitNodes(string schemeContentJson)
{
ChildNodes = new List();
var root = JsonConvert.DeserializeObject(schemeContentJson);
if (root.Type == FlowChild.START)
{
this.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 virtual 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 conditions = conditionNodes[i].Conditions;
var conditionalCollections = new List();
if (conditions.LogicalOperator == "and")
{
var conditionList = new List>();
foreach (var item in conditions.Conditions)
{
conditionList.Add(new KeyValuePair
(WhereType.And,
new ConditionalModel
{
FieldName = item.Field,
ConditionalType = GetConditionalType(item.Operator),
FieldValue = item.Value
})
);
}
if (conditionList.Count > 0)
{
conditionalCollections.Add(new ConditionalCollections
{
ConditionalList = conditionList
}
)
;
}
var groupList = new List>();
foreach (var group in conditions.Groups)
{
if (group.LogicalOperator == "and")
{
foreach (var item1 in group.Conditions)
{
groupList.Add(new KeyValuePair
(WhereType.And,
new ConditionalModel
{
FieldName = item1.Field,
ConditionalType = GetConditionalType(item1.Operator),
FieldValue = item1.Value
})
);
}
}
else
{
foreach (var item1 in group.Conditions)
{
groupList.Add(new KeyValuePair
(WhereType.Or,
new ConditionalModel
{
FieldName = item1.Field,
ConditionalType = GetConditionalType(item1.Operator),
FieldValue = item1.Value
})
);
}
}
}
if (groupList.Count > 0)
{
conditionalCollections.Add(new ConditionalCollections
{
ConditionalList = groupList
}
)
;
}
}
else
{
var conditionList = new List>();
foreach (var item in conditions.Conditions)
{
conditionList.Add(new KeyValuePair
(WhereType.Or,
new ConditionalModel
{
FieldName = item.Field,
ConditionalType = GetConditionalType(item.Operator),
FieldValue = item.Value
})
);
}
if (conditionList.Count > 0)
{
conditionalCollections.Add(new ConditionalCollections
{
ConditionalList = conditionList
}
)
;
}
var groupList = new List>();
foreach (var group in conditions.Groups)
{
if (group.LogicalOperator == "and")
{
foreach (var item1 in group.Conditions)
{
groupList.Add(new KeyValuePair
(WhereType.And,
new ConditionalModel
{
FieldName = item1.Field,
ConditionalType = GetConditionalType(item1.Operator),
FieldValue = item1.Value
})
);
}
}
else
{
foreach (var item1 in group.Conditions)
{
groupList.Add(new KeyValuePair
(WhereType.Or,
new ConditionalModel
{
FieldName = item1.Field,
ConditionalType = GetConditionalType(item1.Operator),
FieldValue = item1.Value
})
);
}
}
}
if (groupList.Count > 0)
{
conditionalCollections.Add(new ConditionalCollections
{
ConditionalList = groupList
}
)
;
}
}
var conditionalModels =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditionalCollections));
// var info = db.Queryable().Where(x => x.Id == BusinessId).Where(conditionalModels)
// .First();
var info = db.Queryable