
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();// 添加 WorkflowCoreservices.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;elserequest.StartDate = DateTime.Now.AddDays(1);Console.Write("結束日期 (yyyy-mm-dd): ");if (DateTime.TryParse(Console.ReadLine(), out DateTime endDate))request.EndDate = endDate;elserequest.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();}}
}