NoticeService.Insert.cs 13 KB

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