MiddleService.cs 53 KB

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