diff --git a/OpenAuth.App/workflow/WorkflowEngineApp.cs b/OpenAuth.App/workflow/WorkflowEngineApp.cs index 4b24dc8..b44bb4c 100644 --- a/OpenAuth.App/workflow/WorkflowEngineApp.cs +++ b/OpenAuth.App/workflow/WorkflowEngineApp.cs @@ -648,9 +648,9 @@ public class WorkflowEngineApp : SqlSugarBaseApp zy.NodeId) // 执行查询,返回 List 结果 .ToList(); - - var allQuery = _sqlSugar.Queryable( - (t, i, n) => new JoinQueryInfos( + + var allQuery = _sqlSugar.Queryable((t, i, n) => + new JoinQueryInfos( // 第一个左联:t(违建表) LEFT JOIN i(流程实例表) ON 业务编号匹配 JoinType.Left, t.BusinessNumber == i.BusinessNo, // 第二个左联:i(流程实例表) LEFT JOIN n(流程节点表) ON 当前节点ID匹配 @@ -665,8 +665,9 @@ public class WorkflowEngineApp : SqlSugarBaseApp w.HandlerId == userId) // 关键1:用WhereIF判断列表非空,避免IN()语法错误;用SqlFunc.Contains替代原生Contains - .WhereIF(nodeIdList != null && nodeIdList.Any(), - w => w.HandlerId == null && w.Status == "ToDo" && SqlFunc.ContainsArray(nodeIdList, w.NodeId)) + .WhereIF(nodeIdList != null && nodeIdList.Any(), + w => w.HandlerId == null && w.Status == "ToDo" && + SqlFunc.ContainsArray(nodeIdList, w.NodeId)) // 主查询与子查询的实例ID关联(原有逻辑,不变) .Where(w => w.InstanceId == i.InstanceId) .Select(w => w.InstanceId) @@ -1002,25 +1003,73 @@ public class WorkflowEngineApp : SqlSugarBaseApp public object SaveDraft(InitiateFlowRequestDto requestDto) { - requestDto.BusinessNo = _businessNoGenerator.GenerateBusinessNo("WF"); - var attachmentPaths = string.Join(",", requestDto.Attachments); - var user = _auth.GetCurrentUser().User; - var illegalConstructionAssessment = new IllegalConstructionAssessment + try { - Id = requestDto.BusinessNo, - Title = requestDto.Title, - BusinessNumber = requestDto.BusinessNo, - Attachments = requestDto.Attachments != null ? attachmentPaths : string.Empty, - AcceptanceTime = DateTime.Now, - Status = "Draft", - Type = requestDto.type, - CreateTime = DateTime.Now, - CreateUser = user.Name, - UpdateTime = DateTime.Now, - CreateUserId = user.Id - }; - _sqlSugar.Insertable(illegalConstructionAssessment).ExecuteCommand(); - return true; + UnitWork.Db.Ado.BeginTran(); + requestDto.BusinessNo = _businessNoGenerator.GenerateBusinessNo("WF"); + var attachmentPaths = string.Join(",", requestDto.Attachments); + var user = _auth.GetCurrentUser().User; + var illegalConstructionAssessment = new IllegalConstructionAssessment + { + Id = requestDto.BusinessNo, + Title = requestDto.Title, + BusinessNumber = requestDto.BusinessNo, + Attachments = requestDto.Attachments != null ? attachmentPaths : string.Empty, + AcceptanceTime = DateTime.Now, + Status = "Draft", + Type = requestDto.type, + CreateTime = DateTime.Now, + CreateUser = user.Name, + UpdateTime = DateTime.Now, + CreateUserId = user.Id + }; + _sqlSugar.Insertable(illegalConstructionAssessment).ExecuteCommand(); + var template = _sqlSugar.Queryable() + .Where(t => t.FlowCode == requestDto.FlowCode && t.IsEnabled == true) + .First(); + if (template == null) + throw new Exception($"流程模板【{requestDto.FlowCode}】不存在或未启用"); + + // 步骤2:查询流程开始节点(区县提交) + var startNode = _sqlSugar.Queryable() + .Where(n => n.TemplateId == template.TemplateId && n.NodeType == "Start" && n.NodeName == "区县提交") + .First(); + if (startNode == null) + throw new Exception("流程开始节点【区县提交】不存在,请配置节点"); + var flowInstance = new ZyFlowInstance + { + TemplateId = template.TemplateId, + FlowCode = template.FlowCode, + BusinessNo = requestDto.BusinessNo, + Status = "Draft", // 已提交 + CurrentNodeId = startNode.NodeId, + InitiatorId = user.Id, + InitiatorName = user.Name, + CreateTime = DateTime.Now + }; + var instanceId = _sqlSugar.Insertable(flowInstance).ExecuteReturnIdentity(); + // 步骤4:插入开始节点工作项(直接标记为已完成) + // 开始节点无ToDo + var startWorkitem = new ZyFlowWorkitem + { + InstanceId = instanceId, + NodeId = startNode.NodeId, + NodeName = startNode.NodeName, + HandlerId = user.Id, + HandlerName = user.Name, + Status = "Draft", // 已完成 + ReceiveTime = DateTime.Now, + HandleTime = DateTime.Now, + Comment = "区县拟办" + }; + _sqlSugar.Insertable(startWorkitem).ExecuteCommand(); + UnitWork.Db.Ado.CommitTran(); + return true; + } + finally + { + UnitWork.Db.Ado.RollbackTran(); + } } public dynamic Detail(string businessNo)