TaskNotification.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Microsoft.AspNetCore.SignalR;
  2. using OASystem.API.OAMethodLib.Hub.HubClients;
  3. using OASystem.API.OAMethodLib.Hub.Hubs;
  4. using OASystem.API.OAMethodLib.SignalR.Hubs;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.PersonnelModule;
  7. using OASystem.Infrastructure.Repositories.PersonnelModule;
  8. using ILogger = Microsoft.Extensions.Logging.ILogger;
  9. namespace OASystem.API.OAMethodLib.Quartz.Business
  10. {
  11. public static class TaskNotification
  12. {
  13. private readonly static ILogger _logger;
  14. private readonly static TaskAllocationRepository _taskAllocationRep = AutofacIocManager.Instance.GetService<TaskAllocationRepository>();
  15. private readonly static IHubContext<ChatHub, IChatClient> _hubContext = AutofacIocManager.Instance.GetService<IHubContext<ChatHub, IChatClient>>();
  16. /// <summary>
  17. /// 任务通知
  18. /// </summary>
  19. public static async void PostTaskMessageNotification()
  20. {
  21. //_logger.LogInformation("调用任务消息推送 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "【在线人数】:" + JsonConvert.SerializeObject(UserStore.OnlineUser));
  22. //在此处编写任务业务代码
  23. #region 消息处理
  24. var newDB = _taskAllocationRep._sqlSugar.CopyNew();
  25. var data = newDB
  26. .Queryable<Pm_TaskAllocation>()
  27. .Where(it => it.IsDel == 0 &&
  28. !string.IsNullOrEmpty(it.PredictEndTime) &&
  29. Convert.ToDateTime(it.PredictEndTime).ToString("yyyy-MM-dd").Equals(DateTime.Now.ToString("yyyy-MM-dd"))
  30. )
  31. .ToList();
  32. if (data.Count > 0)
  33. {
  34. //处理要任务提醒的员工
  35. List<int> primaryIds = new List<int>();
  36. primaryIds = data.Select(it => it.Id).ToList();
  37. var subData = newDB
  38. .Queryable<Pm_TaskRelevanceUser>()
  39. .Where(it => it.IsDel == 0 &&
  40. primaryIds.Contains(it.TAId) &&
  41. (int)it.TaskStatus <= (int)TaskEnum.UnderWay
  42. )
  43. .ToList();
  44. var depDatas = newDB.Queryable<Sys_Department>().Where(it => it.IsDel == 0).ToList();
  45. var groupDatas = newDB.Queryable<Grp_DelegationInfo>().Where(it => it.IsDel == 0).ToList();
  46. var userDatas = newDB.Queryable<Sys_Users>().Where(it => it.IsDel == 0).ToList();
  47. List<int> userIds = new List<int>();
  48. newDB.BeginTran();
  49. foreach (var item in data)
  50. {
  51. string depName = string.Empty;
  52. string groupName = string.Empty;
  53. var depData = depDatas.Where(it => it.Id == item.DepId).FirstOrDefault();
  54. if (depData != null) { depName = depData.DepName; }
  55. if (item.DiId == -1) { groupName = "其他"; }
  56. else
  57. {
  58. var groupData = groupDatas.Where(it => it.Id == item.DiId).FirstOrDefault();
  59. if (groupData != null) { groupName = groupData.TeamName; }
  60. }
  61. var subData1 = subData.Where(it => it.TAId == item.Id).ToList();
  62. foreach (var subItem in subData1)
  63. {
  64. string realityStartTime = string.Empty;
  65. string userName = string.Empty;
  66. if (!string.IsNullOrEmpty(subItem.BeginTime))
  67. {
  68. realityStartTime = subItem.BeginTime;
  69. }
  70. else
  71. {
  72. if (subItem.TaskStatus == TaskerEnum.NotStarted) realityStartTime = "未开始";
  73. else if (subItem.TaskStatus == TaskerEnum.HaveKnown) realityStartTime = "已知晓";
  74. }
  75. var userData = userDatas.Where(it => it.Id == subItem.UserId).FirstOrDefault();
  76. if (userData != null) { userName = userData.CnName; }
  77. string msgContent = string.Format(@$"归属团组:{groupName}<br/>
  78. 任务参与者:{userName}");
  79. Sys_Message _Message = new Sys_Message()
  80. {
  81. Type = MessageTypeEnum.TaskProgressUpdate,
  82. Title = item.TaskName,
  83. IssuerId = 4, //管理员
  84. Content = msgContent,
  85. ReleaseTime = DateTime.Now,
  86. CreateUserId = 4,
  87. CreateTime = DateTime.Now,
  88. DeleteTime = "1990-01-01 00:00:00.000",
  89. DiId = item.DiId,
  90. };
  91. var msgAddId = newDB.Insertable(_Message).ExecuteReturnIdentity();
  92. if (msgAddId < 0)
  93. {
  94. newDB.RollbackTran();
  95. _logger.LogError("任务通知消息推送失败!");
  96. return;
  97. }
  98. Sys_MessageReadAuth _MessageReadAuth = new Sys_MessageReadAuth()
  99. {
  100. MsgId = msgAddId,
  101. ReadableUId = subItem.UserId,
  102. IsRead = 0,
  103. ReadTime = Convert.ToDateTime("1990-01-01 00:00:00.000"),
  104. CreateUserId = 4,
  105. CreateTime = DateTime.Now,
  106. DeleteTime = "1990-01-01 00:00:00.000"
  107. };
  108. var msgAuthAddId = newDB.Insertable(_MessageReadAuth).ExecuteReturnIdentity();
  109. if (msgAuthAddId < 0)
  110. {
  111. _taskAllocationRep._sqlSugar.RollbackTran();
  112. _logger.LogError("任务通知消息推送失败!");
  113. return;
  114. }
  115. userIds.Add(subItem.UserId);
  116. }
  117. }
  118. newDB.CommitTran();
  119. //推送消息
  120. await _hubContext.Clients.Clients(UserStore.OnlineUser.Where(it => userIds.Contains(it.UserId)).Select(it => it.ConnectionId).ToList()).ReceiveMessage("您有新的任务相关消息");
  121. _logger.LogInformation("任务消息推送成功 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "【在线人数】:" + JsonConvert.SerializeObject(UserStore.OnlineUser));
  122. }
  123. #endregion
  124. }
  125. }
  126. }