WorkflowEngineController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.EntityFrameworkCore;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using wispro.sp.entity;
  9. using wispro.sp.entity.workflowInstance;
  10. using wispro.sp.share;
  11. namespace wispro.sp.api.Controllers
  12. {
  13. [Route("api/[controller]/[action]")]
  14. [ApiController]
  15. public class WorkflowEngineController : ControllerBase
  16. {
  17. spDbContext Context;
  18. public WorkflowEngineController(spDbContext context)
  19. {
  20. Context = context;
  21. }
  22. public void DoAction(int instanceId,int stepId,int ActionId,List<InputValue> inputValues)
  23. {
  24. //1、取实例对象
  25. //2、取步骤对象
  26. //3、判断Action定义对象
  27. //4、判断输入值合法性
  28. //5、保存数据
  29. }
  30. [HttpPost]
  31. public ApiSaveResponse AddNew(NewWorkflowObject workflowObject)
  32. {
  33. ApiSaveResponse ret = new ApiSaveResponse();
  34. ret.Success = true;
  35. using(var t = Context.Database.BeginTransaction())
  36. {
  37. try
  38. {
  39. if (workflowObject.InitAction != null)
  40. {
  41. Context.Actions.Add(workflowObject.InitAction);
  42. Context.SaveChanges();
  43. }
  44. else
  45. {
  46. throw new ApplicationException("没有设定初始化Action!");
  47. }
  48. workflowObject.Workflow.InitActionId = workflowObject.InitAction.Id;
  49. workflowObject.Workflow.CreateTime = DateTime.Now;
  50. workflowObject.Workflow.CreateUserId = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == User.Identity.Name).Id;
  51. Context.Workflows.Add(workflowObject.Workflow);
  52. Context.SaveChanges();
  53. t.Commit();
  54. }
  55. catch(Exception ex)
  56. {
  57. t.Rollback();
  58. ret.Success = false;
  59. ret.ErrorMessage = ex.Message;
  60. }
  61. }
  62. return ret;
  63. }
  64. public ApiSaveResponse SaveStep(NewStepObject stepObj)
  65. {
  66. var ret = new ApiSaveResponse();
  67. ret.Success = true;
  68. using (var t = Context.Database.BeginTransaction())
  69. {
  70. try
  71. {
  72. var step = stepObj.Step;
  73. var wf = GetWorkflow(step.workflowId);
  74. if(wf == null)
  75. {
  76. throw new ApplicationException("步骤所属流程不存在!");
  77. }
  78. if (step.Id == 0)
  79. {
  80. Context.Steps.Add(step);
  81. Context.SaveChanges();
  82. if (stepObj.isLastStep)
  83. {
  84. wf.EndStepId = step.Id;
  85. }
  86. }
  87. else
  88. {
  89. var temStep = Context.Steps.FirstOrDefault(a => a.Id == step.Id);
  90. if (temStep != null)
  91. {
  92. if(temStep.workflowId != step.workflowId)
  93. {
  94. throw new ApplicationException("修改步骤时,不能修改步骤所属流程!");
  95. }
  96. temStep.Name = step.Name;
  97. temStep.stepType = step.stepType;
  98. temStep.defaultResponseSetting = step.defaultResponseSetting;
  99. if (stepObj.isLastStep)
  100. {
  101. wf.EndStepId = step.Id;
  102. }
  103. }
  104. else
  105. {
  106. throw new ApplicationException("Id不存在!");
  107. }
  108. }
  109. Context.SaveChanges();
  110. t.Commit();
  111. }
  112. catch(Exception ex)
  113. {
  114. t.Rollback();
  115. ret.Success = false;
  116. ret.ErrorMessage = ex.Message;
  117. }
  118. }
  119. return ret;
  120. }
  121. public ApiSaveResponse SaveAction(entity.workflowDefine.Action action)
  122. {
  123. var ret = new ApiSaveResponse();
  124. ret.Success = true;
  125. using (var t = Context.Database.BeginTransaction())
  126. {
  127. try
  128. {
  129. if (action.Id == 0)
  130. {
  131. Context.Actions.Add(action);
  132. }
  133. else
  134. {
  135. var temAtion = Context.Actions.FirstOrDefault(a => a.Id == action.Id);
  136. if (temAtion != null)
  137. {
  138. temAtion.InputForm = action.InputForm;
  139. temAtion.Name = action.Name;
  140. temAtion.StepId = action.StepId;
  141. temAtion.inputValuesSettings = action.inputValuesSettings;
  142. temAtion.OnActionObjectType = action.OnActionObjectType;
  143. }
  144. else
  145. {
  146. throw new ApplicationException("Id不存在!");
  147. }
  148. }
  149. Context.SaveChanges();
  150. t.Commit();
  151. }
  152. catch (Exception ex)
  153. {
  154. t.Rollback();
  155. ret.Success = false;
  156. ret.ErrorMessage = ex.Message;
  157. }
  158. }
  159. return ret;
  160. }
  161. public ApiSaveResponse SaveTransfer(entity.workflowDefine.TrasferCondition trasfer)
  162. {
  163. var ret = new ApiSaveResponse();
  164. ret.Success = true;
  165. using (var t = Context.Database.BeginTransaction())
  166. {
  167. try
  168. {
  169. if (trasfer.Id == 0)
  170. {
  171. Context.TrasferConditions.Add(trasfer);
  172. }
  173. else
  174. {
  175. var temTransfer = Context.TrasferConditions.FirstOrDefault(a => a.Id == trasfer.Id);
  176. if (temTransfer != null)
  177. {
  178. temTransfer.StepId = trasfer.StepId;
  179. temTransfer.nextStepId = trasfer.nextStepId;
  180. temTransfer.Condition = trasfer.Condition;
  181. }
  182. else
  183. {
  184. throw new ApplicationException("Id不存在!");
  185. }
  186. }
  187. Context.SaveChanges();
  188. t.Commit();
  189. }
  190. catch (Exception ex)
  191. {
  192. t.Rollback();
  193. ret.Success = false;
  194. ret.ErrorMessage = ex.Message;
  195. }
  196. }
  197. return ret;
  198. }
  199. public List<entity.workflowDefine.Workflow> GetAllWorkflows()
  200. {
  201. return Context.Workflows.Include(d => d.CreateUser).ToList();
  202. }
  203. public entity.workflowDefine.Workflow GetWorkflow(int workflowId)
  204. {
  205. return Context.Workflows.Include(d=>d.InitAction).Include(d=>d.CreateUser).FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  206. }
  207. public List<entity.workflowDefine.Step> GetSteps(int workflowId)
  208. {
  209. return Context.Steps.Where<entity.workflowDefine.Step>(w => w.workflowId == workflowId).ToList();
  210. }
  211. public List<entity.workflowDefine.Action> GetActions(int workflowId)
  212. {
  213. var workflow = Context.Workflows.FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  214. return Context.Actions.Include(s=>s.step).Where<entity.workflowDefine.Action>(d => d.step.workflowId == workflowId || d.Id == workflow.InitActionId).ToList();
  215. }
  216. public List<entity.workflowDefine.TrasferCondition> GetTrasfers(int workflowId)
  217. {
  218. return Context.TrasferConditions.Where(d => d.Step.workflowId == workflowId || d.nextStep.workflowId == workflowId).ToList();
  219. }
  220. }
  221. }