WorkflowEngineController.cs 29 KB

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