MiddleService.cs 47 KB

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