DeviationNotificationPlugin.cs 12 KB

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