MessageRepository.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 = new object[] { } };
  165. //消息类型
  166. List<NotificationTypeView> messageTypeViews = AppSettingsHelper.Get<NotificationTypeView>("MessageNotificationType");
  167. List<int> _operationTypeList = messageTypeViews.Where(it =>it.TypeId == 1021).FirstOrDefault().MsgTypeIds.ToList();
  168. List<int> _taskTypeList = messageTypeViews.Where(it => it.TypeId == 1020).FirstOrDefault().MsgTypeIds.ToList();
  169. List<int> _noticeTypeList = messageTypeViews.Where(it => it.TypeId == 1022).FirstOrDefault().MsgTypeIds.ToList();
  170. //userId
  171. string msgSqlWhere = $" And smra.ReadableUId = {dto.UserId}";
  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. var msgTypeResult = await _setData.GetSetDataBySTId(_setData, 77);
  186. if (msgTypeResult.Code != 0)
  187. {
  188. result.Msg = "消息类型不存在!";
  189. return result;
  190. }
  191. string msgTypeDataStr = JsonConvert.SerializeObject(msgTypeResult.Data);
  192. var msgTypeData = JsonConvert.DeserializeObject<List<MessageTypeView>>(msgTypeDataStr);
  193. //操作通知 OperationNotification
  194. var operationNotificationData = data.Where(it => _operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
  195. int operationNotificationDataCount = operationNotificationData.Where(it => it.IsRead == 0).ToList().Count;
  196. //任务通知
  197. var taskNotificationData = data.Where(it => _taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();//操作通知所属类型
  198. int taskNotificationDataCount = taskNotificationData.Where(it => it.IsRead == 0).ToList().Count; //任务通知 TaskNotification
  199. //公告通知 notice
  200. var noticeNotificationData = data.Where(it => _noticeTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();//操作通知所属类型
  201. int noticeNotificationDataCount = noticeNotificationData.Where(it => it.IsRead == 0).ToList().Count; //任务通知 TaskNotification
  202. foreach (var item in msgTypeData)
  203. {
  204. //1021 团组操作通知 1020 任务操作通知 1022 公告通知
  205. if (item.Id == 1020) item.UnReadCount = taskNotificationDataCount;
  206. else if (item.Id == 1021) item.UnReadCount = operationNotificationDataCount;
  207. else if (item.Id == 1022) item.UnReadCount = noticeNotificationDataCount;
  208. }
  209. if (dto.PortType == 1 || dto.PortType == 2) // web/android
  210. {
  211. result.Code = 0;
  212. result.Msg = $"操作成功";
  213. result.Data = msgTypeData;
  214. }
  215. else if (dto.PortType == 3)
  216. {
  217. result.Code = 0;
  218. result.Msg = $"操作成功";
  219. result.Data = new
  220. {
  221. MsgTypeData = msgTypeData,
  222. FirstUnreadData = data[0],
  223. UnreadTotalCount = data.Where(it => it.IsRead == 0).ToList().Count,
  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="uId">可读用户Id</param>
  245. /// <returns></returns>
  246. public async Task<int> GetAnnouncementUnReadCount(int userId)
  247. {
  248. int _unReadCount = 0;
  249. 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 sm.Type = 1 And ReadableUId = {userId}";
  250. var _readableMsg = _sqlSugar.SqlQueryable<MessageUnreadTotalCountView>(msgSqlWhere).First();
  251. _unReadCount = _readableMsg.UnreadCount;
  252. return _unReadCount;
  253. }
  254. /// <summary>
  255. /// 获取消息详细
  256. /// </summary>
  257. /// <param name="dto">msgInfo请求dto</param>
  258. /// <returns></returns>
  259. public async Task<Result> GetMsgInfo(MsgInfoDto dto)
  260. {
  261. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  262. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  263. {
  264. string msgSqlWhere = string.Format(@"Select sm.Type,sc.CompanyName,sd.DepName,sjp.JobName,su.CnName,sm.ReleaseTime,
  265. sm.Title,sm.Content,smra.IsRead,smra.ReadTime,sm.DiId,sm.Param
  266. From Sys_Message sm
  267. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  268. Inner Join Sys_Users su On sm.IssuerId = su.id
  269. Inner Join Sys_JobPost sjp On su.JobPostId = sjp.Id
  270. Inner Join Sys_Department sd On su.DepId = sd.Id
  271. Inner Join Sys_Company sc On su.CompanyId = sc.Id
  272. Where sm.IsDel = 0
  273. And sm.Id = {0}
  274. And smra.IsDel = 0
  275. And smra.ReadableUId = {1}", dto.MsgId,dto.UserId);
  276. var _readableMsgInfo = await _sqlSugar.SqlQueryable<MessageInfoView>(msgSqlWhere).FirstAsync();
  277. if (_readableMsgInfo != null)
  278. {
  279. result.Code = 0;
  280. result.Msg = "成功!";
  281. result.Data = _readableMsgInfo;
  282. }
  283. else
  284. {
  285. result.Msg = "暂无该用户的消息!";
  286. }
  287. }
  288. return result;
  289. }
  290. /// <summary>
  291. /// 消息设置已读
  292. /// </summary>
  293. /// <param name="dto"></param>
  294. /// <returns></returns>
  295. public async Task<Result> SetMsgRead(MsgSetReadDto dto)
  296. {
  297. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  298. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  299. {
  300. var msgReadStatus = await _sqlSugar.Updateable<Sys_MessageReadAuth>()
  301. .SetColumns(a => new Sys_MessageReadAuth
  302. {
  303. IsRead = 1,
  304. ReadTime = DateTime.Now,
  305. })
  306. .Where(a => a.ReadableUId == dto.UserId && a.MsgId == dto.MsgId)
  307. .ExecuteCommandAsync();
  308. if (msgReadStatus > 0)
  309. {
  310. result.Code = 0;
  311. result.Msg = "成功!";
  312. }
  313. else
  314. {
  315. result.Msg = "失败!";
  316. }
  317. }
  318. return result;
  319. }
  320. /// <summary>
  321. /// 消息设置已读
  322. /// </summary>
  323. /// <param name="dto"></param>
  324. /// <returns></returns>
  325. public async Task<Result> DelMsg(MsgDeleteDto dto)
  326. {
  327. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  328. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  329. {
  330. var msgReadStatus = await _sqlSugar.Updateable<Sys_Message>()
  331. .Where(a => a.Id == dto.MsgId)
  332. .SetColumns(a => new Sys_Message
  333. {
  334. IsDel = 1,
  335. DeleteUserId = dto.UserId,
  336. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  337. }).ExecuteCommandAsync();
  338. if (msgReadStatus > 0)
  339. {
  340. result.Code = 0;
  341. result.Msg = "成功!";
  342. }
  343. else
  344. {
  345. result.Msg = "操作失败!";
  346. }
  347. }
  348. return result;
  349. }
  350. }
  351. }