From 494c8d414230def1bf10d0c04e825179d304d7f2 Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Mon, 8 Aug 2022 16:38:56 +0800 Subject: [PATCH] =?UTF-8?q?Email=E6=A0=BC=E5=BC=8F:=20=E5=8A=A0=E5=85=A5no?= =?UTF-8?q?tice=5Fschedule=E5=92=8C=E8=87=AA=E5=8B=95=E5=AF=84=E4=BF=A1?= =?UTF-8?q?=E5=89=8D=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=AA=A2=E6=9F=A5Email?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E7=9A=84=E5=88=A4=E6=96=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Controllers/OperationController.cs | 191 ++++++++++-------- .../Quartz/Jobs/OperationScheduleJob.cs | 161 ++++++++------- SolarPower/Quartz/Jobs/SendEmailJob.cs | 88 ++++---- .../Implement/OperationRepository.cs | 24 ++- 4 files changed, 265 insertions(+), 199 deletions(-) diff --git a/SolarPower/Controllers/OperationController.cs b/SolarPower/Controllers/OperationController.cs index 9b20909..652667a 100644 --- a/SolarPower/Controllers/OperationController.cs +++ b/SolarPower/Controllers/OperationController.cs @@ -12,6 +12,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Transactions; +using System.Text.RegularExpressions; namespace SolarPower.Controllers { @@ -676,30 +677,36 @@ namespace SolarPower.Controllers _ => "" }; var user = await userRepository.GetOneAsync(person); - NoticeSchedule schedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - EmailType = 3, - Type = 1, - UserId = person, - RecipientName = user.Name, - RecipientEmail = user.Email, - Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, - Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, - }; - noticeSchedules.Add(schedule); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + UserId = person, + RecipientName = user.Name, + RecipientEmail = user.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + noticeSchedules.Add(schedule); + } } properties = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Content", - "RecipientName", - "Type", - "ExceptionId" - }; - await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + if (noticeSchedules.Count > 0) + { + await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + } } if (post.FixFirm != 0) @@ -714,27 +721,31 @@ namespace SolarPower.Controllers }; //var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync(post.FixFirm, powerstations.SiteDB, "operation_firm"); - NoticeSchedule schedule = new NoticeSchedule() + if (IsValidEmail(firm.Email)) { - EmailType = 3, - Type = 1, - RecipientName = firm.Name, - RecipientEmail = firm.Email, - Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, - Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, - }; - properties = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Content", - "RecipientName", - "Type", - "ExceptionId" - }; - await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + RecipientName = firm.Name, + RecipientEmail = firm.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目" + post.FixDo + "
注意事項 :" + post.Notice, + }; + + properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); + } } } //using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Suppress)) @@ -898,30 +909,36 @@ namespace SolarPower.Controllers _ => "" }; var user = await userRepository.GetOneAsync(person); - NoticeSchedule schedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - EmailType = 3, - Type = 1, - UserId = person, - RecipientName = user.Name, - RecipientEmail = user.Email, - Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, - Content = "維修項目:" + post.FixDo + "
注意事項 :" + post.Notice, - }; - noticeSchedules.Add(schedule); + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + UserId = person, + RecipientName = user.Name, + RecipientEmail = user.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目:" + post.FixDo + "
注意事項 :" + post.Notice, + }; + noticeSchedules.Add(schedule); + } } properties = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Content", - "RecipientName", - "Type", - "ExceptionId" - }; - await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + if (noticeSchedules.Count > 0) + { + await noticeScheduleRepository.AddAnyThing>(noticeSchedules, properties, "notice_schedule"); + } } if (post.FixFirm != 0) { @@ -935,29 +952,31 @@ namespace SolarPower.Controllers }; // var powerstation = await powerStationRepository.GetOneAsync(post.PowerStationId); var firm = await operationRepository.GetOneWithCustomDBNameAndTableAsync(post.FixFirm, powerstations.SiteDB, "operation_firm"); - NoticeSchedule schedule = new NoticeSchedule() + if (IsValidEmail(firm.Email)) { - EmailType = 3, - Type = 1, - RecipientName = firm.Name, - RecipientEmail = firm.Email, - Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, - Content = "維修項目:" + post.FixDo + "
注意事項 :" + post.Notice, - }; - properties = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Content", - "RecipientName", - "Type", - "ExceptionId" - }; - await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); - + NoticeSchedule schedule = new NoticeSchedule() + { + EmailType = 3, + Type = 1, + RecipientName = firm.Name, + RecipientEmail = firm.Email, + Subject = powerstations.Name + "-" + fixtype + "-" + post.WorkTime, + Content = "維修項目:" + post.FixDo + "
注意事項 :" + post.Notice, + }; + properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + await noticeScheduleRepository.AddAnyThing(schedule, properties, "notice_schedule"); + } } } @@ -1325,5 +1344,11 @@ namespace SolarPower.Controllers } } + bool IsValidEmail(string strIn) + { + // Return true if strIn is in valid e-mail format. + return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); + } + } } diff --git a/SolarPower/Quartz/Jobs/OperationScheduleJob.cs b/SolarPower/Quartz/Jobs/OperationScheduleJob.cs index e97cdde..43e9001 100644 --- a/SolarPower/Quartz/Jobs/OperationScheduleJob.cs +++ b/SolarPower/Quartz/Jobs/OperationScheduleJob.cs @@ -10,6 +10,7 @@ using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace SolarPower.Quartz.Jobs { @@ -249,27 +250,30 @@ namespace SolarPower.Quartz.Jobs var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented)); if (stationReportName != "") { - NoticeSchedule DaySchedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - UserId = user.Id, - EmailType = 0, - RecipientEmail = user.Email, - Subject = "日報表", - Attachment = stationReportName, - RecipientName = user.Name, - Type = 1 - }; - List properties = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Attachment", - "RecipientName", - "Type" - }; - await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); + NoticeSchedule DaySchedule = new NoticeSchedule() + { + UserId = user.Id, + EmailType = 0, + RecipientEmail = user.Email, + Subject = "日報表", + Attachment = stationReportName, + RecipientName = user.Name, + Type = 1 + }; + List properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Attachment", + "RecipientName", + "Type" + }; + await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); + } } } //綜合報表 每日 @@ -284,17 +288,19 @@ namespace SolarPower.Quartz.Jobs Userid = user.Id }; var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented)); - NoticeSchedule MaxSchedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - UserId = user.Id, - EmailType = 2, - RecipientEmail = user.Email, - Subject = "綜合報表", - Attachment = stationMaxReportName, - RecipientName = user.Name, - Type = 1 - }; - List properties = new List() + NoticeSchedule MaxSchedule = new NoticeSchedule() + { + UserId = user.Id, + EmailType = 2, + RecipientEmail = user.Email, + Subject = "綜合報表", + Attachment = stationMaxReportName, + RecipientName = user.Name, + Type = 1 + }; + List properties = new List() { "UserId", "EmailType", @@ -304,7 +310,8 @@ namespace SolarPower.Quartz.Jobs "RecipientName", "Type" }; - await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties); + await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties); + } if (DateTime.Now.ToString("dd") == "01") { @@ -317,27 +324,30 @@ namespace SolarPower.Quartz.Jobs Userid = user.Id }; var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented)); - NoticeSchedule MaxmonthSchedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - RecipientEmail = user.Email, - Subject = "綜合報表", - Attachment = stationReportmaxmonthName, - RecipientName = user.Name, - Type = 1, - UserId = user.Id, - EmailType = 2 - }; - List properties2 = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Attachment", - "RecipientName", - "Type" - }; - await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2); + NoticeSchedule MaxmonthSchedule = new NoticeSchedule() + { + RecipientEmail = user.Email, + Subject = "綜合報表", + Attachment = stationReportmaxmonthName, + RecipientName = user.Name, + Type = 1, + UserId = user.Id, + EmailType = 2 + }; + List properties2 = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Attachment", + "RecipientName", + "Type" + }; + await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2); + } } } @@ -360,27 +370,30 @@ namespace SolarPower.Quartz.Jobs var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented)); if (stationReportmonthName != "") { - NoticeSchedule MonthSchedule = new NoticeSchedule() + if (IsValidEmail(user.Email)) { - RecipientEmail = user.Email, - Subject = "月報表", - Attachment = stationReportmonthName, - RecipientName = user.Name, - Type = 1, - UserId = user.Id, - EmailType = 1 - }; - List properties2 = new List() - { - "UserId", - "EmailType", - "RecipientEmail", - "Subject", - "Attachment", - "RecipientName", - "Type" - }; - await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2); + NoticeSchedule MonthSchedule = new NoticeSchedule() + { + RecipientEmail = user.Email, + Subject = "月報表", + Attachment = stationReportmonthName, + RecipientName = user.Name, + Type = 1, + UserId = user.Id, + EmailType = 1 + }; + List properties2 = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Attachment", + "RecipientName", + "Type" + }; + await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2); + } } } @@ -437,5 +450,11 @@ namespace SolarPower.Quartz.Jobs return tempSerialNumber.ToString().Trim().PadRight(pad, '0'); } } + + bool IsValidEmail(string strIn) + { + // Return true if strIn is in valid e-mail format. + return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); + } } } diff --git a/SolarPower/Quartz/Jobs/SendEmailJob.cs b/SolarPower/Quartz/Jobs/SendEmailJob.cs index 71a356e..0c6897b 100644 --- a/SolarPower/Quartz/Jobs/SendEmailJob.cs +++ b/SolarPower/Quartz/Jobs/SendEmailJob.cs @@ -9,6 +9,7 @@ using System.IO; using System.Linq; using System.Net.Mail; using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace SolarPower.Quartz.Jobs { @@ -43,46 +44,48 @@ namespace SolarPower.Quartz.Jobs var updateNoticeSchedules = new List(); foreach (var notice in noticeSchedules) { - var attachments = new List(); - var recipientEmails = new List() - { - notice.RecipientEmail - }; - if (!string.IsNullOrEmpty(notice.Attachment)) - { - attachments = notice.Attachment.Split(',').ToList(); + if (IsValidEmail(notice.RecipientEmail)) { + var attachments = new List(); + var recipientEmails = new List() + { + notice.RecipientEmail + }; + if (!string.IsNullOrEmpty(notice.Attachment)) + { + attachments = notice.Attachment.Split(',').ToList(); + } + + var result = Send(recipientEmails, notice.Subject, notice.Content, attachments); + + if (result.CompareTo("成功") == 0) + { + NoticeSchedule noticeSchedule = new NoticeSchedule(); + noticeSchedule.Id = notice.Id; + noticeSchedule.IsDelivery = 1; + noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + updateNoticeSchedules.Add(noticeSchedule); + } + else + { + NoticeSchedule noticeSchedule = new NoticeSchedule(); + noticeSchedule.Id = notice.Id; + noticeSchedule.IsDelivery = 2; + noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + noticeSchedule.Reason = result; + + updateNoticeSchedules.Add(noticeSchedule); + } + + List properties = new List() + { + "Id", + "IsDelivery", + "DeliveryAt" + }; + + await noticeScheduleRepository.UpdateList(updateNoticeSchedules, properties); } - - var result = Send(recipientEmails, notice.Subject, notice.Content, attachments); - - if (result.CompareTo("成功") == 0) - { - NoticeSchedule noticeSchedule = new NoticeSchedule(); - noticeSchedule.Id = notice.Id; - noticeSchedule.IsDelivery = 1; - noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - - updateNoticeSchedules.Add(noticeSchedule); - } - else - { - NoticeSchedule noticeSchedule = new NoticeSchedule(); - noticeSchedule.Id = notice.Id; - noticeSchedule.IsDelivery = 2; - noticeSchedule.DeliveryAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - noticeSchedule.Reason = result; - - updateNoticeSchedules.Add(noticeSchedule); - } - - List properties = new List() - { - "Id", - "IsDelivery", - "DeliveryAt" - }; - - await noticeScheduleRepository.UpdateList(updateNoticeSchedules, properties); } logger.LogInformation("【SendEmailJob】【任務完成】"); @@ -160,5 +163,12 @@ namespace SolarPower.Quartz.Jobs } } + + bool IsValidEmail(string strIn) + { + // Return true if strIn is in valid e-mail format. + return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); + } + } } diff --git a/SolarPower/Repository/Implement/OperationRepository.cs b/SolarPower/Repository/Implement/OperationRepository.cs index 2f1a88c..1fe58e6 100644 --- a/SolarPower/Repository/Implement/OperationRepository.cs +++ b/SolarPower/Repository/Implement/OperationRepository.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace SolarPower.Repository.Implement { @@ -648,15 +649,20 @@ namespace SolarPower.Repository.Implement List value = new List(); foreach (MyUser a in personal) { - value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id}, {emailType})"); + if (IsValidEmail(a.Email)) + { + value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id}, {emailType})"); + } } string values = string.Join(",", value.ToArray()); + if (value.Count > 0) + { + var sql = $"INSERT INTO `notice_schedule` (`Type`, `RecipientName`, `RecipientEmail`, `Subject`, `Content`,`UserId`,`EmailType`) VALUES {values};"; - var sql = $"INSERT INTO `notice_schedule` (`Type`, `RecipientName`, `RecipientEmail`, `Subject`, `Content`,`UserId`,`EmailType`) VALUES {values};"; - - await conn.ExecuteAsync(sql, trans); - - trans.Commit(); + await conn.ExecuteAsync(sql, trans); + + trans.Commit(); + } } catch (Exception exception) { @@ -797,5 +803,11 @@ namespace SolarPower.Repository.Implement } } + bool IsValidEmail(string strIn) + { + // Return true if strIn is in valid e-mail format. + return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); + } + } }