BaseStore.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.Database;
  8. using System;
  9. namespace Comal.Stores
  10. {
  11. public class UserPlatform
  12. {
  13. public string UserID { get; set; }
  14. public Platform 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(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. if(Platform == Platform.Wpf || Platform == Platform.TimeBench)
  56. {
  57. var user = Provider.Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
  58. if(user is not null)
  59. {
  60. var current = Platform switch
  61. {
  62. Platform.Wpf => user.Platform.DesktopVersion,
  63. Platform.TimeBench => user.Platform.MobileVersion,
  64. _ => ""
  65. };
  66. if (!Version.Equals(current))
  67. {
  68. switch (Platform)
  69. {
  70. case Platform.Wpf:
  71. user.Platform.DesktopVersion = Version;
  72. break;
  73. case Platform.TimeBench:
  74. user.Platform.MobileVersion = Version;
  75. break;
  76. }
  77. Provider.Save(user);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. protected void UnlinkTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity)
  84. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  85. where TEntity : Entity
  86. where TEntityLink : IEntityLink<TEntity>, new()
  87. {
  88. var kanbans = Provider.Query(
  89. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true),
  90. Columns.Required<TEntityKanban>().Add(
  91. x => x.Entity.ID,
  92. x => x.Kanban.ID,
  93. x => x.Kanban.Locked
  94. )
  95. );
  96. if (!kanbans.Rows.Any())
  97. return;
  98. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  99. kanban.Locked = false;
  100. Provider.Save(kanban);
  101. }
  102. protected void UpdateTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity, Func<TEntity, KanbanStatus> status)
  103. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  104. where TEntity : Entity
  105. where TEntityLink : IEntityLink<TEntity>, new()
  106. {
  107. var kanbans = Provider.Query(
  108. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID),
  109. Columns.Required<TEntityKanban>().Add(x => x.Entity.ID, x => x.Kanban.ID, x => x.Kanban.Status, x => x.Kanban.Closed, x => x.Kanban.Completed, x => x.Kanban.EmployeeLink.ID, x => x.Kanban.Notes, x => x.Kanban.DueDate, x => x.Kanban.Title, x => x.Kanban.Summary)
  110. );
  111. if (!kanbans.Rows.Any())
  112. return;
  113. var oldcategory = kanbans.Rows.First().Get<TEntityKanban, KanbanStatus>(c => c.Kanban.Status);
  114. var newcategory = status(entity);
  115. if (!Equals(newcategory, oldcategory))
  116. {
  117. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  118. kanban.Status = newcategory;
  119. var notes = kanban.Notes.ToList();
  120. notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress"));
  121. kanban.Notes = notes.ToArray();
  122. kanban.Locked = true;
  123. FindSubStore<Kanban>().Save(kanban, "Updated due to Requisition Status Change");
  124. }
  125. }
  126. protected void NotifyEmployee<TEntity>(
  127. TEntity entity,
  128. Func<TEntity, Guid> employeeid,
  129. Func<TEntity, bool> filter,
  130. Func<TEntity, string> title,
  131. Func<TEntity, string> body
  132. ) where TEntity : Entity
  133. {
  134. if (!filter(entity))
  135. return;
  136. var employees = Provider.Query(
  137. new Filter<Employee>(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid),
  138. Columns.None<Employee>().Add(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name)
  139. ).Rows.Select(r => new Tuple<Guid, Guid, string>(
  140. r.Get<Employee, Guid>(c => c.ID),
  141. r.Get<Employee, Guid>(c => c.UserLink.ID),
  142. r.Get<Employee, string>(c => c.Name)
  143. )
  144. );
  145. var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity)));
  146. var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid));
  147. if (recipient == null || sender == null)
  148. return;
  149. if (recipient.Item2 == UserGuid)
  150. return;
  151. var notification = new Notification();
  152. notification.Employee.ID = recipient.Item1;
  153. notification.Sender.ID = sender.Item1;
  154. notification.Title = title(entity);
  155. var sb = new StringBuilder();
  156. sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First()));
  157. sb.Append(body.Invoke(entity));
  158. if (sender != null)
  159. sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First()));
  160. notification.Description = sb.ToString();
  161. notification.EntityType = typeof(TEntity).EntityName();
  162. notification.EntityID = entity.ID;
  163. FindSubStore<Notification>().Save(notification, "");
  164. }
  165. protected Guid GetEmployeeIDFromUserGuid()
  166. {
  167. return Provider.Query(
  168. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(UserGuid),
  169. Columns.None<Employee>().Add(x => x.ID)
  170. ).Rows.FirstOrDefault()?
  171. .Get<Employee, Guid>(x => x.ID) ?? Guid.Empty;
  172. }
  173. }
  174. }