WorkflowEngineController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. entity.workflowDefine.Step endStep = new entity.workflowDefine.Step() {
  54. Name = "结束",
  55. workflowId = workflowObject.Workflow.Id ,
  56. };
  57. Context.Steps.Add(endStep);
  58. Context.SaveChanges();
  59. workflowObject.Workflow.EndStepId = endStep.Id;
  60. Context.SaveChanges();
  61. t.Commit();
  62. }
  63. catch(Exception ex)
  64. {
  65. t.Rollback();
  66. ret.Success = false;
  67. ret.ErrorMessage = ex.Message;
  68. }
  69. }
  70. return ret;
  71. }
  72. public ApiSaveResponse DeleteWorkflow(int workflowId)
  73. {
  74. ApiSaveResponse ret = new ApiSaveResponse();
  75. ret.Success = true;
  76. using (var t = Context.Database.BeginTransaction())
  77. {
  78. try
  79. {
  80. var wInstanes = Context.WorkflowInstances.Where<WorkflowInstance>(w => w.workflowId == workflowId);
  81. if(wInstanes.Count() > 0)
  82. {
  83. ret.Success = false;
  84. ret.ErrorMessage = "流程已经使用,删除会影响到已有流程实例数据!";
  85. return ret;
  86. }
  87. else
  88. {
  89. var wf = Context.Workflows.FirstOrDefault(w=>w.Id == workflowId);
  90. if (wf != null)
  91. {
  92. entity.workflowDefine.Action initAction = new entity.workflowDefine.Action() { Id = wf.InitActionId};
  93. var tfs = GetTrasfers(workflowId);
  94. var Actions = GetActions(workflowId);
  95. var Steps = GetSteps(workflowId);
  96. foreach (var tf in tfs)
  97. {
  98. Context.TrasferConditions.Remove(tf);
  99. }
  100. Context.SaveChanges();
  101. foreach (var action in Actions)
  102. {
  103. var ivSettings1 = Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(
  104. s => s.actionId == action.Id).ToList();
  105. foreach(var iv in ivSettings1)
  106. {
  107. Context.InputValueSettings.Remove(iv);
  108. }
  109. if (action.Id != wf.InitActionId)
  110. {
  111. Context.Actions.Remove(action);
  112. }
  113. else
  114. {
  115. initAction = action;
  116. }
  117. }
  118. Context.SaveChanges();
  119. foreach (var step in Steps)
  120. {
  121. Context.Steps.Remove(step);
  122. }
  123. Context.SaveChanges();
  124. Context.Workflows.Remove(wf);
  125. Context.Actions.Remove(initAction);
  126. Context.SaveChanges();
  127. t.Commit();
  128. }
  129. }
  130. }
  131. catch(Exception ex)
  132. {
  133. t.Rollback();
  134. ret.Success = false;
  135. ret.ErrorMessage = ex.Message;
  136. }
  137. }
  138. return ret;
  139. }
  140. public ApiSaveResponse SaveStep(NewStepObject stepObj)
  141. {
  142. var ret = new ApiSaveResponse();
  143. ret.Success = true;
  144. using (var t = Context.Database.BeginTransaction())
  145. {
  146. try
  147. {
  148. var step = stepObj.Step;
  149. var wf = GetWorkflow(step.workflowId);
  150. if(wf == null)
  151. {
  152. throw new ApplicationException("步骤所属流程不存在!");
  153. }
  154. if (step.Id == 0)
  155. {
  156. Context.Steps.Add(step);
  157. Context.SaveChanges();
  158. if (stepObj.actions != null)
  159. {
  160. foreach (var ac in stepObj.actions)
  161. {
  162. var temAc = Context.Actions.FirstOrDefault(a => a.Id == ac.Id);
  163. if (temAc == null)
  164. {
  165. ac.StepId = step.Id;
  166. Context.Actions.Add(ac);
  167. Context.SaveChanges();
  168. }
  169. else
  170. {
  171. temAc.Name = ac.Name;
  172. temAc.InputForm = ac.InputForm;
  173. temAc.OnActionObjectType = ac.OnActionObjectType;
  174. temAc.StepId = ac.StepId;
  175. Context.SaveChanges();
  176. }
  177. if (ac.inputValuesSettings != null)
  178. {
  179. foreach (var iv in ac.inputValuesSettings)
  180. {
  181. var temIV = Context.InputValueSettings.FirstOrDefault(p => p.Id == iv.Id);
  182. if (temIV == null)
  183. {
  184. iv.actionId = temAc.Id;
  185. Context.InputValueSettings.Add(iv);
  186. Context.SaveChanges();
  187. }
  188. else
  189. {
  190. temIV.actionId = temAc.Id;
  191. temIV.bindField = iv.bindField;
  192. temIV.bindFieldSavetoObjectCondition = iv.bindFieldSavetoObjectCondition;
  193. temIV.DisplayName = iv.DisplayName;
  194. temIV.Options = iv.Options;
  195. temIV.ParentSettingId = iv.ParentSettingId;
  196. temIV.valueType = iv.valueType;
  197. Context.SaveChanges();
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. else
  205. {
  206. var temStep = Context.Steps.FirstOrDefault(a => a.Id == step.Id);
  207. if (temStep != null)
  208. {
  209. if(temStep.workflowId != step.workflowId)
  210. {
  211. throw new ApplicationException("修改步骤时,不能修改步骤所属流程!");
  212. }
  213. temStep.Name = step.Name;
  214. temStep.stepType = step.stepType;
  215. temStep.defaultResponseSetting = step.defaultResponseSetting;
  216. foreach (var ac in stepObj.actions)
  217. {
  218. var temAc = Context.Actions.FirstOrDefault(a => a.Id == ac.Id);
  219. if (temAc == null)
  220. {
  221. ac.StepId = step.Id;
  222. Context.Actions.Add(ac);
  223. Context.SaveChanges();
  224. }
  225. else
  226. {
  227. temAc.Name = ac.Name;
  228. temAc.InputForm = ac.InputForm;
  229. temAc.OnActionObjectType = ac.OnActionObjectType;
  230. temAc.StepId = ac.StepId;
  231. Context.SaveChanges();
  232. }
  233. if (ac.inputValuesSettings != null)
  234. {
  235. foreach (var iv in ac.inputValuesSettings)
  236. {
  237. var temIV = Context.InputValueSettings.FirstOrDefault(p => p.Id == iv.Id);
  238. if (temIV == null)
  239. {
  240. iv.actionId = temAc.Id;
  241. Context.InputValueSettings.Add(iv);
  242. Context.SaveChanges();
  243. }
  244. else
  245. {
  246. temIV.actionId = temAc.Id;
  247. temIV.bindField = iv.bindField;
  248. temIV.bindFieldSavetoObjectCondition = iv.bindFieldSavetoObjectCondition;
  249. temIV.DisplayName = iv.DisplayName;
  250. temIV.Options = iv.Options;
  251. temIV.ParentSettingId = iv.ParentSettingId;
  252. temIV.valueType = iv.valueType;
  253. Context.SaveChanges();
  254. }
  255. }
  256. }
  257. }
  258. }
  259. else
  260. {
  261. throw new ApplicationException("Id不存在!");
  262. }
  263. }
  264. Context.SaveChanges();
  265. t.Commit();
  266. }
  267. catch(Exception ex)
  268. {
  269. t.Rollback();
  270. ret.Success = false;
  271. ret.ErrorMessage = ex.Message;
  272. }
  273. }
  274. return ret;
  275. }
  276. public ApiSaveResponse DeleteAction(int Id)
  277. {
  278. ApiSaveResponse ret = new ApiSaveResponse();
  279. ret.Success = true;
  280. using (var t = Context.Database.BeginTransaction())
  281. {
  282. try
  283. {
  284. var action = Context.Actions.Where<entity.workflowDefine.Action>(w => w.Id == Id).FirstOrDefault();
  285. if (action != null)
  286. {
  287. var ivSettings1 = Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(
  288. s => s.actionId == action.Id).ToList();
  289. foreach (var iv in ivSettings1)
  290. {
  291. Context.InputValueSettings.Remove(iv);
  292. }
  293. Context.Actions.Remove(action);
  294. Context.SaveChanges();
  295. t.Commit();
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. t.Rollback();
  301. ret.Success = false;
  302. ret.ErrorMessage = ex.Message;
  303. }
  304. }
  305. return ret;
  306. }
  307. public ApiSaveResponse DeleteTransfer(int Id)
  308. {
  309. ApiSaveResponse ret = new ApiSaveResponse();
  310. ret.Success = true;
  311. using (var t = Context.Database.BeginTransaction())
  312. {
  313. try
  314. {
  315. Context.TrasferConditions.Remove(new entity.workflowDefine.TrasferCondition() { Id=Id });
  316. Context.SaveChanges();
  317. t.Commit();
  318. }
  319. catch (Exception ex)
  320. {
  321. t.Rollback();
  322. ret.Success = false;
  323. ret.ErrorMessage = ex.Message;
  324. }
  325. }
  326. return ret;
  327. }
  328. public ApiSaveResponse DeleteStep(int Id)
  329. {
  330. ApiSaveResponse ret = new ApiSaveResponse();
  331. ret.Success = true;
  332. using (var t = Context.Database.BeginTransaction())
  333. {
  334. try
  335. {
  336. var step = Context.Steps.FirstOrDefault(s=>s.Id == Id);
  337. if(step != null)
  338. {
  339. var Transfers = Context.TrasferConditions.Where<entity.workflowDefine.TrasferCondition>(t => t.StepId == step.Id || t.nextStepId == step.Id).ToList();
  340. if(Transfers != null)
  341. {
  342. foreach(var tf in Transfers)
  343. {
  344. Context.TrasferConditions.Remove(tf);
  345. }
  346. Context.SaveChanges();
  347. }
  348. var Actions = Context.Actions.Where<entity.workflowDefine.Action>(s => s.StepId == step.Id).ToList();
  349. if(Actions != null)
  350. {
  351. foreach(var ac in Actions)
  352. {
  353. foreach(var iv in Context.InputValueSettings.Where(p=>p.actionId== ac.Id).ToList())
  354. {
  355. Context.InputValueSettings.Remove(iv);
  356. }
  357. Context.Actions.Remove(ac);
  358. }
  359. Context.SaveChanges();
  360. }
  361. Context.Steps.Remove(step);
  362. Context.SaveChanges();
  363. }
  364. t.Commit();
  365. }
  366. catch (Exception ex)
  367. {
  368. t.Rollback();
  369. ret.Success = false;
  370. ret.ErrorMessage = ex.Message;
  371. }
  372. }
  373. return ret;
  374. }
  375. public ApiSaveResponse SaveAction(entity.workflowDefine.Action action)
  376. {
  377. var ret = new ApiSaveResponse();
  378. ret.Success = true;
  379. using (var t = Context.Database.BeginTransaction())
  380. {
  381. try
  382. {
  383. if (action.Id == 0)
  384. {
  385. Context.Actions.Add(action);
  386. }
  387. else
  388. {
  389. var temAtion = Context.Actions.FirstOrDefault(a => a.Id == action.Id);
  390. if (temAtion != null)
  391. {
  392. temAtion.InputForm = action.InputForm;
  393. temAtion.Name = action.Name;
  394. temAtion.StepId = action.StepId;
  395. temAtion.inputValuesSettings = action.inputValuesSettings;
  396. temAtion.OnActionObjectType = action.OnActionObjectType;
  397. }
  398. else
  399. {
  400. throw new ApplicationException("Id不存在!");
  401. }
  402. }
  403. Context.SaveChanges();
  404. t.Commit();
  405. }
  406. catch (Exception ex)
  407. {
  408. t.Rollback();
  409. ret.Success = false;
  410. ret.ErrorMessage = ex.Message;
  411. }
  412. }
  413. return ret;
  414. }
  415. public ApiSaveResponse SaveTransfer(entity.workflowDefine.TrasferCondition trasfer)
  416. {
  417. var ret = new ApiSaveResponse();
  418. ret.Success = true;
  419. using (var t = Context.Database.BeginTransaction())
  420. {
  421. try
  422. {
  423. if (trasfer.Id == 0)
  424. {
  425. Context.TrasferConditions.Add(trasfer);
  426. }
  427. else
  428. {
  429. var temTransfer = Context.TrasferConditions.FirstOrDefault(a => a.Id == trasfer.Id);
  430. if (temTransfer != null)
  431. {
  432. temTransfer.StepId = trasfer.StepId;
  433. temTransfer.nextStepId = trasfer.nextStepId;
  434. temTransfer.Condition = trasfer.Condition;
  435. }
  436. else
  437. {
  438. throw new ApplicationException("Id不存在!");
  439. }
  440. }
  441. Context.SaveChanges();
  442. t.Commit();
  443. }
  444. catch (Exception ex)
  445. {
  446. t.Rollback();
  447. ret.Success = false;
  448. ret.ErrorMessage = ex.Message;
  449. }
  450. }
  451. return ret;
  452. }
  453. public List<entity.workflowDefine.Workflow> GetAllWorkflows()
  454. {
  455. return Context.Workflows.Include(d => d.CreateUser).ToList();
  456. }
  457. public entity.workflowDefine.Workflow GetWorkflow(int workflowId)
  458. {
  459. return Context.Workflows.Include(d=>d.InitAction).Include(d=>d.CreateUser).FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  460. }
  461. public List<entity.workflowDefine.Step> GetSteps(int workflowId)
  462. {
  463. return Context.Steps.Where<entity.workflowDefine.Step>(w => w.workflowId == workflowId).ToList();
  464. }
  465. public List<entity.workflowDefine.Action> GetActions(int workflowId)
  466. {
  467. var workflow = Context.Workflows.FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  468. return Context.Actions.Include(s=>s.step).Where<entity.workflowDefine.Action>(d => d.step.workflowId == workflowId || d.Id == workflow.InitActionId).ToList();
  469. }
  470. public List<entity.workflowDefine.TrasferCondition> GetTrasfers(int workflowId)
  471. {
  472. return Context.TrasferConditions.Where(d => d.Step.workflowId == workflowId || d.nextStep.workflowId == workflowId).ToList();
  473. }
  474. public List<entity.workflowDefine.InputValueSetting> getInputValueSteein(int actionId)
  475. {
  476. var retList= Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(p => p.actionId == actionId).ToList();
  477. return retList;
  478. }
  479. }
  480. }