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); } } } }