C#工作流示例(WorkflowCore)

using Microsoft.Extensions.DependencyInjection; using WorkflowCore.Interface; using WorkflowCore.Models; namespace LeaveRequestWorkflow { // 请假申请单 public class LeaveBill { /// <summary> /// 申请人 /// </summary> public string EmployeeName { get; set; } /// <summary> /// 请假起始时间 /// </summary> public DateTime StartDate { get; set; } /// <summary> /// 请假结束时间 /// </summary> public DateTime EndDate { get; set; } /// <summary> /// 请假原因 /// </summary> public string Reason { get; set; } /// <summary> /// 经理意见 /// </summary> public string ManagerComment { get; set; } /// <summary> /// 是否同意 /// </summary> public bool IsApproved { get; set; } /// <summary> /// 创建时间 /// </summary> public DateTime CreateAt { get; set; } /// <summary> /// 处理时间 /// </summary> public DateTime ProcessedAt { get; set; } } // 请假申请 public class ApplyLeave : StepBody { public override ExecutionResult Run(IStepExecutionContext context) { var bill = (LeaveBill)context.Workflow.Data; bill.CreateAt = DateTime.Now; Console.WriteLine($"请假申请已提交 - 员工: {bill.EmployeeName},时间: {bill.StartDate.ToShortDateString()} 到 {bill.EndDate.ToShortDateString()}, 原因: {bill.Reason}"); return ExecutionResult.Next(); } } // 经理审批 public class ManagerApproval : StepBody { public override ExecutionResult Run(IStepExecutionContext context) { var bill = (LeaveBill)context.Workflow.Data; bill.ProcessedAt = DateTime.Now; Console.WriteLine($"经理审批中 - {bill.EmployeeName} 的请假申请"); // 简单模拟:请假天数小于5天自动批准,否则拒绝 var leaveDays = (bill.EndDate - bill.StartDate).Days; if (leaveDays <= 5) { bill.IsApproved = true; bill.ManagerComment = "申请已批准,祝你休假愉快!"; } else { bill.IsApproved = false; bill.ManagerComment = "请假时间过长,请缩短假期或联系HR"; } Console.WriteLine($"经理决定: {(bill.IsApproved ? "批准" : "拒绝")}"); return ExecutionResult.Next(); } } // 通知结果 public class NotifyResult : StepBody { public override ExecutionResult Run(IStepExecutionContext context) { var bill = (LeaveBill)context.Workflow.Data; bill.ProcessedAt = DateTime.Now; Console.WriteLine($"消息通知 - {bill.EmployeeName},你的请假申请{(bill.IsApproved ? "已批准" : "未被批准")}。备注: {bill.ManagerComment}"); return ExecutionResult.Next(); } } // 定义请假工作流 public class LeaveWorkflow : IWorkflow<LeaveBill> { public string Id => "LeaveRequestWorkflow"; public int Version => 1; public void Build(IWorkflowBuilder<LeaveBill> builder) { builder .StartWith<ApplyLeave>() .Then<ManagerApproval>() .Then<NotifyResult>(); } } class Program { static IServiceProvider ConfigureServices() { IServiceCollection services = new ServiceCollection(); // 添加日志服务 services.AddLogging(); // 添加 WorkflowCore services.AddWorkflow(); // 注册你的工作流步骤(确保它们可以被DI容器创建) services.AddTransient<ApplyLeave>(); services.AddTransient<ManagerApproval>(); services.AddTransient<NotifyResult>(); return services.BuildServiceProvider(); } static async Task Main(string[] args) { IServiceProvider serviceProvider = ConfigureServices(); var host = serviceProvider.GetService<IWorkflowHost>(); // 注册工作流 host?.RegisterWorkflow<LeaveWorkflow, LeaveBill>(); // 启动工作流主机 host?.Start(); Console.WriteLine("\r\n工作流示例(WorkflowCore)\r\n"); // 创建新的请假申请 var request = new LeaveBill(); Console.WriteLine(">>> 请假申请单 <<<\r\n"); Console.Write("员工姓名: "); request.EmployeeName = Console.ReadLine(); Console.Write("开始日期 (yyyy-mm-dd): "); if (DateTime.TryParse(Console.ReadLine(), out DateTime startDate)) request.StartDate = startDate; else request.StartDate = DateTime.Now.AddDays(1); Console.Write("结束日期 (yyyy-mm-dd): "); if (DateTime.TryParse(Console.ReadLine(), out DateTime endDate)) request.EndDate = endDate; else request.EndDate = DateTime.Now.AddDays(2); Console.Write("请假原因: "); request.Reason = Console.ReadLine(); // 启动工作流 var workflowId = await host.StartWorkflow("LeaveRequestWorkflow", request); Console.WriteLine($"请假申请已提交,工作流ID: {workflowId}"); // 等待一会儿让工作流处理 await Task.Delay(2000); host?.Stop(); } } }
qq:505645074

浙公网安备 33010602011771号