WorkflowEngineController.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.StaticFiles;
  5. using Microsoft.EntityFrameworkCore;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Drawing.Imaging;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using wispro.sp.entity;
  14. using wispro.sp.entity.workflowInstance;
  15. using wispro.sp.share;
  16. using wispro.sp.share.Utility;
  17. namespace wispro.sp.api.Controllers
  18. {
  19. [Route("api/[controller]/[action]")]
  20. [ApiController]
  21. [Authorize]
  22. public class WorkflowEngineController : ControllerBase
  23. {
  24. spDbContext Context;
  25. public WorkflowEngineController(spDbContext context)
  26. {
  27. Context = context;
  28. }
  29. public void DoAction(int instanceId, int stepId, int ActionId, List<InputValue> inputValues)
  30. {
  31. //1、取实例对象
  32. //2、取步骤对象
  33. //3、判断Action定义对象
  34. //4、判断输入值合法性
  35. //5、保存数据
  36. }
  37. [HttpPost]
  38. public ApiSaveResponse AddNew(NewWorkflowObject workflowObject)
  39. {
  40. ApiSaveResponse ret = new ApiSaveResponse();
  41. ret.Success = true;
  42. using (var t = Context.Database.BeginTransaction())
  43. {
  44. try
  45. {
  46. if (workflowObject.InitAction != null)
  47. {
  48. Context.Actions.Add(workflowObject.InitAction.Action);
  49. Context.SaveChanges();
  50. foreach (var iv in workflowObject.InitAction.InputValueSettingObjects)
  51. {
  52. iv.InputValueSetting.actionId = workflowObject.InitAction.Action.Id;
  53. Context.InputValueSettings.Add(iv.InputValueSetting);
  54. Context.SaveChanges();
  55. if (iv.Children != null)
  56. {
  57. foreach (var childIV in iv.Children)
  58. {
  59. childIV.actionId = workflowObject.InitAction.Action.Id;
  60. childIV.ParentSettingId = iv.InputValueSetting.Id;
  61. Context.InputValueSettings.Add(childIV);
  62. }
  63. Context.SaveChanges();
  64. }
  65. }
  66. }
  67. else
  68. {
  69. throw new ApplicationException("没有设定初始化Action!");
  70. }
  71. workflowObject.Workflow.InitActionId = workflowObject.InitAction.Action.Id;
  72. workflowObject.Workflow.CreateTime = DateTime.Now;
  73. workflowObject.Workflow.CreateUserId = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == User.Identity.Name).Id;
  74. Context.Workflows.Add(workflowObject.Workflow);
  75. Context.SaveChanges();
  76. entity.workflowDefine.Step endStep = new entity.workflowDefine.Step()
  77. {
  78. Name = "结束",
  79. workflowId = workflowObject.Workflow.Id,
  80. };
  81. Context.Steps.Add(endStep);
  82. Context.SaveChanges();
  83. workflowObject.Workflow.EndStepId = endStep.Id;
  84. Context.SaveChanges();
  85. t.Commit();
  86. }
  87. catch (Exception ex)
  88. {
  89. t.Rollback();
  90. ret.Success = false;
  91. ret.ErrorMessage = ex.Message;
  92. }
  93. }
  94. return ret;
  95. }
  96. public ApiSaveResponse DeleteWorkflow(int workflowId)
  97. {
  98. ApiSaveResponse ret = new ApiSaveResponse();
  99. ret.Success = true;
  100. using (var t = Context.Database.BeginTransaction())
  101. {
  102. try
  103. {
  104. var wInstanes = Context.WorkflowInstances.Where<WorkflowInstance>(w => w.workflowId == workflowId);
  105. if (wInstanes.Count() > 0)
  106. {
  107. ret.Success = false;
  108. ret.ErrorMessage = "流程已经使用,删除会影响到已有流程实例数据!";
  109. return ret;
  110. }
  111. else
  112. {
  113. var wf = Context.Workflows.FirstOrDefault(w => w.Id == workflowId);
  114. if (wf != null)
  115. {
  116. entity.workflowDefine.Action initAction = new entity.workflowDefine.Action() { Id = wf.InitActionId };
  117. var tfs = GetTrasfers(workflowId);
  118. var Actions = GetActions(workflowId);
  119. var Steps = GetSteps(workflowId);
  120. foreach (var tf in tfs)
  121. {
  122. Context.TrasferConditions.Remove(tf);
  123. }
  124. Context.SaveChanges();
  125. foreach (var action in Actions)
  126. {
  127. var ivSettings1 = Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(
  128. s => s.actionId == action.Id).ToList();
  129. foreach (var iv in ivSettings1)
  130. {
  131. Context.InputValueSettings.Remove(iv);
  132. }
  133. if (action.Id != wf.InitActionId)
  134. {
  135. Context.Actions.Remove(action);
  136. }
  137. else
  138. {
  139. initAction = action;
  140. }
  141. }
  142. Context.SaveChanges();
  143. foreach (var step in Steps)
  144. {
  145. Context.Steps.Remove(step);
  146. }
  147. Context.SaveChanges();
  148. Context.Workflows.Remove(wf);
  149. Context.Actions.Remove(initAction);
  150. Context.SaveChanges();
  151. t.Commit();
  152. }
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. t.Rollback();
  158. ret.Success = false;
  159. ret.ErrorMessage = ex.Message;
  160. }
  161. }
  162. return ret;
  163. }
  164. public ApiSaveResponse SaveStep(NewStepObject stepObj)
  165. {
  166. var ret = new ApiSaveResponse();
  167. ret.Success = true;
  168. using (var t = Context.Database.BeginTransaction())
  169. {
  170. try
  171. {
  172. var step = stepObj.Step;
  173. var wf = GetWorkflow(step.workflowId);
  174. if (wf == null)
  175. {
  176. throw new ApplicationException("步骤所属流程不存在!");
  177. }
  178. if (step.Id == 0)
  179. {
  180. Context.Steps.Add(step);
  181. Context.SaveChanges();
  182. if (stepObj.NewActions != null)
  183. {
  184. foreach (var ac in stepObj.NewActions)
  185. {
  186. ac.Action.StepId = step.Id;
  187. Context.Actions.Add(ac.Action);
  188. Context.SaveChanges();
  189. if (ac.NewInputValueSettingObjects != null)
  190. {
  191. foreach (var iv in ac.NewInputValueSettingObjects)
  192. {
  193. iv.InputValueSetting.actionId = ac.Action.Id;
  194. Context.InputValueSettings.Add(iv.InputValueSetting);
  195. Context.SaveChanges();
  196. if (iv.InputValueSetting.valueType == entity.workflowDefine.EnumFieldType.List)
  197. {
  198. if (iv.Children != null)
  199. {
  200. foreach (var cIV in iv.Children)
  201. {
  202. cIV.ParentSettingId = iv.InputValueSetting.Id;
  203. Context.InputValueSettings.Add(cIV);
  204. }
  205. Context.SaveChanges();
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. else
  214. {
  215. var temStep = Context.Steps.FirstOrDefault(a => a.Id == step.Id);
  216. if (temStep != null)
  217. {
  218. if (temStep.workflowId != step.workflowId)
  219. {
  220. throw new ApplicationException("修改步骤时,不能修改步骤所属流程!");
  221. }
  222. temStep.Name = step.Name;
  223. temStep.stepType = step.stepType;
  224. temStep.defaultResponseSetting = step.defaultResponseSetting;
  225. Context.SaveChanges();
  226. if (stepObj.NewActions != null)
  227. {
  228. foreach (var ac in stepObj.NewActions)
  229. {
  230. ac.Action.StepId = temStep.Id;
  231. Context.Actions.Add(ac.Action);
  232. Context.SaveChanges();
  233. if (ac.NewInputValueSettingObjects != null)
  234. {
  235. foreach (var iv in ac.NewInputValueSettingObjects)
  236. {
  237. iv.InputValueSetting.actionId = ac.Action.Id;
  238. Context.InputValueSettings.Add(iv.InputValueSetting);
  239. Context.SaveChanges();
  240. if (iv.InputValueSetting.valueType == entity.workflowDefine.EnumFieldType.List)
  241. {
  242. if (iv.Children != null)
  243. {
  244. foreach (var cIV in iv.Children)
  245. {
  246. cIV.ParentSettingId = iv.InputValueSetting.Id;
  247. Context.InputValueSettings.Add(cIV);
  248. }
  249. Context.SaveChanges();
  250. }
  251. }
  252. }
  253. }
  254. Context.SaveChanges();
  255. }
  256. }
  257. if (stepObj.ModifyActions != null)
  258. {
  259. foreach (var ac in stepObj.ModifyActions)
  260. {
  261. var temAction = Context.Actions.FirstOrDefault(a => a.Id == ac.Action.Id);
  262. if (temAction != null)
  263. {
  264. temAction.Name = ac.Action.Name;
  265. temAction.InputForm = ac.Action.InputForm;
  266. //temAction.inputValuesSettings = ac.Action.inputValuesSettings;
  267. temAction.OnActionObjectType = ac.Action.OnActionObjectType;
  268. Context.SaveChanges();
  269. if (ac.NewInputValueSettingObjects != null)
  270. {
  271. foreach (var iv in ac.NewInputValueSettingObjects)
  272. {
  273. iv.InputValueSetting.actionId = ac.Action.Id;
  274. Context.InputValueSettings.Add(iv.InputValueSetting);
  275. Context.SaveChanges();
  276. if (iv.InputValueSetting.valueType == entity.workflowDefine.EnumFieldType.List)
  277. {
  278. if (iv.Children != null)
  279. {
  280. foreach (var cIV in iv.Children)
  281. {
  282. cIV.ParentSettingId = iv.InputValueSetting.Id;
  283. Context.InputValueSettings.Add(cIV);
  284. }
  285. Context.SaveChanges();
  286. }
  287. }
  288. }
  289. Context.SaveChanges();
  290. }
  291. if (ac.UpdateInputValueSettingObjects != null)
  292. {
  293. foreach (var iv in ac.UpdateInputValueSettingObjects)
  294. {
  295. var temIvObj = Context.InputValueSettings.FirstOrDefault(s => s.Id == iv.InputValueSetting.Id);
  296. if (temIvObj != null)
  297. {
  298. temIvObj.actionId = iv.InputValueSetting.actionId;
  299. temIvObj.bindField = iv.InputValueSetting.bindField;
  300. temIvObj.bindFieldSavetoObjectCondition = iv.InputValueSetting.bindFieldSavetoObjectCondition;
  301. temIvObj.DisplayName = iv.InputValueSetting.DisplayName;
  302. temIvObj.Options = iv.InputValueSetting.Options;
  303. Context.SaveChanges();
  304. }
  305. }
  306. Context.SaveChanges();
  307. }
  308. }
  309. }
  310. }
  311. }
  312. else
  313. {
  314. throw new ApplicationException("Id不存在!");
  315. }
  316. }
  317. Context.SaveChanges();
  318. t.Commit();
  319. }
  320. catch (Exception ex)
  321. {
  322. t.Rollback();
  323. ret.Success = false;
  324. ret.ErrorMessage = ex.Message;
  325. }
  326. }
  327. return ret;
  328. }
  329. public ApiSaveResponse DeleteAction(int Id)
  330. {
  331. ApiSaveResponse ret = new ApiSaveResponse();
  332. ret.Success = true;
  333. using (var t = Context.Database.BeginTransaction())
  334. {
  335. try
  336. {
  337. var action = Context.Actions.Where<entity.workflowDefine.Action>(w => w.Id == Id).FirstOrDefault();
  338. if (action != null)
  339. {
  340. var ivSettings1 = Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(
  341. s => s.actionId == action.Id).ToList();
  342. foreach (var iv in ivSettings1)
  343. {
  344. Context.InputValueSettings.Remove(iv);
  345. }
  346. Context.Actions.Remove(action);
  347. Context.SaveChanges();
  348. t.Commit();
  349. }
  350. }
  351. catch (Exception ex)
  352. {
  353. t.Rollback();
  354. ret.Success = false;
  355. ret.ErrorMessage = ex.Message;
  356. }
  357. }
  358. return ret;
  359. }
  360. public ApiSaveResponse DeleteTransfer(int Id)
  361. {
  362. ApiSaveResponse ret = new ApiSaveResponse();
  363. ret.Success = true;
  364. using (var t = Context.Database.BeginTransaction())
  365. {
  366. try
  367. {
  368. Context.TrasferConditions.Remove(new entity.workflowDefine.TrasferCondition() { Id = Id });
  369. Context.SaveChanges();
  370. t.Commit();
  371. }
  372. catch (Exception ex)
  373. {
  374. t.Rollback();
  375. ret.Success = false;
  376. ret.ErrorMessage = ex.Message;
  377. }
  378. }
  379. return ret;
  380. }
  381. public ApiSaveResponse DeleteInputValueSetting(int Id)
  382. {
  383. ApiSaveResponse ret = new ApiSaveResponse();
  384. ret.Success = true;
  385. using (var t = Context.Database.BeginTransaction())
  386. {
  387. try
  388. {
  389. Context.InputValueSettings.Remove(new entity.workflowDefine.InputValueSetting() { Id = Id });
  390. Context.SaveChanges();
  391. t.Commit();
  392. }
  393. catch (Exception ex)
  394. {
  395. t.Rollback();
  396. ret.Success = false;
  397. ret.ErrorMessage = ex.Message;
  398. }
  399. }
  400. return ret;
  401. }
  402. public ApiSaveResponse DeleteStep(int Id)
  403. {
  404. ApiSaveResponse ret = new ApiSaveResponse();
  405. ret.Success = true;
  406. using (var t = Context.Database.BeginTransaction())
  407. {
  408. try
  409. {
  410. var step = Context.Steps.FirstOrDefault(s => s.Id == Id);
  411. if (step != null)
  412. {
  413. var Transfers = Context.TrasferConditions.Where<entity.workflowDefine.TrasferCondition>(t => t.StepId == step.Id || t.nextStepId == step.Id).ToList();
  414. if (Transfers != null)
  415. {
  416. foreach (var tf in Transfers)
  417. {
  418. Context.TrasferConditions.Remove(tf);
  419. }
  420. Context.SaveChanges();
  421. }
  422. var Actions = Context.Actions.Where<entity.workflowDefine.Action>(s => s.StepId == step.Id).ToList();
  423. if (Actions != null)
  424. {
  425. foreach (var ac in Actions)
  426. {
  427. foreach (var iv in Context.InputValueSettings.Where(p => p.actionId == ac.Id).ToList())
  428. {
  429. Context.InputValueSettings.Remove(iv);
  430. }
  431. Context.Actions.Remove(ac);
  432. }
  433. Context.SaveChanges();
  434. }
  435. Context.Steps.Remove(step);
  436. Context.SaveChanges();
  437. }
  438. t.Commit();
  439. }
  440. catch (Exception ex)
  441. {
  442. t.Rollback();
  443. ret.Success = false;
  444. ret.ErrorMessage = ex.Message;
  445. }
  446. }
  447. return ret;
  448. }
  449. public ApiSaveResponse SaveAction(entity.workflowDefine.Action action)
  450. {
  451. var ret = new ApiSaveResponse();
  452. ret.Success = true;
  453. using (var t = Context.Database.BeginTransaction())
  454. {
  455. try
  456. {
  457. var temInputValueSettings = action.inputValuesSettings;
  458. action.inputValuesSettings = null;
  459. if (action.Id == 0)
  460. {
  461. Context.Actions.Add(action);
  462. Context.SaveChanges();
  463. if (temInputValueSettings != null)
  464. {
  465. foreach (var iv in temInputValueSettings)
  466. {
  467. iv.actionId = action.Id;
  468. Context.InputValueSettings.Add(iv);
  469. }
  470. Context.SaveChanges();
  471. }
  472. }
  473. else
  474. {
  475. var temAtion = Context.Actions.FirstOrDefault(a => a.Id == action.Id);
  476. if (temAtion != null)
  477. {
  478. temAtion.InputForm = action.InputForm;
  479. temAtion.Name = action.Name;
  480. temAtion.StepId = action.StepId;
  481. temAtion.inputValuesSettings = action.inputValuesSettings;
  482. temAtion.OnActionObjectType = action.OnActionObjectType;
  483. if (temInputValueSettings != null)
  484. {
  485. foreach (var iv in temInputValueSettings)
  486. {
  487. var temIV = Context.InputValueSettings.FirstOrDefault(p => p.Id == iv.Id);
  488. if (temIV != null)
  489. {
  490. temIV.actionId = iv.actionId;
  491. temIV.bindField = iv.bindField;
  492. temIV.bindFieldSavetoObjectCondition = iv.bindFieldSavetoObjectCondition;
  493. temIV.DisplayName = iv.DisplayName;
  494. temIV.Options = iv.Options;
  495. temIV.ParentSettingId = iv.ParentSettingId;
  496. }
  497. else
  498. {
  499. iv.actionId = action.Id;
  500. Context.InputValueSettings.Add(iv);
  501. }
  502. }
  503. Context.SaveChanges();
  504. }
  505. }
  506. else
  507. {
  508. throw new ApplicationException("Id不存在!");
  509. }
  510. }
  511. Context.SaveChanges();
  512. t.Commit();
  513. }
  514. catch (Exception ex)
  515. {
  516. t.Rollback();
  517. ret.Success = false;
  518. ret.ErrorMessage = ex.Message;
  519. }
  520. }
  521. return ret;
  522. }
  523. public ApiSaveResponse SaveTransfer(entity.workflowDefine.TrasferCondition trasfer)
  524. {
  525. var ret = new ApiSaveResponse();
  526. ret.Success = true;
  527. using (var t = Context.Database.BeginTransaction())
  528. {
  529. try
  530. {
  531. if (trasfer.Id == 0)
  532. {
  533. Context.TrasferConditions.Add(trasfer);
  534. }
  535. else
  536. {
  537. var temTransfer = Context.TrasferConditions.FirstOrDefault(a => a.Id == trasfer.Id);
  538. if (temTransfer != null)
  539. {
  540. temTransfer.StepId = trasfer.StepId;
  541. temTransfer.nextStepId = trasfer.nextStepId;
  542. temTransfer.Condition = trasfer.Condition;
  543. }
  544. else
  545. {
  546. throw new ApplicationException("Id不存在!");
  547. }
  548. }
  549. Context.SaveChanges();
  550. t.Commit();
  551. }
  552. catch (Exception ex)
  553. {
  554. t.Rollback();
  555. ret.Success = false;
  556. ret.ErrorMessage = ex.Message;
  557. }
  558. }
  559. return ret;
  560. }
  561. public List<entity.workflowDefine.Workflow> GetAllWorkflows()
  562. {
  563. return Context.Workflows.Include(d => d.CreateUser).ToList();
  564. }
  565. public entity.workflowDefine.Workflow GetWorkflow(int workflowId)
  566. {
  567. return Context.Workflows.Include(d => d.InitAction).Include(d => d.CreateUser).FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  568. }
  569. public List<entity.workflowDefine.Step> GetSteps(int workflowId)
  570. {
  571. return Context.Steps.Where<entity.workflowDefine.Step>(w => w.workflowId == workflowId).ToList();
  572. }
  573. public List<entity.workflowDefine.Action> GetActions(int workflowId)
  574. {
  575. var workflow = Context.Workflows.FirstOrDefault<entity.workflowDefine.Workflow>(w => w.Id == workflowId);
  576. return Context.Actions.Include(s => s.step).Where<entity.workflowDefine.Action>(d => d.step.workflowId == workflowId || d.Id == workflow.InitActionId).ToList();
  577. }
  578. public List<entity.workflowDefine.TrasferCondition> GetTrasfers(int workflowId)
  579. {
  580. return Context.TrasferConditions.Where(d => d.Step.workflowId == workflowId || d.nextStep.workflowId == workflowId).ToList();
  581. }
  582. public List<entity.workflowDefine.InputValueSetting> getInputValueSteein(int actionId)
  583. {
  584. var retList = Context.InputValueSettings.Where<entity.workflowDefine.InputValueSetting>(p => p.actionId == actionId).ToList();
  585. return retList;
  586. }
  587. [HttpGet, DisableRequestSizeLimit]
  588. public async Task<IActionResult> ExportToImage(int workflowId)
  589. {
  590. var workflow = GetWorkflow(workflowId);
  591. var Steps = GetSteps(workflowId);
  592. var Actions = GetActions(workflowId);
  593. var Transfers = GetTrasfers(workflowId);
  594. FlowChartUtility flowChart = new FlowChartUtility();
  595. flowChart.workflow = workflow;
  596. flowChart.Steps = Steps;
  597. flowChart.Transfers = Transfers;
  598. string strSvg = flowChart.GetSvgString();
  599. System.Xml.XmlDocument xmdoc = new System.Xml.XmlDocument();
  600. xmdoc.LoadXml(strSvg);
  601. Svg.SvgDocument svg = Svg.SvgDocument.Open(xmdoc);
  602. Bitmap bitmap = svg.Draw();
  603. MemoryStream mStream = new MemoryStream();
  604. bitmap.Save(mStream, ImageFormat.Jpeg);
  605. var filename = $"{workflow.Name}.jpg";
  606. mStream.Position = 0;
  607. return File(mStream, GetContentType(filename), filename);
  608. }
  609. private string GetContentType(string path)
  610. {
  611. var provider = new FileExtensionContentTypeProvider();
  612. string contentType;
  613. if (!provider.TryGetContentType(path, out contentType))
  614. {
  615. contentType = "application/octet-stream";
  616. }
  617. return contentType;
  618. }
  619. public entity.workflowDefine.InputValueSetting getInputValueSteeingById(int Id)
  620. {
  621. return Context.InputValueSettings.FirstOrDefault(s => s.Id == Id);
  622. }
  623. }
  624. }