MessageRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using NPOI.POIFS.Crypt.Dsig;
  2. using NPOI.SS.Formula.Functions;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.System;
  5. using OASystem.Domain.ViewModels.QiYeWeChat;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace OASystem.Infrastructure.Repositories.System
  14. {
  15. public class MessageRepository : BaseRepository<Sys_Message, MessageView>
  16. {
  17. public MessageRepository(SqlSugarClient sqlSugar) : base(sqlSugar) { }
  18. /// <summary>
  19. /// 发布消息
  20. /// </summary>
  21. /// <param name="msgDto"></param>
  22. /// <returns></returns>
  23. public async Task<bool> AddMsg(MessageDto msgDto)
  24. {
  25. #region 参数处理
  26. if (msgDto == null) { return false; }
  27. if (string.IsNullOrEmpty(msgDto.Title)) { return false; }
  28. if (string.IsNullOrEmpty(msgDto.Content)) { return false; }
  29. if (msgDto.UIdList.Count <= 0) { return false; }
  30. #endregion
  31. _sqlSugar.BeginTran();
  32. try
  33. {
  34. Sys_Message message = new Sys_Message()
  35. {
  36. Type = msgDto.Type,
  37. IssuerId = msgDto.IssuerId,
  38. Title = msgDto.Title,
  39. Content = msgDto.Content,
  40. ReleaseTime = msgDto.ReleaseTime,
  41. CreateUserId = msgDto.IssuerId,
  42. CreateTime = DateTime.Now,
  43. DeleteUserId = null,
  44. DeleteTime = "1990-01-01 00:00:00.000",
  45. Remark = "",
  46. IsDel = 0,
  47. DiId = msgDto.DiId
  48. };
  49. int? msgId = await _sqlSugar.Insertable(message).ExecuteReturnIdentityAsync();
  50. if (!msgId.HasValue) { _sqlSugar.RollbackTran(); return false; }
  51. List<Sys_MessageReadAuth> messageReadAuths = new List<Sys_MessageReadAuth>();
  52. foreach (int item in msgDto.UIdList)
  53. {
  54. Sys_MessageReadAuth messageReadAuth = new Sys_MessageReadAuth()
  55. {
  56. MsgId = msgId.Value,
  57. ReadableUId = item,
  58. ReadTime = new DateTime(1990, 1, 1),
  59. CreateUserId = msgDto.IssuerId,
  60. CreateTime = DateTime.Now,
  61. DeleteUserId = null,
  62. DeleteTime = "1990-01-01 00:00:00.000",
  63. Remark = "",
  64. IsDel = 0
  65. };
  66. messageReadAuths.Add(messageReadAuth);
  67. }
  68. int? readIds = await _sqlSugar.Insertable<Sys_MessageReadAuth>(messageReadAuths).ExecuteCommandAsync();
  69. if (!readIds.HasValue)
  70. {
  71. _sqlSugar.RollbackTran();
  72. return false;
  73. }
  74. _sqlSugar.CommitTran();
  75. }
  76. catch (Exception)
  77. {
  78. _sqlSugar.RollbackTran();
  79. return false;
  80. }
  81. return true;
  82. }
  83. /// <summary>
  84. /// 获取消息列表
  85. /// </summary>
  86. /// <param name="uId">可读用户Id</param>
  87. /// <returns></returns>
  88. public async Task<Result> GetMsgList(MsgDto dto)
  89. {
  90. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  91. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3) // web/android
  92. {
  93. string msgSqlWhere = string.Format(@"Select sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser,
  94. sm.ReleaseTime,smra.Id AuthId,smra.ReadableUId,smra.IsRead,smra.ReadTime
  95. From Sys_Message sm
  96. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  97. Inner Join Sys_Users su On sm.IssuerId = su.Id
  98. Inner Join Sys_Department sd On su.DepId = sd.Id
  99. Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
  100. Where sm.IsDel = 0
  101. And smra.IsDel = 0
  102. And smra.ReadableUId = {0}
  103. Order By ReleaseTime Desc ", dto.UserId);
  104. var _readableMsgList = await _sqlSugar.SqlQueryable<ReadbleMessageView>(msgSqlWhere).ToListAsync();
  105. if (_readableMsgList.Count > 0)
  106. {
  107. int pageSize = dto.PageSize; // 每页显示的记录数量
  108. int currentPage = dto.PageIndex; // 当前页码(从1开始)
  109. //操作通知 OperationNotification
  110. List<int> operationTypeList = new List<int>() {1,2,3,4,5 };
  111. var operationNotificatioData = _readableMsgList.Where(it => operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  112. int operationNotificatioDataCount = operationNotificatioData.Count;
  113. // 计算起始索引和结束索引
  114. int operationStartIndex = (currentPage - 1) * pageSize;
  115. int operationEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificatioDataCount);
  116. var operationNotificatioDataView = new {
  117. Count = operationNotificatioDataCount,
  118. UnReadCount = operationNotificatioData.Where(it => it.IsRead == 0).Count(),
  119. OperationNotificatioData = operationNotificatioData.Skip(operationStartIndex).Take(pageSize).ToList()
  120. };
  121. //任务通知 TaskNotification
  122. List<int> taskTypeList = new List<int>() { 6 };
  123. var taskNotificatioData = _readableMsgList.Where(it => taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  124. int taskNotificatioDataCount = taskNotificatioData.Count;
  125. // 计算起始索引和结束索引
  126. int taskStartIndex = (currentPage - 1) * pageSize;
  127. int taskEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificatioDataCount);
  128. var taskNotificatioDataView = new
  129. {
  130. Count = taskNotificatioDataCount,
  131. UnReadCount = taskNotificatioData.Where(it => it.IsRead == 0).Count(),
  132. TaskNotificatioData = taskNotificatioData.Skip(taskStartIndex).Take(pageSize).ToList()
  133. };
  134. var _view = new {
  135. OperationNotificatio = operationNotificatioDataView,
  136. TaskNotificatio = taskNotificatioDataView
  137. };
  138. result.Code = 0;
  139. result.Msg = "成功!";
  140. result.Data = _view;
  141. }
  142. else
  143. {
  144. result.Msg = "暂无该用户的消息!";
  145. }
  146. }
  147. return result;
  148. }
  149. /// <summary>
  150. /// 获取消息详细
  151. /// </summary>
  152. /// <param name="dto">msgInfo请求dto</param>
  153. /// <returns></returns>
  154. public async Task<Result> GetMsgInfo(MsgInfoDto dto)
  155. {
  156. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  157. if (dto.PortType == 1 || dto.PortType == 2)
  158. {
  159. string msgSqlWhere = string.Format(@"Select sm.Type,sc.CompanyName,sd.DepName,sjp.JobName,su.CnName,sm.ReleaseTime,
  160. sm.Title,sm.Content,smra.IsRead,smra.ReadTime
  161. From Sys_Message sm
  162. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  163. Inner Join Sys_Users su On sm.IssuerId = su.id
  164. Inner Join Sys_JobPost sjp On su.JobPostId = sjp.Id
  165. Inner Join Sys_Department sd On su.DepId = sd.Id
  166. Inner Join Sys_Company sc On su.CompanyId = sc.Id
  167. Where sm.IsDel = 0
  168. And sm.Id = {0}
  169. And smra.IsDel = 0", dto.MsgId);
  170. var _readableMsgInfo = await _sqlSugar.SqlQueryable<MessageInfoView>(msgSqlWhere).FirstAsync();
  171. if (_readableMsgInfo != null)
  172. {
  173. result.Code = 0;
  174. result.Msg = "成功!";
  175. result.Data = _readableMsgInfo;
  176. }
  177. else
  178. {
  179. result.Msg = "暂无该用户的消息!";
  180. }
  181. }
  182. return result;
  183. }
  184. /// <summary>
  185. /// 消息设置已读
  186. /// </summary>
  187. /// <param name="dto"></param>
  188. /// <returns></returns>
  189. public async Task<Result> SetMsgRead(MsgSetReadDto dto)
  190. {
  191. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  192. if (dto.PortType == 1 || dto.PortType == 2)
  193. {
  194. var msgReadStatus = await _sqlSugar.Updateable<Sys_MessageReadAuth>()
  195. .Where(a => a.Id == dto.MsgAnthId)
  196. .SetColumns(a => new Sys_MessageReadAuth
  197. {
  198. IsRead = 1
  199. }).ExecuteCommandAsync();
  200. if (msgReadStatus > 0)
  201. {
  202. result.Code = 0;
  203. result.Msg = "成功!";
  204. }
  205. else
  206. {
  207. result.Msg = "失败!";
  208. }
  209. }
  210. return result;
  211. }
  212. /// <summary>
  213. /// 消息设置已读
  214. /// </summary>
  215. /// <param name="dto"></param>
  216. /// <returns></returns>
  217. public async Task<Result> DelMsg(MsgDeleteDto dto)
  218. {
  219. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  220. if (dto.PortType == 1 || dto.PortType == 2)
  221. {
  222. var msgReadStatus = await _sqlSugar.Updateable<Sys_Message>()
  223. .Where(a => a.Id == dto.MsgId)
  224. .SetColumns(a => new Sys_Message
  225. {
  226. IsDel = 1,
  227. DeleteUserId = dto.UserId,
  228. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  229. }).ExecuteCommandAsync();
  230. if (msgReadStatus > 0)
  231. {
  232. result.Code = 0;
  233. result.Msg = "成功!";
  234. }
  235. else
  236. {
  237. result.Msg = "操作失败!";
  238. }
  239. }
  240. return result;
  241. }
  242. }
  243. }