DeviationNotificationPlugin.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using GreenTree.Nachtragsmanagement.Core.Plugins;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
  7. using GreenTree.Nachtragsmanagement.Services.User;
  8. using GreenTree.Nachtragsmanagement.Services.Configuration;
  9. using GreenTree.Nachtragsmanagement.Services.Appendix;
  10. using GreenTree.Nachtragsmanagement.Core.Domain.Appendix;
  11. using System.Net.Mail;
  12. using System.Net;
  13. using System.Globalization;
  14. using GreenTree.Nachtragsmanagement.Services.Deviation;
  15. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  16. using Quartz;
  17. namespace GreenTree.Nachtragsmanagement.Web.Implementations
  18. {
  19. public class DeviationNotificationPlugin : INotificationPlugin, IJob
  20. {
  21. #region Services
  22. private readonly IUserService _userService;
  23. private readonly IConfigurationService _configurationService;
  24. private readonly IDeviationService _deviationService;
  25. #endregion
  26. #region Properties
  27. /// <summary>
  28. /// Id
  29. /// </summary>
  30. public Guid Id
  31. {
  32. get
  33. {
  34. return Guid.Parse("77D662E0-F621-4567-9030-2A106533FE06");
  35. }
  36. }
  37. /// <summary>
  38. /// System name
  39. /// </summary>
  40. public string SystemName
  41. {
  42. get
  43. {
  44. return "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin";
  45. }
  46. }
  47. /// <summary>
  48. /// List of available notification jobs
  49. /// </summary>
  50. public List<NotificationJob> AvailableNotificationJobs
  51. {
  52. get
  53. {
  54. return new List<NotificationJob>
  55. {
  56. new NotificationJob
  57. (
  58. Guid.Parse("144DFF97-4EE4-4B32-967C-C0375D133DCF"),
  59. "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt",
  60. "Eingangsdatum überprüfen",
  61. "Erstellt automatisch Benachrichtigungen für Vertragsabweichungen, die 40 Tagen, bzw. 60 Tagen nach " +
  62. "Einreichung noch keinem Nachtrag zugeordnet sind."
  63. )
  64. };
  65. }
  66. }
  67. /// <summary>
  68. /// Displayed name
  69. /// </summary>
  70. public string Name
  71. {
  72. get
  73. {
  74. return "Vertragsabweichungsbenachrichtigung";
  75. }
  76. }
  77. /// <summary>
  78. /// Further description on how this plugin works
  79. /// </summary>
  80. public string Description
  81. {
  82. get
  83. {
  84. return
  85. "Erstellt automatisch Benachrichtigungen für Vertragsabweichungen, die 40 Tagen (Stufe 1), bzw. 60 Tagen (Stufe 2)" +
  86. "nach Einreichung noch keinem Nachtrag zugeordnet sind.";
  87. }
  88. }
  89. #endregion
  90. /// <summary>
  91. /// Initializes a new instance of the DeviationNotificationPlugin class
  92. /// </summary>
  93. public DeviationNotificationPlugin() { }
  94. /// <summary>
  95. /// Initializes a new instance of the DeviationNotificationPlugin class
  96. /// </summary>
  97. public DeviationNotificationPlugin(
  98. IUserService userService,
  99. IConfigurationService configurationService,
  100. IDeviationService deviationService)
  101. {
  102. _userService = userService;
  103. _configurationService = configurationService;
  104. _deviationService = deviationService;
  105. }
  106. #region Quartz implmentation
  107. /// <summary>
  108. /// Executes the current job
  109. /// </summary>
  110. /// <param name="context"></param>
  111. public void Execute(IJobExecutionContext context)
  112. {
  113. if (!context.JobDetail.JobDataMap.ContainsKey("MailNotifications"))
  114. return;
  115. var mailNotifications = context.JobDetail.JobDataMap.Get("MailNotifications") as IEnumerable<MailNotification>;
  116. if (mailNotifications == null) return;
  117. ProcessNotifications(mailNotifications);
  118. }
  119. #endregion
  120. #region Processing
  121. /// <summary>
  122. /// Process all mail notifications registered for that plugin
  123. /// </summary>
  124. /// <param name="mailNotifications">The notifications which shall be generated.</param>
  125. public void ProcessNotifications(IEnumerable<MailNotification> mailNotifications)
  126. {
  127. if (mailNotifications == null || !mailNotifications.Any()) return;
  128. foreach (var notification in mailNotifications)
  129. {
  130. switch (notification.NotificationJobSystemName)
  131. {
  132. case "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt":
  133. {
  134. ProcessDeviationReceiptNotification(notification);
  135. }
  136. break;
  137. default:
  138. continue;
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Notifies the corresponding recipients about all deviations whose receipt date is older than N days in two different groups
  144. /// </summary>
  145. /// <param name="mailNotification">The notification which shall be generated.</param>
  146. private void ProcessDeviationReceiptNotification(MailNotification mailNotification)
  147. {
  148. var ageDaysLevel1 = _configurationService.TryGetConfigItemValue<int>(
  149. "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel1", 40);
  150. var ageDaysLevel2 = _configurationService.TryGetConfigItemValue<int>(
  151. "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel2", 60);
  152. var deviationsLevel1 = _deviationService.GetAllDeviations()
  153. .Where(d => !d.AppendixId.HasValue &&
  154. (DateTime.Now - d.ReceiptDate).Value.Days >= ageDaysLevel1 &&
  155. (DateTime.Now - d.ReceiptDate).Value.Days < ageDaysLevel2)
  156. .ToList();
  157. var deviationsLevel2 = _deviationService.GetAllDeviations()
  158. .Where(d => !d.AppendixId.HasValue &&
  159. (DateTime.Now - d.ReceiptDate).Value.Days >= ageDaysLevel2)
  160. .ToList();
  161. if (deviationsLevel1.Any() || deviationsLevel2.Any())
  162. {
  163. var mailBody = GenerateDeviationReceiptMailBody(deviationsLevel1, deviationsLevel2);
  164. SendNotification(mailNotification, "Autom. Übersicht nicht zugewiesene Vertragsabweichungen", mailBody);
  165. }
  166. }
  167. #endregion
  168. #region Mail body generation
  169. /// <summary>
  170. /// Generates a mail body with a list of all deviations whose receipt date is older than N days in two different groups
  171. /// </summary>
  172. /// <param name="deviationsLevel1">All deviations matching the level 1 criteria.</param>
  173. /// <param name="deviationsLevel2">All deviations matching the level 2 criteria.</param>
  174. public string GenerateDeviationReceiptMailBody(IEnumerable<Deviation> deviationsLevel1, IEnumerable<Deviation> deviationsLevel2)
  175. {
  176. var ageDaysLevel1 = _configurationService.TryGetConfigItemValue<int>(
  177. "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel1", 40);
  178. var ageDaysLevel2 = _configurationService.TryGetConfigItemValue<int>(
  179. "GreenTree.Nachtragsmanagement.DeviationNotificationPlugin.ProcessDeviationReceipt.AgeDaysLevel2", 60);
  180. var template =
  181. "<html>" +
  182. " <body>" +
  183. " {0}" +
  184. " {1}" +
  185. " </body>" +
  186. "</html>";
  187. var templateLevel1 =
  188. "<h3>Übersicht \"Offene Vertragsabweichungen älter als {0} Tage\"</h3>" +
  189. "<p>Folgende Vertragsabweichungen haben ein Einreichdatum älter als {0} Tage, sind aber noch keinen Nachtrag zugeordnet:</p>" +
  190. "<ul>" +
  191. " {1}" +
  192. "</ul>";
  193. var templateLevel1List = String.Empty;
  194. if (deviationsLevel1.Any())
  195. {
  196. foreach (var deviation in deviationsLevel1)
  197. {
  198. templateLevel1List += String.Format(
  199. "<li>Vertragsabweichung <b>\"{0}\"</b> " +
  200. "in Baustelle <b>\"{1}\"</b> - " +
  201. "Einreichdatum: <b>{2:dd.MM.yyyy} ({3} Tage)</b>",
  202. deviation.CustomNumber, deviation.Site.CustomNumber, deviation.ReceiptDate,
  203. (DateTime.Now - deviation.ReceiptDate).Value.Days);
  204. }
  205. }
  206. var resultLevel1List = String.Format(templateLevel1, ageDaysLevel1, templateLevel1List);
  207. var templateLevel2 =
  208. "<h3>Übersicht \"Offene Vertragsabweichungen älter als {0} Tage\"</h3>" +
  209. "<p>Folgende Vertragsabweichungen haben ein Einreichdatum älter als {0} Tage, sind aber noch keinen Nachtrag zugeordnet:</p>" +
  210. "<ul>" +
  211. " {1}" +
  212. "</ul>";
  213. var templateLevel2List = String.Empty;
  214. if (deviationsLevel2.Any())
  215. {
  216. foreach (var deviation in deviationsLevel2)
  217. {
  218. templateLevel2List += String.Format(
  219. "<li>Vertragsabweichung <b>\"{0}\"</b> " +
  220. "in Baustelle <b>\"{1}\"</b> - " +
  221. "Einreichdatum: <b>{2:dd.MM.yyyy} ({3} Tage)</b>",
  222. deviation.CustomNumber, deviation.Site.CustomNumber, deviation.ReceiptDate,
  223. (DateTime.Now - deviation.ReceiptDate).Value.Days);
  224. }
  225. }
  226. var resultLevel2List = String.Format(templateLevel2, ageDaysLevel2, templateLevel2List);
  227. return String.Format(template, resultLevel1List, resultLevel2List);
  228. }
  229. #endregion
  230. #region Mail sending
  231. /// <summary>
  232. /// Sends a generated mail body to the specified recipients in the mail notification
  233. /// </summary>
  234. /// <param name="mailNotification">The mail notification.</param>
  235. /// <param name="subject">The mail subject.</param>
  236. /// <param name="body">The mail body.</param>
  237. public void SendNotification(MailNotification mailNotification, string subject, string body)
  238. {
  239. if (mailNotification == null) return;
  240. var mailServerConfig = _configurationService.GetCurrentConfiguration().MailServerElement;
  241. var smptClient = new SmtpClient(mailServerConfig.SmtpServer, mailServerConfig.Port)
  242. {
  243. EnableSsl = mailServerConfig.UseSsl,
  244. Credentials = new NetworkCredential(
  245. mailServerConfig.Username,
  246. mailServerConfig.Password,
  247. mailServerConfig.Domain)
  248. };
  249. var recipients =
  250. mailNotification.Users
  251. .Select(u => u.MailAddress);
  252. var mailMessage = new MailMessage
  253. {
  254. IsBodyHtml = true,
  255. Subject = subject,
  256. Body = body,
  257. From = new MailAddress("Nachtragsbenachrichtigung@schweerbau.de")
  258. };
  259. foreach (var recipient in recipients)
  260. mailMessage.To.Add(recipient);
  261. smptClient.Send(mailMessage);
  262. }
  263. #endregion
  264. }
  265. }