MiddleService.cs 53 KB

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