using BookingJieFeng.DB; using log4net; using MimeKit; using MimeKit.Utils; using MailKit.Net.Smtp; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using BookingJieFeng.DB.Model; namespace BookingJieFeng.Job { //截港日期前3小时,发送提醒邮件 public class DeadlineJob : IJob { private const int RemainDeadlineHour = 3; private BookingDB bookingDB = new BookingDB(); private ILog log = LogManager.GetLogger("DeadlineJob"); public void Execute(IJobExecutionContext context) { DateTime dtDeadline = DateTime.Parse(DateTime.Now.AddHours(RemainDeadlineHour).ToString("yyyy-MM-dd HH:00:00")); var deadList = bookingDB.Orders.AsNoTracking().Where(o => o.CLOSEDOCDATE != null && o.CLOSEDOCDATE.Value == dtDeadline).ToList(); log.Debug($"查找截港时间即将到期的订单,数量:{deadList.Count},时间条件:{dtDeadline.ToString("yyyy-MM-dd HH:mm:ss")}"); foreach (var item in deadList) { var corp = bookingDB.Users.FirstOrDefault(c => c.GID == item.CORPID); if (corp != null && !string.IsNullOrEmpty(corp.EMAIL)) { var etd = item.ETD.HasValue ? item.ETD.Value.ToString("yyyy-MM-dd") : string.Empty; OP_SEND_MAIL sendMail = new OP_SEND_MAIL(); sendMail.GID = Guid.NewGuid().ToString(); sendMail.SEND_TO = corp.EMAIL; sendMail.TITLE = $"您提交的订舱{item.ORDERNO},距截港时间只剩{RemainDeadlineHour}小时,请尽快处理"; sendMail.BODY = $"订舱公司:{corp.COMPANY_NAME}\r\n订舱编号:{item.ORDERNO}\r\nETD:{etd}\r\n船公司:{item.VESSEL}\r\n箱型箱量:{item.CNTRTOTAL}\r\n卸货港:{item.PORTDISCHARGE}\r\n录入人:{item.INPUTBY}"; sendMail.CREATE_TIME = DateTime.Now; sendMail.STATUS = SendMailStatus.Create.ToString(); bookingDB.SendMails.Add(sendMail); bookingDB.SaveChanges(); log.Debug($"成功提交邮件发送任务:订单编号:{item.ORDERNO},提单号:{item.MBLNO}"); } else { log.Error($"ID为{item.ORDNO}的订单未找到所属公司,或所属公司的邮箱为空,未能发送提醒邮件"); } } } } }