MiddleService.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. using TransactionScope transactionScope = new TransactionScope();
  301. var middleIds = request.MiddleIds;
  302. //获取
  303. await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.MiddleIds, middleIds);
  304. request.MiddleIds = middleIds;
  305. //查询middle
  306. var middles = await _middleRepository.FindAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), request.MiddleIds));
  307. var ids = middles.Select(n => n.MiddleId).ToList();
  308. //收藏
  309. if (request.FolderType == AllTypeConst.Collect.GetHashCode())
  310. {
  311. //查询收藏
  312. var collections = await _collectionRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Collection.Id), ids));
  313. foreach (var item in collections)
  314. {
  315. var analyzeRequest = new AnalyzeRequest();
  316. analyzeRequest.AnalyzeType = 3;
  317. analyzeRequest.SourceId = item.CollectionDataId;
  318. analyzeRequest.UserId = item.UserId;
  319. //删除文章收藏
  320. if (item.CollectionType == AllTypeConst.Article.GetHashCode())
  321. {
  322. analyzeRequest.TypeValue = item.CollectionType;
  323. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  324. if (analyze != null)
  325. await _analyzeRepository.DeleteAsync(analyze.Id);
  326. }
  327. //删除话题收藏
  328. if (item.CollectionType == AllTypeConst.Topic.GetHashCode())
  329. {
  330. analyzeRequest.TypeValue = item.CollectionType;
  331. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  332. if (analyze != null)
  333. await _analyzeRepository.DeleteAsync(analyze.Id);
  334. }
  335. //删除笔记收藏
  336. if (item.CollectionType == AllTypeConst.Note.GetHashCode() || item.CollectionType == AllTypeConst.TopicNote.GetHashCode()
  337. || item.CollectionType == AllTypeConst.TopicNoteAll.GetHashCode() || item.CollectionType == AllTypeConst.TopicNoteFolder.GetHashCode())
  338. {
  339. analyzeRequest.TypeValue = item.CollectionType;
  340. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  341. if (analyze != null)
  342. await _analyzeRepository.DeleteAsync(analyze.Id);
  343. }
  344. //删除通知收藏
  345. if (item.CollectionType == AllTypeConst.Notice.GetHashCode())
  346. {
  347. analyzeRequest.TypeValue = item.CollectionType;
  348. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  349. if (analyze != null)
  350. await _analyzeRepository.DeleteAsync(analyze.Id);
  351. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.UserId), request.UserId).WhereIn(nameof(Entity.Addressee.SourceId), ids).Set(nameof(Entity.Addressee.IsDelete), true));
  352. }
  353. //删除站内信收藏
  354. if (item.CollectionType == AllTypeConst.Missive.GetHashCode())
  355. {
  356. analyzeRequest.TypeValue = item.CollectionType;
  357. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  358. if (analyze != null)
  359. await _analyzeRepository.DeleteAsync(analyze.Id);
  360. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.UserId), request.UserId).WhereIn(nameof(Entity.Addressee.SourceId), ids).Set(nameof(Entity.Addressee.IsDelete), true));
  361. }
  362. //删除收藏会议
  363. if (item.CollectionType == AllTypeConst.Meeting.GetHashCode())
  364. {
  365. analyzeRequest.TypeValue = item.CollectionType;
  366. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  367. if (analyze != null)
  368. await _analyzeRepository.DeleteAsync(analyze.Id);
  369. }
  370. //收藏笔记文件夹
  371. if (item.CollectionType == AllTypeConst.NoteFolder.GetHashCode() || item.CollectionType == AllTypeConst.CollectFolder.GetHashCode())
  372. {
  373. analyzeRequest.TypeValue = item.CollectionType;
  374. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  375. if (analyze != null)
  376. await _analyzeRepository.DeleteAsync(analyze.Id);
  377. }
  378. }
  379. //删除收藏
  380. request.IsDelete = true;
  381. //删除
  382. await _collectionRepository.RecursionDeleteAsync(request.MiddleIds);
  383. }
  384. //删除笔记
  385. if (request.FolderType == AllTypeConst.Note.GetHashCode())
  386. {
  387. await _noteRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Note.Note.Id), ids).Set(nameof(Entity.Note.Note.IsDelete), true));
  388. //删除默认共享文件夹
  389. await recordFolderRepository.DeleteAsync(request.FolderType, request.UserId, request.MiddleIds);
  390. }
  391. //小组
  392. if (request.FolderType == AllTypeConst.Group.GetHashCode())
  393. await _groupRepository.DeleteAsync(Q.WhereIn(nameof(Entity.Group.Id), ids));
  394. //删除收件箱
  395. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  396. {
  397. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.UserId), request.UserId).WhereIn(nameof(Entity.Addressee.SourceId), ids).Set(nameof(Entity.Addressee.IsDelete), true).Set(nameof(Entity.Addressee.IsRead), true));
  398. }
  399. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  400. {
  401. foreach (var item in middles)
  402. {
  403. if (item.AttributeValue == 2)
  404. {
  405. //永久删除文件夹
  406. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.Id));
  407. //修改文件夹下级数据
  408. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), 1).Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  409. }
  410. }
  411. }
  412. //删除信源以及文件夹
  413. if (request.FolderType == AllTypeConst.Wechat.GetHashCode())
  414. {
  415. foreach (var middle in middles)
  416. {
  417. //判断是否文件夹
  418. if (middle.AttributeValue == 2)
  419. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), 0).Where(nameof(Entity.Middle.Middle.ParentId), middle.Id).Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Wechat.GetHashCode()));
  420. }
  421. }
  422. await _middleRepository.DeleteAsync(request);
  423. transactionScope.Complete();
  424. }
  425. catch (Exception ex)
  426. {
  427. throw new BusinessException(ex.Message);
  428. }
  429. return true;
  430. }
  431. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  432. {
  433. return await _middleRepository.RecoverDeleteAsync(request);
  434. }
  435. /// <summary>
  436. /// 移动到
  437. /// </summary>
  438. /// <param name="request"></param>
  439. /// <returns></returns>
  440. public async Task<bool> SetMove(MiddleMoveRequest request)
  441. {
  442. if (request.ParentId == 0 && request.TypeId == -AllTypeConst.Note.GetHashCode())
  443. request.Ids = await _middleRepository.GetMiddleIsFolderAsync(request.Ids);
  444. try
  445. {
  446. using (var transactionScope = new TransactionScope())
  447. {
  448. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  449. if (request.ParentId > 0 && middle != null)
  450. {
  451. //获取父级子集集合
  452. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.Ids.ToList(), request.Ids.ToList());
  453. //修改
  454. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), middle.RoleId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  455. 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));
  456. //修改共享
  457. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), middle.MiddleSonId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  458. }
  459. if (request.ParentId == 0)
  460. {
  461. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.Ids.First()));
  462. if (middle.RoleId == 3)
  463. {
  464. /// <summary>
  465. /// 获取共享人员
  466. /// </summary>
  467. /// <returns></returns>
  468. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middle.MiddleSonId);
  469. foreach (var id in request.Ids)
  470. {
  471. var userMiddleInRequest = new List<UserMiddleInRequest>();
  472. foreach (var userMiddle in userMiddles)
  473. {
  474. userMiddleInRequest.Add(new UserMiddleInRequest
  475. {
  476. MiddleType = userMiddle.MiddleType,
  477. SourceType = userMiddle.SourceType,
  478. SourceId = userMiddle.SourceId,
  479. SourceName = userMiddle.SourceName,
  480. DataSourceId = id,
  481. SourceTypeValue = userMiddle.SourceTypeValue
  482. });
  483. }
  484. //添加共享人员
  485. await _userMiddleService.AddUserMiddleAsync(userMiddleInRequest);
  486. }
  487. }
  488. //修改
  489. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
  490. foreach (var item in request.Ids)
  491. {
  492. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(new List<int> { item }, new List<int> { });
  493. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), item).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  494. }
  495. }
  496. transactionScope.Complete();
  497. }
  498. }
  499. catch
  500. {
  501. return false;
  502. }
  503. return true;
  504. }
  505. /// <summary>
  506. /// 恢复全部
  507. /// </summary>
  508. /// <param name="folderType"></param>
  509. /// <returns></returns>
  510. public async Task<bool> RecoverAllAsync(int folderType, int userId)
  511. {
  512. var query = Q.NewQuery();
  513. query.Set(nameof(Entity.Middle.Middle.IsDelete), false);
  514. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
  515. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  516. return await _middleRepository.UpdateAsync(query);
  517. }
  518. /// <summary>
  519. /// 修改文件夹名称
  520. /// </summary>
  521. /// <param name="request"></param>
  522. /// <returns></returns>
  523. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  524. {
  525. var middle = await _middleRepository.GetMiddleAsync(request.Id);
  526. if (middle == null)
  527. throw new BusinessException("文件夹不存在");
  528. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  529. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  530. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  531. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  532. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  533. .Where(nameof(Entity.Middle.Middle.IsDelete), false));
  534. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  535. throw new BusinessException("文件夹名称重复,请重新命名!");
  536. return await _middleRepository.UpdateFolderNameAsync(request);
  537. }
  538. /// <summary>
  539. /// 查询文件夹
  540. /// </summary>
  541. /// <param name="request"></param>
  542. /// <returns></returns>
  543. public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
  544. {
  545. var query = Q.NewQuery();
  546. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  547. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  548. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  549. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  550. .Where(nameof(Entity.Middle.Middle.IsDelete), false);
  551. if (request.FolderIds != null && request.FolderIds.Count > 0)
  552. query.WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  553. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  554. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  555. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
  556. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  557. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  558. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
  559. 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)));
  560. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  561. foreach (var item in middles)
  562. {
  563. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  564. {
  565. Name = item.FolderName,
  566. Id = item.Id,
  567. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  568. RoleId = item.RoleId,
  569. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  570. IsTop = item.IsTop,
  571. IsSystemDefault = item.IsSystemDefault,
  572. ParentId = item.ParentId
  573. };
  574. if (item.RoleId == 3)
  575. {
  576. var sourceId = item.Id;
  577. if (item.ParentId != 0)
  578. sourceId = item.MiddleSonId;
  579. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  580. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  581. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  582. }
  583. result.Add(middleSearchFolderResult);
  584. }
  585. return result;
  586. }
  587. /// <summary>
  588. /// 查询文件夹包含父级
  589. /// </summary>
  590. /// <param name="request"></param>
  591. /// <returns></returns>
  592. public async Task<MiddleSearchFolderByParendIsResult> SearchFolderAndParentIdAsync(MiddleSearchFolderRequest request)
  593. {
  594. var query = Q.NewQuery();
  595. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  596. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  597. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  598. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  599. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  600. .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  601. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  602. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  603. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
  604. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  605. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  606. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
  607. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
  608. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  609. var result = new MiddleSearchFolderByParendIsResult() { Name = middle == null ? "" : middle.FolderName };
  610. if (middle != null && middle.ParentId > 0)
  611. {
  612. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId));
  613. result.Id = middle == null ? 0 : middle.Id;
  614. result.Name = middle == null ? "" : middle.FolderName;
  615. }
  616. result.Item = new List<MiddleSearchFolderResult>();
  617. foreach (var item in middles)
  618. {
  619. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  620. {
  621. Name = item.FolderName,
  622. Id = item.Id,
  623. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  624. RoleId = item.RoleId,
  625. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  626. IsTop = item.IsTop,
  627. ParentId = item.ParentId
  628. };
  629. if (item.RoleId == 3)
  630. {
  631. var sourceId = item.Id;
  632. if (item.ParentId != 0)
  633. sourceId = item.MiddleSonId;
  634. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  635. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  636. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  637. }
  638. result.Item.Add(middleSearchFolderResult);
  639. }
  640. return result;
  641. }
  642. /// <summary>
  643. /// 修改权限
  644. /// </summary>
  645. /// <param name="request"></param>
  646. /// <returns></returns>
  647. public async Task<bool> UpdateFolderRoleAsync(MiddleUpdateFolderRoleRequest request)
  648. {
  649. var middle = await _middleRepository.GetMiddleAsync(request.MiddleId);
  650. if (middle == null)
  651. throw new BusinessException("文件夹不存在");
  652. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  653. .Where(nameof(Entity.Middle.Middle.UserId), middle.UserId)
  654. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  655. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  656. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  657. .Where(nameof(Entity.Middle.Middle.ParentId), middle.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
  658. if (string.IsNullOrEmpty(request.FolderName))
  659. throw new BusinessException("文件名称未填写");
  660. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  661. throw new BusinessException("文件夹名称重复,请重新命名!");
  662. if (middle.RoleId == 3)
  663. {
  664. if (request.RoleId != 3)
  665. {
  666. //删除共享用户
  667. await _folderUserRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.FolderUser.MiddleId), middle.Id));
  668. //删除
  669. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  670. }
  671. }
  672. if (request.RoleId == 3 && middle.ParentId == 0)
  673. {
  674. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0)
  675. throw new BusinessException("共享成员必须");
  676. //添加人员
  677. await _folderUserRepository.DeleteAsync(Q.Where(nameof(FolderUser.MiddleId), request.MiddleId));
  678. var middleInsertRequest = new MiddleInsertRequest();
  679. middleInsertRequest.UserIds = request.UserIds;
  680. middleInsertRequest.UserMiddles = request.UserMiddles;
  681. middleInsertRequest.FolderType = middle.FolderType;
  682. if (request.UserMiddles.Count > 0)
  683. {
  684. //删除原有的数据
  685. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  686. //await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  687. //查询用户
  688. // var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middle.UserId);
  689. // middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  690. }
  691. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  692. }
  693. middle.RoleId = request.RoleId;
  694. middle.FolderName = request.FolderName;
  695. await _middleRepository.UpdateAsync(middle);
  696. //修改子集权限
  697. if (middle.ParentId == 0)
  698. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), request.RoleId).Where(nameof(Entity.Middle.Middle.MiddleSonId), middle.Id));
  699. return true;
  700. }
  701. /// <summary>
  702. /// 修改共享文件夹下的所有文件夹属性
  703. /// </summary>
  704. /// <param name="request"></param>
  705. /// <returns></returns>
  706. public async Task<bool> UpdateChildrenMiddleFolder(MiddleUpdateFolderRoleRequest request)
  707. {
  708. //获取下级数据
  709. var middles = await _middleRepository.GetMiddleChildrenAsync(request.MiddleId);
  710. if (middles.Count() == 0)
  711. return true;
  712. foreach (var item in middles)
  713. {
  714. if (request.UserMiddles.Count > 0)
  715. {
  716. var middleInsertRequest = new MiddleInsertRequest();
  717. middleInsertRequest.UserIds = request.UserIds;
  718. middleInsertRequest.UserMiddles = request.UserMiddles;
  719. //删除原有的数据
  720. await _userMiddleService.DeleteAsync(item.Id, item.FolderType);
  721. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  722. //查询用户
  723. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, item.UserId);
  724. middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  725. //插入共享成员
  726. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  727. item.RoleId = request.RoleId;
  728. item.FolderName = request.FolderName;
  729. //修改
  730. await _middleRepository.UpdateAsync(item);
  731. request.MiddleId = item.Id;
  732. //修改下级权限和共享范围人
  733. await UpdateChildrenMiddleFolder(request);
  734. }
  735. }
  736. return true;
  737. }
  738. /// <summary>
  739. /// 根据文件夹ID获取文件夹信息
  740. /// </summary>
  741. /// <param name="middleId"></param>
  742. /// <returns></returns>
  743. public async Task<MiddleFolderInfoResult> GetFolderInfo(int middleId)
  744. {
  745. if (middleId == 0)
  746. throw new Common.Exceptions.BusinessException("文件夹不存在");
  747. var result = new MiddleFolderInfoResult();
  748. var middle = await _middleRepository.GetMiddleAsync(middleId);
  749. result.Id = middle.Id;
  750. result.FolderName = middle.FolderName;
  751. result.FolderType = middle.FolderType;
  752. result.UserId = middle.UserId;
  753. result.RoleId = middle.RoleId;
  754. result.FolderUsers = await _folderUserRepository.GetAllAsync(middleId);
  755. //获取共享人员
  756. if (result.RoleId == 3)
  757. {
  758. var sourceId = middle.Id;
  759. if (middle.ParentId > 0)
  760. sourceId = middle.MiddleSonId;
  761. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, sourceId);
  762. result.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  763. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middle.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  764. // result.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  765. }
  766. result.UserMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middleId);
  767. return result;
  768. }
  769. /// <summary>
  770. /// 获取默认的共享文件夹范围
  771. /// </summary>
  772. /// <param name="userId"></param>
  773. /// <returns></returns>
  774. public async Task<MiddleDefaultResult> GetEnjoyDefaultAsync(int userId, int folderType)
  775. {
  776. var recordFolder = await recordFolderRepository.GetAsync(folderType, userId);
  777. var result = new MiddleDefaultResult();
  778. //获取笔记默认
  779. if (recordFolder.Id == 0 && folderType == AllTypeConst.TopicNote.GetHashCode())
  780. {
  781. var query = Q.NewQuery();
  782. query.Where(nameof(Entity.Middle.Middle.FolderType), folderType);
  783. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  784. query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
  785. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  786. var middle = await _middleRepository.GetMiddleAsync(query);
  787. result.IsDefault = middle != null ? middle.IsSystemDefault : false;
  788. result.FolderName = middle != null ? middle.FolderName : "";
  789. result.FolderType = middle != null ? middle.FolderType : 0;
  790. result.MiddleId = middle == null ? middle.Id : 0;
  791. return result;
  792. }
  793. result = new MiddleDefaultResult();
  794. result.IsDefault = recordFolder != null;
  795. result.FolderName = recordFolder == null ? "" : recordFolder.FolderName;
  796. result.FolderType = recordFolder == null ? 0 : recordFolder.FolderType;
  797. result.MiddleId = recordFolder == null ? 0 : recordFolder.FolderId;
  798. return result;
  799. }
  800. /// <summary>
  801. /// 根据类型搜索文件夹
  802. /// </summary>
  803. /// <param name="request"></param>
  804. /// <returns></returns>
  805. public async Task<IEnumerable<Entity.Middle.Middle>> GetSearchFolderResult(MiddleSearchFolderRequest request)
  806. {
  807. var query = Q.NewQuery();
  808. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  809. query.Where(query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%"));
  810. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
  811. query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.LastModifiedDate));
  812. return await _middleRepository.GetAllAsync(query);
  813. }
  814. /// <summary>
  815. /// 获取话题 笔记共享范围文件夹
  816. /// </summary>
  817. /// <param name="request"></param>
  818. /// <returns></returns>
  819. public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
  820. {
  821. string sqlStr = string.Empty;
  822. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  823. sqlStr += $" and FolderName like '%{request.KeyWord}%'";
  824. string sql = $@"SELECT
  825. *
  826. FROM
  827. tede_middle
  828. WHERE
  829. FolderType ={request.TypeId} AND AttributeValue = 2 {sqlStr}
  830. AND UserId = {request.UserId}
  831. AND RoleId IN (1 , 3)
  832. AND IsDelete = 0
  833. AND ParentId ={request.ParentId}
  834. ORDER BY IsSystemDefault DESC,AttributeValue desc , IsTop DESC , Sort DESC , CreatedDate DESC";
  835. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  836. var connection = database.GetConnection();
  837. var middles = await connection.QueryAsync<Entity.Middle.Middle>(sql);
  838. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  839. if (request.TypeId == AllTypeConst.TopicNote.GetHashCode() && request.ParentId == 0 && string.IsNullOrWhiteSpace(request.KeyWord))
  840. {
  841. result.Add(new MiddleSearchFolderResult
  842. {
  843. Name = "草稿箱",
  844. Id = 0,
  845. IsDisable = false,
  846. RoleId = 1,
  847. IsFolder = false,
  848. IsTop = false,
  849. FileCount = await _noteRepository.GetDraftCountAsync(request.UserId, true),
  850. IsSystemDefault = true,
  851. IsDraft = true
  852. });
  853. }
  854. foreach (var item in middles)
  855. {
  856. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  857. {
  858. Name = item.FolderName,
  859. Id = item.Id,
  860. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  861. RoleId = item.RoleId,
  862. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  863. IsTop = item.IsTop,
  864. 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)),
  865. IsSystemDefault = item.IsSystemDefault
  866. };
  867. if (item.RoleId == 3)
  868. {
  869. var sourceId = item.Id;
  870. if (item.ParentId != 0)
  871. sourceId = item.MiddleSonId;
  872. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  873. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  874. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  875. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  876. }
  877. result.Add(middleSearchFolderResult);
  878. }
  879. return result;
  880. }
  881. /// <summary>
  882. /// 获取话题最近使用的文件夹
  883. /// </summary>
  884. /// <param name="request"></param>
  885. /// <returns></returns>
  886. public async Task<List<MiddleSearchFolderResult>> GetLatelyTopicFolderAsync(int userId)
  887. {
  888. //获取用户最近发布的话题
  889. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, true);
  890. var query = Q.NewQuery();
  891. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  892. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.TopicNote.GetHashCode())
  893. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  894. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  895. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  896. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  897. var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
  898. var middles = new List<Entity.Middle.Middle>();
  899. foreach (var folderId in folderIds.Distinct())
  900. {
  901. foreach (var item in models)
  902. {
  903. if (item.Id == folderId)
  904. {
  905. middles.Add(item);
  906. break;
  907. }
  908. }
  909. }
  910. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  911. foreach (var item in middles)
  912. {
  913. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  914. {
  915. Name = item.FolderName,
  916. Id = item.Id,
  917. IsDisable = false,
  918. RoleId = item.RoleId,
  919. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  920. IsTop = item.IsTop
  921. };
  922. if (item.RoleId == 3)
  923. {
  924. var sourceId = item.Id;
  925. if (item.ParentId != 0)
  926. sourceId = item.MiddleSonId;
  927. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  928. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  929. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  930. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  931. }
  932. result.Add(middleSearchFolderResult);
  933. }
  934. return result;
  935. }
  936. /// <summary>
  937. /// 获取笔记最近使用的文件夹
  938. /// </summary>
  939. /// <param name="request"></param>
  940. /// <returns></returns>
  941. public async Task<List<MiddleSearchFolderResult>> GetLatelyNoteFolderAsync(int userId)
  942. {
  943. //获取用户最近发布的话题
  944. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, false);
  945. var query = Q.NewQuery();
  946. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  947. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode())
  948. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  949. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  950. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  951. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 2 });
  952. var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
  953. var middles = new List<Entity.Middle.Middle>();
  954. foreach (var folderId in folderIds.Distinct())
  955. {
  956. foreach (var item in models)
  957. {
  958. if (item.Id == folderId)
  959. {
  960. middles.Add(item);
  961. break;
  962. }
  963. }
  964. }
  965. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  966. foreach (var item in middles)
  967. {
  968. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  969. {
  970. Name = item.FolderName,
  971. Id = item.Id,
  972. IsDisable = false,
  973. RoleId = item.RoleId,
  974. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  975. IsTop = item.IsTop
  976. };
  977. if (item.RoleId == 3)
  978. {
  979. var sourceId = item.Id;
  980. if (item.ParentId != 0)
  981. sourceId = item.MiddleSonId;
  982. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  983. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  984. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  985. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  986. }
  987. result.Add(middleSearchFolderResult);
  988. }
  989. return result;
  990. }
  991. /// <summary> =-
  992. /// 删除全部
  993. /// </summary>
  994. /// <param name="folderType"></param>
  995. /// <param name="userId"></param>
  996. /// <returns></returns>
  997. public async Task<bool> DeleteAllAsync(int folderType, int userId)
  998. {
  999. var query = Q.NewQuery();
  1000. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
  1001. query.Where(nameof(Entity.Middle.Middle.UserId), userId);
  1002. query.Where(nameof(Entity.Middle.Middle.IsDelete), true);
  1003. return await _middleRepository.DeleteAsync(query);
  1004. }
  1005. }
  1006. }