MessageRepository.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using Newtonsoft.Json;
  2. using NPOI.POIFS.Crypt.Dsig;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Dtos.System;
  6. using OASystem.Domain.ViewModels.QiYeWeChat;
  7. using OASystem.Infrastructure.Tools;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace OASystem.Infrastructure.Repositories.System
  16. {
  17. public class MessageRepository : BaseRepository<Sys_Message, MessageView>
  18. {
  19. private readonly SetDataRepository _setData;
  20. public MessageRepository(SqlSugarClient sqlSugar, SetDataRepository setData)
  21. : base(sqlSugar)
  22. {
  23. _setData = setData;
  24. }
  25. /// <summary>
  26. /// 发布消息
  27. /// </summary>
  28. /// <param name="msgDto"></param>
  29. /// <returns></returns>
  30. public async Task<bool> AddMsg(MessageDto msgDto)
  31. {
  32. #region 参数处理
  33. if (msgDto == null) { return false; }
  34. if (string.IsNullOrEmpty(msgDto.Title)) { return false; }
  35. if (string.IsNullOrEmpty(msgDto.Content)) { return false; }
  36. if (msgDto.UIdList.Count <= 0) { return false; }
  37. #endregion
  38. _sqlSugar.BeginTran();
  39. try
  40. {
  41. Sys_Message message = new Sys_Message()
  42. {
  43. Type = msgDto.Type,
  44. IssuerId = msgDto.IssuerId,
  45. Title = msgDto.Title,
  46. Content = msgDto.Content,
  47. ReleaseTime = msgDto.ReleaseTime,
  48. CreateUserId = msgDto.IssuerId,
  49. CreateTime = DateTime.Now,
  50. DeleteUserId = null,
  51. DeleteTime = "1990-01-01 00:00:00.000",
  52. Remark = "",
  53. IsDel = 0,
  54. DiId = msgDto.DiId,
  55. Param = msgDto.Param,
  56. };
  57. int? msgId = _sqlSugar.Insertable(message).ExecuteReturnIdentity();
  58. if (!msgId.HasValue) { _sqlSugar.RollbackTran(); return false; }
  59. List<Sys_MessageReadAuth> messageReadAuths = new List<Sys_MessageReadAuth>();
  60. foreach (int item in msgDto.UIdList)
  61. {
  62. Sys_MessageReadAuth messageReadAuth = new Sys_MessageReadAuth()
  63. {
  64. MsgId = msgId.Value,
  65. ReadableUId = item,
  66. ReadTime = new DateTime(1990, 1, 1),
  67. CreateUserId = msgDto.IssuerId,
  68. CreateTime = DateTime.Now,
  69. DeleteUserId = null,
  70. DeleteTime = "1990-01-01 00:00:00.000",
  71. Remark = "",
  72. IsDel = 0
  73. };
  74. messageReadAuths.Add(messageReadAuth);
  75. }
  76. int? readIds = _sqlSugar.Insertable<Sys_MessageReadAuth>(messageReadAuths).ExecuteCommand();
  77. if (!readIds.HasValue)
  78. {
  79. _sqlSugar.RollbackTran();
  80. return false;
  81. }
  82. _sqlSugar.CommitTran();
  83. }
  84. catch (Exception)
  85. {
  86. _sqlSugar.RollbackTran();
  87. return false;
  88. }
  89. return true;
  90. }
  91. /// <summary>
  92. /// 获取消息列表
  93. /// </summary>
  94. /// <param name="uId">可读用户Id</param>
  95. /// <returns></returns>
  96. public async Task<Result> GetMsgList(MsgDto dto)
  97. {
  98. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  99. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3) // web/android
  100. {
  101. string msgSqlWhere = string.Format(@"Select sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser,
  102. sm.ReleaseTime,smra.Id AuthId,smra.ReadableUId,smra.IsRead,smra.ReadTime
  103. From Sys_Message sm
  104. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  105. Inner Join Sys_Users su On sm.IssuerId = su.Id
  106. Inner Join Sys_Department sd On su.DepId = sd.Id
  107. Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
  108. Where sm.IsDel = 0
  109. And smra.IsDel = 0
  110. And smra.ReadableUId = {0}
  111. Order By ReleaseTime Desc ", dto.UserId);
  112. var _readableMsgList = await _sqlSugar.SqlQueryable<ReadbleMessageView1>(msgSqlWhere).ToListAsync();
  113. if (_readableMsgList.Count > 0)
  114. {
  115. int pageSize = dto.PageSize; // 每页显示的记录数量
  116. int currentPage = dto.PageIndex; // 当前页码(从1开始)
  117. //操作通知 OperationNotification
  118. List<int> operationTypeList = new List<int>() {1,2,3,4,5 };
  119. var operationNotificationData = _readableMsgList.Where(it => operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  120. int operationNotificationDataCount = operationNotificationData.Count;
  121. // 计算起始索引和结束索引
  122. int operationStartIndex = (currentPage - 1) * pageSize;
  123. int operationEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificationDataCount);
  124. var operationNotificationDataView = new {
  125. Count = operationNotificationDataCount,
  126. UnReadCount = operationNotificationData.Where(it => it.IsRead == 0).Count(),
  127. OperationNotificatioData = operationNotificationData.Skip(operationStartIndex).Take(pageSize).ToList()
  128. };
  129. //任务通知 TaskNotification
  130. List<int> taskTypeList = new List<int>() { 6 };
  131. var taskNotificationData = _readableMsgList.Where(it => taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  132. int taskNotificationDataCount = taskNotificationData.Count;
  133. // 计算起始索引和结束索引
  134. int taskStartIndex = (currentPage - 1) * pageSize;
  135. int taskEndIndex = Math.Min(taskStartIndex + pageSize, taskNotificationDataCount);
  136. var taskNotificationDataView = new
  137. {
  138. Count = taskNotificationDataCount,
  139. UnReadCount = taskNotificationData.Where(it => it.IsRead == 0).Count(),
  140. TaskNotificationData = taskNotificationData.Skip(taskStartIndex).Take(pageSize).ToList()
  141. };
  142. var _view = new {
  143. OperationNotification = operationNotificationDataView,
  144. TaskNotification = taskNotificationDataView
  145. };
  146. result.Code = 0;
  147. result.Msg = "成功!";
  148. result.Data = _view;
  149. }
  150. else
  151. {
  152. result.Msg = "暂无该用户的消息!";
  153. }
  154. }
  155. return result;
  156. }
  157. /// <summary>
  158. /// 获取消息类型列表
  159. /// </summary>
  160. /// <param name="uId">可读用户Id</param>
  161. /// <returns></returns>
  162. public async Task<Result> PotsMsgTypeData(MsgTypeDto dto)
  163. {
  164. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  165. //userId
  166. string msgSqlWhere = $" And smra.ReadableUId = {dto.UserId}";
  167. //消息类型
  168. List<NotificationTypeView> messageTypeViews = AppSettingsHelper.Get<NotificationTypeView>("MessageNotificationType");
  169. List<int> _operationTypeList = messageTypeViews.Where(it =>it.TypeId == 1021).FirstOrDefault().MsgTypeIds.ToList();
  170. List<int> _taskTypeList = messageTypeViews.Where(it => it.TypeId == 1020).FirstOrDefault().MsgTypeIds.ToList();
  171. List<int> _noticeTypeList = messageTypeViews.Where(it => it.TypeId == 1022).FirstOrDefault().MsgTypeIds.ToList();
  172. string msgSql = string.Format(@"Select * From(
  173. Select row_number() over(order by sm.ReleaseTime Desc) as RowNumber,
  174. sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser,
  175. sm.ReleaseTime,smra.ReadableUId,smra.IsRead,sm.DiId,sm.Param
  176. From Sys_Message sm
  177. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  178. Inner Join Sys_Users su On sm.IssuerId = su.Id
  179. Inner Join Sys_Department sd On su.DepId = sd.Id
  180. Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
  181. Where sm.IsDel = 0
  182. And smra.IsDel = 0 {0}
  183. ) Temp", msgSqlWhere);
  184. var data = await _sqlSugar.SqlQueryable<MessageListView>(msgSql).ToListAsync();
  185. if (data.Count > 0)
  186. {
  187. var msgTypeResult = await _setData.GetSetDataBySTId(_setData, 77);
  188. if (msgTypeResult.Code != 0)
  189. {
  190. result.Msg = "消息类型不存在!";
  191. return result;
  192. }
  193. string msgTypeDataStr = JsonConvert.SerializeObject(msgTypeResult.Data);
  194. var msgTypeData = JsonConvert.DeserializeObject<List<MessageTypeView>>(msgTypeDataStr);
  195. //操作通知 OperationNotification
  196. var operationNotificationData = data.Where(it => _operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  197. int operationNotificationDataCount = operationNotificationData.Where(it => it.IsRead == 0).ToList().Count;
  198. //任务通知
  199. var taskNotificationData = data.Where(it => _taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();//操作通知所属类型
  200. int taskNotificationDataCount = taskNotificationData.Where(it => it.IsRead == 0).ToList().Count; //任务通知 TaskNotification
  201. //公告通知 notice
  202. var noticeNotificationData = data.Where(it => _noticeTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();//操作通知所属类型
  203. int noticeNotificationDataCount = noticeNotificationData.Where(it => it.IsRead == 0).ToList().Count; //任务通知 TaskNotification
  204. foreach (var item in msgTypeData)
  205. {
  206. //1021 团组操作通知 1020 任务操作通知 1022 公告通知
  207. if (item.Id == 1020) item.UnReadCount = taskNotificationDataCount;
  208. else if (item.Id == 1021) item.UnReadCount = operationNotificationDataCount;
  209. else if (item.Id == 1022) item.UnReadCount = noticeNotificationDataCount;
  210. }
  211. if (dto.PortType == 1 || dto.PortType == 2) // web/android
  212. {
  213. result.Code = 0;
  214. result.Data = msgTypeData;
  215. }
  216. else if (dto.PortType == 3)
  217. {
  218. result.Code = 0;
  219. result.Data = new {
  220. MsgTypeData = msgTypeData,
  221. FirstUnreadData = data[0],
  222. UnreadTotalCount = data.Where(it => it.IsRead == 0).ToList().Count,
  223. };
  224. }
  225. }
  226. return result;
  227. }
  228. /// <summary>
  229. /// 获取消息未读消息条数
  230. /// </summary>
  231. /// <param name="uId">可读用户Id</param>
  232. /// <returns></returns>
  233. public async Task<int> GetUnReadCount(int userId)
  234. {
  235. int _unReadCount = 0;
  236. string msgSqlWhere = $"Select COUNT(*) As UnreadCount From Sys_MessageReadAuth smra Left Join Sys_Message sm On sm.Id = smra.MsgId Where IsRead = 0 And smra.IsDel = 0 And sm.IsDel = 0 And ReadableUId = {userId}";
  237. var _readableMsg = _sqlSugar.SqlQueryable<MessageUnreadTotalCountView>(msgSqlWhere).First();
  238. _unReadCount = _readableMsg.UnreadCount;
  239. return _unReadCount;
  240. }
  241. /// <summary>
  242. /// 获取消息详细
  243. /// </summary>
  244. /// <param name="dto">msgInfo请求dto</param>
  245. /// <returns></returns>
  246. public async Task<Result> GetMsgInfo(MsgInfoDto dto)
  247. {
  248. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  249. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  250. {
  251. string msgSqlWhere = string.Format(@"Select sm.Type,sc.CompanyName,sd.DepName,sjp.JobName,su.CnName,sm.ReleaseTime,
  252. sm.Title,sm.Content,smra.IsRead,smra.ReadTime,sm.DiId,sm.Param
  253. From Sys_Message sm
  254. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  255. Inner Join Sys_Users su On sm.IssuerId = su.id
  256. Inner Join Sys_JobPost sjp On su.JobPostId = sjp.Id
  257. Inner Join Sys_Department sd On su.DepId = sd.Id
  258. Inner Join Sys_Company sc On su.CompanyId = sc.Id
  259. Where sm.IsDel = 0
  260. And sm.Id = {0}
  261. And smra.IsDel = 0
  262. And smra.ReadableUId = {1}", dto.MsgId,dto.UserId);
  263. var _readableMsgInfo = await _sqlSugar.SqlQueryable<MessageInfoView>(msgSqlWhere).FirstAsync();
  264. if (_readableMsgInfo != null)
  265. {
  266. result.Code = 0;
  267. result.Msg = "成功!";
  268. result.Data = _readableMsgInfo;
  269. }
  270. else
  271. {
  272. result.Msg = "暂无该用户的消息!";
  273. }
  274. }
  275. return result;
  276. }
  277. /// <summary>
  278. /// 消息设置已读
  279. /// </summary>
  280. /// <param name="dto"></param>
  281. /// <returns></returns>
  282. public async Task<Result> SetMsgRead(MsgSetReadDto dto)
  283. {
  284. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  285. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  286. {
  287. var msgReadStatus = await _sqlSugar.Updateable<Sys_MessageReadAuth>()
  288. .SetColumns(a => new Sys_MessageReadAuth
  289. {
  290. IsRead = 1,
  291. ReadTime = DateTime.Now,
  292. })
  293. .Where(a => a.ReadableUId == dto.UserId && a.MsgId == dto.MsgId)
  294. .ExecuteCommandAsync();
  295. if (msgReadStatus > 0)
  296. {
  297. result.Code = 0;
  298. result.Msg = "成功!";
  299. }
  300. else
  301. {
  302. result.Msg = "失败!";
  303. }
  304. }
  305. return result;
  306. }
  307. /// <summary>
  308. /// 消息设置已读
  309. /// </summary>
  310. /// <param name="dto"></param>
  311. /// <returns></returns>
  312. public async Task<Result> DelMsg(MsgDeleteDto dto)
  313. {
  314. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  315. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  316. {
  317. var msgReadStatus = await _sqlSugar.Updateable<Sys_Message>()
  318. .Where(a => a.Id == dto.MsgId)
  319. .SetColumns(a => new Sys_Message
  320. {
  321. IsDel = 1,
  322. DeleteUserId = dto.UserId,
  323. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  324. }).ExecuteCommandAsync();
  325. if (msgReadStatus > 0)
  326. {
  327. result.Code = 0;
  328. result.Msg = "成功!";
  329. }
  330. else
  331. {
  332. result.Msg = "操作失败!";
  333. }
  334. }
  335. return result;
  336. }
  337. }
  338. }