MessageRepository.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using OASystem.Domain.Dtos.System;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OASystem.Infrastructure.Repositories.System
  8. {
  9. public class MessageRepository : BaseRepository<Sys_Message, MessageView>
  10. {
  11. public MessageRepository(SqlSugarClient sqlSugar) : base(sqlSugar) { }
  12. /// <summary>
  13. /// 发布消息
  14. /// </summary>
  15. /// <param name="msgDto"></param>
  16. /// <returns></returns>
  17. public async Task<bool> AddMsg(MessageDto msgDto)
  18. {
  19. #region 参数处理
  20. if (msgDto == null) { return false; }
  21. if (string.IsNullOrEmpty(msgDto.Title)) { return false; }
  22. if (string.IsNullOrEmpty(msgDto.Content)) { return false; }
  23. if (msgDto.UIdList.Count <= 0) { return false; }
  24. #endregion
  25. _sqlSugar.BeginTran();
  26. try
  27. {
  28. Sys_Message message = new Sys_Message()
  29. {
  30. Type = msgDto.Type,
  31. IssuerId = msgDto.IssuerId,
  32. Title = msgDto.Title,
  33. Content = msgDto.Content,
  34. ReleaseTime = msgDto.ReleaseTime,
  35. CreateUserId = msgDto.IssuerId,
  36. CreateTime = DateTime.Now,
  37. DeleteUserId = null,
  38. DeleteTime = "1990-01-01 00:00:00.000",
  39. Remark = "",
  40. IsDel = 0
  41. };
  42. int? msgId = await _sqlSugar.Insertable(message).ExecuteReturnIdentityAsync();
  43. if (!msgId.HasValue) { _sqlSugar.RollbackTran(); return false; }
  44. List<Sys_MessageReadAuth> messageReadAuths = new List<Sys_MessageReadAuth>();
  45. foreach (int item in msgDto.UIdList)
  46. {
  47. Sys_MessageReadAuth messageReadAuth = new Sys_MessageReadAuth()
  48. {
  49. MsgId = msgId.Value,
  50. ReadableUId = item,
  51. ReadTime = new DateTime(1990,1,1),
  52. CreateUserId = msgDto.IssuerId,
  53. CreateTime = DateTime.Now,
  54. DeleteUserId = null,
  55. DeleteTime = "1990-01-01 00:00:00.000",
  56. Remark = "",
  57. IsDel = 0
  58. };
  59. messageReadAuths.Add(messageReadAuth);
  60. }
  61. int? readIds = await _sqlSugar.Insertable<Sys_MessageReadAuth>(messageReadAuths).ExecuteCommandAsync();
  62. if (!readIds.HasValue)
  63. {
  64. _sqlSugar.RollbackTran();
  65. return false;
  66. }
  67. _sqlSugar.CommitTran();
  68. }
  69. catch (Exception)
  70. {
  71. _sqlSugar.RollbackTran();
  72. return false;
  73. }
  74. return true;
  75. }
  76. }
  77. }