MiddleService.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. namespace GxPress.Service.Implement.Middle
  25. {
  26. public class MiddleService : IMiddleService
  27. {
  28. private readonly IMiddleRepository _middleRepository;
  29. private readonly IFolderUserRepository _folderUserRepository;
  30. private readonly IUserRepository _userRepository;
  31. private readonly ICollectionRepository _collectionRepository;
  32. private readonly INoteRepository _noteRepository;
  33. private readonly IMissiveRepository _missiveRepository;
  34. private readonly IGroupRepository _groupRepository;
  35. private readonly IAnalyzeRepository _analyzeRepository;
  36. private readonly INoticeRepository _noticeRepository;
  37. private readonly IUserMiddleService _userMiddleService;
  38. private readonly IAddresseeRepository _addresseeRepository;
  39. private readonly IRecordFolderRepository recordFolderRepository;
  40. private readonly IAnalyzeService analyzeService;
  41. public MiddleService(IMiddleRepository middleRepository, IFolderUserRepository folderUserRepository,
  42. IUserRepository userRepository, ICollectionRepository collectionRepository, INoteRepository noteRepository,
  43. IMissiveRepository missiveRepository, IGroupRepository groupRepository, INoticeRepository noticeRepository, IAnalyzeRepository analyzeRepository, IUserMiddleService userMiddleService, IAddresseeRepository addresseeRepository, IRecordFolderRepository recordFolderRepository, IAnalyzeService analyzeService)
  44. {
  45. _middleRepository = middleRepository;
  46. _folderUserRepository = folderUserRepository;
  47. _userRepository = userRepository;
  48. _collectionRepository = collectionRepository;
  49. _noteRepository = noteRepository;
  50. _missiveRepository = missiveRepository;
  51. _groupRepository = groupRepository;
  52. _noticeRepository = noticeRepository;
  53. _analyzeRepository = analyzeRepository;
  54. _userMiddleService = userMiddleService;
  55. _addresseeRepository = addresseeRepository;
  56. this.recordFolderRepository = recordFolderRepository;
  57. this.analyzeService = analyzeService;
  58. }
  59. /// <summary>
  60. /// 添加文件夹
  61. /// </summary>
  62. /// <param name="request"></param>
  63. /// <returns></returns>
  64. public async Task<int> InsertAsync(MiddleInsertRequest request)
  65. {
  66. var folderRoleId = new List<int> { 1, 2, 3 };
  67. if (!folderRoleId.Contains(request.RoleId) && (request.FolderType == 3 || request.FolderType == 4))
  68. throw new BusinessException("文件夹权限有误!");
  69. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  70. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  71. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  72. .Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType)
  73. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  74. .Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
  75. if (string.IsNullOrEmpty(request.FolderName))
  76. throw new BusinessException("文件名称未填写!");
  77. if (oldMiddle != null)
  78. throw new BusinessException("文件夹名称重复,请重新命名!");
  79. var middle = new Entity.Middle.Middle
  80. {
  81. FolderName = request.FolderName,
  82. FolderType = request.FolderType,
  83. IsTop = false,
  84. AttributeValue = 2,
  85. ParentId = request.ParentId,
  86. RoleId = request.RoleId,
  87. UserId = request.UserId,
  88. Sort = 0
  89. };
  90. //存在父级
  91. var middleDto = new Entity.Middle.Middle();
  92. if (request.ParentId > 0)
  93. {
  94. //获取父级数据
  95. middleDto = await _middleRepository.GetMiddleAsync(request.ParentId);
  96. if (middleDto != null)
  97. {
  98. middle.RoleId = middleDto.RoleId;
  99. request.RoleId = middleDto.RoleId;
  100. if (middleDto.ParentId == 0)
  101. middle.MiddleSonId = middleDto.Id;
  102. else middle.MiddleSonId = middleDto.MiddleSonId;
  103. }
  104. }
  105. if (request.RoleId == 0)
  106. middle.RoleId = 1;
  107. if (request.RoleId == 3)
  108. {
  109. if (request.UserIds == null)
  110. request.UserIds = new List<int>();
  111. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0 && request.ParentId == 0)
  112. throw new BusinessException("共享用户必须");
  113. }
  114. if (request.RoleId > 0)
  115. middle.RoleId = request.RoleId;
  116. var middleId = await _middleRepository.InsertAsync(middle);
  117. if (middle.ParentId == 0)
  118. {
  119. middle = await _middleRepository.GetMiddleAsync(middleId);
  120. if (middle != null)
  121. {
  122. middle.MiddleSonId = middleId;
  123. await _middleRepository.UpdateAsync(middle);
  124. }
  125. }
  126. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
  127. .Where(nameof(Entity.Middle.Middle.Id), middleId));
  128. if (request.RoleId == 3 && request.ParentId == 0)
  129. //添加人员
  130. await InsertFolderUsers(request, middleId);
  131. return middleId;
  132. }
  133. /// <summary>
  134. /// 添加文件夹用户
  135. /// </summary>
  136. /// <param name="userIds"></param>
  137. /// <param name="middleId"></param>
  138. /// <returns></returns>
  139. public async Task<bool> InsertFolderUsers(MiddleInsertRequest request, int middleId)
  140. {
  141. //添加
  142. if (request.UserMiddles.Count > 0)
  143. {
  144. foreach (var item in request.UserMiddles)
  145. {
  146. item.DataSourceId = middleId;
  147. item.MiddleType = request.FolderType;
  148. }
  149. //添加中间表
  150. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  151. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middleId);
  152. request.UserIds = users.Select(n => n.Id).ToList();
  153. }
  154. if (request.UserIds.Count > 0)
  155. {
  156. var users = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), request.UserIds));
  157. //插入用户
  158. var folderUsers = new List<FolderUser>();
  159. foreach (var user in users)
  160. {
  161. var folderUser = new FolderUser();
  162. folderUser.UserId = user.Id;
  163. folderUser.UserName = user.Name;
  164. folderUser.MiddleId = middleId;
  165. folderUsers.Add(folderUser);
  166. }
  167. return await _folderUserRepository.InsertsAsync(folderUsers.ToList());
  168. }
  169. return false;
  170. }
  171. /// <summary>
  172. /// 修改文件夹名称
  173. /// </summary>
  174. /// <param name="request"></param>
  175. /// <returns></returns>
  176. public async Task<bool> UpdateNameAsync(MiddleUpdateNameRequest request)
  177. {
  178. var middle =
  179. await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.MiddleId)
  180. .Where(nameof(Entity.Middle.Middle.AttributeValue), 1));
  181. if (middle == null)
  182. throw new BusinessException("文件不存在");
  183. return await _middleRepository.UpdateAsync(
  184. Q.Set(nameof(Entity.Middle.Middle.FolderName), request.FolderName).Where(
  185. nameof(Entity.Middle.Middle.Id),
  186. request.MiddleId));
  187. }
  188. /// <summary>
  189. /// 添加数据
  190. /// </summary>
  191. /// <param name="request"></param>
  192. /// <returns></returns>
  193. public async Task<int> InsertAsync(MiddleInsertTypeRequest request)
  194. {
  195. var middle = new Entity.Middle.Middle
  196. {
  197. MiddleId = request.MiddleId,
  198. FolderType = request.FolderType,
  199. IsTop = false,
  200. AttributeValue = request.AttributeValue,
  201. ParentId = request.FolderId,
  202. UserId = request.UserId,
  203. MiddleSonId = request.MiddleSonId,
  204. IsAdmin = request.IsAdmin,
  205. NoticeAddresseeType = request.NoticeAddresseeType,
  206. Title = request.Title,
  207. IsRead = false,
  208. IsUpload = false,
  209. TitleLong = request.TitleLong,
  210. Content = "",
  211. AddressUser = request.AddressUser,
  212. CcUser = request.CcUser,
  213. CcUserComplete = request.CcUserComplete,
  214. AddressUserComplete = request.AddressUserComplete,
  215. IsRecall = false
  216. };
  217. if (request.AttributeValue == 0)
  218. middle.AttributeValue = 1;
  219. var middleId = await _middleRepository.InsertAsync(middle);
  220. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
  221. .Where(nameof(Entity.Middle.Middle.Id), middleId));
  222. return middleId;
  223. }
  224. /// <summary>
  225. /// 添加数据
  226. /// </summary>
  227. /// <param name="request"></param>
  228. /// <returns></returns>
  229. public async Task<bool> AddOrUpdateAsync(MiddleInsertTypeRequest request)
  230. {
  231. var middle = await _middleRepository.GetMiddleAsync(Q
  232. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  233. .Where(nameof(Entity.Middle.Middle.AttributeValue), 3)
  234. .Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType));
  235. if (middle == null)
  236. {
  237. middle = new Entity.Middle.Middle
  238. {
  239. MiddleId = request.MiddleId,
  240. FolderType = request.FolderType,
  241. IsTop = false,
  242. AttributeValue = request.AttributeValue,
  243. ParentId = request.FolderId,
  244. UserId = request.UserId,
  245. MiddleSonId = request.MiddleSonId
  246. };
  247. var middleId = await _middleRepository.InsertAsync(middle);
  248. //修改Middle sort
  249. await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middleId).Set(nameof(Entity.Middle.Middle.Sort), middleId));
  250. return true;
  251. }
  252. return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false)
  253. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  254. .Where(nameof(Entity.Middle.Middle.AttributeValue), 3));
  255. }
  256. /// <summary>
  257. /// 排序
  258. /// </summary>
  259. /// <param name="request"></param>
  260. /// <returns></returns>
  261. public async Task<bool> MiddleSortAsync(MiddleSortRequest request)
  262. {
  263. return await _middleRepository.SetSortAsync(request);
  264. }
  265. /// <summary>
  266. /// 设置置顶
  267. /// </summary>
  268. /// <param name="id"></param>
  269. /// <returns></returns>
  270. public async Task<bool> SetTopAsync(int id)
  271. {
  272. return await _middleRepository.SetTopAsync(id);
  273. }
  274. /// <summary>
  275. /// 删除 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7 小组
  276. /// </summary>
  277. /// <param name="request"></param>
  278. /// <returns></returns>
  279. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  280. {
  281. try
  282. {
  283. using TransactionScope transactionScope = new TransactionScope();
  284. //查询middle
  285. var middles = await _middleRepository.FindAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), request.MiddleIds));
  286. var ids = middles.Select(n => n.MiddleId).ToList();
  287. //收藏
  288. if (request.FolderType == AllTypeConst.Collect.GetHashCode())
  289. {
  290. //查询收藏
  291. var collections = await _collectionRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Collection.Id), ids));
  292. foreach (var item in collections)
  293. {
  294. var analyzeRequest = new AnalyzeRequest();
  295. analyzeRequest.AnalyzeType = 3;
  296. analyzeRequest.SourceId = item.CollectionDataId;
  297. analyzeRequest.UserId = item.UserId;
  298. //删除文章收藏
  299. if (item.CollectionType == AllTypeConst.Article.GetHashCode())
  300. {
  301. analyzeRequest.TypeValue = item.CollectionType;
  302. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  303. if (analyze != null)
  304. await _analyzeRepository.DeleteAsync(analyze.Id);
  305. }
  306. //删除话题收藏
  307. if (item.CollectionType == AllTypeConst.Topic.GetHashCode())
  308. {
  309. analyzeRequest.TypeValue = item.CollectionType;
  310. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  311. if (analyze != null)
  312. await _analyzeRepository.DeleteAsync(analyze.Id);
  313. }
  314. //删除笔记收藏
  315. if (item.CollectionType == AllTypeConst.Note.GetHashCode())
  316. {
  317. analyzeRequest.TypeValue = item.CollectionType;
  318. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  319. if (analyze != null)
  320. await _analyzeRepository.DeleteAsync(analyze.Id);
  321. }
  322. //删除通知收藏
  323. if (item.CollectionType == AllTypeConst.Notice.GetHashCode())
  324. {
  325. analyzeRequest.TypeValue = item.CollectionType;
  326. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  327. if (analyze != null)
  328. await _analyzeRepository.DeleteAsync(analyze.Id);
  329. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.UserId), request.UserId).WhereIn(nameof(Entity.Addressee.SourceId), ids).Set(nameof(Entity.Addressee.IsDelete), true));
  330. }
  331. //删除站内信收藏
  332. if (item.CollectionType == AllTypeConst.Missive.GetHashCode())
  333. {
  334. analyzeRequest.TypeValue = item.CollectionType;
  335. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  336. if (analyze != null)
  337. await _analyzeRepository.DeleteAsync(analyze.Id);
  338. await _addresseeRepository.UpdateAsync(Q.Where(nameof(Entity.Addressee.UserId), request.UserId).WhereIn(nameof(Entity.Addressee.SourceId), ids).Set(nameof(Entity.Addressee.IsDelete), true));
  339. }
  340. //删除收藏会议
  341. if (item.CollectionType == AllTypeConst.Meeting.GetHashCode())
  342. {
  343. analyzeRequest.TypeValue = item.CollectionType;
  344. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  345. if (analyze != null)
  346. await _analyzeRepository.DeleteAsync(analyze.Id);
  347. }
  348. //收藏笔记文件夹
  349. if (item.CollectionType == AllTypeConst.NoteFolder.GetHashCode() || item.CollectionType == AllTypeConst.CollectFolder.GetHashCode())
  350. {
  351. analyzeRequest.TypeValue = item.CollectionType;
  352. var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
  353. if (analyze != null)
  354. await _analyzeRepository.DeleteAsync(analyze.Id);
  355. }
  356. }
  357. //删除收藏
  358. request.IsDelete = true;
  359. //删除
  360. await _collectionRepository.RecursionDeleteAsync(request.MiddleIds);
  361. }
  362. //删除笔记
  363. if (request.FolderType == AllTypeConst.Note.GetHashCode())
  364. {
  365. await _noteRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Note.Note.Id), ids).Set(nameof(Entity.Note.Note.IsDelete), true));
  366. //删除默认共享文件夹
  367. await recordFolderRepository.DeleteAsync(request.FolderType, request.UserId, request.MiddleIds);
  368. }
  369. //小组
  370. if (request.FolderType == AllTypeConst.Group.GetHashCode())
  371. await _groupRepository.DeleteAsync(Q.WhereIn(nameof(Entity.Group.Id), ids));
  372. //删除收件箱
  373. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  374. 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));
  375. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  376. {
  377. foreach (var item in middles)
  378. {
  379. if (item.AttributeValue == 2)
  380. {
  381. //永久删除文件夹
  382. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.Id));
  383. //修改文件夹下级数据
  384. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), 1).Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  385. }
  386. }
  387. }
  388. //删除信源以及文件夹
  389. if (request.FolderType == AllTypeConst.Wechat.GetHashCode())
  390. {
  391. foreach (var middle in middles)
  392. {
  393. //判断是否文件夹
  394. if (middle.AttributeValue == 2)
  395. 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()));
  396. }
  397. }
  398. await _middleRepository.DeleteAsync(request);
  399. transactionScope.Complete();
  400. }
  401. catch (Exception ex)
  402. {
  403. throw new BusinessException(ex.Message);
  404. }
  405. return true;
  406. }
  407. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  408. {
  409. return await _middleRepository.RecoverDeleteAsync(request);
  410. }
  411. public async Task<bool> SetMove(MiddleMoveRequest request)
  412. {
  413. return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
  414. }
  415. /// <summary>
  416. /// 恢复全部
  417. /// </summary>
  418. /// <param name="folderType"></param>
  419. /// <returns></returns>
  420. public async Task<bool> RecoverAllAsync(int folderType, int userId)
  421. {
  422. return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.FolderType), folderType).Where(nameof(Entity.Middle.Middle.UserId), userId));
  423. }
  424. /// <summary>
  425. /// 修改文件夹名称
  426. /// </summary>
  427. /// <param name="request"></param>
  428. /// <returns></returns>
  429. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  430. {
  431. var middle = await _middleRepository.GetMiddleAsync(request.Id);
  432. if (middle == null)
  433. throw new BusinessException("文件夹不存在");
  434. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  435. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  436. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  437. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  438. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  439. .Where(nameof(Entity.Middle.Middle.IsDelete), false));
  440. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  441. throw new BusinessException("文件夹名称重复,请重新命名!");
  442. return await _middleRepository.UpdateFolderNameAsync(request);
  443. }
  444. /// <summary>
  445. /// 查询文件夹
  446. /// </summary>
  447. /// <param name="request"></param>
  448. /// <returns></returns>
  449. public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
  450. {
  451. var query = Q.NewQuery();
  452. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  453. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  454. .Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId)
  455. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  456. .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  457. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  458. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  459. if (request.RoleId > 0)
  460. query.Where(nameof(Entity.Middle.Middle.RoleId), request.RoleId);
  461. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
  462. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  463. foreach (var item in middles)
  464. {
  465. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  466. {
  467. Name = item.FolderName,
  468. Id = item.Id,
  469. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  470. RoleId = item.RoleId,
  471. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  472. IsTop = item.IsTop
  473. };
  474. if (item.RoleId == 3)
  475. {
  476. var sourceId = item.Id;
  477. if (item.ParentId != 0)
  478. sourceId = item.MiddleSonId;
  479. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  480. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  481. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  482. }
  483. result.Add(middleSearchFolderResult);
  484. }
  485. return result;
  486. }
  487. /// <summary>
  488. /// 修改权限
  489. /// </summary>
  490. /// <param name="request"></param>
  491. /// <returns></returns>
  492. public async Task<bool> UpdateFolderRoleAsync(MiddleUpdateFolderRoleRequest request)
  493. {
  494. var middle = await _middleRepository.GetMiddleAsync(request.MiddleId);
  495. if (middle == null)
  496. throw new BusinessException("文件夹不存在");
  497. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  498. .Where(nameof(Entity.Middle.Middle.UserId), middle.UserId)
  499. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  500. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  501. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  502. .Where(nameof(Entity.Middle.Middle.ParentId), middle.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
  503. if (string.IsNullOrEmpty(request.FolderName))
  504. throw new BusinessException("文件名称未填写");
  505. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  506. throw new BusinessException("文件夹名称重复,请重新命名!");
  507. if (middle.RoleId == 3)
  508. {
  509. if (request.RoleId != 3)
  510. {
  511. //删除共享用户
  512. await _folderUserRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.FolderUser.MiddleId), middle.Id));
  513. //删除
  514. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  515. }
  516. }
  517. if (request.RoleId == 3 && middle.ParentId == 0)
  518. {
  519. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0)
  520. throw new BusinessException("共享成员必须");
  521. //添加人员
  522. await _folderUserRepository.DeleteAsync(Q.Where(nameof(FolderUser.MiddleId), request.MiddleId));
  523. var middleInsertRequest = new MiddleInsertRequest();
  524. middleInsertRequest.UserIds = request.UserIds;
  525. middleInsertRequest.UserMiddles = request.UserMiddles;
  526. middleInsertRequest.FolderType = middle.FolderType;
  527. if (request.UserMiddles.Count > 0)
  528. {
  529. //删除原有的数据
  530. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  531. //await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  532. //查询用户
  533. // var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middle.UserId);
  534. // middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  535. }
  536. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  537. }
  538. middle.RoleId = request.RoleId;
  539. middle.FolderName = request.FolderName;
  540. await _middleRepository.UpdateAsync(middle);
  541. //修改子集权限
  542. if (middle.ParentId == 0)
  543. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), request.RoleId).Where(nameof(Entity.Middle.Middle.MiddleSonId), middle.Id));
  544. return true;
  545. }
  546. /// <summary>
  547. /// 修改共享文件夹下的所有文件夹属性
  548. /// </summary>
  549. /// <param name="request"></param>
  550. /// <returns></returns>
  551. public async Task<bool> UpdateChildrenMiddleFolder(MiddleUpdateFolderRoleRequest request)
  552. {
  553. //获取下级数据
  554. var middles = await _middleRepository.GetMiddleChildrenAsync(request.MiddleId);
  555. if (middles.Count() == 0)
  556. return true;
  557. foreach (var item in middles)
  558. {
  559. if (request.UserMiddles.Count > 0)
  560. {
  561. var middleInsertRequest = new MiddleInsertRequest();
  562. middleInsertRequest.UserIds = request.UserIds;
  563. middleInsertRequest.UserMiddles = request.UserMiddles;
  564. //删除原有的数据
  565. await _userMiddleService.DeleteAsync(item.Id, item.FolderType);
  566. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  567. //查询用户
  568. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, item.UserId);
  569. middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  570. //插入共享成员
  571. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  572. item.RoleId = request.RoleId;
  573. item.FolderName = request.FolderName;
  574. //修改
  575. await _middleRepository.UpdateAsync(item);
  576. request.MiddleId = item.Id;
  577. //修改下级权限和共享范围人
  578. await UpdateChildrenMiddleFolder(request);
  579. }
  580. }
  581. return true;
  582. }
  583. /// <summary>
  584. /// 根据文件夹ID获取文件夹信息
  585. /// </summary>
  586. /// <param name="middleId"></param>
  587. /// <returns></returns>
  588. public async Task<MiddleFolderInfoResult> GetFolderInfo(int middleId)
  589. {
  590. if (middleId == 0)
  591. throw new Common.Exceptions.BusinessException("文件夹不存在");
  592. var result = new MiddleFolderInfoResult();
  593. var middle = await _middleRepository.GetMiddleAsync(middleId);
  594. result.Id = middle.Id;
  595. result.FolderName = middle.FolderName;
  596. result.FolderType = middle.FolderType == 3 ? 12 : 13;
  597. result.UserId = middle.UserId;
  598. result.RoleId = middle.RoleId;
  599. result.FolderUsers = await _folderUserRepository.GetAllAsync(middleId);
  600. //获取共享人员
  601. if (result.RoleId == 3)
  602. {
  603. var sourceId = middle.Id;
  604. if (middle.ParentId > 0)
  605. sourceId = middle.MiddleSonId;
  606. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, sourceId);
  607. result.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  608. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middle.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  609. // result.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  610. }
  611. result.UserMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middleId);
  612. return result;
  613. }
  614. /// <summary>
  615. /// 获取默认的共享文件夹范围
  616. /// </summary>
  617. /// <param name="userId"></param>
  618. /// <returns></returns>
  619. public async Task<MiddleDefaultResult> GetEnjoyDefaultAsync(int userId, int folderType)
  620. {
  621. // var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.UserId), userId).WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 }).Where(nameof(Entity.Middle.Middle.IsDefault), true));
  622. // var result = new MiddleDefaultResult();
  623. // result.IsDefault = middle != null;
  624. // result.FolderName = middle == null ? "" : middle.FolderName;
  625. // result.MiddleId = middle == null ? 0 : middle.Id;
  626. var recordFolder = await recordFolderRepository.GetAsync(folderType, userId);
  627. var result = new MiddleDefaultResult();
  628. result.IsDefault = recordFolder != null;
  629. result.FolderName = recordFolder == null ? "" : recordFolder.FolderName;
  630. result.FolderType = recordFolder == null ? 0 : recordFolder.FolderType;
  631. result.MiddleId = recordFolder == null ? 0 : recordFolder.FolderId;
  632. return result;
  633. }
  634. /// <summary>
  635. /// 获取话题 笔记共享范围文件夹
  636. /// </summary>
  637. /// <param name="request"></param>
  638. /// <returns></returns>
  639. public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
  640. {
  641. var query = Q.NewQuery();
  642. if (request.ParentId >= 0)
  643. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId);
  644. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  645. .Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId)
  646. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  647. .Where(nameof(Entity.Middle.Middle.IsDelete), false);
  648. if (request.FolderIds.Count > 0)
  649. query.WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  650. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  651. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  652. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  653. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsSystemDefault),
  654. nameof(Entity.Middle.Middle.AttributeValue), nameof(Entity.Middle.Middle.Sort),
  655. nameof(Entity.Middle.Middle.CreatedDate)));
  656. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  657. foreach (var item in middles)
  658. {
  659. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  660. {
  661. Name = item.FolderName,
  662. Id = item.Id,
  663. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  664. RoleId = item.RoleId,
  665. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  666. IsTop = item.IsTop,
  667. FileCount = await _middleRepository.CountAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id)),
  668. IsSystemDefault = item.IsSystemDefault
  669. };
  670. if (item.RoleId == 3)
  671. {
  672. var sourceId = item.Id;
  673. if (item.ParentId != 0)
  674. sourceId = item.MiddleSonId;
  675. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  676. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  677. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  678. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  679. }
  680. result.Add(middleSearchFolderResult);
  681. }
  682. return result;
  683. }
  684. /// <summary>
  685. /// 获取话题最近使用的文件夹
  686. /// </summary>
  687. /// <param name="request"></param>
  688. /// <returns></returns>
  689. public async Task<List<MiddleSearchFolderResult>> GetLatelyNoteFolderAsync(int userId)
  690. {
  691. //获取用户最近发布的话题
  692. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId);
  693. var query = Q.NewQuery();
  694. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  695. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode())
  696. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  697. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  698. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  699. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  700. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsSystemDefault),
  701. nameof(Entity.Middle.Middle.AttributeValue), nameof(Entity.Middle.Middle.IsTop),
  702. nameof(Entity.Middle.Middle.CreatedDate)));
  703. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  704. foreach (var item in middles)
  705. {
  706. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  707. {
  708. Name = item.FolderName,
  709. Id = item.Id,
  710. IsDisable = false,
  711. RoleId = item.RoleId,
  712. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  713. IsTop = item.IsTop
  714. };
  715. if (item.RoleId == 3)
  716. {
  717. var sourceId = item.Id;
  718. if (item.ParentId != 0)
  719. sourceId = item.MiddleSonId;
  720. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  721. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  722. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  723. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  724. }
  725. result.Add(middleSearchFolderResult);
  726. }
  727. return result;
  728. }
  729. /// <summary> =-
  730. /// 删除全部
  731. /// </summary>
  732. /// <param name="folderType"></param>
  733. /// <param name="userId"></param>
  734. /// <returns></returns>
  735. public async Task<bool> DeleteAllAsync(int folderType, int userId)
  736. {
  737. return await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), folderType).Where(nameof(Entity.Middle.Middle.UserId), userId).Where(nameof(Entity.Middle.Middle.IsDelete), true));
  738. }
  739. }
  740. }