MiddleService.cs 52 KB

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