異常email 寄信

This commit is contained in:
b110212000 2021-08-13 16:54:03 +08:00
parent c2a84e7059
commit 7ab0656a1e
8 changed files with 168 additions and 2 deletions

View File

@ -19,6 +19,7 @@ namespace SolarPower.Models
public string Reason { get; set; } public string Reason { get; set; }
public int UserId { get; set; } public int UserId { get; set; }
public int EmailType { get; set; } public int EmailType { get; set; }
public int ExceptionId { get; set; }
} }
public class OperationPersonnel public class OperationPersonnel

View File

@ -178,4 +178,24 @@ namespace SolarPower.Models
public string TOTALTIME { get; set; } 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; }
}
} }

View File

@ -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<ExceptionSchedule> logger;
private readonly IOverviewRepository overviewRepository;
private readonly INoticeScheduleRepository noticeScheduleRepository;
private readonly IUserRepository userRepository;
public ExceptionSchedule(ILogger<ExceptionSchedule> 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}" + "<br>" +
$"設備編號:{Exception.errDevice}" + "<br>" +
$"異常ID編號:{Exception.id}" + "<br>" +
$"異常類別:{Exception.alarmClassName}" + "<br>" +
$"設備訊息:{Exception.errMsg}" + "<br>" +
$"發生時間:{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<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Content",
"RecipientName",
"Type",
"ExceptionId"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
}
}
}
catch (Exception exception)
{
logger.LogError("【{0}】{1}", nameof(logger), exception.Message);
}
}
}
}

View File

@ -631,5 +631,59 @@ namespace SolarPower.Repository.Implement
return result; return result;
} }
} }
public async Task<List<ExceptionEmailInfo>> GetEmailExceptionList()
{
List<ExceptionEmailInfo> exceptionEmailInfos = new List<ExceptionEmailInfo>();
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<ExceptionEmailInfo>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
};
return exceptionEmailInfos;
}
public async Task<List<UserPowerStationTo>> GetUserListWithPowerstation(int id)
{
List<UserPowerStationTo> result = new List<UserPowerStationTo>();
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<UserPowerStationTo>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
};
return result;
}
} }
} }

View File

@ -30,5 +30,7 @@ namespace SolarPower.Repository.Interface
Task<List<InverterHistory>> GetListInverterYearByPowerStationId(int powerStationId, string year); Task<List<InverterHistory>> GetListInverterYearByPowerStationId(int powerStationId, string year);
Task<List<HistoryTable>> GethistoryTable(HistorySent post); Task<List<HistoryTable>> GethistoryTable(HistorySent post);
Task<List<ExceptionDataTable>> GetExceptionTable2(ExceptionSent2 post); Task<List<ExceptionDataTable>> GetExceptionTable2(ExceptionSent2 post);
Task<List<ExceptionEmailInfo>> GetEmailExceptionList();
Task<List<UserPowerStationTo>> GetUserListWithPowerstation(int id);
} }
} }

View File

@ -132,6 +132,13 @@ namespace SolarPower
); );
#endregion #endregion
#region <EFBFBD>Email
services.AddSingleton<ExceptionSchedule>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ExceptionSchedule), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:ExceptionSchedule"))
);
#endregion
services.AddHostedService<QuartzHostedService>(); services.AddHostedService<QuartzHostedService>();
#endregion #endregion
} }

View File

@ -26,7 +26,8 @@
"CalcAvgPowerStationJob": "0 0 2 * * ?", "CalcAvgPowerStationJob": "0 0 2 * * ?",
"OperationScheduleJob": "0 0 2 * * ?", "OperationScheduleJob": "0 0 2 * * ?",
"CalcInverter15minJob": "0 2/15 * * * ?", "CalcInverter15minJob": "0 2/15 * * * ?",
"SendEmailJob": "0 15 2 * * ?" "SendEmailJob": "0 15 2 * * ?",
"ExceptionSchedule": "0 0/5 * * * ?"
}, },
"SMTPConfig": { "SMTPConfig": {
"Host": "smtp.gmail.com", "Host": "smtp.gmail.com",

View File

@ -27,7 +27,8 @@
"CalcAvgPowerStationJob": "0 0 2 * * ?", "CalcAvgPowerStationJob": "0 0 2 * * ?",
"OperationScheduleJob": "0 0 2 * * ?", "OperationScheduleJob": "0 0 2 * * ?",
"CalcInverter15minJob": "0 2/15 * * * ?", "CalcInverter15minJob": "0 2/15 * * * ?",
"SendEmailJob": "0 15 2 * * ?" "SendEmailJob": "0 15 2 * * ?",
"ExceptionSchedule": "0 0/5 * * * ?"
}, },
"SMTPConfig": { "SMTPConfig": {
"Host": "smtp.gmail.com", "Host": "smtp.gmail.com",