NoticeService.Insert.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Transactions;
  4. using GxPress.Common.Exceptions;
  5. using GxPress.Entity;
  6. using GxPress.Request.App.Middle;
  7. using GxPress.Request.Notice;
  8. using GxPress.EnumConst;
  9. using System.Linq;
  10. using System.Collections.Generic;
  11. using Datory;
  12. namespace GxPress.Service.Implement.Notice
  13. {
  14. public partial class NoticeService
  15. {
  16. public async Task<bool> InsertAsync(NoticeInRequest request)
  17. {
  18. if (string.IsNullOrEmpty(request.Content))
  19. throw new BusinessException("内容必填");
  20. if ((request.AddresseeUserMiddles.Count == 0 && request.NoticeType == NoticeTypeConst.Send))
  21. throw new BusinessException("收件人未选择");
  22. try
  23. {
  24. using (TransactionScope transactionScope = new TransactionScope())
  25. {
  26. var userEntity = await _userRepository.GetAsync(request.UserId);
  27. if (userEntity == null)
  28. throw new BusinessException("用户不存在");
  29. var noticeId = 0;
  30. if (request.Id == 0)
  31. {
  32. noticeId = await _noticeRepository.InsertAsync(request);
  33. var middleInsertTypes = new MiddleInsertTypeRequest
  34. {
  35. FolderId = 0,
  36. MiddleId = noticeId,
  37. FolderType = 1,
  38. UserId = request.UserId,
  39. MiddleSonId = 0,
  40. NoticeAddresseeType = request.NoticeType,
  41. AttributeValue = 1,
  42. IsAdmin = true
  43. };
  44. middleInsertTypes.FolderId = request.FolderId;
  45. //添加自己
  46. await _middleService.InsertAsync(middleInsertTypes);
  47. }
  48. else
  49. {
  50. noticeId = request.Id;
  51. var notice = await _noticeRepository.GetAsync(noticeId);
  52. notice.NoticeType = request.NoticeType;
  53. notice.Content = request.Content;
  54. notice.Title = request.Title;
  55. notice.HtmlContent = request.HtmlContent;
  56. notice.SourceId = request.SoureId;
  57. await _noticeRepository.UpdateNoticeAsync(notice);
  58. //删除middle
  59. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), noticeId).Where(nameof(Entity.Middle.Middle.FolderType), 1).Where(nameof(Entity.Middle.Middle.IsAdmin), false));
  60. //删除收件人
  61. await _noticeAddresseeRepository.DeleteAsync(noticeId);
  62. //删除抄送人
  63. await _noticeCcRepository.DeleteAsync(noticeId);
  64. //删除userMiddle
  65. await _userMiddleService.DeleteAsync(noticeId, 10);
  66. await _userMiddleService.DeleteAsync(noticeId, 20);
  67. }
  68. //判断是不是新版通知
  69. if (request.AddresseeUserMiddles.Count > 0)
  70. {
  71. //删除收件人
  72. await _userMiddleService.DeleteAsync(request.Id, 10);
  73. foreach (var item in request.AddresseeUserMiddles)
  74. {
  75. item.DataSourceId = noticeId;
  76. item.MiddleType = 10;
  77. }
  78. await _userMiddleService.AddUserMiddleAsync(request.AddresseeUserMiddles);
  79. //获取用户
  80. var users = await _userMiddleService.FindUsersAsync(request.AddresseeUserMiddles, request.UserId);
  81. request.NoticeAddresseeUserIds = users.Select(n => n.Id).ToList();
  82. }
  83. if (request.CcUserMiddles.Count > 0)
  84. {
  85. //删除抄送人
  86. await _userMiddleService.DeleteAsync(request.Id, 20);
  87. foreach (var item in request.CcUserMiddles)
  88. {
  89. item.DataSourceId = noticeId;
  90. item.MiddleType = 20;
  91. }
  92. await _userMiddleService.AddUserMiddleAsync(request.CcUserMiddles);
  93. var users = await _userMiddleService.FindUsersAsync(request.CcUserMiddles, request.UserId);
  94. request.NoticeCcUserIds = users.Select(n => n.Id).ToList();
  95. //添加抄送人
  96. request.NoticeAddresseeUserIds.AddRange(request.NoticeCcUserIds);
  97. }
  98. //创建人
  99. var noticeAddresseeEntity = new NoticeAddressee
  100. {
  101. NoticeId = noticeId,
  102. UserId = request.UserId,
  103. UserName = userEntity.Name,
  104. IsAdmin = true,
  105. NoticeAddresseeType = request.NoticeType,
  106. IsTop = false,
  107. IsRead = true
  108. };
  109. var noticeAddresseeId = await _noticeAddresseeRepository.InsertAsync(noticeAddresseeEntity);
  110. request.NoticeAddresseeUserIds = request.NoticeAddresseeUserIds.Distinct().ToList();
  111. request.NoticeAddresseeUserIds.AddRange(request.NoticeCcUserIds);
  112. //通知收件人
  113. var noticeAddressees = new List<NoticeAddressee>();
  114. //中间件
  115. var middles = new List<Entity.Middle.Middle>();
  116. //抄送人
  117. var noticeCcs = new List<NoticeCc>();
  118. //收件人
  119. if (request.NoticeType == NoticeTypeConst.Send)
  120. {
  121. foreach (var noticeAddresseeUserId in request.NoticeAddresseeUserIds.Distinct().ToList())
  122. {
  123. if (noticeAddresseeUserId == request.UserId)
  124. continue;
  125. var noticeAddressee = new NoticeAddressee
  126. {
  127. NoticeId = noticeId,
  128. UserId = noticeAddresseeUserId,
  129. NoticeAddresseeType = request.NoticeType,
  130. IsRead = false
  131. };
  132. noticeAddressees.Add(noticeAddressee);
  133. }
  134. }
  135. request.NoticeCcUserIds = request.NoticeCcUserIds.Distinct().ToList();
  136. if (request.NoticeType == NoticeTypeConst.Send)
  137. {
  138. //抄送
  139. foreach (var noticeCcUserId in request.NoticeCcUserIds)
  140. {
  141. if (noticeCcUserId == 0)
  142. continue;
  143. var noticeCc = new NoticeCc
  144. {
  145. UserId = noticeCcUserId,
  146. NoticeId = noticeId,
  147. NoticeCcType = request.NoticeType
  148. };
  149. noticeCcs.Add(noticeCc);
  150. }
  151. }
  152. //添加收件人
  153. await _noticeAddresseeRepository.InsertAsync(noticeAddressees);
  154. //添加草送人
  155. await _noticeCcRepository.InsertAsync(noticeCcs);
  156. //查询添加的收件人
  157. var noticeAddresseelist = await _noticeAddresseeRepository.GetAllAsync(Q.Where(nameof(Entity.NoticeAddressee.NoticeId), noticeId));
  158. if (request.NoticeType == NoticeTypeConst.Send)
  159. {
  160. foreach (var item in noticeAddresseelist)
  161. {
  162. if (item.UserId == request.UserId)
  163. continue;
  164. var middle = new Entity.Middle.Middle
  165. {
  166. MiddleId = noticeId,
  167. FolderType = 1,
  168. IsTop = false,
  169. AttributeValue = 1,
  170. ParentId = 0,
  171. UserId = item.UserId,
  172. MiddleSonId = item.Id,
  173. IsAdmin = false,
  174. NoticeAddresseeType = request.NoticeType,
  175. IsRead = false
  176. };
  177. middles.Add(middle);
  178. }
  179. }
  180. //添加文件
  181. if (request.NoticeType == NoticeTypeConst.Send)
  182. {
  183. //添加收件人
  184. await _middleRepository.InsertAsync(middles);
  185. //发送短信
  186. var userPhones = await _userRepository.GetUserIdsAsync(middles.Select(n => n.UserId).ToList());
  187. Common.Sms.MasSms.SendSmsTemplate(userPhones.ToList(), "dd01d79af7ec46a289cd0fca0cb3b149");
  188. }
  189. transactionScope.Complete();
  190. }
  191. }
  192. catch (Exception e)
  193. {
  194. throw new BusinessException(e.Message);
  195. }
  196. return true;
  197. }
  198. }
  199. }