NoticeService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using AutoMapper;
  7. using Datory;
  8. using GxPress.Common.Exceptions;
  9. using GxPress.Common.Page;
  10. using GxPress.Common.Tools;
  11. using GxPress.Entity;
  12. using GxPress.EnumConst;
  13. using GxPress.Repository.Interface;
  14. using GxPress.Request.Analyze;
  15. using GxPress.Request.Notice;
  16. using GxPress.Result;
  17. using GxPress.Result.Notice;
  18. using GxPress.Service.Interface.Analyze;
  19. using GxPress.Service.Interface.Middle;
  20. using GxPress.Service.Interface.Notice;
  21. using GxPress.Service.Interface.Visit;
  22. using Newtonsoft.Json;
  23. using GxPress.Service.Interface.UserMiddle;
  24. using GxPress.Service.Interface.ContentJson;
  25. namespace GxPress.Service.Implement.Notice
  26. {
  27. public partial class NoticeService : INoticeService
  28. {
  29. private readonly IUserRepository _userRepository;
  30. private readonly INoticeRepository _noticeRepository;
  31. private readonly ICcRepository _ccRepository;
  32. private readonly IAddresseeRepository _addresseeRepository;
  33. private readonly IDepartmentRepository _departmentRepository;
  34. private readonly IMiddleService _middleService;
  35. private readonly IMiddleRepository _middleRepository;
  36. private readonly IMapper _mapper;
  37. private readonly IAnalyzeService _analyzeService;
  38. private readonly IVisitService _visitService;
  39. private readonly ICommentRepository _commentRepository;
  40. private readonly IUserMiddleService _userMiddleService;
  41. private readonly IContentJsonService _contentJsonService;
  42. public NoticeService(IUserRepository userRepository, IMapper mapper, INoticeRepository noticeRepository,
  43. ICcRepository ccRepository, IAddresseeRepository addresseeRepository,
  44. IDepartmentRepository departmentRepository, IMiddleService middleService,
  45. IMiddleRepository middleRepository, IAnalyzeService analyzeService, ICommentRepository commentRepository, IVisitService visitService, IUserMiddleService userMiddleService, IContentJsonService contentJsonService)
  46. {
  47. _noticeRepository = noticeRepository;
  48. _userRepository = userRepository;
  49. _addresseeRepository = addresseeRepository;
  50. _ccRepository = ccRepository;
  51. _departmentRepository = departmentRepository;
  52. _mapper = mapper;
  53. _middleService = middleService;
  54. _middleRepository = middleRepository;
  55. _analyzeService = analyzeService;
  56. _commentRepository = commentRepository;
  57. _visitService = visitService;
  58. _userMiddleService = userMiddleService;
  59. _contentJsonService = contentJsonService;
  60. }
  61. /// <summary>
  62. /// 获取通知的点赞列表
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <param name="userId"></param>
  66. /// <returns></returns>
  67. public async Task<NoticeDetailResult> GetNoticeDetailAsync(int id, int userId)
  68. {
  69. var notice = await _noticeRepository.GetAsync(id);
  70. if (notice == null)
  71. throw new BusinessException("通知不存在");
  72. var noticeDetailResult = new Result.Notice.NoticeDetailResult();
  73. var result = _mapper.Map<Result.Notice.NoticeDetailResult>(notice);
  74. //获取新版
  75. result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
  76. result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
  77. //json
  78. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
  79. foreach (var item in contentJsonData)
  80. item.File = StringUtils.AddDomain(item.File);
  81. result.Data = contentJsonData;
  82. //获取用户
  83. var user = await _userRepository.GetAsync(result.UserId);
  84. result.UserName = user.Name;
  85. result.AllCount = await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id));
  86. result.ReadCount = await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id).Where(nameof(Addressee.IsRead), true));
  87. //获取收件人
  88. var noticeAddresseeList =
  89. await _addresseeRepository.GetUserByNoticeIdAsync(id);
  90. result.NoticeAddresseeNames = noticeAddresseeList.Select(n => n.Name).ToList();
  91. var IsRead = await _addresseeRepository.ExistsAsync(Q.Where(nameof(Entity.Middle.Middle.IsRead), true)
  92. .Where(nameof(Addressee.SourceId), id)
  93. .Where(nameof(Addressee.UserId), userId));
  94. //修改阅读量
  95. if (IsRead == false)
  96. {
  97. if (await _addresseeRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true)
  98. .Where(nameof(Addressee.SourceId), id)
  99. .Where(nameof(Addressee.UserId), userId)))
  100. result.ReadCount++;
  101. }
  102. //修改访问量
  103. await _visitService.AddVisit(userId, 5, id);
  104. //修改middle的阅读
  105. // await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true).Where(nameof(Entity.Middle.Middle.UserId), userId).Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), 1));
  106. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  107. analyzeRequest.TypeValue = GxPress.EnumConst.AllTypeConst.Notice.GetHashCode();
  108. analyzeRequest.AnalyzeType = 1;
  109. analyzeRequest.SourceId = id;
  110. analyzeRequest.UserId = userId;
  111. //点赞数量
  112. result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  113. //获取话题的评论数量
  114. var commentCount =
  115. await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0));
  116. result.CommentCount = commentCount;
  117. //获取话题的转发数量
  118. analyzeRequest.AnalyzeType = 4;
  119. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  120. result.RetransmissionCount = retransmissionCount;
  121. result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  122. //获取话题的收藏数量
  123. analyzeRequest.AnalyzeType = 3;
  124. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  125. result.CollectCount = collectCount;
  126. //是否收藏
  127. result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  128. //获取话题的点赞数量
  129. analyzeRequest.AnalyzeType = 1;
  130. var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  131. result.PraiseCount = praiseCount;
  132. //是否点赞
  133. result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  134. var request = new PraisePageSearchRequest { SourceId = id, TypeValue = AllTypeConst.Notice.GetHashCode(), Page = 1, PerPage = 3 };
  135. result.NoticePraisePagedList = await _analyzeService.GetPraisePageAsync(request);
  136. //是否是当前用户创建
  137. result.IsAdmin = notice.UserId == userId;
  138. return result;
  139. }
  140. /// <summary>
  141. /// 根据通知ID获取通知详情
  142. /// </summary>
  143. /// <param name="request"></param>
  144. /// <returns></returns>
  145. public async Task<NoticeEditDetailResult> NoticeEditDetailAsync(int id)
  146. {
  147. var notice = await _noticeRepository.GetAsync(id);
  148. var result = _mapper.Map<NoticeEditDetailResult>(notice);
  149. //抄送人
  150. var noticeCcList = await _ccRepository.GetNoticeCcByNoticeIdAsync(id, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode());
  151. //收件人
  152. var noticeAddressee = await _addresseeRepository.GetNoticeAddresseeByNoticeIdAsync(id);
  153. //获取新版
  154. result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
  155. result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
  156. var noticeAddresseeUsers =
  157. await _userRepository.UserListInsAsync(noticeAddressee.Select(n => n.UserId).ToList());
  158. result.NoticeAddresseeUsers =
  159. noticeAddresseeUsers.Select(n => _mapper.Map<NoticeAddresseeUser>(n)).ToList();
  160. var noticeCcUsers = await _userRepository.UserListInsAsync(noticeCcList.Select(n => n.UserId).ToList());
  161. result.NoticeCcUsers = noticeCcUsers.Select(n => _mapper.Map<NoticeCcUser>(n)).ToList();
  162. foreach (var noticeAddresseeUser in result.NoticeAddresseeUsers)
  163. {
  164. noticeAddresseeUser.AvatarUrl = StringUtils.AddDomainMin(noticeAddresseeUser.AvatarUrl);
  165. }
  166. foreach (var noticeCcUser in result.NoticeCcUsers)
  167. {
  168. noticeCcUser.AvatarUrl = StringUtils.AddDomainMin(noticeCcUser.AvatarUrl);
  169. }
  170. var jsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
  171. foreach (var item in jsonData)
  172. item.File = StringUtils.AddDomain(item.File);
  173. result.Data = jsonData;
  174. return result;
  175. }
  176. /// <summary>
  177. /// 获取通知详情
  178. /// </summary>
  179. /// <param name="request"></param>
  180. /// <returns></returns>
  181. public async Task<Result.Notice.NoticeDetailResult> GetNoticeDetailResultAsync(int id, int userId)
  182. {
  183. var notice = await _noticeRepository.GetAsync(id);
  184. if (notice == null)
  185. throw new BusinessException("通知不存在");
  186. var noticeDetailResult = new Result.Notice.NoticeDetailResult();
  187. var result = _mapper.Map<Result.Notice.NoticeDetailResult>(notice);
  188. //获取新版
  189. result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
  190. result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
  191. //json
  192. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
  193. foreach (var item in contentJsonData)
  194. item.File = StringUtils.AddDomain(item.File);
  195. result.Data = contentJsonData;
  196. //获取用户
  197. var user = await _userRepository.GetAsync(result.UserId);
  198. result.UserName = user.Name;
  199. result.AllCount =
  200. await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id));
  201. result.ReadCount = await _addresseeRepository.CountAsync(Q
  202. .Where(nameof(Addressee.SourceId), id).Where(nameof(Addressee.IsRead), true));
  203. //获取收件人
  204. var noticeAddresseeList =
  205. await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), id)
  206. .Where(nameof(Entity.Middle.Middle.IsAdmin), false));
  207. result.NoticeAddresseeNames = noticeAddresseeList.Select(n => n.UserName).ToList();
  208. //修改阅读量
  209. await _addresseeRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true)
  210. .Where(nameof(Addressee.SourceId), id)
  211. .Where(nameof(Addressee.UserId), userId));
  212. //修改middle的阅读
  213. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true).Where(nameof(Entity.Middle.Middle.UserId), userId).Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), 1));
  214. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  215. analyzeRequest.TypeValue = 2;
  216. analyzeRequest.AnalyzeType = 1;
  217. analyzeRequest.SourceId = id;
  218. analyzeRequest.UserId = userId;
  219. //点赞数量
  220. result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  221. //获取话题的评论数量
  222. var commentCount =
  223. await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), 1));
  224. result.CommentCount = commentCount;
  225. //获取话题的转发数量
  226. analyzeRequest.AnalyzeType = 4;
  227. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  228. result.RetransmissionCount = retransmissionCount;
  229. result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  230. //获取话题的收藏数量
  231. analyzeRequest.AnalyzeType = 3;
  232. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  233. result.CollectCount = collectCount;
  234. //是否收藏
  235. result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  236. //获取话题的点赞数量
  237. analyzeRequest.AnalyzeType = 1;
  238. var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  239. result.PraiseCount = praiseCount;
  240. //是否点赞
  241. result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  242. return result;
  243. }
  244. /// <summary>
  245. /// 编辑通知
  246. /// </summary>
  247. /// <param name="request"></param>
  248. /// <returns></returns>
  249. public async Task<bool> NoticeUpAsync(NoticeUpRequest request)
  250. {
  251. try
  252. {
  253. using (TransactionScope transactionScope = new TransactionScope())
  254. {
  255. var notice = await _noticeRepository.GetAsync(request.Id);
  256. if (notice == null)
  257. throw new BusinessException("通知不存在");
  258. var noticeInRequest = new NoticeInRequest();
  259. noticeInRequest.Content = request.Content;
  260. noticeInRequest.HtmlContent = request.HtmlContent;
  261. noticeInRequest.NoticeType = request.NoticeType;
  262. noticeInRequest.AddresseeUserMiddles = request.AddresseeUserMiddles;
  263. noticeInRequest.CcUserMiddles = request.CcUserMiddles;
  264. noticeInRequest.Title = request.Title;
  265. noticeInRequest.UserId = request.UserId;
  266. noticeInRequest.SoureId = 0;
  267. noticeInRequest.Id = request.Id;
  268. if (request.NoticeType == NoticeTypeConst.Send)
  269. {
  270. if (notice.NoticeType == NoticeTypeConst.Draft)
  271. {
  272. //删除
  273. await _noticeRepository.DeleteAsync(Q.Where(nameof(Entity.Notice.Id), notice.Id));
  274. //删除middle
  275. await _middleRepository.DeleteAsync(notice.Id, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode());
  276. if (notice.SourceId > 0)
  277. {
  278. var noticeSource = await _noticeRepository.GetAsync(notice.SourceId);
  279. if (noticeSource != null)
  280. noticeInRequest.Id = noticeSource.Id;
  281. else
  282. noticeInRequest.Id = 0;
  283. }
  284. else
  285. noticeInRequest.Id = 0;
  286. }
  287. }
  288. else
  289. {
  290. if (notice.NoticeType == NoticeTypeConst.Draft)
  291. noticeInRequest.Id = request.Id;
  292. else
  293. {
  294. noticeInRequest.SoureId = notice.Id;
  295. var noticeSource = await _noticeRepository.GetAsync(Q.Where(nameof(Entity.Notice.SourceId), notice.Id));
  296. if (noticeSource != null)
  297. noticeInRequest.Id = noticeSource.Id;
  298. else noticeInRequest.Id = 0;
  299. }
  300. }
  301. await InsertAsync(noticeInRequest);
  302. transactionScope.Complete();
  303. }
  304. }
  305. catch (Exception e)
  306. {
  307. Console.WriteLine(e);
  308. throw;
  309. }
  310. return true;
  311. }
  312. /// <summary>
  313. /// 根据已读未读查询通知数据
  314. /// </summary>
  315. /// <param name="request"></param>
  316. /// <returns></returns>
  317. public async Task<IEnumerable<NoticeReadListResult>> NoticeReadListAsync(NoticeReadListRequest request)
  318. {
  319. var noticeReadListResults = await _addresseeRepository.GetNoticeAddresseeByUserIdIdAsync(request);
  320. return noticeReadListResults;
  321. }
  322. /// <summary>
  323. /// 获取通知列表
  324. /// </summary>
  325. /// <param name="request"></param>
  326. /// <returns></returns>
  327. public async Task<PagedList<NoticeListPageResult>> GetNoticeListPageAsync(NoticePageSearchRequest request)
  328. {
  329. var result = await _noticeRepository.GetNoticeListPageAsync(request);
  330. foreach (var item in result.Items)
  331. {
  332. if (item.TypeValue > 0)
  333. {
  334. item.UserName = item.SendUserName;
  335. item.ReadCount = item.CheckedCount;
  336. item.UReadCount = item.UCheckedCount;
  337. item.EnjoyUser = item.FlowUserName;
  338. //我发起的
  339. if (item.TypeValue == AllTypeConst.MySubmitted.GetHashCode())
  340. item.IsRead = item.IsMyFlowRead;
  341. //我抄送
  342. if (item.TypeValue == AllTypeConst.MyCc.GetHashCode())
  343. item.IsRead = item.IsFlowRead;
  344. //我审批
  345. if (item.TypeValue == AllTypeConst.MyChecked.GetHashCode())
  346. item.IsRead = item.IsFlowChecked;
  347. }
  348. if (item.IsDone && item.IsChecked)
  349. item.FlowState = FlowState.Checked;
  350. else if (item.IsDone && !item.IsChecked)
  351. {
  352. if (item.DoneType == 1)
  353. item.FlowState = FlowState.Meeting;
  354. else if (item.DoneType == 2)
  355. item.FlowState = FlowState.Wait;
  356. else
  357. item.FlowState = FlowState.Denied;
  358. }
  359. else if (item.IsDone && !item.IsChecked)
  360. item.FlowState = FlowState.Denied;
  361. else
  362. {
  363. item.FlowState = FlowState.Checking;
  364. }
  365. item.Title = _contentJsonService.GetTitile(item.Title, item.Content);
  366. if (item.IsDelete == true)
  367. item.NoticeStatus = 0;
  368. else if (item.NoticeAddresseeType == NoticeTypeConst.Draft)
  369. item.NoticeStatus = 2;
  370. else
  371. item.NoticeStatus = 1;
  372. }
  373. return result;
  374. }
  375. /// <summary>
  376. /// 撤销通知
  377. /// </summary>
  378. /// <param name="id"></param>
  379. /// <returns></returns>
  380. public async Task<bool> RecallAsync(int id)
  381. {
  382. try
  383. {
  384. using (TransactionScope transactionScope = new TransactionScope())
  385. {
  386. //获取通知
  387. await _noticeRepository.UpdateAsync(Q.Where(nameof(Entity.Notice.Id), id).Set(nameof(Entity.Notice.IsRecall), true));
  388. //删除middle
  389. await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode()).Where(nameof(Entity.Middle.Middle.IsAdmin), false).Set(nameof(Entity.Middle.Middle.IsRecall), true));
  390. //修改为草稿箱
  391. await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()).Where(nameof(Entity.Middle.Middle.IsAdmin), true).Set(nameof(Entity.Middle.Middle.NoticeAddresseeType), 2));
  392. //修改
  393. await _noticeRepository.UpdateAsync(Q.Where(nameof(Entity.Notice.Id), id).Set(nameof(Entity.Notice.NoticeType), NoticeTypeConst.Draft.GetHashCode()));
  394. transactionScope.Complete();
  395. }
  396. }
  397. catch (System.Exception ex)
  398. {
  399. throw new BusinessException(ex.Message);
  400. }
  401. return true;
  402. }
  403. }
  404. }