MessageRepository.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. {
  221. MsgTypeData = msgTypeData,
  222. FirstUnreadData = data[0],
  223. UnreadTotalCount = data.Where(it => it.IsRead == 0).ToList().Count,
  224. };
  225. }
  226. }
  227. else
  228. {
  229. result.Code = 0;
  230. result.Msg = "暂无消息";
  231. }
  232. return result;
  233. }
  234. /// <summary>
  235. /// 获取消息未读消息条数
  236. /// </summary>
  237. /// <param name="uId">可读用户Id</param>
  238. /// <returns></returns>
  239. public async Task<int> GetUnReadCount(int userId)
  240. {
  241. int _unReadCount = 0;
  242. 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}";
  243. var _readableMsg = _sqlSugar.SqlQueryable<MessageUnreadTotalCountView>(msgSqlWhere).First();
  244. _unReadCount = _readableMsg.UnreadCount;
  245. return _unReadCount;
  246. }
  247. /// <summary>
  248. /// 获取消息详细
  249. /// </summary>
  250. /// <param name="dto">msgInfo请求dto</param>
  251. /// <returns></returns>
  252. public async Task<Result> GetMsgInfo(MsgInfoDto dto)
  253. {
  254. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  255. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  256. {
  257. string msgSqlWhere = string.Format(@"Select sm.Type,sc.CompanyName,sd.DepName,sjp.JobName,su.CnName,sm.ReleaseTime,
  258. sm.Title,sm.Content,smra.IsRead,smra.ReadTime,sm.DiId,sm.Param
  259. From Sys_Message sm
  260. Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
  261. Inner Join Sys_Users su On sm.IssuerId = su.id
  262. Inner Join Sys_JobPost sjp On su.JobPostId = sjp.Id
  263. Inner Join Sys_Department sd On su.DepId = sd.Id
  264. Inner Join Sys_Company sc On su.CompanyId = sc.Id
  265. Where sm.IsDel = 0
  266. And sm.Id = {0}
  267. And smra.IsDel = 0
  268. And smra.ReadableUId = {1}", dto.MsgId,dto.UserId);
  269. var _readableMsgInfo = await _sqlSugar.SqlQueryable<MessageInfoView>(msgSqlWhere).FirstAsync();
  270. if (_readableMsgInfo != null)
  271. {
  272. result.Code = 0;
  273. result.Msg = "成功!";
  274. result.Data = _readableMsgInfo;
  275. }
  276. else
  277. {
  278. result.Msg = "暂无该用户的消息!";
  279. }
  280. }
  281. return result;
  282. }
  283. /// <summary>
  284. /// 消息设置已读
  285. /// </summary>
  286. /// <param name="dto"></param>
  287. /// <returns></returns>
  288. public async Task<Result> SetMsgRead(MsgSetReadDto dto)
  289. {
  290. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  291. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  292. {
  293. var msgReadStatus = await _sqlSugar.Updateable<Sys_MessageReadAuth>()
  294. .SetColumns(a => new Sys_MessageReadAuth
  295. {
  296. IsRead = 1,
  297. ReadTime = DateTime.Now,
  298. })
  299. .Where(a => a.ReadableUId == dto.UserId && a.MsgId == dto.MsgId)
  300. .ExecuteCommandAsync();
  301. if (msgReadStatus > 0)
  302. {
  303. result.Code = 0;
  304. result.Msg = "成功!";
  305. }
  306. else
  307. {
  308. result.Msg = "失败!";
  309. }
  310. }
  311. return result;
  312. }
  313. /// <summary>
  314. /// 消息设置已读
  315. /// </summary>
  316. /// <param name="dto"></param>
  317. /// <returns></returns>
  318. public async Task<Result> DelMsg(MsgDeleteDto dto)
  319. {
  320. Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
  321. if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)
  322. {
  323. var msgReadStatus = await _sqlSugar.Updateable<Sys_Message>()
  324. .Where(a => a.Id == dto.MsgId)
  325. .SetColumns(a => new Sys_Message
  326. {
  327. IsDel = 1,
  328. DeleteUserId = dto.UserId,
  329. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  330. }).ExecuteCommandAsync();
  331. if (msgReadStatus > 0)
  332. {
  333. result.Code = 0;
  334. result.Msg = "成功!";
  335. }
  336. else
  337. {
  338. result.Msg = "操作失败!";
  339. }
  340. }
  341. return result;
  342. }
  343. }
  344. }