MiddleService.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Repository.Interface;
  7. using GxPress.Request.App.Middle;
  8. using GxPress.Request.Notice;
  9. using GxPress.Service.Interface.Middle;
  10. using Datory;
  11. using GxPress.Entity.Middle;
  12. using GxPress.Result.App.Middle;
  13. using System.Transactions;
  14. using GxPress.Repository.Interface.Collection;
  15. using GxPress.Repository.Interface.Note;
  16. using GxPress.Repository.Interface.Missive;
  17. using GxPress.Common.Tools;
  18. using GxPress.Repository.Interface.Analyze;
  19. using GxPress.Service.Interface.UserMiddle;
  20. using GxPress.Repository.Interface.RecordFolder;
  21. using GxPress.Service.Interface.Analyze;
  22. using GxPress.Request.App.Analyze;
  23. using GxPress.EnumConst;
  24. using System.Transactions;
  25. using GxPress.Request.UserMiddle;
  26. using Dapper;
  27. namespace GxPress.Service.Implement.Middle
  28. {
  29. public class MiddleService : IMiddleService
  30. {
  31. private readonly IMiddleRepository _middleRepository;
  32. private readonly IFolderUserRepository _folderUserRepository;
  33. private readonly IUserRepository _userRepository;
  34. private readonly ICollectionRepository _collectionRepository;
  35. private readonly INoteRepository _noteRepository;
  36. private readonly IMissiveRepository _missiveRepository;
  37. private readonly IGroupRepository _groupRepository;
  38. private readonly IAnalyzeRepository _analyzeRepository;
  39. private readonly INoticeRepository _noticeRepository;
  40. private readonly IUserMiddleService _userMiddleService;
  41. private readonly IAddresseeRepository _addresseeRepository;
  42. private readonly IRecordFolderRepository recordFolderRepository;
  43. private readonly IAnalyzeService analyzeService;
  44. public MiddleService(IMiddleRepository middleRepository, IFolderUserRepository folderUserRepository,
  45. IUserRepository userRepository, ICollectionRepository collectionRepository, INoteRepository noteRepository,
  46. IMissiveRepository missiveRepository, IGroupRepository groupRepository, INoticeRepository noticeRepository, IAnalyzeRepository analyzeRepository, IUserMiddleService userMiddleService, IAddresseeRepository addresseeRepository, IRecordFolderRepository recordFolderRepository, IAnalyzeService analyzeService)
  47. {
  48. _middleRepository = middleRepository;
  49. _folderUserRepository = folderUserRepository;
  50. _userRepository = userRepository;
  51. _collectionRepository = collectionRepository;
  52. _noteRepository = noteRepository;
  53. _missiveRepository = missiveRepository;
  54. _groupRepository = groupRepository;
  55. _noticeRepository = noticeRepository;
  56. _analyzeRepository = analyzeRepository;
  57. _userMiddleService = userMiddleService;
  58. _addresseeRepository = addresseeRepository;
  59. this.recordFolderRepository = recordFolderRepository;
  60. this.analyzeService = analyzeService;
  61. }
  62. /// <summary>
  63. /// 添加文件夹
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. public async Task<MiddleInResult> InsertAsync(MiddleInsertRequest request)
  68. {
  69. var folderRoleId = new List<int> { 1, 2, 3 };
  70. if (!folderRoleId.Contains(request.RoleId) && (request.FolderType == 3 || request.FolderType == 4))
  71. throw new BusinessException("文件夹权限有误!");
  72. var query = Q.NewQuery();
  73. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
  74. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  75. query.Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType);
  76. if (request.FolderType == AllTypeConst.Note.GetHashCode())
  77. {
  78. if (request.RoleId == 1 || request.RoleId == 3)
  79. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  80. else
  81. query.Where(nameof(Entity.Middle.Middle.RoleId), request.RoleId);
  82. }
  83. query.Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName);
  84. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId);
  85. query.Where(nameof(Entity.Middle.Middle.IsDelete), false);
  86. var oldMiddle = await _middleRepository.GetMiddleAsync(query);
  87. if (string.IsNullOrEmpty(request.FolderName))
  88. throw new BusinessException("文件名称未填写!");
  89. if (oldMiddle != null)
  90. throw new BusinessException("文件夹名称重复,请重新命名!");
  91. var middle = new Entity.Middle.Middle
  92. {
  93. FolderName = request.FolderName,
  94. FolderType = request.FolderType,
  95. IsTop = false,
  96. AttributeValue = 2,
  97. ParentId = request.ParentId,
  98. RoleId = request.RoleId,
  99. UserId = request.UserId,
  100. Sort = 0
  101. };
  102. //存在父级
  103. var middleDto = new Entity.Middle.Middle();
  104. if (request.ParentId > 0)
  105. {
  106. //获取父级数据
  107. middleDto = await _middleRepository.GetMiddleAsync(request.ParentId);
  108. if (middleDto != null)
  109. {
  110. middle.RoleId = middleDto.RoleId;
  111. request.RoleId = middleDto.RoleId;
  112. if (middleDto.ParentId == 0)
  113. middle.MiddleSonId = middleDto.Id;
  114. else middle.MiddleSonId = middleDto.MiddleSonId;
  115. }
  116. }
  117. if (request.RoleId == 0)
  118. middle.RoleId = 1;
  119. if (request.RoleId == 3)
  120. {
  121. if (request.UserIds == null)
  122. request.UserIds = new List<int>();
  123. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0 && request.ParentId == 0)
  124. throw new BusinessException("共享用户必须");
  125. }
  126. if (request.RoleId > 0)
  127. middle.RoleId = request.RoleId;
  128. var middleId = await _middleRepository.InsertAsync(middle);
  129. if (middle.ParentId == 0)
  130. {
  131. middle = await _middleRepository.GetMiddleAsync(middleId);
  132. if (middle != null)
  133. {
  134. middle.MiddleSonId = middleId;
  135. await _middleRepository.UpdateAsync(middle);
  136. }
  137. }
  138. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
  139. .Where(nameof(Entity.Middle.Middle.Id), middleId));
  140. if (request.RoleId == 3 && request.ParentId == 0)
  141. //添加人员
  142. await InsertFolderUsers(request, middleId);
  143. var result = new MiddleInResult()
  144. {
  145. Id = middleId,
  146. Name = request.FolderName
  147. };
  148. return result;
  149. }
  150. /// <summary>
  151. /// 添加文件夹用户
  152. /// </summary>
  153. /// <param name="userIds"></param>
  154. /// <param name="middleId"></param>
  155. /// <returns></returns>
  156. public async Task<bool> InsertFolderUsers(MiddleInsertRequest request, int middleId)
  157. {
  158. //添加
  159. if (request.UserMiddles.Count > 0)
  160. {
  161. foreach (var item in request.UserMiddles)
  162. {
  163. item.DataSourceId = middleId;
  164. item.MiddleType = request.FolderType;
  165. }
  166. //添加中间表
  167. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  168. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middleId);
  169. request.UserIds = users.Select(n => n.Id).ToList();
  170. }
  171. if (request.UserIds.Count > 0)
  172. {
  173. var users = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), request.UserIds));
  174. //插入用户
  175. var folderUsers = new List<FolderUser>();
  176. foreach (var user in users)
  177. {
  178. var folderUser = new FolderUser();
  179. folderUser.UserId = user.Id;
  180. folderUser.UserName = user.Name;
  181. folderUser.MiddleId = middleId;
  182. folderUsers.Add(folderUser);
  183. }
  184. return await _folderUserRepository.InsertsAsync(folderUsers.ToList());
  185. }
  186. return false;
  187. }
  188. /// <summary>
  189. /// 修改文件夹名称
  190. /// </summary>
  191. /// <param name="request"></param>
  192. /// <returns></returns>
  193. public async Task<bool> UpdateNameAsync(MiddleUpdateNameRequest request)
  194. {
  195. var middle =
  196. await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.MiddleId)
  197. .Where(nameof(Entity.Middle.Middle.AttributeValue), 1));
  198. if (middle == null)
  199. throw new BusinessException("文件不存在");
  200. return await _middleRepository.UpdateAsync(
  201. Q.Set(nameof(Entity.Middle.Middle.FolderName), request.FolderName).Where(
  202. nameof(Entity.Middle.Middle.Id),
  203. request.MiddleId));
  204. }
  205. /// <summary>
  206. /// 添加数据
  207. /// </summary>
  208. /// <param name="request"></param>
  209. /// <returns></returns>
  210. public async Task<int> InsertAsync(MiddleInsertTypeRequest request)
  211. {
  212. var middle = new Entity.Middle.Middle
  213. {
  214. MiddleId = request.MiddleId,
  215. FolderType = request.FolderType,
  216. IsTop = false,
  217. AttributeValue = request.AttributeValue,
  218. ParentId = request.FolderId,
  219. UserId = request.UserId,
  220. MiddleSonId = request.MiddleSonId,
  221. IsAdmin = request.IsAdmin,
  222. NoticeAddresseeType = request.NoticeAddresseeType,
  223. Title = request.Title,
  224. IsRead = false,
  225. IsUpload = false,
  226. TitleLong = request.TitleLong,
  227. Content = "",
  228. AddressUser = request.AddressUser,
  229. CcUser = request.CcUser,
  230. CcUserComplete = request.CcUserComplete,
  231. AddressUserComplete = request.AddressUserComplete,
  232. IsRecall = false
  233. };
  234. if (request.AttributeValue == 0)
  235. middle.AttributeValue = 1;
  236. var middleId = await _middleRepository.InsertAsync(middle);
  237. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
  238. .Where(nameof(Entity.Middle.Middle.Id), middleId));
  239. return middleId;
  240. }
  241. /// <summary>
  242. /// 添加数据
  243. /// </summary>
  244. /// <param name="request"></param>
  245. /// <returns></returns>
  246. public async Task<bool> AddOrUpdateAsync(MiddleInsertTypeRequest request)
  247. {
  248. var middle = await _middleRepository.GetMiddleAsync(Q
  249. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  250. .Where(nameof(Entity.Middle.Middle.AttributeValue), 3)
  251. .Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType));
  252. if (middle == null)
  253. {
  254. middle = new Entity.Middle.Middle
  255. {
  256. MiddleId = request.MiddleId,
  257. FolderType = request.FolderType,
  258. IsTop = false,
  259. AttributeValue = request.AttributeValue,
  260. ParentId = request.FolderId,
  261. UserId = request.UserId,
  262. MiddleSonId = request.MiddleSonId
  263. };
  264. var middleId = await _middleRepository.InsertAsync(middle);
  265. //修改Middle sort
  266. await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middleId).Set(nameof(Entity.Middle.Middle.Sort), middleId));
  267. return true;
  268. }
  269. return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false)
  270. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  271. .Where(nameof(Entity.Middle.Middle.AttributeValue), 3));
  272. }
  273. /// <summary>
  274. /// 排序
  275. /// </summary>
  276. /// <param name="request"></param>
  277. /// <returns></returns>
  278. public async Task<bool> MiddleSortAsync(MiddleSortRequest request)
  279. {
  280. return await _middleRepository.SetSortAsync(request);
  281. }
  282. /// <summary>
  283. /// 设置置顶
  284. /// </summary>
  285. /// <param name="id"></param>
  286. /// <returns></returns>
  287. public async Task<bool> SetTopAsync(int id)
  288. {
  289. return await _middleRepository.SetTopAsync(id);
  290. }
  291. /// <summary>
  292. /// 删除 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7 小组
  293. /// </summary>
  294. /// <param name="request"></param>
  295. /// <returns></returns>
  296. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  297. {
  298. try
  299. {
  300. var query = Q.NewQuery();
  301. using TransactionScope transactionScope = new TransactionScope();
  302. var middleIds = request.MiddleIds;
  303. //获取
  304. await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.MiddleIds, middleIds);
  305. request.MiddleIds = middleIds;
  306. //查询middle
  307. var middles = await _middleRepository.FindAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), request.MiddleIds));
  308. var ids = middles.Select(n => n.MiddleId).ToList();
  309. //收藏
  310. if (request.FolderType == AllTypeConst.Collect.GetHashCode())
  311. {
  312. //查询收藏
  313. var collections = await _collectionRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Collection.Id), ids));
  314. foreach (var item in collections)
  315. {
  316. var analyzeRequest = new AnalyzeRequest();
  317. analyzeRequest.AnalyzeType = 3;
  318. analyzeRequest.SourceId = item.CollectionDataId;
  319. analyzeRequest.UserId = item.UserId;
  320. //删除文章收藏
  321. if (item.CollectionType == AllTypeConst.Article.GetHashCode())
  322. {
  323. analyzeRequest.TypeValue = item.CollectionType;
  324. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  325. if (analyze != null)
  326. await _analyzeRepository.DeleteAsync(analyze.Id);
  327. }
  328. //删除话题收藏
  329. if (item.CollectionType == AllTypeConst.Topic.GetHashCode())
  330. {
  331. analyzeRequest.TypeValue = item.CollectionType;
  332. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  333. if (analyze != null)
  334. await _analyzeRepository.DeleteAsync(analyze.Id);
  335. }
  336. //删除笔记收藏
  337. if (item.CollectionType == AllTypeConst.Note.GetHashCode() || item.CollectionType == AllTypeConst.TopicNote.GetHashCode()
  338. || item.CollectionType == AllTypeConst.TopicNoteAll.GetHashCode() || item.CollectionType == AllTypeConst.TopicNoteFolder.GetHashCode())
  339. {
  340. analyzeRequest.TypeValue = item.CollectionType;
  341. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  342. if (analyze != null)
  343. await _analyzeRepository.DeleteAsync(analyze.Id);
  344. }
  345. //删除通知收藏
  346. if (item.CollectionType == AllTypeConst.Notice.GetHashCode())
  347. {
  348. analyzeRequest.TypeValue = item.CollectionType;
  349. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  350. if (analyze != null)
  351. await _analyzeRepository.DeleteAsync(analyze.Id);
  352. query = Q.NewQuery();
  353. query.Where(nameof(Entity.Addressee.UserId), request.UserId);
  354. query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
  355. query.Set(nameof(Entity.Addressee.IsDelete), true);
  356. await _addresseeRepository.UpdateAsync(query);
  357. }
  358. //删除站内信收藏
  359. if (item.CollectionType == AllTypeConst.Missive.GetHashCode())
  360. {
  361. analyzeRequest.TypeValue = item.CollectionType;
  362. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  363. if (analyze != null)
  364. await _analyzeRepository.DeleteAsync(analyze.Id);
  365. query = Q.NewQuery();
  366. query.Where(nameof(Entity.Addressee.UserId), request.UserId);
  367. query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
  368. query.Set(nameof(Entity.Addressee.IsDelete), true);
  369. await _addresseeRepository.UpdateAsync(query);
  370. }
  371. //删除收藏会议
  372. if (item.CollectionType == AllTypeConst.Meeting.GetHashCode())
  373. {
  374. analyzeRequest.TypeValue = item.CollectionType;
  375. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  376. if (analyze != null)
  377. await _analyzeRepository.DeleteAsync(analyze.Id);
  378. }
  379. //收藏笔记文件夹
  380. if (item.CollectionType == AllTypeConst.NoteFolder.GetHashCode() || item.CollectionType == AllTypeConst.CollectFolder.GetHashCode())
  381. {
  382. analyzeRequest.TypeValue = item.CollectionType;
  383. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  384. if (analyze != null)
  385. await _analyzeRepository.DeleteAsync(analyze.Id);
  386. }
  387. }
  388. //删除收藏
  389. request.IsDelete = true;
  390. //删除
  391. await _collectionRepository.RecursionDeleteAsync(request.MiddleIds);
  392. }
  393. //删除笔记
  394. if (request.FolderType == AllTypeConst.Note.GetHashCode())
  395. {
  396. await _noteRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Note.Note.Id), ids).Set(nameof(Entity.Note.Note.IsDelete), true));
  397. //删除默认共享文件夹
  398. await recordFolderRepository.DeleteAsync(request.FolderType, request.UserId, request.MiddleIds);
  399. }
  400. //小组
  401. if (request.FolderType == AllTypeConst.Group.GetHashCode())
  402. await _groupRepository.DeleteAsync(Q.WhereIn(nameof(Entity.Group.Id), ids));
  403. //删除收件箱
  404. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  405. {
  406. query = Q.NewQuery();
  407. query.Where(nameof(Entity.Addressee.UserId), request.UserId);
  408. query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
  409. query.Set(nameof(Entity.Addressee.IsDelete), true);
  410. query.Set(nameof(Entity.Addressee.IsRead), true);
  411. await _addresseeRepository.UpdateAsync(query);
  412. //request.IsDelete = true;
  413. }
  414. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  415. {
  416. foreach (var item in middles)
  417. {
  418. if (item.AttributeValue == 2)
  419. {
  420. //永久删除文件夹
  421. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.Id));
  422. //修改文件夹下级数据
  423. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), 1).Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  424. }
  425. }
  426. }
  427. //删除信源以及文件夹
  428. if (request.FolderType == AllTypeConst.Wechat.GetHashCode())
  429. {
  430. foreach (var middle in middles)
  431. {
  432. //判断是否文件夹
  433. if (middle.AttributeValue == 2)
  434. {
  435. query = Q.NewQuery();
  436. query.Set(nameof(Entity.Middle.Middle.ParentId), 0);
  437. query.Where(nameof(Entity.Middle.Middle.ParentId), middle.Id);
  438. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Wechat.GetHashCode());
  439. await _middleRepository.UpdateAsync(query);
  440. }
  441. }
  442. }
  443. await _middleRepository.DeleteAsync(request);
  444. transactionScope.Complete();
  445. }
  446. catch (Exception ex)
  447. {
  448. throw new BusinessException(ex.Message);
  449. }
  450. return true;
  451. }
  452. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  453. {
  454. return await _middleRepository.RecoverDeleteAsync(request);
  455. }
  456. /// <summary>
  457. /// 移动到
  458. /// </summary>
  459. /// <param name="request"></param>
  460. /// <returns></returns>
  461. public async Task<bool> SetMove(MiddleMoveRequest request)
  462. {
  463. if (request.ParentId == 0 && request.TypeId == -AllTypeConst.Note.GetHashCode())
  464. request.Ids = await _middleRepository.GetMiddleIsFolderAsync(request.Ids);
  465. try
  466. {
  467. using (var transactionScope = new TransactionScope())
  468. {
  469. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  470. if (request.ParentId > 0 && middle != null)
  471. {
  472. //获取父级子集集合
  473. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.Ids.ToList(), request.Ids.ToList());
  474. //修改
  475. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), middle.RoleId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  476. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).Set(nameof(Entity.Middle.Middle.RoleId), middle.RoleId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
  477. //修改共享
  478. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), middle.MiddleSonId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  479. }
  480. if (request.ParentId == 0)
  481. {
  482. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.Ids.First()));
  483. if (middle.RoleId == 3)
  484. {
  485. /// <summary>
  486. /// 获取共享人员
  487. /// </summary>
  488. /// <returns></returns>
  489. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middle.MiddleSonId);
  490. foreach (var id in request.Ids)
  491. {
  492. var userMiddleInRequest = new List<UserMiddleInRequest>();
  493. foreach (var userMiddle in userMiddles)
  494. {
  495. userMiddleInRequest.Add(new UserMiddleInRequest
  496. {
  497. MiddleType = userMiddle.MiddleType,
  498. SourceType = userMiddle.SourceType,
  499. SourceId = userMiddle.SourceId,
  500. SourceName = userMiddle.SourceName,
  501. DataSourceId = id,
  502. SourceTypeValue = userMiddle.SourceTypeValue
  503. });
  504. }
  505. //添加共享人员
  506. await _userMiddleService.AddUserMiddleAsync(userMiddleInRequest);
  507. }
  508. }
  509. //修改
  510. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
  511. foreach (var item in request.Ids)
  512. {
  513. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(new List<int> { item }, new List<int> { });
  514. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), item).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  515. }
  516. }
  517. transactionScope.Complete();
  518. }
  519. }
  520. catch
  521. {
  522. return false;
  523. }
  524. return true;
  525. }
  526. /// <summary>
  527. /// 恢复全部
  528. /// </summary>
  529. /// <param name="folderType"></param>
  530. /// <returns></returns>
  531. public async Task<bool> RecoverAllAsync(int folderType, int userId)
  532. {
  533. var query = Q.NewQuery();
  534. query.Set(nameof(Entity.Middle.Middle.IsDelete), false);
  535. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
  536. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  537. return await _middleRepository.UpdateAsync(query);
  538. }
  539. /// <summary>
  540. /// 修改文件夹名称
  541. /// </summary>
  542. /// <param name="request"></param>
  543. /// <returns></returns>
  544. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  545. {
  546. var middle = await _middleRepository.GetMiddleAsync(request.Id);
  547. if (middle == null)
  548. throw new BusinessException("文件夹不存在");
  549. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  550. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  551. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  552. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  553. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  554. .Where(nameof(Entity.Middle.Middle.IsDelete), false));
  555. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  556. throw new BusinessException("文件夹名称重复,请重新命名!");
  557. return await _middleRepository.UpdateFolderNameAsync(request);
  558. }
  559. /// <summary>
  560. /// 查询文件夹
  561. /// </summary>
  562. /// <param name="request"></param>
  563. /// <returns></returns>
  564. public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
  565. {
  566. var query = Q.NewQuery();
  567. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  568. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  569. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  570. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  571. .Where(nameof(Entity.Middle.Middle.IsDelete), false);
  572. if (request.FolderIds != null && request.FolderIds.Count > 0)
  573. query.WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  574. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  575. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  576. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
  577. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  578. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  579. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
  580. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsSystemDefault), nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
  581. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  582. foreach (var item in middles)
  583. {
  584. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  585. {
  586. Name = item.FolderName,
  587. Id = item.Id,
  588. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  589. RoleId = item.RoleId,
  590. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  591. IsTop = item.IsTop,
  592. IsSystemDefault = item.IsSystemDefault,
  593. ParentId = item.ParentId
  594. };
  595. if (item.RoleId == 3)
  596. {
  597. var sourceId = item.Id;
  598. if (item.ParentId != 0)
  599. sourceId = item.MiddleSonId;
  600. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  601. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  602. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  603. }
  604. result.Add(middleSearchFolderResult);
  605. }
  606. return result;
  607. }
  608. /// <summary>
  609. /// 查询文件夹包含父级
  610. /// </summary>
  611. /// <param name="request"></param>
  612. /// <returns></returns>
  613. public async Task<MiddleSearchFolderByParendIsResult> SearchFolderAndParentIdAsync(MiddleSearchFolderRequest request)
  614. {
  615. var query = Q.NewQuery();
  616. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  617. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  618. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  619. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  620. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  621. .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  622. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  623. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  624. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
  625. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  626. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  627. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
  628. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
  629. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  630. var result = new MiddleSearchFolderByParendIsResult() { Name = middle == null ? "" : middle.FolderName };
  631. if (middle != null && middle.ParentId > 0)
  632. {
  633. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId));
  634. result.Id = middle == null ? 0 : middle.Id;
  635. result.Name = middle == null ? "" : middle.FolderName;
  636. }
  637. result.Item = new List<MiddleSearchFolderResult>();
  638. foreach (var item in middles)
  639. {
  640. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  641. {
  642. Name = item.FolderName,
  643. Id = item.Id,
  644. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  645. RoleId = item.RoleId,
  646. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  647. IsTop = item.IsTop,
  648. ParentId = item.ParentId
  649. };
  650. if (item.RoleId == 3)
  651. {
  652. var sourceId = item.Id;
  653. if (item.ParentId != 0)
  654. sourceId = item.MiddleSonId;
  655. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  656. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  657. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  658. }
  659. result.Item.Add(middleSearchFolderResult);
  660. }
  661. return result;
  662. }
  663. /// <summary>
  664. /// 修改权限
  665. /// </summary>
  666. /// <param name="request"></param>
  667. /// <returns></returns>
  668. public async Task<bool> UpdateFolderRoleAsync(MiddleUpdateFolderRoleRequest request)
  669. {
  670. var middle = await _middleRepository.GetMiddleAsync(request.MiddleId);
  671. if (middle == null)
  672. throw new BusinessException("文件夹不存在");
  673. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  674. .Where(nameof(Entity.Middle.Middle.UserId), middle.UserId)
  675. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  676. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  677. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  678. .Where(nameof(Entity.Middle.Middle.ParentId), middle.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
  679. if (string.IsNullOrEmpty(request.FolderName))
  680. throw new BusinessException("文件名称未填写");
  681. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  682. throw new BusinessException("文件夹名称重复,请重新命名!");
  683. if (middle.RoleId == 3)
  684. {
  685. if (request.RoleId != 3)
  686. {
  687. //删除共享用户
  688. await _folderUserRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.FolderUser.MiddleId), middle.Id));
  689. //删除
  690. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  691. }
  692. }
  693. if (request.RoleId == 3 && middle.ParentId == 0)
  694. {
  695. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0)
  696. throw new BusinessException("共享成员必须");
  697. //添加人员
  698. await _folderUserRepository.DeleteAsync(Q.Where(nameof(FolderUser.MiddleId), request.MiddleId));
  699. var middleInsertRequest = new MiddleInsertRequest();
  700. middleInsertRequest.UserIds = request.UserIds;
  701. middleInsertRequest.UserMiddles = request.UserMiddles;
  702. middleInsertRequest.FolderType = middle.FolderType;
  703. if (request.UserMiddles.Count > 0)
  704. {
  705. //删除原有的数据
  706. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  707. //await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  708. //查询用户
  709. // var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middle.UserId);
  710. // middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  711. }
  712. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  713. }
  714. middle.RoleId = request.RoleId;
  715. middle.FolderName = request.FolderName;
  716. await _middleRepository.UpdateAsync(middle);
  717. //修改子集权限
  718. if (middle.ParentId == 0)
  719. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), request.RoleId).Where(nameof(Entity.Middle.Middle.MiddleSonId), middle.Id));
  720. return true;
  721. }
  722. /// <summary>
  723. /// 修改共享文件夹下的所有文件夹属性
  724. /// </summary>
  725. /// <param name="request"></param>
  726. /// <returns></returns>
  727. public async Task<bool> UpdateChildrenMiddleFolder(MiddleUpdateFolderRoleRequest request)
  728. {
  729. //获取下级数据
  730. var middles = await _middleRepository.GetMiddleChildrenAsync(request.MiddleId);
  731. if (middles.Count() == 0)
  732. return true;
  733. foreach (var item in middles)
  734. {
  735. if (request.UserMiddles.Count > 0)
  736. {
  737. var middleInsertRequest = new MiddleInsertRequest();
  738. middleInsertRequest.UserIds = request.UserIds;
  739. middleInsertRequest.UserMiddles = request.UserMiddles;
  740. //删除原有的数据
  741. await _userMiddleService.DeleteAsync(item.Id, item.FolderType);
  742. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  743. //查询用户
  744. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, item.UserId);
  745. middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  746. //插入共享成员
  747. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  748. item.RoleId = request.RoleId;
  749. item.FolderName = request.FolderName;
  750. //修改
  751. await _middleRepository.UpdateAsync(item);
  752. request.MiddleId = item.Id;
  753. //修改下级权限和共享范围人
  754. await UpdateChildrenMiddleFolder(request);
  755. }
  756. }
  757. return true;
  758. }
  759. /// <summary>
  760. /// 根据文件夹ID获取文件夹信息
  761. /// </summary>
  762. /// <param name="middleId"></param>
  763. /// <returns></returns>
  764. public async Task<MiddleFolderInfoResult> GetFolderInfo(int middleId)
  765. {
  766. if (middleId == 0)
  767. throw new Common.Exceptions.BusinessException("文件夹不存在");
  768. var result = new MiddleFolderInfoResult();
  769. var middle = await _middleRepository.GetMiddleAsync(middleId);
  770. result.Id = middle.Id;
  771. result.FolderName = middle.FolderName;
  772. result.FolderType = middle.FolderType;
  773. result.UserId = middle.UserId;
  774. result.RoleId = middle.RoleId;
  775. result.FolderUsers = await _folderUserRepository.GetAllAsync(middleId);
  776. //获取共享人员
  777. if (result.RoleId == 3)
  778. {
  779. var sourceId = middle.Id;
  780. if (middle.ParentId > 0)
  781. sourceId = middle.MiddleSonId;
  782. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, sourceId);
  783. result.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  784. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middle.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  785. // result.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  786. }
  787. result.UserMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middleId);
  788. return result;
  789. }
  790. /// <summary>
  791. /// 获取默认的共享文件夹范围
  792. /// </summary>
  793. /// <param name="userId"></param>
  794. /// <returns></returns>
  795. public async Task<MiddleDefaultResult> GetEnjoyDefaultAsync(int userId, int folderType)
  796. {
  797. var recordFolder = await recordFolderRepository.GetAsync(folderType, userId);
  798. var result = new MiddleDefaultResult();
  799. //获取笔记默认
  800. if (recordFolder.Id == 0 && folderType == AllTypeConst.TopicNote.GetHashCode())
  801. {
  802. var query = Q.NewQuery();
  803. query.Where(nameof(Entity.Middle.Middle.FolderType), folderType);
  804. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  805. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  806. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  807. var middle = await _middleRepository.GetMiddleAsync(query);
  808. result.IsDefault = middle != null ? middle.IsSystemDefault : false;
  809. result.FolderName = middle != null ? middle.FolderName : "";
  810. result.FolderType = middle != null ? middle.FolderType : 0;
  811. result.MiddleId = middle == null ? middle.Id : 0;
  812. return result;
  813. }
  814. result = new MiddleDefaultResult();
  815. result.IsDefault = recordFolder != null;
  816. result.FolderName = recordFolder == null ? "" : recordFolder.FolderName;
  817. result.FolderType = recordFolder == null ? 0 : recordFolder.FolderType;
  818. result.MiddleId = recordFolder == null ? 0 : recordFolder.FolderId;
  819. return result;
  820. }
  821. /// <summary>
  822. /// 根据类型搜索文件夹
  823. /// </summary>
  824. /// <param name="request"></param>
  825. /// <returns></returns>
  826. public async Task<IEnumerable<Entity.Middle.Middle>> GetSearchFolderResult(MiddleSearchFolderRequest request)
  827. {
  828. var query = Q.NewQuery();
  829. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  830. query.Where(query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%"));
  831. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
  832. query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.LastModifiedDate));
  833. return await _middleRepository.GetAllAsync(query);
  834. }
  835. /// <summary>
  836. /// 获取话题 笔记共享范围文件夹
  837. /// </summary>
  838. /// <param name="request"></param>
  839. /// <returns></returns>
  840. public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
  841. {
  842. string sqlStr = string.Empty;
  843. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  844. sqlStr += $" and FolderName like '%{request.KeyWord}%'";
  845. string sql = $@"SELECT
  846. *
  847. FROM
  848. tede_middle
  849. WHERE
  850. FolderType ={request.TypeId} AND AttributeValue = 2 {sqlStr}
  851. AND UserId = {request.UserId}
  852. AND RoleId IN (1 , 3)
  853. AND IsDelete = 0
  854. AND ParentId ={request.ParentId}
  855. ORDER BY IsSystemDefault DESC,AttributeValue desc , IsTop DESC , Sort DESC , CreatedDate DESC";
  856. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  857. var connection = database.GetConnection();
  858. var middles = await connection.QueryAsync<Entity.Middle.Middle>(sql);
  859. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  860. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode() && request.ParentId == 0 && string.IsNullOrWhiteSpace(request.KeyWord))
  861. {
  862. result.Add(new MiddleSearchFolderResult
  863. {
  864. Name = "草稿箱",
  865. Id = 0,
  866. IsDisable = false,
  867. RoleId = 1,
  868. IsFolder = false,
  869. IsTop = false,
  870. FileCount = await _noteRepository.GetDraftCountAsync(request.UserId, true),
  871. IsSystemDefault = true,
  872. IsDraft = true
  873. });
  874. }
  875. foreach (var item in middles)
  876. {
  877. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  878. {
  879. Name = item.FolderName,
  880. Id = item.Id,
  881. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  882. RoleId = item.RoleId,
  883. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  884. IsTop = item.IsTop,
  885. FileCount = await _middleRepository.CountAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id).Where(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId)),
  886. IsSystemDefault = item.IsSystemDefault
  887. };
  888. if (item.RoleId == 3)
  889. {
  890. var sourceId = item.Id;
  891. if (item.ParentId != 0)
  892. sourceId = item.MiddleSonId;
  893. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  894. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  895. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  896. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  897. }
  898. result.Add(middleSearchFolderResult);
  899. }
  900. return result;
  901. }
  902. /// <summary>
  903. /// 获取话题最近使用的文件夹
  904. /// </summary>
  905. /// <param name="request"></param>
  906. /// <returns></returns>
  907. public async Task<List<MiddleSearchFolderResult>> GetLatelyTopicFolderAsync(int userId)
  908. {
  909. //获取用户最近发布的话题
  910. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, true);
  911. var query = Q.NewQuery();
  912. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  913. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.TopicNote.GetHashCode())
  914. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  915. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  916. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  917. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  918. var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
  919. var middles = new List<Entity.Middle.Middle>();
  920. foreach (var folderId in folderIds.Distinct())
  921. {
  922. foreach (var item in models)
  923. {
  924. if (item.Id == folderId)
  925. {
  926. middles.Add(item);
  927. break;
  928. }
  929. }
  930. }
  931. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  932. foreach (var item in middles)
  933. {
  934. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  935. {
  936. Name = item.FolderName,
  937. Id = item.Id,
  938. IsDisable = false,
  939. RoleId = item.RoleId,
  940. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  941. IsTop = item.IsTop
  942. };
  943. if (item.RoleId == 3)
  944. {
  945. var sourceId = item.Id;
  946. if (item.ParentId != 0)
  947. sourceId = item.MiddleSonId;
  948. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  949. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  950. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  951. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  952. }
  953. result.Add(middleSearchFolderResult);
  954. }
  955. return result;
  956. }
  957. /// <summary>
  958. /// 获取笔记最近使用的文件夹
  959. /// </summary>
  960. /// <param name="request"></param>
  961. /// <returns></returns>
  962. public async Task<List<MiddleSearchFolderResult>> GetLatelyNoteFolderAsync(int userId)
  963. {
  964. //获取用户最近发布的话题
  965. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, false);
  966. var query = Q.NewQuery();
  967. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  968. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode())
  969. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  970. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  971. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  972. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 2 });
  973. var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
  974. var middles = new List<Entity.Middle.Middle>();
  975. foreach (var folderId in folderIds.Distinct())
  976. {
  977. foreach (var item in models)
  978. {
  979. if (item.Id == folderId)
  980. {
  981. middles.Add(item);
  982. break;
  983. }
  984. }
  985. }
  986. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  987. foreach (var item in middles)
  988. {
  989. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  990. {
  991. Name = item.FolderName,
  992. Id = item.Id,
  993. IsDisable = false,
  994. RoleId = item.RoleId,
  995. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  996. IsTop = item.IsTop
  997. };
  998. if (item.RoleId == 3)
  999. {
  1000. var sourceId = item.Id;
  1001. if (item.ParentId != 0)
  1002. sourceId = item.MiddleSonId;
  1003. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  1004. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  1005. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  1006. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  1007. }
  1008. result.Add(middleSearchFolderResult);
  1009. }
  1010. return result;
  1011. }
  1012. /// <summary> =-
  1013. /// 删除全部
  1014. /// </summary>
  1015. /// <param name="folderType"></param>
  1016. /// <param name="userId"></param>
  1017. /// <returns></returns>
  1018. public async Task<bool> DeleteAllAsync(int folderType, int userId)
  1019. {
  1020. var query = Q.NewQuery();
  1021. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
  1022. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  1023. query.Where(nameof(Entity.Middle.Middle.IsDelete), true);
  1024. return await _middleRepository.DeleteAsync(query);
  1025. }
  1026. }
  1027. }