TopicService.cs 45 KB

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