NoticeService.Insert.cs 12 KB

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