C#工作流示例(WorkflowCore)

cs 复制代码
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();
        }
    }
}
相关推荐
偶尔的鼠标人1 天前
Avalonia DataGrid 控件的LostFocus事件会多次触发
开发语言·c#
ytttr8731 天前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
future_studio1 天前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
c#上位机2 天前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
CodeCraft Studio2 天前
国产化PDF处理控件Spire.PDF教程:如何在 C# 中从 HTML 和 PDF 模板生成 PDF
pdf·c#·html·.net·spire.pdf·pdf文档开发·html创建模板pdf
ysdysyn2 天前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..2 天前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
reasonsummer2 天前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
William_cl3 天前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
c#上位机3 天前
wpf之命令
c#·wpf