MiddleService.cs 52 KB

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