TopicService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 GxPress.Common.Tools;
  8. using GxPress.Entity.Topic;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Request.App.Topic;
  11. using GxPress.Service.Interface.Topic;
  12. using Datory;
  13. using GxPress.Common.Exceptions;
  14. using GxPress.Common.Page;
  15. using GxPress.Repository.Interface.Topic;
  16. using GxPress.Result;
  17. using GxPress.Result.App.Topic;
  18. using GxPress.Service.Interface.Note;
  19. using Newtonsoft.Json;
  20. using GxPress.Repository.Interface.Visit;
  21. using GxPress.Service.Interface.Visit;
  22. using GxPress.Service.Interface.Analyze;
  23. using GxPress.EnumConst;
  24. namespace GxPress.Service.Implement.Topic
  25. {
  26. public partial class TopicService : ITopicService
  27. {
  28. private readonly ITopicRepository _topicRepository;
  29. private readonly IUserRepository _userRepository;
  30. private readonly ITopicAddresseeRepository _topicAddresseeRepository;
  31. private readonly IDepartmentRepository _departmentRepository;
  32. private readonly ITopicGroupRepository _topicGroupRepository;
  33. private readonly ITopicGroupUserRepository _topicGroupUserRepository;
  34. private readonly IAnalyzeService _analyzeService;
  35. private readonly ICommentRepository _commentRepository;
  36. private readonly IMapper _mapper;
  37. private readonly IGroupUserRepository _groupUserRepository;
  38. private readonly IFolderUserRepository _folderUserRepository;
  39. private readonly INoteService _noteService;
  40. private readonly IMiddleRepository _middleRepository;
  41. private readonly IVisitRepository _visitRepository;
  42. private readonly IVisitService _visitService;
  43. public TopicService(ITopicRepository topicRepository, IUserRepository userRepository,
  44. ITopicAddresseeRepository topicAddresseeRepository,
  45. ITopicGroupRepository topicGroupRepository,
  46. ITopicGroupUserRepository topicGroupUserRepository, IAnalyzeService analyzeService,
  47. ICommentRepository commentRepository, IMapper mapper, IDepartmentRepository departmentRepository,
  48. IGroupUserRepository groupUserRepository, IFolderUserRepository folderUserRepository,
  49. INoteService noteService, IMiddleRepository middleRepository, IVisitRepository visitRepository, IVisitService visitService)
  50. {
  51. _topicRepository = topicRepository;
  52. _userRepository = userRepository;
  53. _topicAddresseeRepository = topicAddresseeRepository;
  54. _topicGroupRepository = topicGroupRepository;
  55. _topicGroupUserRepository = topicGroupUserRepository;
  56. _analyzeService = analyzeService;
  57. _commentRepository = commentRepository;
  58. _departmentRepository = departmentRepository;
  59. _mapper = mapper;
  60. _groupUserRepository = groupUserRepository;
  61. _folderUserRepository = folderUserRepository;
  62. _noteService = noteService;
  63. _middleRepository = middleRepository;
  64. _visitRepository = visitRepository;
  65. _visitService = visitService;
  66. }
  67. /// <summary>
  68. /// 根据GroupId删除
  69. /// </summary>
  70. /// <param name="ids"></param>
  71. /// <returns></returns>
  72. public async Task<bool> DeleteTopicGroupAsync(List<int> ids)
  73. {
  74. try
  75. {
  76. using (TransactionScope transactionScope = new TransactionScope())
  77. {
  78. await _topicGroupRepository.DeleteAsync(ids);
  79. await _topicGroupUserRepository.DeleteGroupIdAsync(ids);
  80. transactionScope.Complete();
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. Console.WriteLine(e);
  86. throw;
  87. }
  88. return false;
  89. }
  90. /// <summary>
  91. /// 获取话题列表
  92. /// </summary>
  93. /// <param name="request"></param>
  94. /// <returns></returns>
  95. public async Task<PagedList<TopicListPageResult>> GetTopicPageAsync(TopicPageSearchRequest request)
  96. {
  97. var result = await _topicRepository.GetTopicPage(request);
  98. //获取数量
  99. // result.Total = 10;
  100. foreach (var item in result.Items)
  101. {
  102. if (string.IsNullOrWhiteSpace(item.UserName))
  103. {
  104. item.Content = "[]";
  105. item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  106. item.Content = "";
  107. continue;
  108. }
  109. if (string.IsNullOrWhiteSpace(item.Content))
  110. item.Content = "[]";
  111. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  112. foreach (var contentJsonDataItem in contentJsonData)
  113. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  114. if (contentJsonData.Count == 0)
  115. item.DataType = 1;
  116. item.Content = "";
  117. if (contentJsonData.Count > 0)
  118. {
  119. var imgData = new List<ContentJsonData>();
  120. var FileData = new List<ContentJsonData>();
  121. var forCount = 1;
  122. var firstContent = string.Empty;
  123. foreach (var jsonData in contentJsonData)
  124. {
  125. if (jsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode() && forCount == 1)
  126. //文本
  127. firstContent = jsonData.Text;
  128. else if (jsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode() && forCount > 1)
  129. //文本
  130. continue;
  131. //图片
  132. else if (jsonData.TypeValue == OldTextEditorTypeConst.Image.GetHashCode() && FileData.Count == 0)
  133. {
  134. if (imgData.Count > 9)
  135. break;
  136. imgData.Add(jsonData);
  137. }
  138. //附件
  139. else
  140. {
  141. FileData.Add(jsonData);
  142. break;
  143. }
  144. forCount++;
  145. }
  146. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  147. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  148. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  149. else item.DataType = 1;
  150. var contType = new List<int> { OldTextEditorTypeConst.Text.GetHashCode(), OldTextEditorTypeConst.Image.GetHashCode() };
  151. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  152. }
  153. if (item.Data == null || item.Data.Count == 0)
  154. item.Data = new List<ContentJsonData>();
  155. }
  156. return result;
  157. }
  158. /// <summary>
  159. /// APP列表显示用
  160. /// </summary>
  161. /// <param name="contentJsonDataList"></param>
  162. /// <returns></returns>
  163. public List<ContentJsonData> GetListAsync(List<ContentJsonData> contentJsonDataList)
  164. {
  165. var result = new List<ContentJsonData>();
  166. if (contentJsonDataList.Count > 0)
  167. {
  168. //查询文本
  169. var txtType = contentJsonDataList.FirstOrDefault(n => n.Type == 1);
  170. if (txtType != null)
  171. result.Add(txtType);
  172. //拼接图片
  173. var imgType = contentJsonDataList.FindAll(n => n.Type == 2);
  174. if (imgType.Any())
  175. {
  176. foreach (var item in imgType)
  177. {
  178. //判断图片数量
  179. if (result.Count(n => n.Type == 2) > 8)
  180. break;
  181. result.Add(item);
  182. }
  183. }
  184. else
  185. {
  186. var values = new List<int> { 1, 2 };
  187. var list = contentJsonDataList.FindAll(n => !values.Contains(n.Type));
  188. if (list.Any())
  189. result.Add(list.FirstOrDefault());
  190. }
  191. }
  192. return result;
  193. }
  194. /// <summary>
  195. /// 添加话题分组成员
  196. /// </summary>
  197. /// <param name="userIds"></param>
  198. /// <param name="topicGroupId"></param>
  199. /// <returns></returns>
  200. public async Task<bool> InsertTopicGroupUserAsync(List<int> userIds, int topicGroupId)
  201. {
  202. try
  203. {
  204. using (TransactionScope transactionScope = new TransactionScope())
  205. {
  206. //获取话题小组成员
  207. var topicGroupUsers =
  208. await _topicGroupUserRepository.GetAllAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  209. topicGroupId));
  210. var groupUsers = topicGroupUsers as TopicGroupUser[] ?? topicGroupUsers.ToArray();
  211. foreach (var userId in userIds)
  212. {
  213. if (groupUsers.Any(n => n.UserId == userId))
  214. continue;
  215. var topicGroupUser = new TopicGroupUser { TopicGroupId = topicGroupId, UserId = userId };
  216. await _topicGroupUserRepository.InsertAsync(topicGroupUser);
  217. }
  218. //获取人数
  219. var includeCount =
  220. await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  221. topicGroupId));
  222. //修改人数
  223. await _topicGroupRepository.UpdateAsync(Q.Where(nameof(TopicGroup.Id), topicGroupId)
  224. .Set(nameof(TopicGroup.IncludeCount), includeCount));
  225. transactionScope.Complete();
  226. return true;
  227. }
  228. }
  229. catch (Exception e)
  230. {
  231. Console.WriteLine(e);
  232. throw;
  233. }
  234. }
  235. /// <summary>
  236. /// 根据ID删除换题分组成员
  237. /// </summary>
  238. /// <param name="ids"></param>
  239. /// <returns></returns>
  240. public async Task<bool> DeleteTopicGroupUserAsync(List<int> ids)
  241. {
  242. if (ids.Count == 0)
  243. throw new BusinessException("删除的话题分组成员不存在");
  244. try
  245. {
  246. using (TransactionScope transactionScope = new TransactionScope())
  247. {
  248. //查询分组
  249. var topicGroupUser = await _topicGroupUserRepository.GetAsync(ids[0]);
  250. await _topicGroupUserRepository.DeleteAsync(Q.WhereIn(nameof(TopicGroupUser.Id), ids));
  251. var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  252. topicGroupUser.TopicGroupId));
  253. //修改数量
  254. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
  255. .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
  256. transactionScope.Complete();
  257. }
  258. }
  259. catch (Exception e)
  260. {
  261. Console.WriteLine(e);
  262. throw;
  263. }
  264. return true;
  265. }
  266. /// <summary>
  267. /// 话题分组移动成员到
  268. /// </summary>
  269. /// <param name="request"></param>
  270. /// <returns></returns>
  271. public async Task<bool> MoveToGroupAsync(MoveToGroupRequest request)
  272. {
  273. using (TransactionScope transactionScope = new TransactionScope())
  274. {
  275. //获取源小组ID
  276. var topicGroupUser = await _topicGroupUserRepository.GetAsync(request.Ids[0]);
  277. //
  278. await _topicGroupUserRepository.UpdateAsync(Q
  279. .Set(nameof(TopicGroupUser.TopicGroupId), request.TopicGroupId)
  280. .WhereIn(nameof(TopicGroupUser.Id), request.Ids));
  281. //原始修改数量
  282. var originalCount = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  283. topicGroupUser.TopicGroupId));
  284. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), originalCount)
  285. .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
  286. //修改数量
  287. var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  288. request.TopicGroupId));
  289. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
  290. .Where(nameof(TopicGroup.Id), request.TopicGroupId));
  291. transactionScope.Complete();
  292. }
  293. return true;
  294. }
  295. /// <summary>
  296. /// 根据GroupId获取通知
  297. /// </summary>
  298. /// <param name="request"></param>
  299. /// <returns></returns>
  300. public async Task<PagedList<TopicListPageResult>> FindTopicByGroupIdAsync(TopicDetailListRequest request)
  301. {
  302. //根据groupID查询话题
  303. var topics = await _topicRepository.GetAllAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), request.GroupId));
  304. //根据话题ID修改用户的阅读
  305. await _topicAddresseeRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Topic.TopicAddressee.TopicId), topics.Select(n => n.Id).ToList()).Where(nameof(Entity.Topic.TopicAddressee.UserId), request.UserId).Set(nameof(Entity.Topic.TopicAddressee.IsRead), true));
  306. var topicListPageResults = await _topicRepository.FindTopicByGroupIdAsync(request);
  307. foreach (var item in topicListPageResults.Items)
  308. {
  309. // var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  310. // analyzeRequest.TypeValue = 1;
  311. // analyzeRequest.AnalyzeType = 1;
  312. // analyzeRequest.SourceId = item.Id;
  313. // analyzeRequest.UserId = request.UserId;
  314. // //点赞数量
  315. // item.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  316. // //获取话题的评论数量
  317. // var commentCount =
  318. // await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), item.Id).Where(nameof(Entity.Comment.TypeValue), 1));
  319. // item.CommentCount = commentCount;
  320. // //获取话题的转发数量
  321. // analyzeRequest.AnalyzeType = 4;
  322. // var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  323. // item.RetransmissionCount = retransmissionCount;
  324. // item.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  325. // //获取话题的收藏数量
  326. // analyzeRequest.AnalyzeType = 3;
  327. // var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  328. // item.CollectCount = collectCount;
  329. // //是否收藏
  330. // item.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  331. // //获取话题的点赞数量
  332. // analyzeRequest.AnalyzeType = 1;
  333. // var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  334. // item.PraiseCount = praiseCount;
  335. // //是否点赞
  336. // item.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  337. if (string.IsNullOrWhiteSpace(item.Content))
  338. item.Content = "[]";
  339. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  340. foreach (var contentJsonDataItem in contentJsonData)
  341. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  342. if (contentJsonData.Count == 0)
  343. item.DataType = 1;
  344. item.Content = "";
  345. if (contentJsonData.Count > 0)
  346. {
  347. var imgData = new List<ContentJsonData>();
  348. var FileData = new List<ContentJsonData>();
  349. var forCount = 1;
  350. var firstContent = string.Empty;
  351. foreach (var jsonData in contentJsonData)
  352. {
  353. if (jsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode() && forCount == 1)
  354. //文本
  355. firstContent = jsonData.Text;
  356. else if (jsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode() && forCount > 1)
  357. //文本
  358. continue;
  359. //图片
  360. else if (jsonData.TypeValue == OldTextEditorTypeConst.Image.GetHashCode() && FileData.Count == 0)
  361. {
  362. if (imgData.Count > 9)
  363. break;
  364. imgData.Add(jsonData);
  365. }
  366. //附件
  367. else
  368. {
  369. FileData.Add(jsonData);
  370. break;
  371. }
  372. forCount++;
  373. }
  374. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  375. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  376. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  377. else item.DataType = 1;
  378. var contType = new List<int> { OldTextEditorTypeConst.Text.GetHashCode(), OldTextEditorTypeConst.Image.GetHashCode() };
  379. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  380. }
  381. if (item.Data == null || item.Data.Count == 0)
  382. item.Data = new List<ContentJsonData>();
  383. //item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  384. }
  385. return topicListPageResults;
  386. }
  387. }
  388. }