TopicService.cs 47 KB

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