MessageRepository.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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="uId">可读用户Id</param>
  153. /// <returns></returns>
  154. public async Task<int> GetUnReadCount(int userId)
  155. {
  156. int _unReadCount = 0;
  157. string msgSqlWhere = string.Format(@"Select sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser,
  158. sm.ReleaseTime,smra.Id AuthId,smra.ReadableUId,smra.IsRead,smra.ReadTime
  159. From Sys_Message sm
  160. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  161. Inner Join Sys_Users su On sm.IssuerId = su.Id
  162. Inner Join Sys_Department sd On su.DepId = sd.Id
  163. Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
  164. Where sm.IsDel = 0
  165. And smra.IsDel = 0
  166. And smra.ReadableUId = {0}
  167. Order By ReleaseTime Desc ", userId);
  168. var _readableMsgList = await _sqlSugar.SqlQueryable<ReadbleMessageView>(msgSqlWhere).ToListAsync();
  169. if (_readableMsgList.Count > 0)
  170. {
  171. _unReadCount = _readableMsgList.Where(it => it.IsRead == 0).Count();
  172. }
  173. return _unReadCount;
  174. }
  175. /// <summary>
  176. /// 获取消息详细
  177. /// </summary>
  178. /// <param name="dto">msgInfo请求dto</param>
  179. /// <returns></returns>
  180. public async Task<Result> GetMsgInfo(MsgInfoDto dto)
  181. {
  182. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  183. if (dto.PortType == 1 || dto.PortType == 2)
  184. {
  185. string msgSqlWhere = string.Format(@"Select sm.Type,sc.CompanyName,sd.DepName,sjp.JobName,su.CnName,sm.ReleaseTime,
  186. sm.Title,sm.Content,smra.IsRead,smra.ReadTime
  187. From Sys_Message sm
  188. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  189. Inner Join Sys_Users su On sm.IssuerId = su.id
  190. Inner Join Sys_JobPost sjp On su.JobPostId = sjp.Id
  191. Inner Join Sys_Department sd On su.DepId = sd.Id
  192. Inner Join Sys_Company sc On su.CompanyId = sc.Id
  193. Where sm.IsDel = 0
  194. And sm.Id = {0}
  195. And smra.IsDel = 0", dto.MsgId);
  196. var _readableMsgInfo = await _sqlSugar.SqlQueryable<MessageInfoView>(msgSqlWhere).FirstAsync();
  197. if (_readableMsgInfo != null)
  198. {
  199. result.Code = 0;
  200. result.Msg = "成功!";
  201. result.Data = _readableMsgInfo;
  202. }
  203. else
  204. {
  205. result.Msg = "暂无该用户的消息!";
  206. }
  207. }
  208. return result;
  209. }
  210. /// <summary>
  211. /// 消息设置已读
  212. /// </summary>
  213. /// <param name="dto"></param>
  214. /// <returns></returns>
  215. public async Task<Result> SetMsgRead(MsgSetReadDto dto)
  216. {
  217. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  218. if (dto.PortType == 1 || dto.PortType == 2)
  219. {
  220. var msgReadStatus = await _sqlSugar.Updateable<Sys_MessageReadAuth>()
  221. .Where(a => a.Id == dto.MsgAnthId)
  222. .SetColumns(a => new Sys_MessageReadAuth
  223. {
  224. IsRead = 1
  225. }).ExecuteCommandAsync();
  226. if (msgReadStatus > 0)
  227. {
  228. result.Code = 0;
  229. result.Msg = "成功!";
  230. }
  231. else
  232. {
  233. result.Msg = "失败!";
  234. }
  235. }
  236. return result;
  237. }
  238. /// <summary>
  239. /// 消息设置已读
  240. /// </summary>
  241. /// <param name="dto"></param>
  242. /// <returns></returns>
  243. public async Task<Result> DelMsg(MsgDeleteDto dto)
  244. {
  245. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  246. if (dto.PortType == 1 || dto.PortType == 2)
  247. {
  248. var msgReadStatus = await _sqlSugar.Updateable<Sys_Message>()
  249. .Where(a => a.Id == dto.MsgId)
  250. .SetColumns(a => new Sys_Message
  251. {
  252. IsDel = 1,
  253. DeleteUserId = dto.UserId,
  254. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  255. }).ExecuteCommandAsync();
  256. if (msgReadStatus > 0)
  257. {
  258. result.Code = 0;
  259. result.Msg = "成功!";
  260. }
  261. else
  262. {
  263. result.Msg = "操作失败!";
  264. }
  265. }
  266. return result;
  267. }
  268. }
  269. }