NoticeService.Insert.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. var middleId = 0;
  31. var addresseeUsers = new List<User>();
  32. var ccUsers = new List<User>();
  33. if (request.Id == 0)
  34. {
  35. noticeId = await _noticeRepository.InsertAsync(request);
  36. var middleInsertTypes = new MiddleInsertTypeRequest
  37. {
  38. FolderId = request.FolderId,
  39. MiddleId = noticeId,
  40. FolderType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(),
  41. UserId = request.UserId,
  42. MiddleSonId = 0,
  43. NoticeAddresseeType = request.NoticeType,
  44. AttributeValue = 1,
  45. Title = _contentJsonService.GetTitile(request.Title, request.Content),
  46. IsAdmin = true,
  47. TitleLong = request.Title,
  48. Content = request.Content,
  49. };
  50. //添加自己
  51. middleId = await _middleService.InsertAsync(middleInsertTypes);
  52. }
  53. else
  54. {
  55. noticeId = request.Id;
  56. var notice = await _noticeRepository.GetAsync(noticeId);
  57. notice.NoticeType = request.NoticeType;
  58. notice.Content = request.Content;
  59. notice.Title = request.Title;
  60. notice.HtmlContent = request.HtmlContent;
  61. notice.SourceId = request.SoureId;
  62. await _noticeRepository.UpdateNoticeAsync(notice);
  63. //删除middle
  64. 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));
  65. //删除收件人
  66. await _addresseeRepository.DeleteAsync(noticeId, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(), 2);
  67. //删除抄送人
  68. await _ccRepository.DeleteAsync(noticeId, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode());
  69. //删除userMiddle
  70. await _userMiddleService.DeleteAsync(noticeId, 10);
  71. await _userMiddleService.DeleteAsync(noticeId, 20);
  72. }
  73. //判断是不是新版通知
  74. if (request.AddresseeUserMiddles.Count > 0)
  75. {
  76. //删除收件人
  77. await _userMiddleService.DeleteAsync(request.Id, 10);
  78. foreach (var item in request.AddresseeUserMiddles)
  79. {
  80. item.DataSourceId = noticeId;
  81. item.MiddleType = 10;
  82. }
  83. await _userMiddleService.AddUserMiddleAsync(request.AddresseeUserMiddles);
  84. //获取用户
  85. addresseeUsers = await _userMiddleService.FindUsersAsync(request.AddresseeUserMiddles, request.UserId);
  86. request.NoticeAddresseeUserIds = addresseeUsers.Select(n => n.Id).ToList();
  87. }
  88. if (request.CcUserMiddles.Count > 0)
  89. {
  90. //删除抄送人
  91. await _userMiddleService.DeleteAsync(request.Id, 20);
  92. foreach (var item in request.CcUserMiddles)
  93. {
  94. item.DataSourceId = noticeId;
  95. item.MiddleType = 20;
  96. }
  97. await _userMiddleService.AddUserMiddleAsync(request.CcUserMiddles);
  98. ccUsers = await _userMiddleService.FindUsersAsync(request.CcUserMiddles, request.UserId);
  99. request.NoticeCcUserIds = ccUsers.Select(n => n.Id).ToList();
  100. //添加抄送人
  101. request.NoticeAddresseeUserIds.AddRange(request.NoticeCcUserIds);
  102. }
  103. //创建人
  104. var addresseeEntity = new Addressee
  105. {
  106. SourceId = noticeId,
  107. UserId = request.UserId,
  108. UserName = userEntity.Name,
  109. IsAdmin = true,
  110. NoticeAddresseeType = request.NoticeType,
  111. IsTop = false,
  112. IsRead = true,
  113. SourceType = AllTypeConst.Notice.GetHashCode()
  114. };
  115. var noticeAddresseeId = await _addresseeRepository.InsertAsync(addresseeEntity);
  116. request.NoticeAddresseeUserIds = request.NoticeAddresseeUserIds.Distinct().ToList();
  117. request.NoticeAddresseeUserIds.AddRange(request.NoticeCcUserIds);
  118. //通知收件人
  119. var addressees = new List<Addressee>();
  120. //中间件
  121. var middles = new List<Entity.Middle.Middle>();
  122. //抄送人
  123. var ccs = new List<Cc>();
  124. //收件人
  125. if (request.NoticeType == NoticeTypeConst.Send)
  126. {
  127. foreach (var noticeAddresseeUserId in request.NoticeAddresseeUserIds.Distinct().ToList())
  128. {
  129. if (noticeAddresseeUserId == request.UserId)
  130. continue;
  131. var noticeAddressee = new Addressee
  132. {
  133. SourceId = noticeId,
  134. UserId = noticeAddresseeUserId,
  135. NoticeAddresseeType = request.NoticeType,
  136. IsRead = false,
  137. SourceType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()
  138. };
  139. addressees.Add(noticeAddressee);
  140. }
  141. }
  142. request.NoticeCcUserIds = request.NoticeCcUserIds.Distinct().ToList();
  143. if (request.NoticeType == NoticeTypeConst.Send)
  144. {
  145. //抄送
  146. foreach (var noticeCcUserId in request.NoticeCcUserIds)
  147. {
  148. if (noticeCcUserId == 0)
  149. continue;
  150. var cc = new Cc
  151. {
  152. UserId = noticeCcUserId,
  153. SourceId = noticeId,
  154. SourceType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()
  155. };
  156. ccs.Add(cc);
  157. }
  158. }
  159. //添加收件人
  160. await _addresseeRepository.InsertAsync(addressees);
  161. //添加草送人
  162. await _ccRepository.InsertAsync(ccs);
  163. //查询添加的收件人
  164. var noticeAddresseelist = await _addresseeRepository.GetAllAsync(Q.Where(nameof(Entity.Addressee.SourceId), noticeId));
  165. if (request.NoticeType == NoticeTypeConst.Send)
  166. {
  167. foreach (var item in noticeAddresseelist)
  168. {
  169. if (item.UserId == request.UserId)
  170. continue;
  171. var middle = new Entity.Middle.Middle
  172. {
  173. MiddleId = noticeId,
  174. FolderType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(),
  175. IsTop = false,
  176. AttributeValue = 1,
  177. ParentId = 0,
  178. UserId = item.UserId,
  179. MiddleSonId = item.Id,
  180. IsAdmin = false,
  181. NoticeAddresseeType = request.NoticeType,
  182. Title = _contentJsonService.GetTitile(request.Title, request.Content),
  183. IsRead = false,
  184. TitleLong = request.Title,
  185. Content = request.Content,
  186. AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name)),
  187. AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10),
  188. CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name)),
  189. CcUser = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name), 10),
  190. };
  191. middles.Add(middle);
  192. }
  193. }
  194. //修改middler
  195. if (middleId > 0)
  196. {
  197. var middle = await _middleRepository.GetMiddleAsync(middleId);
  198. middle.AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  199. middle.AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  200. middle.CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  201. middle.CcUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  202. await _middleRepository.UpdateAsync(middle);
  203. }
  204. //添加文件
  205. if (request.NoticeType == NoticeTypeConst.Send)
  206. //添加收件人
  207. await _middleRepository.InsertAsync(middles);
  208. transactionScope.Complete();
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. throw new BusinessException(e.Message);
  214. }
  215. return true;
  216. }
  217. }
  218. }