| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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<LegalContractKeyPointInfo, string> lCkpRepository, IRepository<LawyerRelatedInfo> lrRepository, IRepository<StaffRelatedInfo> srRepository, IRepository<User, long> userRepository)
- {
- LCkpRepository = lCkpRepository;
- LrRepository = lrRepository;
- SrRepository = srRepository;
- UserRepository = userRepository;
- NotificationManager = NullNotificationManager.Instance;
- AbpSession = NullIwbSession.Instance;
- }
- public IRepository<LawyerRelatedInfo> LrRepository { get; }
- public IRepository<StaffRelatedInfo> SrRepository { get; }
- public IRepository<User,long> UserRepository { get; }
- public IRepository<LegalContractKeyPointInfo,string> 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<UserIdentifier>();
- 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));
- }
- }
- }
-
- }
- }
|