TopicService.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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.Repository.Interface.DepartmentUser;
  24. using GxPress.EnumConst;
  25. using GxPress.Repository.Interface.Note;
  26. using GxPress.Result.Department;
  27. namespace GxPress.Service.Implement.Topic
  28. {
  29. public partial class TopicService : ITopicService
  30. {
  31. private readonly ITopicRepository _topicRepository;
  32. private readonly IUserRepository _userRepository;
  33. private readonly IDepartmentUserRepository departmentUserRepository;
  34. private readonly ITopicAddresseeRepository _topicAddresseeRepository;
  35. private readonly IDepartmentRepository _departmentRepository;
  36. private readonly ITopicGroupRepository _topicGroupRepository;
  37. private readonly ITopicGroupUserRepository _topicGroupUserRepository;
  38. private readonly IAnalyzeService _analyzeService;
  39. private readonly ICommentRepository _commentRepository;
  40. private readonly IMapper _mapper;
  41. private readonly IGroupUserRepository _groupUserRepository;
  42. private readonly IFolderUserRepository _folderUserRepository;
  43. private readonly INoteService _noteService;
  44. private readonly IMiddleRepository _middleRepository;
  45. private readonly IVisitRepository _visitRepository;
  46. private readonly IVisitService _visitService;
  47. private readonly INoteRepository noteRepository;
  48. public TopicService(ITopicRepository topicRepository, IUserRepository userRepository,
  49. ITopicAddresseeRepository topicAddresseeRepository,
  50. ITopicGroupRepository topicGroupRepository,
  51. ITopicGroupUserRepository topicGroupUserRepository, IAnalyzeService analyzeService,
  52. ICommentRepository commentRepository, IMapper mapper, IDepartmentRepository departmentRepository,
  53. IGroupUserRepository groupUserRepository, IFolderUserRepository folderUserRepository,
  54. INoteService noteService, IMiddleRepository middleRepository, IVisitRepository visitRepository, IVisitService visitService, IDepartmentUserRepository departmentUserRepository, INoteRepository noteRepository)
  55. {
  56. _topicRepository = topicRepository;
  57. _userRepository = userRepository;
  58. _topicAddresseeRepository = topicAddresseeRepository;
  59. _topicGroupRepository = topicGroupRepository;
  60. _topicGroupUserRepository = topicGroupUserRepository;
  61. _analyzeService = analyzeService;
  62. _commentRepository = commentRepository;
  63. _departmentRepository = departmentRepository;
  64. _mapper = mapper;
  65. _groupUserRepository = groupUserRepository;
  66. _folderUserRepository = folderUserRepository;
  67. _noteService = noteService;
  68. _middleRepository = middleRepository;
  69. _visitRepository = visitRepository;
  70. _visitService = visitService;
  71. this.departmentUserRepository = departmentUserRepository;
  72. this.noteRepository = noteRepository;
  73. }
  74. /// <summary>
  75. /// 根据GroupId删除
  76. /// </summary>
  77. /// <param name="ids"></param>
  78. /// <returns></returns>
  79. public async Task<bool> DeleteTopicGroupAsync(List<int> ids)
  80. {
  81. try
  82. {
  83. using (TransactionScope transactionScope = new TransactionScope())
  84. {
  85. await _topicGroupRepository.DeleteAsync(ids);
  86. await _topicGroupUserRepository.DeleteGroupIdAsync(ids);
  87. transactionScope.Complete();
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. Console.WriteLine(e);
  93. throw;
  94. }
  95. return false;
  96. }
  97. /// <summary>
  98. ///最新 小组话题分页列表
  99. /// </summary>
  100. /// <param name="request"></param>
  101. /// <returns></returns>
  102. public async Task<PagedList<TopicListPageResult>> GetGroupTopicPageAsync(TopicDetailListRequest request)
  103. {
  104. var result = await _topicRepository.GetGroupTopicPageAsync(request);
  105. //获取数量
  106. // result.Total = 10;
  107. foreach (var item in result.Items)
  108. {
  109. if (string.IsNullOrWhiteSpace(item.UserName))
  110. {
  111. item.Content = "[]";
  112. item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  113. item.Content = "";
  114. continue;
  115. }
  116. if (string.IsNullOrWhiteSpace(item.Content))
  117. item.Content = "[]";
  118. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  119. foreach (var contentJsonDataItem in contentJsonData)
  120. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  121. if (contentJsonData.Count == 0)
  122. item.DataType = 1;
  123. item.Content = "";
  124. if (contentJsonData.Count > 0)
  125. {
  126. var imgData = new List<ContentJsonData>();
  127. var FileData = new List<ContentJsonData>();
  128. var forCount = 1;
  129. var firstContent = string.Empty;
  130. foreach (var jsonData in contentJsonData)
  131. {
  132. if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount == 1)
  133. //文本
  134. firstContent = jsonData.Text;
  135. else if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount > 1)
  136. //文本
  137. continue;
  138. //图片
  139. else if (jsonData.Type == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
  140. {
  141. if (imgData.Count >= 9)
  142. break;
  143. imgData.Add(jsonData);
  144. }
  145. //附件
  146. else
  147. {
  148. FileData.Add(jsonData);
  149. break;
  150. }
  151. forCount++;
  152. }
  153. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  154. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  155. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  156. else item.DataType = 1;
  157. var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
  158. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  159. }
  160. if (item.Data == null || item.Data.Count == 0)
  161. item.Data = new List<ContentJsonData>();
  162. }
  163. return result;
  164. }
  165. public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
  166. {
  167. var result = await _topicRepository.GetTopicByGroupAsync(request);
  168. //获取数量
  169. // result.Total = 10;
  170. foreach (var item in result.Items)
  171. {
  172. if (string.IsNullOrWhiteSpace(item.UserName))
  173. {
  174. item.Content = "[]";
  175. item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  176. item.Content = "";
  177. continue;
  178. }
  179. if (string.IsNullOrWhiteSpace(item.Content))
  180. item.Content = "[]";
  181. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  182. foreach (var contentJsonDataItem in contentJsonData)
  183. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  184. if (contentJsonData.Count == 0)
  185. item.DataType = 1;
  186. item.Content = "";
  187. if (contentJsonData.Count > 0)
  188. {
  189. var imgData = new List<ContentJsonData>();
  190. var FileData = new List<ContentJsonData>();
  191. var forCount = 1;
  192. var firstContent = string.Empty;
  193. foreach (var jsonData in contentJsonData)
  194. {
  195. if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount == 1)
  196. //文本
  197. firstContent = jsonData.Text;
  198. else if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount > 1)
  199. //文本
  200. continue;
  201. //图片
  202. else if (jsonData.Type == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
  203. {
  204. if (imgData.Count > 9)
  205. break;
  206. imgData.Add(jsonData);
  207. }
  208. //附件
  209. else
  210. {
  211. FileData.Add(jsonData);
  212. break;
  213. }
  214. forCount++;
  215. }
  216. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  217. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  218. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  219. else item.DataType = 1;
  220. var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
  221. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  222. }
  223. if (item.Data == null || item.Data.Count == 0)
  224. item.Data = new List<ContentJsonData>();
  225. }
  226. return result;
  227. }
  228. public async Task<string> GetTopicPageSqlAsync(TopicPageSearchRequest request)
  229. {
  230. var topicTypeValue = GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode();
  231. var sql = $@"SELECT
  232. a.Id,a.Title,a.UserId,a.Content,a.CreatedDate,a.IsTopic,a.ReadCount,c.ParentId,
  233. (SELECT
  234. FolderName
  235. FROM
  236. tede_middle
  237. WHERE
  238. id = c.ParentId and IsDelete=0) AS FolderName,
  239. (SELECT
  240. COUNT(1)
  241. FROM
  242. tede_analyze
  243. WHERE
  244. TypeValue = {topicTypeValue}
  245. AND SourceId = a.Id
  246. AND AnalyzeType = 1) AS PraiseCount,
  247. (SELECT
  248. COUNT(1)
  249. FROM
  250. tede_analyze
  251. WHERE
  252. UserId = {request.UserId} AND TypeValue = {topicTypeValue}
  253. AND SourceId = a.Id
  254. AND AnalyzeType = 1
  255. LIMIT 0 , 1) AS IsPraise,
  256. (SELECT
  257. COUNT(1)
  258. FROM
  259. tede_comment
  260. WHERE
  261. ArticleId = a.Id AND TypeValue = {topicTypeValue}
  262. AND Pid = 0) AS CommentCount,
  263. (SELECT
  264. COUNT(1)
  265. FROM
  266. tede_analyze
  267. WHERE
  268. UserId = {request.UserId} AND TypeValue = {topicTypeValue}
  269. AND SourceId = a.Id
  270. AND AnalyzeType = 4) AS RetransmissionCount,
  271. (SELECT
  272. COUNT(1)
  273. FROM
  274. tede_analyze
  275. WHERE
  276. UserId = {request.UserId} AND TypeValue = {topicTypeValue}
  277. AND SourceId = a.Id
  278. AND AnalyzeType = 4
  279. LIMIT 0 , 1) AS IsRetransmission,
  280. (SELECT
  281. COUNT(1)
  282. FROM
  283. tede_analyze
  284. WHERE
  285. UserId = {request.UserId} AND TypeValue = {topicTypeValue}
  286. AND SourceId = a.Id
  287. AND AnalyzeType = 3
  288. LIMIT 0 , 1) AS IsCollect,
  289. b.Name as UserName, b.AvatarUrl
  290. FROM
  291. tede_note a
  292. INNER JOIN
  293. tede_user b ON a.UserId = b.Id
  294. INNER JOIN
  295. tede_middle c ON c.MiddleId = a.Id
  296. WHERE
  297. c.FolderType = {topicTypeValue} AND a.IsTopic = 1 and c.IsDelete=0
  298. ";
  299. if (request.UserId <= 0)
  300. {
  301. sql += $@" AND c.ParentId IN (SELECT
  302. id
  303. FROM
  304. tede_middle
  305. WHERE
  306. FolderType = {topicTypeValue} AND AttributeValue = 2
  307. AND RoleId = 1)";
  308. }
  309. else
  310. {
  311. sql += $@" AND (a.userId={request.UserId} or c.ParentId IN (SELECT
  312. MiddleId
  313. FROM
  314. tede_folder_user
  315. WHERE
  316. MiddleId = c.ParentId AND UserId = {request.UserId})
  317. OR a.UserId IN (SELECT
  318. Id
  319. FROM
  320. tede_user
  321. WHERE
  322. 1 = (SELECT
  323. RoleId
  324. FROM
  325. tede_middle
  326. WHERE
  327. id = c.ParentId) and Id=a.UserId
  328. ))";
  329. }
  330. if (request.TopicGroupIds.Count > 0)
  331. {
  332. var topicGroupId = "";
  333. foreach (var item in request.TopicGroupIds)
  334. {
  335. if (item <= 0)
  336. continue;
  337. topicGroupId += $"{item},";
  338. }
  339. if (!string.IsNullOrEmpty(topicGroupId))
  340. {
  341. topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
  342. sql += $@" AND a.UserId IN (SELECT
  343. UserId
  344. FROM
  345. tede_topic_group_user
  346. WHERE
  347. TopicGroupId IN ({topicGroupId}))";
  348. }
  349. }
  350. if (request.TopicGroupIds.Count > 0)
  351. {
  352. //我的
  353. if (request.TopicGroupIds.Contains(TopicNotoGropConst.My.GetHashCode()))
  354. {
  355. sql += $" and a.UserId={request.UserId} ";
  356. }
  357. //同单位
  358. if (request.TopicGroupIds.Contains(TopicNotoGropConst.Work.GetHashCode()))
  359. {
  360. if (request.UserId > 0)
  361. {
  362. var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(request.UserId);
  363. var departments = new List<DepartmentResult>();
  364. foreach (var departmentId in departmentIds)
  365. {
  366. await _departmentRepository.GetDepartmentById(departmentId, departments);
  367. }
  368. if (departments.Count > 0)
  369. {
  370. var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
  371. if (userIds.Count() > 0)
  372. {
  373. var str = "";
  374. foreach (var item in userIds)
  375. str += $"{item},";
  376. str = str.Remove(str.Length - 1, 1);
  377. sql += $" and a.UserId in({str})";
  378. }
  379. }
  380. }
  381. }
  382. }
  383. if (!string.IsNullOrWhiteSpace(request.Keyword))
  384. {
  385. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  386. OR a.Title LIKE '%{request.Keyword}%'
  387. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  388. }
  389. sql += $@" ORDER BY a.CreatedDate DESC
  390. LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  391. return sql;
  392. }
  393. public async Task<string> AssembleSqlCount(TopicPageSearchRequest request)
  394. {
  395. var topicTypeValue = GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode();
  396. var sql = $@"SELECT count(1) FROM
  397. tede_note a
  398. INNER JOIN
  399. tede_user b ON a.UserId = b.Id
  400. INNER JOIN
  401. tede_middle c ON c.MiddleId = a.Id
  402. WHERE
  403. c.FolderType = {topicTypeValue} AND a.IsTopic = 1 and c.IsDelete=0";
  404. if (request.UserId <= 0)
  405. {
  406. sql += $@" AND c.ParentId IN (SELECT
  407. id
  408. FROM
  409. tede_middle
  410. WHERE
  411. FolderType = {topicTypeValue} AND AttributeValue = 2
  412. AND RoleId = 1)";
  413. }
  414. else
  415. {
  416. sql += $@" AND (a.userId={request.UserId} or c.ParentId IN (SELECT
  417. MiddleId
  418. FROM
  419. tede_folder_user
  420. WHERE
  421. MiddleId = c.ParentId AND UserId = {request.UserId})
  422. OR a.UserId IN (SELECT
  423. Id
  424. FROM
  425. tede_user
  426. WHERE
  427. 1 = (SELECT
  428. RoleId
  429. FROM
  430. tede_middle
  431. WHERE
  432. id = c.ParentId) and Id=a.UserId
  433. ))";
  434. }
  435. if (request.TopicGroupIds.Count > 0)
  436. {
  437. var topicGroupId = "";
  438. foreach (var item in request.TopicGroupIds)
  439. {
  440. if (item <= 0)
  441. continue;
  442. topicGroupId += $"{item},";
  443. }
  444. if (!string.IsNullOrEmpty(topicGroupId))
  445. {
  446. topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
  447. sql += $@" AND a.UserId IN (SELECT
  448. UserId
  449. FROM
  450. tede_topic_group_user
  451. WHERE
  452. TopicGroupId IN ({topicGroupId}))";
  453. }
  454. }
  455. if (request.TopicGroupIds.Count > 0)
  456. {
  457. //我的
  458. if (request.TopicGroupIds.Contains(TopicNotoGropConst.My.GetHashCode()))
  459. {
  460. sql += $" and a.UserId={request.UserId} ";
  461. }
  462. //同单位
  463. if (request.TopicGroupIds.Contains(TopicNotoGropConst.Work.GetHashCode()))
  464. {
  465. if (request.UserId > 0)
  466. {
  467. var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(request.UserId);
  468. var departments = new List<DepartmentResult>();
  469. foreach (var departmentId in departmentIds)
  470. {
  471. await _departmentRepository.GetDepartmentById(departmentId, departments);
  472. }
  473. if (departments.Count > 0)
  474. {
  475. var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
  476. if (userIds.Count() > 0)
  477. {
  478. var str = "";
  479. foreach (var item in userIds)
  480. str += $"{item},";
  481. str = str.Remove(str.Length - 1, 1);
  482. sql += $" and a.UserId in({str})";
  483. }
  484. }
  485. }
  486. }
  487. }
  488. if (!string.IsNullOrWhiteSpace(request.Keyword))
  489. {
  490. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  491. OR a.Title LIKE '%{request.Keyword}%'
  492. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  493. }
  494. return sql;
  495. }
  496. /// <summary>
  497. /// 新版 获取笔记话题列表
  498. /// </summary>
  499. /// <param name="request"></param>
  500. /// <returns></returns>
  501. public async Task<PagedList<TopicListPageResult>> GetTopicPageAsync(TopicPageSearchRequest request)
  502. {
  503. var result = await _topicRepository.GetTopicPage(request, await GetTopicPageSqlAsync(request), await AssembleSqlCount(request));
  504. //获取数量
  505. // result.Total = 10;
  506. foreach (var item in result.Items)
  507. {
  508. // if (string.IsNullOrEmpty(item.FolderName))
  509. // item.FolderName = "根目录";
  510. if (string.IsNullOrWhiteSpace(item.UserName))
  511. {
  512. item.Content = "[]";
  513. item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  514. item.Content = "";
  515. continue;
  516. }
  517. if (string.IsNullOrWhiteSpace(item.Content))
  518. item.Content = "[]";
  519. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  520. foreach (var contentJsonDataItem in contentJsonData)
  521. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  522. if (contentJsonData.Count == 0)
  523. item.DataType = 1;
  524. item.Content = "";
  525. if (contentJsonData.Count > 0)
  526. {
  527. var imgData = new List<ContentJsonData>();
  528. var FileData = new List<ContentJsonData>();
  529. var forCount = 1;
  530. var firstContent = string.Empty;
  531. foreach (var jsonData in contentJsonData)
  532. {
  533. if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount == 1)
  534. //文本
  535. firstContent = jsonData.Text;
  536. else if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount > 1)
  537. //文本
  538. continue;
  539. //图片
  540. else if (jsonData.TypeValue == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
  541. {
  542. if (imgData.Count > 9)
  543. break;
  544. imgData.Add(jsonData);
  545. }
  546. //附件
  547. else
  548. {
  549. FileData.Add(jsonData);
  550. break;
  551. }
  552. forCount++;
  553. }
  554. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  555. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  556. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  557. else item.DataType = 1;
  558. var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
  559. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  560. }
  561. if (item.Data == null || item.Data.Count == 0)
  562. item.Data = new List<ContentJsonData>();
  563. if (item.Title == null)
  564. item.Title = string.Empty;
  565. }
  566. //查询系统默认的公开文件夹是否存在
  567. var query = Q.NewQuery();
  568. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
  569. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  570. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.TopicNote.GetHashCode());
  571. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  572. var isExists = await _middleRepository.ExistsAsync(query);
  573. if (!isExists)
  574. {
  575. //生成系统默认的公开文件夹
  576. await _middleRepository.InsertAsync(new Entity.Middle.Middle
  577. {
  578. Id = 0,
  579. FolderName = "公开",
  580. AttributeValue = 2,
  581. UserId = request.UserId,
  582. FolderType = AllTypeConst.TopicNote.GetHashCode(),
  583. RoleId = 1,
  584. IsSystemDefault = true
  585. });
  586. }
  587. result.IsDraft = await noteRepository.IsExistsDraftAsync(request.UserId, true);
  588. result.DraftId = await noteRepository.GetNoteIdByDraftAsync(request.UserId, true);
  589. return result;
  590. }
  591. /// <summary>
  592. /// APP列表显示用
  593. /// </summary>
  594. /// <param name="contentJsonDataList"></param>
  595. /// <returns></returns>
  596. public List<ContentJsonData> GetListAsync(List<ContentJsonData> contentJsonDataList)
  597. {
  598. var result = new List<ContentJsonData>();
  599. if (contentJsonDataList.Count > 0)
  600. {
  601. //查询文本
  602. var txtType = contentJsonDataList.FirstOrDefault(n => n.Type == 1);
  603. if (txtType != null)
  604. result.Add(txtType);
  605. //拼接图片
  606. var imgType = contentJsonDataList.FindAll(n => n.Type == 2);
  607. if (imgType.Any())
  608. {
  609. foreach (var item in imgType)
  610. {
  611. //判断图片数量
  612. if (result.Count(n => n.Type == 2) > 8)
  613. break;
  614. result.Add(item);
  615. }
  616. }
  617. else
  618. {
  619. var values = new List<int> { 1, 2 };
  620. var list = contentJsonDataList.FindAll(n => !values.Contains(n.Type));
  621. if (list.Any())
  622. result.Add(list.FirstOrDefault());
  623. }
  624. }
  625. return result;
  626. }
  627. /// <summary>
  628. /// 添加话题分组成员
  629. /// </summary>
  630. /// <param name="userIds"></param>
  631. /// <param name="topicGroupId"></param>
  632. /// <returns></returns>
  633. public async Task<bool> InsertTopicGroupUserAsync(List<int> userIds, int topicGroupId)
  634. {
  635. try
  636. {
  637. using (TransactionScope transactionScope = new TransactionScope())
  638. {
  639. //获取话题小组成员
  640. var topicGroupUsers =
  641. await _topicGroupUserRepository.GetAllAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  642. topicGroupId));
  643. var groupUsers = topicGroupUsers as TopicGroupUser[] ?? topicGroupUsers.ToArray();
  644. foreach (var userId in userIds)
  645. {
  646. if (groupUsers.Any(n => n.UserId == userId))
  647. continue;
  648. var topicGroupUser = new TopicGroupUser { TopicGroupId = topicGroupId, UserId = userId };
  649. await _topicGroupUserRepository.InsertAsync(topicGroupUser);
  650. }
  651. //获取人数
  652. var includeCount =
  653. await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  654. topicGroupId));
  655. //修改人数
  656. await _topicGroupRepository.UpdateAsync(Q.Where(nameof(TopicGroup.Id), topicGroupId)
  657. .Set(nameof(TopicGroup.IncludeCount), includeCount));
  658. transactionScope.Complete();
  659. return true;
  660. }
  661. }
  662. catch (Exception e)
  663. {
  664. Console.WriteLine(e);
  665. throw;
  666. }
  667. }
  668. /// <summary>
  669. /// 根据ID删除换题分组成员
  670. /// </summary>
  671. /// <param name="ids"></param>
  672. /// <returns></returns>
  673. public async Task<bool> DeleteTopicGroupUserAsync(List<int> ids)
  674. {
  675. if (ids.Count == 0)
  676. throw new BusinessException("删除的话题分组成员不存在");
  677. try
  678. {
  679. using (TransactionScope transactionScope = new TransactionScope())
  680. {
  681. //查询分组
  682. var topicGroupUser = await _topicGroupUserRepository.GetAsync(ids[0]);
  683. await _topicGroupUserRepository.DeleteAsync(Q.WhereIn(nameof(TopicGroupUser.Id), ids));
  684. var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  685. topicGroupUser.TopicGroupId));
  686. //修改数量
  687. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
  688. .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
  689. transactionScope.Complete();
  690. }
  691. }
  692. catch (Exception e)
  693. {
  694. Console.WriteLine(e);
  695. throw;
  696. }
  697. return true;
  698. }
  699. /// <summary>
  700. /// 话题分组移动成员到
  701. /// </summary>
  702. /// <param name="request"></param>
  703. /// <returns></returns>
  704. public async Task<bool> MoveToGroupAsync(MoveToGroupRequest request)
  705. {
  706. using (TransactionScope transactionScope = new TransactionScope())
  707. {
  708. //获取源小组ID
  709. var topicGroupUser = await _topicGroupUserRepository.GetAsync(request.Ids[0]);
  710. //
  711. await _topicGroupUserRepository.UpdateAsync(Q
  712. .Set(nameof(TopicGroupUser.TopicGroupId), request.TopicGroupId)
  713. .WhereIn(nameof(TopicGroupUser.Id), request.Ids));
  714. //原始修改数量
  715. var originalCount = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  716. topicGroupUser.TopicGroupId));
  717. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), originalCount)
  718. .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
  719. //修改数量
  720. var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
  721. request.TopicGroupId));
  722. await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
  723. .Where(nameof(TopicGroup.Id), request.TopicGroupId));
  724. transactionScope.Complete();
  725. }
  726. return true;
  727. }
  728. /// <summary>
  729. /// 根据GroupId获取通知
  730. /// </summary>
  731. /// <param name="request"></param>
  732. /// <returns></returns>
  733. public async Task<PagedList<TopicListPageResult>> FindTopicByGroupIdAsync(TopicDetailListRequest request)
  734. {
  735. //根据groupID查询话题
  736. var topics = await _topicRepository.GetAllAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), request.GroupId));
  737. //根据话题ID修改用户的阅读
  738. 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));
  739. var topicListPageResults = await _topicRepository.FindTopicByGroupIdAsync(request);
  740. foreach (var item in topicListPageResults.Items)
  741. {
  742. // var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  743. // analyzeRequest.TypeValue = 1;
  744. // analyzeRequest.AnalyzeType = 1;
  745. // analyzeRequest.SourceId = item.Id;
  746. // analyzeRequest.UserId = request.UserId;
  747. // //点赞数量
  748. // item.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  749. // //获取话题的评论数量
  750. // var commentCount =
  751. // await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), item.Id).Where(nameof(Entity.Comment.TypeValue), 1));
  752. // item.CommentCount = commentCount;
  753. // //获取话题的转发数量
  754. // analyzeRequest.AnalyzeType = 4;
  755. // var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  756. // item.RetransmissionCount = retransmissionCount;
  757. // item.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  758. // //获取话题的收藏数量
  759. // analyzeRequest.AnalyzeType = 3;
  760. // var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  761. // item.CollectCount = collectCount;
  762. // //是否收藏
  763. // item.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  764. // //获取话题的点赞数量
  765. // analyzeRequest.AnalyzeType = 1;
  766. // var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  767. // item.PraiseCount = praiseCount;
  768. // //是否点赞
  769. // item.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  770. if (string.IsNullOrWhiteSpace(item.Content))
  771. item.Content = "[]";
  772. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  773. foreach (var contentJsonDataItem in contentJsonData)
  774. contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
  775. if (contentJsonData.Count == 0)
  776. item.DataType = 1;
  777. item.Content = "";
  778. if (contentJsonData.Count > 0)
  779. {
  780. var imgData = new List<ContentJsonData>();
  781. var FileData = new List<ContentJsonData>();
  782. var forCount = 1;
  783. var firstContent = string.Empty;
  784. foreach (var jsonData in contentJsonData)
  785. {
  786. if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount == 1)
  787. //文本
  788. firstContent = jsonData.Text;
  789. else if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount > 1)
  790. //文本
  791. continue;
  792. //图片
  793. else if (jsonData.TypeValue == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
  794. {
  795. if (imgData.Count > 9)
  796. break;
  797. imgData.Add(jsonData);
  798. }
  799. //附件
  800. else
  801. {
  802. FileData.Add(jsonData);
  803. break;
  804. }
  805. forCount++;
  806. }
  807. item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
  808. if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
  809. else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
  810. else item.DataType = 1;
  811. var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
  812. item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
  813. }
  814. if (item.Data == null || item.Data.Count == 0)
  815. item.Data = new List<ContentJsonData>();
  816. //item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  817. }
  818. return topicListPageResults;
  819. }
  820. }
  821. }