TimeKeyPointJob.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Abp;
  7. using Abp.Dependency;
  8. using Abp.Domain.Repositories;
  9. using Abp.Domain.Uow;
  10. using Abp.Quartz;
  11. using ContractService.Authorization.Users;
  12. using ContractService.Client;
  13. using ContractService.CommonManager.Notifications;
  14. using ContractService.Lawyer;
  15. using ContractService.LegalContract;
  16. using IwbZero.Runtime.Session;
  17. using Quartz;
  18. namespace ContractService.Quartz
  19. {
  20. public class TimeKeyPointJob:JobBase,ITransientDependency
  21. {
  22. public TimeKeyPointJob(IRepository<LegalContractKeyPointInfo, string> lCkpRepository, IRepository<LawyerRelatedInfo> lrRepository, IRepository<StaffRelatedInfo> srRepository, IRepository<User, long> userRepository)
  23. {
  24. LCkpRepository = lCkpRepository;
  25. LrRepository = lrRepository;
  26. SrRepository = srRepository;
  27. UserRepository = userRepository;
  28. NotificationManager = NullNotificationManager.Instance;
  29. AbpSession = NullIwbSession.Instance;
  30. }
  31. public IRepository<LawyerRelatedInfo> LrRepository { get; }
  32. public IRepository<StaffRelatedInfo> SrRepository { get; }
  33. public IRepository<User,long> UserRepository { get; }
  34. public IRepository<LegalContractKeyPointInfo,string> LCkpRepository { get; }
  35. public IIwbSession AbpSession { get; set; }
  36. public INotificationManager NotificationManager { get; set; }
  37. [UnitOfWork]
  38. public override async Task Execute(IJobExecutionContext context)
  39. {
  40. Logger.Info("定时任务:[TimeKeyPointJob] 准备执行");
  41. await KpNotify();
  42. }
  43. public async Task KpNotify()
  44. {
  45. try
  46. {
  47. var today = DateTime.Today;
  48. Logger.Info($"开始执行:[TimeKeyPointJob] [{today:yyyy-MM-dd}]");
  49. var alarmKps = await LCkpRepository.GetAllIncluding(a => a.ContractInfo).Where(a =>
  50. a.AlarmDate <= today && a.ExpireDate > today && a.KeyPointState == LegalKeyPointStateDefinition.Run && a.NotifyType == NotifyTypeDefinition.None)
  51. .ToListAsync();
  52. int i = 0;
  53. if (alarmKps.Any())
  54. {
  55. foreach (var kp in alarmKps)
  56. {
  57. Logger.Info($"[ALARM] =================> ID:[{kp.Id}]");
  58. await KpNotify(kp);
  59. kp.NotifyType = NotifyTypeDefinition.Alarm;
  60. await LCkpRepository.UpdateAsync(kp);
  61. i++;
  62. }
  63. }
  64. var expireKps = await LCkpRepository.GetAllIncluding(a => a.ContractInfo).Where(a => a.ExpireDate <= today && a.KeyPointState == LegalKeyPointStateDefinition.Run && a.NotifyType != NotifyTypeDefinition.Expire).ToListAsync();
  65. if (alarmKps.Any())
  66. {
  67. foreach (var kp in expireKps)
  68. {
  69. Logger.Info($"[EXPIRE] =================> ID:[{kp.Id}]");
  70. await KpNotify(kp, false);
  71. kp.NotifyType = NotifyTypeDefinition.Expire;
  72. await LCkpRepository.UpdateAsync(kp);
  73. i++;
  74. }
  75. }
  76. Logger.Info($"执行结束:[TimeKeyPointJob] 共处理[{i}]项关键点。");
  77. }
  78. catch (Exception e)
  79. {
  80. Logger.Error(e.Message, e);
  81. }
  82. }
  83. private async Task KpNotify(LegalContractKeyPointInfo kp, bool isAlarm = true)
  84. {
  85. var list = new List<UserIdentifier>();
  86. var staffs = await SrRepository.GetAllIncluding(a => a.StaffInfo)
  87. .Where(a => a.ContractNo == kp.ContractNo && a.RelatedType == StaffRelatedDefinition.LegalContract && !string.IsNullOrEmpty(a.StaffInfo.UserName))
  88. .Select(a => a.StaffInfo).Distinct().ToListAsync();
  89. if (staffs.Any())
  90. {
  91. foreach (var s in staffs)
  92. {
  93. var user = await UserRepository.FirstOrDefaultAsync(a => a.UserName == s.UserName);
  94. if (user != null)
  95. {
  96. list.Add(new UserIdentifier(AbpSession.TenantId, user.Id));
  97. }
  98. }
  99. }
  100. var lawyers = await LrRepository.GetAllIncluding(a => a.LawyerInfo)
  101. .Where(a => a.ContractNo == kp.ContractNo && a.RelatedType == LawyerRelatedDefinition.LegalContract && !string.IsNullOrEmpty(a.LawyerInfo.UserName))
  102. .Select(a => a.LawyerInfo).Distinct().ToListAsync();
  103. if (lawyers.Any())
  104. {
  105. foreach (var l in lawyers)
  106. {
  107. var user = await UserRepository.FirstOrDefaultAsync(a => a.UserName == l.UserName);
  108. if (user != null)
  109. {
  110. list.Add(new UserIdentifier(AbpSession.TenantId, user.Id));
  111. }
  112. }
  113. }
  114. if (list.Any())
  115. {
  116. if (isAlarm)
  117. {
  118. await NotificationManager.SendKeyPointAlarmMsg(list, new KeyPointNotificationData(kp));
  119. }
  120. else
  121. {
  122. await NotificationManager.SendKeyPointExpireMsg(list, new KeyPointNotificationData(kp));
  123. }
  124. }
  125. }
  126. }
  127. }