MissiveService.cs 30 KB

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