TopicService.cs 43 KB

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