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