WorkflowEngineController.cs 29 KB

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