NoticeService.Insert.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. request.Title = _contentJsonService.GetTitile(request.Title, request.Content);
  35. //添加通知
  36. if (request.Id == 0)
  37. {
  38. noticeId = await _noticeRepository.InsertAsync(request);
  39. if (noticeId == 0) return false;
  40. var middleInsertTypes = new MiddleInsertTypeRequest
  41. {
  42. FolderId = request.FolderId,
  43. MiddleId = noticeId,
  44. FolderType = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode(),
  45. UserId = request.UserId,
  46. MiddleSonId = 0,
  47. NoticeAddresseeType = request.NoticeType,
  48. AttributeValue = 1,
  49. IsAdmin = true
  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), AllTypeConst.Inbox.GetHashCode()).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 = request.TypeValue
  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 = AllTypeConst.Inbox.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. IsRead = false,
  184. IsRecall = false
  185. };
  186. middles.Add(middle);
  187. }
  188. }
  189. // //修改middler
  190. // if (middleId > 0)
  191. // {
  192. // var middle = await _middleRepository.GetMiddleAsync(middleId);
  193. // middle.AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  194. // middle.AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  195. // middle.CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  196. // middle.CcUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  197. // await _middleRepository.UpdateAsync(middle);
  198. // }
  199. //添加文件
  200. if (request.NoticeType == NoticeTypeConst.Send)
  201. //添加收件人
  202. await _middleRepository.InsertAsync(middles);
  203. transactionScope.Complete();
  204. }
  205. }
  206. catch (Exception e)
  207. {
  208. throw new BusinessException(e.Message);
  209. }
  210. return true;
  211. }
  212. /// <summary>
  213. /// 添加收件箱工作
  214. /// </summary>
  215. /// <param name="request"></param>
  216. /// <returns></returns>
  217. public async Task<bool> AddNoticeFlowAsync(NoticeInRequest request)
  218. {
  219. try
  220. {
  221. using (var transactionScope = new TransactionScope())
  222. {
  223. var noticeId = await _noticeRepository.InsertAsync(request);
  224. if (noticeId == 0) return false;
  225. var middleInsertTypes = new MiddleInsertTypeRequest
  226. {
  227. FolderId = request.FolderId,
  228. MiddleId = noticeId,
  229. FolderType = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode(),
  230. UserId = request.UserId,
  231. MiddleSonId = 0,
  232. NoticeAddresseeType = request.NoticeType,
  233. AttributeValue = 1,
  234. //Title = _contentJsonService.GetTitile(request.Title, request.Content),
  235. IsAdmin = true,
  236. //TitleLong = request.Title,
  237. //Content = request.Content,
  238. };
  239. //添加自己
  240. var middleId = await _middleService.InsertAsync(middleInsertTypes);
  241. transactionScope.Complete();
  242. }
  243. }
  244. catch
  245. {
  246. return false;
  247. }
  248. return true;
  249. }
  250. }
  251. }