123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using wispro.sp.entity;
- using wispro.sp.entity.workflowInstance;
- using wispro.sp.share;
- namespace wispro.sp.api.Controllers
- {
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class WorkflowEngineController : ControllerBase
- {
- spDbContext Context;
- public WorkflowEngineController(spDbContext context)
- {
- Context = context;
- }
- public void DoAction(int instanceId,int stepId,int ActionId,List<InputValue> inputValues)
- {
- //1、取实例对象
- //2、取步骤对象
- //3、判断Action定义对象
- //4、判断输入值合法性
- //5、保存数据
- }
- [HttpPost]
- public ApiSaveResponse AddNew(NewWorkflowObject workflowObject)
- {
- ApiSaveResponse ret = new ApiSaveResponse();
- ret.Success = true;
- using(var t = Context.Database.BeginTransaction())
- {
- try
- {
- if (workflowObject.InitAction != null)
- {
- Context.Actions.Add(workflowObject.InitAction);
- Context.SaveChanges();
- }
- else
- {
- throw new ApplicationException("没有设定初始化Action!");
- }
- workflowObject.Workflow.InitActionId = workflowObject.InitAction.Id;
- workflowObject.Workflow.CreateTime = DateTime.Now;
- workflowObject.Workflow.CreateUserId = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == User.Identity.Name).Id;
- Context.Workflows.Add(workflowObject.Workflow);
- Context.SaveChanges();
- t.Commit();
- }
- catch(Exception ex)
- {
- t.Rollback();
- ret.Success = false;
- ret.ErrorMessage = ex.Message;
- }
- }
- return ret;
- }
- public ApiSaveResponse SaveStep(NewStepObject stepObj)
- {
- var ret = new ApiSaveResponse();
- ret.Success = true;
-
- using (var t = Context.Database.BeginTransaction())
- {
- try
- {
- var step = stepObj.Step;
- var wf = GetWorkflow(step.workflowId);
- if(wf == null)
- {
- throw new ApplicationException("步骤所属流程不存在!");
- }
- if (step.Id == 0)
- {
- Context.Steps.Add(step);
- Context.SaveChanges();
- if (stepObj.isLastStep)
- {
- wf.EndStepId = step.Id;
- }
- }
- else
- {
- var temStep = Context.Steps.FirstOrDefault(a => a.Id == step.Id);
- if (temStep != null)
- {
- if(temStep.workflowId != step.workflowId)
- {
- throw new ApplicationException("修改步骤时,不能修改步骤所属流程!");
- }
- temStep.Name = step.Name;
- temStep.stepType = step.stepType;
- temStep.defaultResponseSetting = step.defaultResponseSetting;
- if (stepObj.isLastStep)
- {
- wf.EndStepId = step.Id;
- }
- }
- else
- {
- throw new ApplicationException("Id不存在!");
- }
- }
- Context.SaveChanges();
- t.Commit();
- }
- catch(Exception ex)
- {
- t.Rollback();
- ret.Success = false;
- ret.ErrorMessage = ex.Message;
- }
- }
- return ret;
- }
- public ApiSaveResponse SaveAction(entity.workflowDefine.Action action)
- {
- var ret = new ApiSaveResponse();
- ret.Success = true;
- using (var t = Context.Database.BeginTransaction())
- {
- try
- {
- if (action.Id == 0)
- {
- Context.Actions.Add(action);
- }
- else
- {
- var temAtion = Context.Actions.FirstOrDefault(a => a.Id == action.Id);
- if (temAtion != null)
- {
- temAtion.InputForm = action.InputForm;
- temAtion.Name = action.Name;
- temAtion.StepId = action.StepId;
- temAtion.inputValuesSettings = action.inputValuesSettings;
- temAtion.OnActionObjectType = action.OnActionObjectType;
- }
- else
- {
- throw new ApplicationException("Id不存在!");
- }
- }
- Context.SaveChanges();
- t.Commit();
- }
- catch (Exception ex)
- {
- t.Rollback();
- ret.Success = false;
- ret.ErrorMessage = ex.Message;
- }
- }
- return ret;
- }
- public ApiSaveResponse SaveTransfer(entity.workflowDefine.TrasferCondition trasfer)
- {
- var ret = new ApiSaveResponse();
- ret.Success = true;
- using (var t = Context.Database.BeginTransaction())
- {
- try
- {
-
-
- if (trasfer.Id == 0)
- {
- Context.TrasferConditions.Add(trasfer);
- }
- else
- {
- var temTransfer = Context.TrasferConditions.FirstOrDefault(a => a.Id == trasfer.Id);
- if (temTransfer != null)
- {
- temTransfer.StepId = trasfer.StepId;
- temTransfer.nextStepId = trasfer.nextStepId;
- temTransfer.Condition = trasfer.Condition;
- }
- else
- {
- throw new ApplicationException("Id不存在!");
- }
- }
- Context.SaveChanges();
- t.Commit();
- }
- catch (Exception ex)
- {
- t.Rollback();
- ret.Success = false;
- ret.ErrorMessage = ex.Message;
- }
- }
- return ret;
- }
- public List<entity.workflowDefine.Workflow> GetAllWorkflows()
- {
- return Context.Workflows.Include(d => d.CreateUser).ToList();
- }
- public entity.workflowDefine.Workflow GetWorkflow(int workflowId)
- {
- return Context.Workflows.Include(d=>d.InitAction).Include(d=>d.CreateUser).FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
- }
- public List<entity.workflowDefine.Step> GetSteps(int workflowId)
- {
- return Context.Steps.Where<entity.workflowDefine.Step>(w => w.workflowId == workflowId).ToList();
- }
- public List<entity.workflowDefine.Action> GetActions(int workflowId)
- {
- var workflow = Context.Workflows.FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
- return Context.Actions.Include(s=>s.step).Where<entity.workflowDefine.Action>(d => d.step.workflowId == workflowId || d.Id == workflow.InitActionId).ToList();
- }
- public List<entity.workflowDefine.TrasferCondition> GetTrasfers(int workflowId)
- {
- return Context.TrasferConditions.Where(d => d.Step.workflowId == workflowId || d.nextStep.workflowId == workflowId).ToList();
- }
- }
- }
|