PageRenderTime 337ms CodeModel.GetById 30ms RepoModel.GetById 3ms app.codeStats 0ms

/Automation.BusinessNew/FlowEngine.cs

https://gitlab.com/justbediego/Isaap
C# | 248 lines | 246 code | 1 blank | 1 comment | 41 complexity | 3e4a043d2b3338c10d1238f96bd4938e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Automation.DataAccess;
  7. using Automation.Model;
  8. using System.Data.Entity;
  9. using Automation.Model.Exception;
  10. namespace Automation.BusinessNew
  11. {
  12. public class FlowEngine
  13. {
  14. AutomationContext dbContext;
  15. ValidationBusiness validation;
  16. public FlowEngine(AutomationContext dbContext, ValidationBusiness validation)
  17. {
  18. this.dbContext = dbContext;
  19. this.validation = validation;
  20. }
  21. public Flow SendUpToProvinceInspector(int documentId, string description)
  22. {
  23. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Province.ProvinceInspectors).FirstOrDefault();
  24. Province province = document.WorkContact.County.Province;
  25. if (province.ProvinceInspectors.Count(pi => !pi.IsDeactivated) > 0)
  26. {
  27. Flow flow = new Flow()
  28. {
  29. Type = Flow.FlowType.SentToProvinceInspector,
  30. Staff = province.ProvinceInspectors.Single(),
  31. Description = description,
  32. };
  33. return flow;
  34. }
  35. else
  36. throw new InternalException("عدم پیدا کردن مقصد");
  37. }
  38. public Flow SendUpToCountyInspector(int documentId, string description)
  39. {
  40. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.CountyInspectors).FirstOrDefault();
  41. County county = document.WorkContact.County;
  42. if (county.CountyInspectors.Count(ci => !ci.IsDeactivated) > 0)
  43. {
  44. Flow flow = new Flow()
  45. {
  46. Type = Flow.FlowType.SentToCountyInspector,
  47. Staff = county.CountyInspectors.Single(),
  48. Description = description,
  49. };
  50. return flow;
  51. }
  52. else
  53. return SendUpToProvinceInspector(documentId, description);
  54. }
  55. public Flow SendUpToClub(int documentId, string description)
  56. {
  57. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Province.Clubs).FirstOrDefault();
  58. Province province = document.WorkContact.County.Province;
  59. if (province.Clubs.Count(cl => !cl.IsDeactivated) > 0)
  60. {
  61. Flow flow = new Flow()
  62. {
  63. Type = Flow.FlowType.SentToClub,
  64. Staff = province.Clubs.Single(),
  65. Description = description,
  66. };
  67. return flow;
  68. }
  69. else
  70. return SendUpToCountyInspector(documentId, description);
  71. }
  72. public Flow SendUpToAssociation(int documentId, string description)
  73. {
  74. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Associations).FirstOrDefault();
  75. County county = document.WorkContact.County;
  76. if (county.Associations.Count(a => !a.IsDeactivated) > 0)
  77. {
  78. Flow flow = new Flow()
  79. {
  80. Type = Flow.FlowType.SentToAssociation,
  81. Staff = county.Associations.Single(),
  82. Description = description,
  83. };
  84. return flow;
  85. }
  86. else
  87. return SendUpToClub(documentId, description);
  88. }
  89. public Flow SendDownToExpert(int documentId, string description)
  90. {
  91. Flow flow = new Flow()
  92. {
  93. Type = Flow.FlowType.NotCompleted,
  94. Description = description,
  95. };
  96. return flow;
  97. }
  98. public Flow SendDownToAssociation(int documentId, string description)
  99. {
  100. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Associations).FirstOrDefault();
  101. County county = document.WorkContact.County;
  102. if (county.Associations.Count(a => !a.IsDeactivated) > 0)
  103. {
  104. Flow flow = new Flow()
  105. {
  106. Type = Flow.FlowType.SentToAssociation,
  107. Staff = county.Associations.Single(),
  108. Description = description,
  109. };
  110. return flow;
  111. }
  112. else
  113. return SendDownToExpert(documentId, description);
  114. }
  115. public Flow SendDownToClub(int documentId, string description)
  116. {
  117. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Province.Clubs).FirstOrDefault();
  118. Province province = document.WorkContact.County.Province;
  119. if (province.Clubs.Count(cl => !cl.IsDeactivated) > 0)
  120. {
  121. Flow flow = new Flow()
  122. {
  123. Type = Flow.FlowType.SentToClub,
  124. Staff = province.Clubs.Single(),
  125. Description = description,
  126. };
  127. return flow;
  128. }
  129. else
  130. return SendDownToAssociation(documentId, description);
  131. }
  132. public Flow SendDownToCountyInspector(int documentId, string description)
  133. {
  134. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.CountyInspectors).FirstOrDefault();
  135. County county = document.WorkContact.County;
  136. if (county.CountyInspectors.Count(ci => !ci.IsDeactivated) > 0)
  137. {
  138. Flow flow = new Flow()
  139. {
  140. Type = Flow.FlowType.SentToCountyInspector,
  141. Staff = county.CountyInspectors.Single(),
  142. Description = description,
  143. };
  144. return flow;
  145. }
  146. else
  147. return SendDownToClub(documentId, description);
  148. }
  149. public Flow AuthorizeDocument(int documentId, string description)
  150. {
  151. Random rnd = new Random();
  152. string nezamCode = string.Empty;
  153. string nezamSerialCode = string.Empty;
  154. Document document = dbContext.Documents.Where(d => d.Id == documentId)
  155. .Include(d => d.WorkContact.County.Province)
  156. .Include(d => d.PhDCertification.FieldOfStudy)
  157. .Include(d => d.MastersCertification.FieldOfStudy)
  158. .Include(d => d.BachelorsCertification.FieldOfStudy)
  159. .Include(d => d.DiplomaCertification.FieldOfStudy)
  160. .Include(d => d.HighSchoolCertification.FieldOfStudy)
  161. .FirstOrDefault();
  162. var otherDocuments = dbContext.Documents.Where(d => d.NationalCode == document.NationalCode && d.Id != document.Id && d.NezamCode != null).ToList();
  163. if (otherDocuments.Count == 0)
  164. {
  165. nezamCode = document.WorkContact.County.Province.Code;
  166. if (document.PhDCertification != null)
  167. nezamCode += document.PhDCertification.FieldOfStudy.Code + document.PhDCertification.FieldOfStudy.OrginalityCode;
  168. else if (document.MastersCertification != null)
  169. nezamCode += document.MastersCertification.FieldOfStudy.Code + document.MastersCertification.FieldOfStudy.OrginalityCode;
  170. else if (document.BachelorsCertification != null)
  171. nezamCode += document.BachelorsCertification.FieldOfStudy.Code + document.BachelorsCertification.FieldOfStudy.OrginalityCode;
  172. else if (document.DiplomaCertification != null)
  173. nezamCode += document.DiplomaCertification.FieldOfStudy.Code + document.DiplomaCertification.FieldOfStudy.OrginalityCode;
  174. else if (document.HighSchoolCertification != null)
  175. nezamCode += document.HighSchoolCertification.FieldOfStudy.Code + document.HighSchoolCertification.FieldOfStudy.OrginalityCode;
  176. else
  177. throw new NotFoundException("کد رشته");
  178. do
  179. {
  180. nezamSerialCode = string.Empty;
  181. for (int i = 0; i < 6; i++)
  182. nezamSerialCode += rnd.Next(0, 10).ToString();
  183. } while (dbContext.Documents.Where(d => d.NezamCode == nezamCode + nezamSerialCode).Count() != 0);
  184. document.NezamCode = nezamCode + nezamSerialCode;
  185. }
  186. else
  187. {
  188. //has registered before
  189. document.NezamCode = otherDocuments.First().NezamCode;
  190. }
  191. Flow flow = new Flow()
  192. {
  193. Type = Flow.FlowType.Authorized,
  194. Description = description,
  195. };
  196. return flow;
  197. }
  198. public Flow RejectOnce(int documentId, string description)
  199. {
  200. Flow flow = new Flow()
  201. {
  202. Type = Flow.FlowType.RejectedOnce,
  203. Description = description,
  204. };
  205. return flow;
  206. }
  207. public Flow RejectTwice(int documentId, string description)
  208. {
  209. Flow flow = new Flow()
  210. {
  211. Type = Flow.FlowType.RejectedTwice,
  212. Description = description,
  213. };
  214. return flow;
  215. }
  216. public Flow RejectCompletely(int documentId, string description)
  217. {
  218. Flow flow = new Flow()
  219. {
  220. Type = Flow.FlowType.NotCompleted,
  221. Description = description,
  222. };
  223. return flow;
  224. }
  225. public Flow ObjectifyOnce(int documentId, string description)
  226. {
  227. Document document = dbContext.Documents.Where(d => d.Id == documentId).Include(d => d.WorkContact.County.Province.ProvinceManagers).FirstOrDefault();
  228. Flow flow = new Flow()
  229. {
  230. Type = Flow.FlowType.SentToProvinceManager,
  231. Staff = document.WorkContact.County.Province.ProvinceManagers.Single(),
  232. Description = description,
  233. };
  234. return flow;
  235. }
  236. public Flow ObjectifyTwice(int documentId, string description)
  237. {
  238. Flow flow = new Flow()
  239. {
  240. Type = Flow.FlowType.SentToInspectionOffice,
  241. Staff = dbContext.InspectionOffices.Single(),
  242. Description = description,
  243. };
  244. return flow;
  245. }
  246. }
  247. }