NoticeService.Insert.cs 13 KB

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