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();
        }
    }
}
相关推荐
yngsqq8 小时前
平面图环 内轮廓
c#
rockey62710 小时前
AScript之eval函数详解
c#·.net·script·eval·expression·动态脚本
He少年14 小时前
【AI 辅助案例分享】
人工智能·c#·编辑器·ai编程
工程师00716 小时前
栈和堆的概念
c#·栈和堆
不会编程的懒洋洋16 小时前
C# P/Invoke 基础
开发语言·c++·笔记·安全·机器学习·c#·p/invoke
Avalon71216 小时前
Unity3D响应式渲染UI框架UniVue
游戏·ui·unity·c#·游戏引擎
njsgcs17 小时前
solidworks折弯自动标注5 非90度折弯
c#·solidworks
狼与自由17 小时前
clickhouse引擎
clickhouse·c#·linq
wangnaisheng17 小时前
【C#】死锁详解:发生原因、优化解决方案
c#
tiger从容淡定是人生18 小时前
AI替代软件战略(一):从 CCleaner 到 MCP 架构重构 —— TigerCleaner 的工程实践
人工智能·重构·架构·c#·mcp