MissiveService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 = _mapper.Map<MissiveInfo>(missive);
  407. //是否可撤回
  408. if (missive.UserId == userId)
  409. {
  410. // var createdDate = Convert.ToDateTime(missive.CreatedDate);
  411. // if (createdDate.AddMinutes(5) > DateTime.Now)
  412. missiveInfoResult.IsRecall = true;
  413. }
  414. //获取新版
  415. missiveInfoResult.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, missiveId);
  416. missiveInfoResult.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, missiveId);
  417. //获取当前站内信类型
  418. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), missiveId).Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Notice.GetHashCode()));
  419. if (middle != null)
  420. //获取当前的站内信的类型 1 发送 2 草稿
  421. missiveInfoResult.MissiveSendType = middle.NoticeAddresseeType.GetHashCode();
  422. //获取当前站内信的用户数据
  423. var missiveAddressees =
  424. await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), missiveId));
  425. var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveAddressees.Select(n => n.UserId)));
  426. missiveInfoResult.AddresseeUsers = users.Select(n => new UserResult { Name = n.Name, Id = n.Id }).ToList();
  427. //获取当前站内信的抄送人用户数据
  428. // var missiveCcs =
  429. // await _missiveCcRepository.GetAllAsync(Q.Where(nameof(MissiveAddressee.MissiveId), missiveId));
  430. // users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveCcs.Select(n => n.UserId)));
  431. // missiveInfoResult.CcUsers = users.Select(n => new UserResult { Name = n.Name, Id = n.Id }).ToList();
  432. //获取已读
  433. missiveInfoResult.ReadCount = missiveAddressees.Count(n => n.IsRead);
  434. //总数量
  435. missiveInfoResult.AllCount = missiveAddressees.Count();
  436. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  437. analyzeRequest.TypeValue = 4;
  438. analyzeRequest.AnalyzeType = 1;
  439. analyzeRequest.SourceId = missiveId;
  440. analyzeRequest.UserId = userId;
  441. //点赞数量
  442. missiveInfoResult.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  443. //是否点赞
  444. missiveInfoResult.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  445. //获取话题的转发数量
  446. analyzeRequest.AnalyzeType = 4;
  447. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  448. missiveInfoResult.RetransmissionCount = retransmissionCount;
  449. missiveInfoResult.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  450. //获取话题的收藏数量
  451. analyzeRequest.AnalyzeType = 3;
  452. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  453. missiveInfoResult.CollectCount = collectCount;
  454. //是否收藏
  455. missiveInfoResult.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  456. //发件人姓名
  457. if (missiveInfoResult.MissiveType == 1)
  458. {
  459. var user = await _userRepository.GetAsync(missiveInfoResult.UserId);
  460. missiveInfoResult.SendName = user == null ? "" : user.Name;
  461. }
  462. else
  463. {
  464. if (missiveInfoResult.UserId == userId)
  465. {
  466. var user = await _userRepository.GetAsync(missiveInfoResult.UserId);
  467. missiveInfoResult.SendName = user == null ? "" : user.Name;
  468. }
  469. else
  470. missiveInfoResult.SendName = "匿名";
  471. }
  472. //
  473. missiveInfoResult.IsAdmin = missive.UserId == userId;
  474. missiveInfoResult.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(missiveInfoResult.Content);
  475. foreach (var item in missiveInfoResult.Data)
  476. item.File = StringUtils.AddDomain(item.File);
  477. missiveInfoResults.Add(missiveInfoResult);
  478. if (missive.ParentId == 0)
  479. return missiveInfoResults;
  480. return await GetResult(missiveInfoResults, missive.ParentId, userId);
  481. }
  482. /// <summary>
  483. /// 获取站内信
  484. /// </summary>
  485. /// <param name="request"></param>
  486. /// <returns></returns>
  487. public async Task<PagedList<MissiveSearchResult>> GetMissiveSearchResultAsync(MissiveSearchRequest request)
  488. {
  489. var result = await _missiveRepository.GetMissiveSearchResultAsync(request);
  490. foreach (var item in result.Items)
  491. {
  492. //获取已读
  493. // var missiveAddressees = await _missiveAddresseeRepository.GetAllAsync(Q.Where(nameof(MissiveAddressee.MissiveId), item.MiddleId));
  494. // var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(Entity.User.Id), missiveAddressees.Select(n => n.UserId)));
  495. // item.AddresseeUsers = users.Select(n => new UserResult { Id = n.Id, Name = n.Name }).ToList();
  496. //var userMiddles = await _userMiddleService.FindUserMiddlesAsync(30, item.MiddleId);
  497. //item.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName).ToList(), "、");
  498. if (item.IsDelete == true)
  499. item.MissiveStatus = 0;
  500. else if (item.NoticeAddresseeType == NoticeTypeConst.Draft)
  501. item.MissiveStatus = 2;
  502. else
  503. item.MissiveStatus = 1;
  504. if (!string.IsNullOrWhiteSpace(item.Title))
  505. continue;
  506. var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  507. if (data.Count > 0)
  508. {
  509. var first = data.First();
  510. if (first.TypeValue == 1 && !string.IsNullOrWhiteSpace(first.Text))
  511. {
  512. if (!string.IsNullOrWhiteSpace(first.Text))
  513. item.Title = first.Text;
  514. }
  515. else
  516. {
  517. foreach (var contentJsonData in data)
  518. {
  519. //是否是文本
  520. if (contentJsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode())
  521. continue;
  522. else if (contentJsonData.TypeValue == OldTextEditorTypeConst.Image.GetHashCode())
  523. {
  524. item.Title = "[图片]";
  525. break;
  526. }
  527. else if (contentJsonData.TypeValue == OldTextEditorTypeConst.File.GetHashCode())
  528. {
  529. item.Title = "[附件]";
  530. break;
  531. }
  532. else
  533. {
  534. item.Title = "[附件]";
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. return result;
  542. }
  543. /// <summary>
  544. /// 获取站内信详情
  545. /// </summary>
  546. /// <param name="id"></param>
  547. /// <returns></returns>
  548. public async Task<MissiveDetailResult> GetMissiveDetailAsync(int id)
  549. {
  550. var missive = await _missiveRepository.GetAsync(id);
  551. var result = _mapper.Map<MissiveDetailResult>(missive);
  552. result.Items = await GetResult(new List<MissiveInfo>(), id, missive.UserId);
  553. for (int i = 0; i < result.Items.Count; i++)
  554. {
  555. if (result.Items[i].Id == id)
  556. {
  557. result.Items.Remove(result.Items[i]);
  558. break;
  559. }
  560. }
  561. //获取抄送人
  562. var missiveCcs = await _ccRepository.GetNoticeCcByNoticeIdAsync(id, GxPress.EnumConst.FolderTypeConst.Missive.GetHashCode());
  563. var missiveCcList = new List<MissiveCcUser>();
  564. foreach (var missiveCc in missiveCcs)
  565. missiveCcList.Add(new MissiveCcUser { Id = missiveCc.UserId, Name = missiveCc.UserName });
  566. //获取收件人
  567. var missiveAddresseeList = new List<MissiveAddresseeUser>();
  568. var missiveAddresss = await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), id));
  569. foreach (var missiveAddress in missiveAddresss)
  570. missiveAddresseeList.Add(new MissiveAddresseeUser { Id = missiveAddress.UserId, Name = missiveAddress.UserName });
  571. result.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
  572. foreach (var item in result.Data)
  573. item.File = StringUtils.AddDomain(item.File);
  574. //收集人
  575. result.MissiveAddresseeUsers = missiveAddresseeList;
  576. //抄送人
  577. result.MissiveCcUsers = missiveCcList;
  578. //获取新版
  579. result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(30, id);
  580. result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(40, id);
  581. return result;
  582. }
  583. /// <summary>
  584. /// 获取站内信已读未读数据
  585. /// </summary>
  586. /// <param name="request"></param>
  587. /// <returns></returns>
  588. public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
  589. {
  590. return await _addresseeRepository.GetMissiveReadListAsync(request);
  591. }
  592. }
  593. }