using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Threading.Tasks; using Abp; using Abp.Dependency; using Abp.Domain.Repositories; using Abp.Domain.Uow; using Abp.Quartz; using ContractService.Authorization.Users; using ContractService.Client; using ContractService.CommonManager.Notifications; using ContractService.Lawyer; using ContractService.LegalContract; using IwbZero.Runtime.Session; using Quartz; namespace ContractService.Quartz { public class TimeKeyPointJob:JobBase,ITransientDependency { public TimeKeyPointJob(IRepository lCkpRepository, IRepository lrRepository, IRepository srRepository, IRepository userRepository) { LCkpRepository = lCkpRepository; LrRepository = lrRepository; SrRepository = srRepository; UserRepository = userRepository; NotificationManager = NullNotificationManager.Instance; AbpSession = NullIwbSession.Instance; } public IRepository LrRepository { get; } public IRepository SrRepository { get; } public IRepository UserRepository { get; } public IRepository LCkpRepository { get; } public IIwbSession AbpSession { get; set; } public INotificationManager NotificationManager { get; set; } [UnitOfWork] public override async Task Execute(IJobExecutionContext context) { Logger.Info("定时任务:[TimeKeyPointJob] 准备执行"); await KpNotify(); } public async Task KpNotify() { try { var today = DateTime.Today; Logger.Info($"开始执行:[TimeKeyPointJob] [{today:yyyy-MM-dd}]"); var alarmKps = await LCkpRepository.GetAllIncluding(a => a.ContractInfo).Where(a => a.AlarmDate <= today && a.ExpireDate > today && a.KeyPointState == LegalKeyPointStateDefinition.Run && a.NotifyType == NotifyTypeDefinition.None) .ToListAsync(); int i = 0; if (alarmKps.Any()) { foreach (var kp in alarmKps) { Logger.Info($"[ALARM] =================> ID:[{kp.Id}]"); await KpNotify(kp); kp.NotifyType = NotifyTypeDefinition.Alarm; await LCkpRepository.UpdateAsync(kp); i++; } } var expireKps = await LCkpRepository.GetAllIncluding(a => a.ContractInfo).Where(a => a.ExpireDate <= today && a.KeyPointState == LegalKeyPointStateDefinition.Run && a.NotifyType != NotifyTypeDefinition.Expire).ToListAsync(); if (alarmKps.Any()) { foreach (var kp in expireKps) { Logger.Info($"[EXPIRE] =================> ID:[{kp.Id}]"); await KpNotify(kp, false); kp.NotifyType = NotifyTypeDefinition.Expire; await LCkpRepository.UpdateAsync(kp); i++; } } Logger.Info($"执行结束:[TimeKeyPointJob] 共处理[{i}]项关键点。"); } catch (Exception e) { Logger.Error(e.Message, e); } } private async Task KpNotify(LegalContractKeyPointInfo kp, bool isAlarm = true) { var list = new List(); var staffs = await SrRepository.GetAllIncluding(a => a.StaffInfo) .Where(a => a.ContractNo == kp.ContractNo && a.RelatedType == StaffRelatedDefinition.LegalContract && !string.IsNullOrEmpty(a.StaffInfo.UserName)) .Select(a => a.StaffInfo).Distinct().ToListAsync(); if (staffs.Any()) { foreach (var s in staffs) { var user = await UserRepository.FirstOrDefaultAsync(a => a.UserName == s.UserName); if (user != null) { list.Add(new UserIdentifier(AbpSession.TenantId, user.Id)); } } } var lawyers = await LrRepository.GetAllIncluding(a => a.LawyerInfo) .Where(a => a.ContractNo == kp.ContractNo && a.RelatedType == LawyerRelatedDefinition.LegalContract && !string.IsNullOrEmpty(a.LawyerInfo.UserName)) .Select(a => a.LawyerInfo).Distinct().ToListAsync(); if (lawyers.Any()) { foreach (var l in lawyers) { var user = await UserRepository.FirstOrDefaultAsync(a => a.UserName == l.UserName); if (user != null) { list.Add(new UserIdentifier(AbpSession.TenantId, user.Id)); } } } if (list.Any()) { if (isAlarm) { await NotificationManager.SendKeyPointAlarmMsg(list, new KeyPointNotificationData(kp)); } else { await NotificationManager.SendKeyPointExpireMsg(list, new KeyPointNotificationData(kp)); } } } } }