MissiveService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Datory;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Page;
  7. using GxPress.Common.Tools;
  8. using GxPress.EnumConst;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Repository.Interface.Missive;
  11. using GxPress.Request.App.Middle;
  12. using GxPress.Request.App.Missive;
  13. using GxPress.Result;
  14. using GxPress.Result.App.Missive;
  15. using GxPress.Result.User;
  16. using GxPress.Service.Interface.Analyze;
  17. using GxPress.Service.Interface.Middle;
  18. using GxPress.Service.Interface.Missive;
  19. using GxPress.Service.Interface.UserMiddle;
  20. using Newtonsoft.Json;
  21. using IMapper = AutoMapper.IMapper;
  22. using System.Transactions;
  23. using GxPress.Entity;
  24. using GxPress.Service.Interface.ContentJson;
  25. namespace GxPress.Service.Implement.Missive
  26. {
  27. public partial class MissiveService : IMissiveService
  28. {
  29. private readonly IMissiveRepository _missiveRepository;
  30. private readonly IUserRepository _userRepository;
  31. private readonly IMiddleService _middleService;
  32. private readonly IMapper _mapper;
  33. private readonly IMiddleRepository _middleRepository;
  34. private readonly IAnalyzeService _analyzeService;
  35. private readonly IUserMiddleService _userMiddleService;
  36. private readonly INoticeRepository noticeRepository;
  37. private readonly IAddresseeRepository _addresseeRepository;
  38. private readonly ICcRepository _ccRepository;
  39. private readonly IContentJsonService _contentJsonService;
  40. public MissiveService(IMissiveRepository missiveRepository,
  41. IUserRepository userRepository,
  42. IMiddleService middleService, IMapper mapper, IMiddleRepository middleRepository, IAnalyzeService analyzeService, IUserMiddleService userMiddleService, IAddresseeRepository addresseeRepository, ICcRepository ccRepository, IContentJsonService contentJsonService, INoticeRepository noticeRepository)
  43. {
  44. _missiveRepository = missiveRepository;
  45. _userRepository = userRepository;
  46. _middleService = middleService;
  47. _mapper = mapper;
  48. _middleRepository = middleRepository;
  49. _analyzeService = analyzeService;
  50. _userMiddleService = userMiddleService;
  51. _addresseeRepository = addresseeRepository;
  52. _ccRepository = ccRepository;
  53. _contentJsonService = contentJsonService;
  54. this.noticeRepository = noticeRepository;
  55. }
  56. /// <summary>
  57. /// 添加修改站内信
  58. /// </summary>
  59. /// <param name="request"></param>
  60. /// <returns></returns>
  61. public async Task<bool> InsertOrUpdateAsync(MissiveInRequest request)
  62. {
  63. var missive = new Entity.Missive.Missive();
  64. var middle = new Entity.Middle.Middle();
  65. if (request.Id > 0)
  66. {
  67. //获取当前站内信
  68. missive = await _missiveRepository.GetAsync(request.Id);
  69. if (missive != null)
  70. {
  71. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), missive.Id).Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()));
  72. }
  73. }
  74. //收件人
  75. if (request.MissiveAttributeType == NoticeTypeConst.Send)
  76. {
  77. if (request.MissiveAddresseeUserIds.Count == 0 && request.AddresseeUserMiddles.Count == 0)
  78. throw new BusinessException("收件人不能为空");
  79. if (request.Id > 0)
  80. {
  81. //获取当前站内信
  82. if (missive != null)
  83. {
  84. if (middle.NoticeAddresseeType == NoticeTypeConst.Draft)
  85. {
  86. //删除草稿
  87. await _missiveRepository.DeleteAsync(Q.Where(nameof(Entity.Missive.Missive.Id), request.Id));
  88. //删除middle
  89. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), request.Id).Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()));
  90. missive = await _missiveRepository.GetAsync(Q.Where(nameof(Entity.Missive.Missive.Id), missive.SourceId));
  91. if (missive != null)
  92. request.Id = missive.Id;
  93. else
  94. request.Id = 0;
  95. }
  96. }
  97. }
  98. }
  99. if (request.Id == 0)
  100. {
  101. if (string.IsNullOrEmpty(request.Title) && string.IsNullOrEmpty(request.HtmlContent))
  102. throw new BusinessException("标题内容为空");
  103. if (request.UserId == 0)
  104. throw new BusinessException("用户不存在");
  105. }
  106. var user = await _userRepository.GetAsync(request.UserId);
  107. if (request.Id == 0)
  108. {
  109. var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(request.Content);
  110. foreach (var item in data)
  111. item.File = StringUtils.RemoveDomain(item.File);
  112. request.Content = JsonConvert.SerializeObject(data);
  113. using (TransactionScope transactionScope = new TransactionScope())
  114. {
  115. missive = new Entity.Missive.Missive
  116. {
  117. ParentId = request.ParentId,
  118. Content = request.Content,
  119. Title = request.Title,
  120. UserId = request.UserId,
  121. UserName = user.Name,
  122. HtmlContent = request.HtmlContent,
  123. MissiveType = request.MissiveType,
  124. SendUserId = request.UserId
  125. };
  126. request.Id = await _missiveRepository.InsertAsync(missive);
  127. var addressee = new Addressee
  128. {
  129. SourceId = request.Id,
  130. UserId = request.UserId,
  131. UserName = "",
  132. IsAdmin = true,
  133. IsRead = true,
  134. IsDelete = false,
  135. SourceType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()
  136. };
  137. await _addresseeRepository.InsertAsync(addressee);
  138. //插入中间件
  139. var result = new MiddleInsertTypeRequest
  140. {
  141. MiddleSonId = 0,
  142. FolderId = request.FolderId,
  143. FolderType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(),
  144. MiddleId = request.Id,
  145. UserId = request.UserId,
  146. AttributeValue = 1,
  147. NoticeAddresseeType = request.MissiveAttributeType,
  148. IsAdmin = true,
  149. Title = _contentJsonService.GetTitile(request.Title, request.Content),
  150. TitleLong = request.Title,
  151. Content = request.Content,
  152. };
  153. await _middleService.InsertAsync(result);
  154. transactionScope.Complete();
  155. }
  156. }
  157. else
  158. {
  159. //
  160. //修改
  161. if (!string.IsNullOrEmpty(request.Content))
  162. {
  163. var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(request.Content);
  164. foreach (var item in data)
  165. item.File = StringUtils.RemoveDomain(item.File);
  166. request.Content = JsonConvert.SerializeObject(data);
  167. missive.Content = request.Content;
  168. missive.HtmlContent = request.HtmlContent;
  169. }
  170. if (!string.IsNullOrEmpty(request.Title))
  171. missive.Title = request.Title;
  172. //如果是草稿
  173. if (request.MissiveAttributeType == NoticeTypeConst.Draft && middle.NoticeAddresseeType == NoticeTypeConst.Send)
  174. {
  175. missive.SourceId = request.Id;
  176. var missiveId = await _missiveRepository.InsertAsync(missive);
  177. //插入中间件
  178. var result = new MiddleInsertTypeRequest
  179. {
  180. MiddleSonId = 0,
  181. FolderId = request.FolderId,
  182. FolderType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(),
  183. MiddleId = missiveId,
  184. UserId = request.UserId,
  185. AttributeValue = 1,
  186. NoticeAddresseeType = request.MissiveAttributeType,
  187. IsAdmin = true,
  188. Title = _contentJsonService.GetTitile(request.Title, request.Content),
  189. TitleLong = request.Title,
  190. Content = request.Content,
  191. };
  192. await _middleService.InsertAsync(result);
  193. request.Id = missiveId;
  194. }
  195. else
  196. await _missiveRepository.UpdateAsync(missive);
  197. }
  198. if (await SendMissive(request, request.Id)) return true;
  199. return true;
  200. }
  201. /// <summary>
  202. /// 发送
  203. /// </summary>
  204. /// <param name="request"></param>
  205. /// <param name="missiveId"></param>
  206. /// <returns></returns>
  207. private async Task<bool> SendMissive(MissiveInRequest request, int missiveId)
  208. {
  209. var addresseeUsers = new List<User>();
  210. var ccUsers = new List<User>();
  211. //删除收件人
  212. await _addresseeRepository.DeleteAsync(missiveId, GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(), 0);
  213. //删除抄送人
  214. await _ccRepository.DeleteAsync(missiveId, GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode());
  215. //获取收件人
  216. request.MissiveAddresseeUserIds = request.MissiveAddresseeUserIds.Distinct().ToList();
  217. //获取抄送人
  218. request.MissiveCcUserIds = request.MissiveCcUserIds.Distinct().ToList();
  219. //删除新版收件人
  220. await _userMiddleService.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), 30).Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), request.Id));
  221. //删除新版抄送人
  222. await _userMiddleService.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), 40).Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), request.Id));
  223. //判断是不是新版通知
  224. if (request.AddresseeUserMiddles.Count > 0)
  225. {
  226. foreach (var item in request.AddresseeUserMiddles)
  227. {
  228. item.DataSourceId = missiveId;
  229. item.MiddleType = 30;
  230. }
  231. await _userMiddleService.AddUserMiddleAsync(request.AddresseeUserMiddles);
  232. //获取用户
  233. addresseeUsers = await _userMiddleService.FindUsersAsync(request.AddresseeUserMiddles, request.UserId);
  234. request.MissiveAddresseeUserIds = addresseeUsers.Select(n => n.Id).ToList();
  235. }
  236. if (request.CcUserMiddles.Count > 0)
  237. {
  238. foreach (var item in request.CcUserMiddles)
  239. {
  240. item.DataSourceId = missiveId;
  241. item.MiddleType = 40;
  242. }
  243. await _userMiddleService.AddUserMiddleAsync(request.CcUserMiddles);
  244. ccUsers = await _userMiddleService.FindUsersAsync(request.CcUserMiddles, request.UserId);
  245. request.MissiveCcUserIds = ccUsers.Select(n => n.Id).ToList();
  246. //添加抄送人
  247. request.MissiveAddresseeUserIds.AddRange(request.MissiveCcUserIds);
  248. }
  249. request.MissiveCcUserIds = request.MissiveCcUserIds.Distinct().ToList();
  250. request.MissiveAddresseeUserIds = request.MissiveAddresseeUserIds.Distinct().ToList();
  251. request.MissiveCcUserIds.Remove(request.UserId);
  252. request.MissiveAddresseeUserIds.Remove(request.UserId);
  253. var addressees = new List<Addressee>();
  254. var middles = new List<Entity.Middle.Middle>();
  255. var ccs = new List<Entity.Cc>();
  256. //添加收件人
  257. foreach (var userId in request.MissiveAddresseeUserIds)
  258. {
  259. var addressee = new Addressee
  260. {
  261. SourceId = missiveId,
  262. UserId = userId,
  263. UserName = "",
  264. IsAdmin = false,
  265. IsRead = false,
  266. IsDelete = false,
  267. SourceType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()
  268. };
  269. addressees.Add(addressee);
  270. // await _missiveAddresseeRepository.InsertAsync(missiveAddressee);
  271. //如果是发送
  272. if (request.MissiveAttributeType == NoticeTypeConst.Send)
  273. {
  274. var middle = new Entity.Middle.Middle
  275. {
  276. MiddleId = missiveId,
  277. FolderType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode(),
  278. IsTop = false,
  279. AttributeValue = 1,
  280. ParentId = 0,
  281. UserId = userId,
  282. MiddleSonId = 0,
  283. IsAdmin = false,
  284. NoticeAddresseeType = request.MissiveAttributeType,
  285. IsRead = false,
  286. Title = _contentJsonService.GetTitile(request.Title, request.Content),
  287. TitleLong = request.Title,
  288. Content = request.Content,
  289. AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name)),
  290. AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10),
  291. CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name)),
  292. CcUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10),
  293. };
  294. middles.Add(middle);
  295. }
  296. }
  297. //抄送人
  298. foreach (var userId in request.MissiveCcUserIds)
  299. {
  300. var cc = new Cc
  301. {
  302. SourceId = missiveId,
  303. UserId = userId,
  304. UserName = "",
  305. SourceType = GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()
  306. };
  307. ccs.Add(cc);
  308. }
  309. //插入
  310. await _addresseeRepository.InsertAsync(addressees);
  311. await _ccRepository.InsertAsync(ccs);
  312. await _middleRepository.InsertAsync(middles);
  313. //
  314. var middleModel = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode()).Where(nameof(Entity.Middle.Middle.MiddleId), missiveId));
  315. middleModel.AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  316. middleModel.AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  317. middleModel.CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name));
  318. middleModel.CcUser = Common.Tools.StringUtils.ObjectCollectionToString(addresseeUsers.Select(n => n.Name), 10);
  319. await _middleRepository.UpdateAsync(middleModel);
  320. return true;
  321. }
  322. /// <summary>
  323. /// 根据站内信ID获取详情
  324. /// </summary>
  325. /// <param name="missiveId"></param>
  326. /// <param name="userId"></param>
  327. /// <returns></returns>
  328. public async Task<MissiveInfoResult> GetMissiveInfoAsync(int missiveId, int userId)
  329. {
  330. //修改阅读
  331. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.SourceId), missiveId).Where(nameof(Entity.Addressee.UserId), userId).Set(nameof(Addressee.IsRead), true));
  332. var result = new MissiveInfoResult
  333. {
  334. Items = await GetResult(new List<MissiveInfo>(), missiveId, userId)
  335. };
  336. result.Total = result.Items.Count();
  337. //单回复
  338. var missiveInfo = result.Items.FirstOrDefault();
  339. if (missiveInfo == null)
  340. return result;
  341. //全部回复
  342. result.missiveAddressees = new List<MissiveAddresseeUser>();
  343. result.UserMiddleResults = new List<Result.App.UserMiddle.UserMiddleResult>();
  344. result.UseCcrMiddleResults = new List<Result.App.UserMiddle.UserMiddleResult>();
  345. //回复全部
  346. var userMiddleResult = new Result.App.UserMiddle.UserMiddleResult { MiddleType = 30, SourceId = missiveInfo.UserId, SourceName = missiveInfo.UserName, SourceType = 0, DataSourceId = missiveId };
  347. //判断是否匿名
  348. result.UserMiddleResult = new Result.App.UserMiddle.UserMiddleResult();
  349. result.UserMiddleResults.Add(userMiddleResult);
  350. foreach (var item in result.Items)
  351. {
  352. result.missiveAddressee = new MissiveAddresseeUser();
  353. result.missiveAddressee.Id = item.UserId;
  354. result.missiveAddressee.Name = item.UserName;
  355. result.UserMiddleResult.SourceType = 0;
  356. result.UserMiddleResult.SourceName = item.UserName;
  357. if (missiveInfo.MissiveType == 2)
  358. {
  359. if (missiveInfo.UserId != userId)
  360. {
  361. result.UserMiddleResult.SourceName = "匿名";
  362. result.missiveAddressee.Name = "匿名";
  363. }
  364. }
  365. //全部回复
  366. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(30, item.Id);
  367. foreach (var userMiddle in userMiddles)
  368. {
  369. userMiddleResult = new Result.App.UserMiddle.UserMiddleResult { MiddleType = userMiddle.MiddleType, SourceId = userMiddle.SourceId, SourceName = userMiddle.SourceName, SourceType = userMiddle.SourceType, DataSourceId = userMiddle.DataSourceId };
  370. result.UserMiddleResults.Add(userMiddleResult);
  371. }
  372. //判断是否是匿名
  373. foreach (var userMiddle in result.UserMiddleResults)
  374. {
  375. if (item.MissiveType == 2)
  376. {
  377. if (userMiddle.SourceId == item.UserId && item.UserId != userId && userMiddle.SourceType == 0)
  378. userMiddle.SourceName = "匿名";
  379. }
  380. }
  381. //抄送人
  382. userMiddles = await _userMiddleService.FindUserMiddlesAsync(40, item.Id);
  383. foreach (var userMiddle in userMiddles)
  384. {
  385. userMiddleResult = new Result.App.UserMiddle.UserMiddleResult { MiddleType = userMiddle.MiddleType, SourceId = userMiddle.SourceId, SourceName = userMiddle.SourceName, SourceType = userMiddle.SourceType, DataSourceId = userMiddle.DataSourceId };
  386. result.UseCcrMiddleResults.Add(userMiddleResult);
  387. }
  388. break;
  389. }
  390. return result;
  391. }
  392. /// <summary>
  393. /// 递归拿出所有被回复的站内信数据
  394. /// </summary>
  395. /// <param name="missiveInfoResults"></param>
  396. /// <param name="missiveId"></param>
  397. /// <returns></returns>
  398. public async Task<List<MissiveInfo>> GetResult(List<MissiveInfo> missiveInfoResults, int missiveId, int userId)
  399. {
  400. var missive = await noticeRepository.GetAsync(missiveId);
  401. if (missive == null)
  402. {
  403. throw new Common.Exceptions.BusinessException("站内信已删除");
  404. //return missiveInfoResults;
  405. }
  406. var missiveInfoResult = new MissiveInfo
  407. {
  408. Id = missive.Id,
  409. Title = missive.Title,
  410. HtmlContent = missive.HtmlContent,
  411. Content = missive.Content,
  412. UserId = missive.UserId,
  413. UserName = missive.UserName,
  414. ParentId = missive.ParentId,
  415. MissiveType = missive.TypeValue == 18 ? 2 : 1,
  416. MissiveSendType = missive.NoticeType.GetHashCode()
  417. };
  418. //是否可撤回
  419. if (missive.UserId == userId)
  420. {
  421. // var createdDate = Convert.ToDateTime(missive.CreatedDate);
  422. // if (createdDate.AddMinutes(5) > DateTime.Now)
  423. missiveInfoResult.IsRecall = true;
  424. }
  425. //获取新版
  426. missiveInfoResult.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, missiveId);
  427. missiveInfoResult.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, missiveId);
  428. //获取当前站内信类型
  429. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), missiveId).Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Notice.GetHashCode()));
  430. if (middle != null)
  431. //获取当前的站内信的类型 1 发送 2 草稿
  432. missiveInfoResult.MissiveSendType = middle.NoticeAddresseeType.GetHashCode();
  433. //获取当前站内信的用户数据
  434. var missiveAddressees =
  435. await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), missiveId));
  436. var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveAddressees.Select(n => n.UserId)));
  437. missiveInfoResult.AddresseeUsers = users.Select(n => new UserResult { Name = n.Name, Id = n.Id }).ToList();
  438. //获取当前站内信的抄送人用户数据
  439. // var missiveCcs =
  440. // await _missiveCcRepository.GetAllAsync(Q.Where(nameof(MissiveAddressee.MissiveId), missiveId));
  441. // users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveCcs.Select(n => n.UserId)));
  442. // missiveInfoResult.CcUsers = users.Select(n => new UserResult { Name = n.Name, Id = n.Id }).ToList();
  443. //获取已读
  444. missiveInfoResult.ReadCount = missiveAddressees.Count(n => n.IsRead);
  445. //总数量
  446. missiveInfoResult.AllCount = missiveAddressees.Count();
  447. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  448. analyzeRequest.TypeValue = 4;
  449. analyzeRequest.AnalyzeType = 1;
  450. analyzeRequest.SourceId = missiveId;
  451. analyzeRequest.UserId = userId;
  452. //点赞数量
  453. missiveInfoResult.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  454. //是否点赞
  455. missiveInfoResult.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  456. //获取话题的转发数量
  457. analyzeRequest.AnalyzeType = 4;
  458. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  459. missiveInfoResult.RetransmissionCount = retransmissionCount;
  460. missiveInfoResult.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  461. //获取话题的收藏数量
  462. analyzeRequest.AnalyzeType = 3;
  463. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  464. missiveInfoResult.CollectCount = collectCount;
  465. //是否收藏
  466. missiveInfoResult.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  467. //发件人姓名
  468. if (missiveInfoResult.MissiveType == 1)
  469. {
  470. var user = await _userRepository.GetAsync(missiveInfoResult.UserId);
  471. missiveInfoResult.SendName = user == null ? "" : user.Name;
  472. }
  473. else
  474. {
  475. if (missiveInfoResult.UserId == userId)
  476. {
  477. var user = await _userRepository.GetAsync(missiveInfoResult.UserId);
  478. missiveInfoResult.SendName = user == null ? "" : user.Name;
  479. }
  480. else
  481. missiveInfoResult.SendName = "匿名";
  482. }
  483. //
  484. missiveInfoResult.IsAdmin = missive.UserId == userId;
  485. missiveInfoResult.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(missiveInfoResult.Content);
  486. foreach (var item in missiveInfoResult.Data)
  487. item.File = StringUtils.AddDomain(item.File);
  488. missiveInfoResults.Add(missiveInfoResult);
  489. if (missive.ParentId == 0)
  490. return missiveInfoResults;
  491. return await GetResult(missiveInfoResults, missive.ParentId, userId);
  492. }
  493. /// <summary>
  494. /// 获取站内信
  495. /// </summary>
  496. /// <param name="request"></param>
  497. /// <returns></returns>
  498. public async Task<PagedList<MissiveSearchResult>> GetMissiveSearchResultAsync(MissiveSearchRequest request)
  499. {
  500. var result = await _missiveRepository.GetMissiveSearchResultAsync(request);
  501. foreach (var item in result.Items)
  502. {
  503. //获取已读
  504. // var missiveAddressees = await _missiveAddresseeRepository.GetAllAsync(Q.Where(nameof(MissiveAddressee.MissiveId), item.MiddleId));
  505. // var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveAddressees.Select(n => n.UserId)));
  506. // item.AddresseeUsers = users.Select(n => new UserResult { Id = n.Id, Name = n.Name }).ToList();
  507. //var userMiddles = await _userMiddleService.FindUserMiddlesAsync(30, item.MiddleId);
  508. //item.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName).ToList(), "、");
  509. if (item.IsDelete == true)
  510. item.MissiveStatus = 0;
  511. else if (item.NoticeAddresseeType == NoticeTypeConst.Draft)
  512. item.MissiveStatus = 2;
  513. else
  514. item.MissiveStatus = 1;
  515. if (!string.IsNullOrWhiteSpace(item.Title))
  516. continue;
  517. var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  518. if (data.Count > 0)
  519. {
  520. var first = data.First();
  521. if (first.TypeValue == 1 && !string.IsNullOrWhiteSpace(first.Text))
  522. {
  523. if (!string.IsNullOrWhiteSpace(first.Text))
  524. item.Title = first.Text;
  525. }
  526. else
  527. {
  528. foreach (var contentJsonData in data)
  529. {
  530. //是否是文本
  531. if (contentJsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode())
  532. continue;
  533. else if (contentJsonData.TypeValue == OldTextEditorTypeConst.Image.GetHashCode())
  534. {
  535. item.Title = "[图片]";
  536. break;
  537. }
  538. else if (contentJsonData.TypeValue == OldTextEditorTypeConst.File.GetHashCode())
  539. {
  540. item.Title = "[附件]";
  541. break;
  542. }
  543. else
  544. {
  545. item.Title = "[附件]";
  546. break;
  547. }
  548. }
  549. }
  550. }
  551. }
  552. return result;
  553. }
  554. /// <summary>
  555. /// 获取站内信详情
  556. /// </summary>
  557. /// <param name="id"></param>
  558. /// <returns></returns>
  559. public async Task<MissiveDetailResult> GetMissiveDetailAsync(int id)
  560. {
  561. var missive = await _missiveRepository.GetAsync(id);
  562. var result = _mapper.Map<MissiveDetailResult>(missive);
  563. result.Items = await GetResult(new List<MissiveInfo>(), id, missive.UserId);
  564. for (int i = 0; i < result.Items.Count; i++)
  565. {
  566. if (result.Items[i].Id == id)
  567. {
  568. result.Items.Remove(result.Items[i]);
  569. break;
  570. }
  571. }
  572. //获取抄送人
  573. var missiveCcs = await _ccRepository.GetNoticeCcByNoticeIdAsync(id, GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode());
  574. var missiveCcList = new List<MissiveCcUser>();
  575. foreach (var missiveCc in missiveCcs)
  576. missiveCcList.Add(new MissiveCcUser { Id = missiveCc.UserId, Name = missiveCc.UserName });
  577. //获取收件人
  578. var missiveAddresseeList = new List<MissiveAddresseeUser>();
  579. var missiveAddresss = await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), id));
  580. foreach (var missiveAddress in missiveAddresss)
  581. missiveAddresseeList.Add(new MissiveAddresseeUser { Id = missiveAddress.UserId, Name = missiveAddress.UserName });
  582. result.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
  583. foreach (var item in result.Data)
  584. item.File = StringUtils.AddDomain(item.File);
  585. //收集人
  586. result.MissiveAddresseeUsers = missiveAddresseeList;
  587. //抄送人
  588. result.MissiveCcUsers = missiveCcList;
  589. //获取新版
  590. result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(30, id);
  591. result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(40, id);
  592. return result;
  593. }
  594. /// <summary>
  595. /// 获取站内信已读未读数据
  596. /// </summary>
  597. /// <param name="request"></param>
  598. /// <returns></returns>
  599. public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
  600. {
  601. return await _addresseeRepository.GetMissiveReadListAsync(request);
  602. }
  603. }
  604. }