BaseStore.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Core;
  8. using InABox.Database;
  9. namespace Comal.Stores
  10. {
  11. public class UserPlatform
  12. {
  13. public string UserID { get; set; }
  14. public string Platform { get; set; }
  15. public string Version { get; set; }
  16. }
  17. public static class PlatformCache
  18. {
  19. static PlatformCache()
  20. {
  21. Platforms = new List<UserPlatform>();
  22. }
  23. public static List<UserPlatform> Platforms { get; }
  24. }
  25. public class BaseStore<T> : Store<T> where T : Entity, new()
  26. {
  27. protected override Filter<T> PrepareFilter(Filter<T> filter)
  28. {
  29. CheckPlatformVersion();
  30. return base.PrepareFilter(filter);
  31. }
  32. protected override void BeforeSave(T entity)
  33. {
  34. CheckPlatformVersion();
  35. base.BeforeSave(entity);
  36. }
  37. protected override void BeforeDelete(T entity)
  38. {
  39. CheckPlatformVersion();
  40. base.BeforeDelete(entity);
  41. }
  42. private void CheckPlatformVersion()
  43. {
  44. if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Platform) || string.IsNullOrEmpty(Version))
  45. return;
  46. var platform = PlatformCache.Platforms.FirstOrDefault(x => x.UserID.Equals(UserID) && x.Platform.Equals(Platform));
  47. if (platform == null)
  48. {
  49. platform = new UserPlatform { UserID = UserID, Platform = Platform, Version = "???" };
  50. PlatformCache.Platforms.Add(platform);
  51. }
  52. if (!platform.Version.Equals(Version))
  53. {
  54. platform.Version = Version;
  55. var prop = DatabaseSchema.Property(typeof(User), Platform);
  56. if (prop != null && prop.Getter() != null && prop.Setter() != null)
  57. {
  58. var user = Provider.Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
  59. if (user != null)
  60. {
  61. var value = prop.Getter().Invoke(user);
  62. if (!Version.Equals(value))
  63. {
  64. prop.Setter().Invoke(user, Version);
  65. Provider.Save(user);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. protected void EnsureJobMaterials(ProductLink product, JobLink job, ProductStyleLink style, StockDimensions dimensions)
  72. {
  73. // if (product.ID == Guid.Empty || job.ID == Guid.Empty)
  74. // return;
  75. //
  76. // var jobmaterials = Provider.List(
  77. // new Filter<JobMaterial>(x => x.Product.ID).IsEqualTo(product.ID)
  78. // .And(x => x.Job.ID).IsEqualTo(job.ID)
  79. // .And(x => x.Style.ID).IsEqualTo(style.ID)
  80. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(dimensions.Unit.ID)
  81. // .And(x => x.Dimensions.Length).IsEqualTo(dimensions.Length)
  82. // .And(x => x.Dimensions.Width).IsEqualTo(dimensions.Width)
  83. // .And(x => x.Dimensions.Height).IsEqualTo(dimensions.Height)
  84. // .And(x => x.Dimensions.Weight).IsEqualTo(dimensions.Weight)
  85. // ,
  86. // new Columns<JobMaterial>(x => x.ID)
  87. // );
  88. // if (!jobmaterials.Any())
  89. // {
  90. // var material = new JobMaterial();
  91. // material.Product.ID = product.ID;
  92. // material.Job.ID = job.ID;
  93. // material.Style.ID = style.ID;
  94. // material.Dimensions.CopyFrom(dimensions);
  95. // Provider.Save(material);
  96. // }
  97. }
  98. protected void CleanupJobMaterials(Guid id, ProductLink Product, JobLink Job, ProductStyleLink Style, StockDimensions Dimensions, bool delete)
  99. {
  100. // if (Product.ID == Guid.Empty || Job.ID == Guid.Empty)
  101. // return;
  102. //
  103. // var hasProduct = !delete && Product.HasOriginalValue(x=>x.ID);
  104. // var hasJob = !delete && Job.HasOriginalValue(x=>x.ID);
  105. // var hasStyle = !delete && Style.HasOriginalValue(x=>x.ID);
  106. // var hasUnit = !delete && Dimensions.HasOriginalValue(x=>x.Unit.ID);
  107. // var hasLength = !delete && Dimensions.HasOriginalValue(x => x.Length);
  108. // var hasWidth = !delete && Dimensions.HasOriginalValue(x => x.Width);
  109. // var hasHeight = !delete && Dimensions.HasOriginalValue(x => x.Height);
  110. // var hasWeight = !delete && Dimensions.HasOriginalValue(x => x.Weight);
  111. // if (delete || hasProduct || hasJob || hasStyle || hasUnit || hasLength || hasWidth || hasHeight || hasWeight)
  112. // {
  113. // var productid = hasProduct ? Product.GetOriginalValue(x=>x.ID) : Product.ID;
  114. // var jobid = hasJob ? Job.GetOriginalValue(x=>x.ID) : Job.ID;
  115. // var styleid = hasStyle ? Style.GetOriginalValue(x=>x.ID) : Style.ID;
  116. // var unitid = hasUnit ? Dimensions.GetOriginalValue(x=>x.Unit.ID) : Dimensions.Unit.ID;
  117. // var length = hasLength ? Dimensions.GetOriginalValue(x=>x.Length) : Dimensions.Length;
  118. // var width = hasWidth ? Dimensions.GetOriginalValue(x=>x.Width) : Dimensions.Width;
  119. // var height = hasHeight ? Dimensions.GetOriginalValue(x=>x.Height) : Dimensions.Height;
  120. // var weight = hasWeight ? Dimensions.GetOriginalValue(x=>x.Weight) : Dimensions.Weight;
  121. //
  122. // var bomtask = Task.Run(() =>
  123. // {
  124. // return Provider.List(
  125. // new Filter<JobBillOfMaterialsItem>(x => x.Product.ID).IsEqualTo(productid)
  126. // .And(x => x.Job.ID).IsEqualTo(jobid)
  127. // .And(x => x.Style.ID).IsEqualTo(styleid)
  128. // .And(x => x.ID).IsNotEqualTo(id)
  129. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
  130. // .And(x => x.Dimensions.Length).IsEqualTo(length)
  131. // .And(x => x.Dimensions.Width).IsEqualTo(width)
  132. // .And(x => x.Dimensions.Height).IsEqualTo(height)
  133. // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
  134. // ,
  135. // new Columns<JobBillOfMaterialsItem>(x => x.ID)
  136. // ).Any();
  137. // });
  138. //
  139. // var reqtask = Task.Run(() =>
  140. // {
  141. // return Provider.List(
  142. // new Filter<JobRequisitionItem>(x => x.Product.ID).IsEqualTo(productid)
  143. // .And(x => x.Requisition.Job.ID).IsEqualTo(jobid)
  144. // .And(x => x.Style.ID).IsEqualTo(styleid)
  145. // .And(x => x.ID).IsNotEqualTo(id)
  146. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
  147. // .And(x => x.Dimensions.Length).IsEqualTo(length)
  148. // .And(x => x.Dimensions.Width).IsEqualTo(width)
  149. // .And(x => x.Dimensions.Height).IsEqualTo(height)
  150. // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
  151. // ,
  152. // new Columns<JobRequisitionItem>(x => x.ID)
  153. // ).Any();
  154. // });
  155. //
  156. // var movetask = Task.Run(() =>
  157. // {
  158. // return Provider.List(
  159. // new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(productid)
  160. // .And(x => x.Job.ID).IsEqualTo(jobid)
  161. // .And(x => x.Style.ID).IsEqualTo(styleid)
  162. // .And(x => x.ID).IsNotEqualTo(id)
  163. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
  164. // .And(x => x.Dimensions.Length).IsEqualTo(length)
  165. // .And(x => x.Dimensions.Width).IsEqualTo(width)
  166. // .And(x => x.Dimensions.Height).IsEqualTo(height)
  167. // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
  168. // ,
  169. // new Columns<StockMovement>(x => x.ID)
  170. // ).Any();
  171. // });
  172. //
  173. // var potask = Task.Run(() =>
  174. // {
  175. // return Provider.List(
  176. // new Filter<PurchaseOrderItem>(x => x.ProductLink.ID).IsEqualTo(productid)
  177. // .And(x => x.Job.ID).IsEqualTo(jobid)
  178. // .And(x => x.StyleLink.ID).IsEqualTo(styleid)
  179. // .And(x => x.ID).IsNotEqualTo(id)
  180. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
  181. // .And(x => x.Dimensions.Length).IsEqualTo(length)
  182. // .And(x => x.Dimensions.Width).IsEqualTo(width)
  183. // .And(x => x.Dimensions.Height).IsEqualTo(height)
  184. // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
  185. // ,
  186. // new Columns<PurchaseOrderItem>(x => x.ID)
  187. // ).Any();
  188. // });
  189. //
  190. // Task.WaitAll(bomtask, reqtask, movetask, potask);
  191. //
  192. // if (bomtask.Result || reqtask.Result || movetask.Result || potask.Result)
  193. // return;
  194. //
  195. // var remove = Provider.Query(
  196. // new Filter<JobMaterial>(x => x.Product.ID).IsEqualTo(productid)
  197. // .And(x => x.Job.ID).IsEqualTo(jobid)
  198. // .And(x => x.Style.ID).IsEqualTo(styleid)
  199. // .And(x => x.Dimensions.Unit.ID).IsEqualTo(unitid)
  200. // .And(x => x.Dimensions.Length).IsEqualTo(length)
  201. // .And(x => x.Dimensions.Width).IsEqualTo(width)
  202. // .And(x => x.Dimensions.Height).IsEqualTo(height)
  203. // .And(x => x.Dimensions.Weight).IsEqualTo(weight)
  204. // ,
  205. // new Columns<JobMaterial>(x => x.ID)
  206. // ).Rows.Select(x => x.ToObject<JobMaterial>());
  207. //
  208. // Provider.Delete(remove);
  209. // }
  210. }
  211. protected void UnlinkTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity)
  212. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  213. where TEntity : Entity
  214. where TEntityLink : IEntityLink<TEntity>, new()
  215. {
  216. var kanbans = Provider.Query(
  217. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true),
  218. new Columns<TEntityKanban>(
  219. x => x.Entity.ID,
  220. x => x.Kanban.ID,
  221. x => x.Kanban.Locked
  222. )
  223. );
  224. if (!kanbans.Rows.Any())
  225. return;
  226. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  227. kanban.Locked = false;
  228. Provider.Save(kanban);
  229. }
  230. protected void UpdateTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity, Func<TEntity, KanbanCategory> category)
  231. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  232. where TEntity : Entity
  233. where TEntityLink : IEntityLink<TEntity>, new()
  234. {
  235. var kanbans = Provider.Query(
  236. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID),
  237. new Columns<TEntityKanban>(
  238. x => x.Entity.ID,
  239. x => x.Kanban.ID,
  240. x => x.Kanban.Category,
  241. x => x.Kanban.Closed,
  242. x => x.Kanban.Completed,
  243. x => x.Kanban.EmployeeLink.ID,
  244. x => x.Kanban.Notes,
  245. x => x.Kanban.DueDate,
  246. x => x.Kanban.Title,
  247. x => x.Kanban.Summary
  248. )
  249. );
  250. if (!kanbans.Rows.Any())
  251. return;
  252. var oldcategory = Kanban.StringToCategory(kanbans.Rows.First().Get<TEntityKanban, string>(c => c.Kanban.Category));
  253. var newcategory = category(entity);
  254. if (!Equals(newcategory, oldcategory))
  255. {
  256. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  257. kanban.Category = Kanban.CategoryToString(newcategory);
  258. var notes = kanban.Notes.ToList();
  259. notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress"));
  260. kanban.Notes = notes.ToArray();
  261. kanban.Locked = true;
  262. FindSubStore<Kanban>().Save(kanban, "Updated due to Requisition Status Change");
  263. }
  264. }
  265. protected void NotifyEmployee<TEntity>(
  266. TEntity entity,
  267. Func<TEntity, Guid> employeeid,
  268. Func<TEntity, bool> filter,
  269. Func<TEntity, string> title,
  270. Func<TEntity, string> body
  271. ) where TEntity : Entity
  272. {
  273. if (!filter(entity))
  274. return;
  275. var employees = Provider.Query(
  276. new Filter<Employee>(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid),
  277. new Columns<Employee>(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name)
  278. ).Rows.Select(r => new Tuple<Guid, Guid, string>(
  279. r.Get<Employee, Guid>(c => c.ID),
  280. r.Get<Employee, Guid>(c => c.UserLink.ID),
  281. r.Get<Employee, string>(c => c.Name)
  282. )
  283. );
  284. var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity)));
  285. var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid));
  286. if (recipient == null || sender == null)
  287. return;
  288. if (recipient.Item2 == UserGuid)
  289. return;
  290. var notification = new Notification();
  291. notification.Employee.ID = recipient.Item1;
  292. notification.Sender.ID = sender.Item1;
  293. notification.Title = title(entity);
  294. var sb = new StringBuilder();
  295. sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First()));
  296. sb.Append(body.Invoke(entity));
  297. if (sender != null)
  298. sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First()));
  299. notification.Description = sb.ToString();
  300. notification.EntityType = typeof(TEntity).EntityName();
  301. notification.EntityID = entity.ID;
  302. FindSubStore<Notification>().Save(notification, "");
  303. }
  304. }
  305. }