DeviationNotificationPlugin.cs 13 KB

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