From 7ab0656a1e479c62d1381d0d8f00a660ee1b7d4b Mon Sep 17 00:00:00 2001 From: b110212000 Date: Fri, 13 Aug 2021 16:54:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=95=B0=E5=B8=B8email=20=E5=AF=84=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Models/NoticeSchedule.cs | 1 + SolarPower/Models/Overview.cs | 20 +++++ SolarPower/Quartz/Jobs/ExceptionSchedule.cs | 80 +++++++++++++++++++ .../Implement/OverviewRepository.cs | 54 +++++++++++++ .../Interface/IOverviewRepository.cs | 2 + SolarPower/Startup.cs | 7 ++ SolarPower/appsettings.Development.json | 3 +- SolarPower/appsettings.json | 3 +- 8 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 SolarPower/Quartz/Jobs/ExceptionSchedule.cs diff --git a/SolarPower/Models/NoticeSchedule.cs b/SolarPower/Models/NoticeSchedule.cs index a0c0d62..da28d98 100644 --- a/SolarPower/Models/NoticeSchedule.cs +++ b/SolarPower/Models/NoticeSchedule.cs @@ -19,6 +19,7 @@ namespace SolarPower.Models public string Reason { get; set; } public int UserId { get; set; } public int EmailType { get; set; } + public int ExceptionId { get; set; } } public class OperationPersonnel diff --git a/SolarPower/Models/Overview.cs b/SolarPower/Models/Overview.cs index e8abc06..061b09f 100644 --- a/SolarPower/Models/Overview.cs +++ b/SolarPower/Models/Overview.cs @@ -178,4 +178,24 @@ namespace SolarPower.Models public string TOTALTIME { get; set; } } + public class ExceptionEmailInfo + { + public int id { get; set; }//Id編號 + public string site_id { get; set; }//電站編號 + public string alarmClassName { get; set; }//異常類別 + public string errDevice { get; set; }//設備編號 + public string errValue { get; set; }//原始值 + public string dev_time { get; set; }//發生時間 + public string errMsg { get; set; }//錯誤原因 + public string PowerStationName { get; set; }//電站名稱 + public int PowerStationId { get; set; }//電站流水號 + } + + public class UserPowerStationTo + { + public string Name { get; set; } + public string Email { get; set; } + public int Id { get; set; } + } + } diff --git a/SolarPower/Quartz/Jobs/ExceptionSchedule.cs b/SolarPower/Quartz/Jobs/ExceptionSchedule.cs new file mode 100644 index 0000000..5c0cdf9 --- /dev/null +++ b/SolarPower/Quartz/Jobs/ExceptionSchedule.cs @@ -0,0 +1,80 @@ +using Microsoft.Extensions.Logging; +using Quartz; +using SolarPower.Models; +using SolarPower.Repository.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace SolarPower.Quartz.Jobs +{ + [DisallowConcurrentExecution] + public class ExceptionSchedule : IJob + { + private readonly ILogger logger; + private readonly IOverviewRepository overviewRepository; + private readonly INoticeScheduleRepository noticeScheduleRepository; + private readonly IUserRepository userRepository; + + public ExceptionSchedule(ILogger logger, IOverviewRepository overviewRepository,INoticeScheduleRepository noticeScheduleRepository,IUserRepository userRepository) + { + this.logger = logger; + this.overviewRepository = overviewRepository; + this.noticeScheduleRepository = noticeScheduleRepository; + this.userRepository = userRepository; + } + + public async Task Execute(IJobExecutionContext context) + { + try + { + var ExceptionList = await overviewRepository.GetEmailExceptionList(); + if(ExceptionList.Count > 0 ) + { + foreach(var Exception in ExceptionList) + { + var UserListWithPowerstation = await overviewRepository.GetUserListWithPowerstation(Exception.PowerStationId); + foreach(var user in UserListWithPowerstation) + { + var Content = $"電站名稱:{Exception.PowerStationName}" + "
" + + $"設備編號:{Exception.errDevice}" + "
" + + $"異常ID編號:{Exception.id}" + "
" + + $"異常類別:{Exception.alarmClassName}" + "
" + + $"設備訊息:{Exception.errMsg}" + "
" + + $"發生時間:{Exception.dev_time}" ; + NoticeSchedule DaySchedule = new NoticeSchedule() + { + UserId = user.Id, + EmailType = 4, + RecipientEmail = user.Email, + Subject = "異常通知", + Content = Content, + RecipientName = user.Name, + Type = 1, + ExceptionId = Exception.id + }; + List properties = new List() + { + "UserId", + "EmailType", + "RecipientEmail", + "Subject", + "Content", + "RecipientName", + "Type", + "ExceptionId" + }; + await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); + } + } + } + } + catch (Exception exception) + { + logger.LogError("【{0}】{1}", nameof(logger), exception.Message); + } + } + } +} \ No newline at end of file diff --git a/SolarPower/Repository/Implement/OverviewRepository.cs b/SolarPower/Repository/Implement/OverviewRepository.cs index 2b34507..964199d 100644 --- a/SolarPower/Repository/Implement/OverviewRepository.cs +++ b/SolarPower/Repository/Implement/OverviewRepository.cs @@ -631,5 +631,59 @@ namespace SolarPower.Repository.Implement return result; } } + + public async Task> GetEmailExceptionList() + { + List exceptionEmailInfos = new List(); + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT a.`*`,ns.Id FROM + ( + select a.id, site_id, `timestamp`, FROM_UNIXTIME((`timestamp` / 1000), '%Y-%m-%d %H:%i:%s') dev_time , a.sourceState err_status, FROM_UNIXTIME( (a.normalTime / 1000), '%Y-%m-%d %H:%i:%s') normalTime, + a.alarmClass, b.alarmClass as alarmClassName,ps.Name as PowerStationName,ps.Id as PowerStationId, + errDevice, err_valueKind, errValue, FROM_UNIXTIME( (a.lastUpdate / 1000), '%Y-%m-%d %H:%i:%s') lastUpdate, + case when c.errMsg_tw is null then d.errMsg_tw else c.errMsg_tw end errMsg + from err_main a + join alarmorion_orionalarmclass b on a.alarmclass = b.id + left join ref_err_device c on trim(b.alarmClass) = c.deviceType + left join ref_err_inv d on lower(b.alarmClass) = d.deviceType + and case when lower(b.alarmClass) = 'inverter' and err_valuekind = 'e' then errvalue else '' end = d.errCode + left join power_station ps on ps.Code = site_id + left join operation_record pr on pr.ErrorCode = a.id + ) a + LEFT JOIN + notice_schedule ns ON ns.ExceptionId = a.id + WHERE ns.Id IS NULL AND a.err_status = 1 AND a.PowerStationId IS NOT null"; + exceptionEmailInfos = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + }; + return exceptionEmailInfos; + } + + public async Task> GetUserListWithPowerstation(int id) + { + List result = new List(); + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT u.Name,u.Email,u.Id FROM power_station_operation_personnel pss + LEFT JOIN user u ON u.Id = pss.UserId + WHERE pss.Deleted = 0 AND pss.EmailException = 1 AND pss.PowerStationId = {id} AND u.Deleted = 0"; + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + }; + return result; + } } } diff --git a/SolarPower/Repository/Interface/IOverviewRepository.cs b/SolarPower/Repository/Interface/IOverviewRepository.cs index d599b6c..d594ce5 100644 --- a/SolarPower/Repository/Interface/IOverviewRepository.cs +++ b/SolarPower/Repository/Interface/IOverviewRepository.cs @@ -30,5 +30,7 @@ namespace SolarPower.Repository.Interface Task> GetListInverterYearByPowerStationId(int powerStationId, string year); Task> GethistoryTable(HistorySent post); Task> GetExceptionTable2(ExceptionSent2 post); + Task> GetEmailExceptionList(); + Task> GetUserListWithPowerstation(int id); } } diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index 1869650..1edcb3f 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -132,6 +132,13 @@ namespace SolarPower ); #endregion + #region d߲`sWEmail + services.AddSingleton(); + services.AddSingleton( + new JobSchedule(jobType: typeof(ExceptionSchedule), cronExpression: Configuration.GetValue("BackgroundServiceCron:ExceptionSchedule")) + ); + #endregion + services.AddHostedService(); #endregion } diff --git a/SolarPower/appsettings.Development.json b/SolarPower/appsettings.Development.json index 226ca35..ff5776a 100644 --- a/SolarPower/appsettings.Development.json +++ b/SolarPower/appsettings.Development.json @@ -26,7 +26,8 @@ "CalcAvgPowerStationJob": "0 0 2 * * ?", "OperationScheduleJob": "0 0 2 * * ?", "CalcInverter15minJob": "0 2/15 * * * ?", - "SendEmailJob": "0 15 2 * * ?" + "SendEmailJob": "0 15 2 * * ?", + "ExceptionSchedule": "0 0/5 * * * ?" }, "SMTPConfig": { "Host": "smtp.gmail.com", diff --git a/SolarPower/appsettings.json b/SolarPower/appsettings.json index 1c81efa..40e4a29 100644 --- a/SolarPower/appsettings.json +++ b/SolarPower/appsettings.json @@ -27,7 +27,8 @@ "CalcAvgPowerStationJob": "0 0 2 * * ?", "OperationScheduleJob": "0 0 2 * * ?", "CalcInverter15minJob": "0 2/15 * * * ?", - "SendEmailJob": "0 15 2 * * ?" + "SendEmailJob": "0 15 2 * * ?", + "ExceptionSchedule": "0 0/5 * * * ?" }, "SMTPConfig": { "Host": "smtp.gmail.com",