NoticeService.Insert.cs 12 KB

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