MiddleService.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. {
  377. 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));
  378. }
  379. if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
  380. {
  381. foreach (var item in middles)
  382. {
  383. if (item.AttributeValue == 2)
  384. {
  385. //永久删除文件夹
  386. await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.Id));
  387. //修改文件夹下级数据
  388. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), 1).Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
  389. }
  390. }
  391. }
  392. //删除信源以及文件夹
  393. if (request.FolderType == AllTypeConst.Wechat.GetHashCode())
  394. {
  395. foreach (var middle in middles)
  396. {
  397. //判断是否文件夹
  398. if (middle.AttributeValue == 2)
  399. 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()));
  400. }
  401. }
  402. await _middleRepository.DeleteAsync(request);
  403. transactionScope.Complete();
  404. }
  405. catch (Exception ex)
  406. {
  407. throw new BusinessException(ex.Message);
  408. }
  409. return true;
  410. }
  411. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  412. {
  413. return await _middleRepository.RecoverDeleteAsync(request);
  414. }
  415. /// <summary>
  416. /// 移动到
  417. /// </summary>
  418. /// <param name="request"></param>
  419. /// <returns></returns>
  420. public async Task<bool> SetMove(MiddleMoveRequest request)
  421. {
  422. if (request.ParentId == 0 && request.TypeId == -AllTypeConst.Note.GetHashCode())
  423. request.Ids = await _middleRepository.GetMiddleIsFolderAsync(request.Ids);
  424. try
  425. {
  426. using (var transactionScope = new TransactionScope())
  427. {
  428. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  429. if (request.ParentId > 0 && middle != null)
  430. {
  431. //获取父级子集集合
  432. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.Ids.ToList(), request.Ids.ToList());
  433. //修改
  434. 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));
  435. //修改共享
  436. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), middle.MiddleSonId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  437. }
  438. if (request.ParentId == 0)
  439. {
  440. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.Ids.First()));
  441. if (middle.RoleId == 3)
  442. {
  443. /// <summary>
  444. /// 获取共享人员
  445. /// </summary>
  446. /// <returns></returns>
  447. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middle.MiddleSonId);
  448. foreach (var id in request.Ids)
  449. {
  450. var userMiddleInRequest = new List<UserMiddleInRequest>();
  451. foreach (var userMiddle in userMiddles)
  452. {
  453. userMiddleInRequest.Add(new UserMiddleInRequest
  454. {
  455. MiddleType = userMiddle.MiddleType,
  456. SourceType = userMiddle.SourceType,
  457. SourceId = userMiddle.SourceId,
  458. SourceName = userMiddle.SourceName,
  459. DataSourceId = id,
  460. SourceTypeValue = userMiddle.SourceTypeValue
  461. });
  462. }
  463. //添加共享人员
  464. await _userMiddleService.AddUserMiddleAsync(userMiddleInRequest);
  465. }
  466. }
  467. //修改
  468. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
  469. foreach (var item in request.Ids)
  470. {
  471. var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(new List<int> { item }, new List<int> { });
  472. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), item).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
  473. }
  474. }
  475. transactionScope.Complete();
  476. }
  477. }
  478. catch
  479. {
  480. return false;
  481. }
  482. return true;
  483. }
  484. /// <summary>
  485. /// 恢复全部
  486. /// </summary>
  487. /// <param name="folderType"></param>
  488. /// <returns></returns>
  489. public async Task<bool> RecoverAllAsync(int folderType, int userId)
  490. {
  491. 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));
  492. }
  493. /// <summary>
  494. /// 修改文件夹名称
  495. /// </summary>
  496. /// <param name="request"></param>
  497. /// <returns></returns>
  498. public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
  499. {
  500. var middle = await _middleRepository.GetMiddleAsync(request.Id);
  501. if (middle == null)
  502. throw new BusinessException("文件夹不存在");
  503. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  504. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  505. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  506. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  507. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  508. .Where(nameof(Entity.Middle.Middle.IsDelete), false));
  509. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  510. throw new BusinessException("文件夹名称重复,请重新命名!");
  511. return await _middleRepository.UpdateFolderNameAsync(request);
  512. }
  513. /// <summary>
  514. /// 查询文件夹
  515. /// </summary>
  516. /// <param name="request"></param>
  517. /// <returns></returns>
  518. public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
  519. {
  520. var query = Q.NewQuery();
  521. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  522. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  523. if (request.TypeId == -AllTypeConst.Note.GetHashCode())
  524. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode());
  525. else
  526. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  527. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  528. .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  529. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  530. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  531. if (request.TypeId == -AllTypeConst.Note.GetHashCode())
  532. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  533. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  534. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1 });
  535. 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)));
  536. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  537. foreach (var item in middles)
  538. {
  539. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  540. {
  541. Name = item.FolderName,
  542. Id = item.Id,
  543. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  544. RoleId = item.RoleId,
  545. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  546. IsTop = item.IsTop,
  547. IsSystemDefault = item.IsSystemDefault
  548. };
  549. if (item.RoleId == 3)
  550. {
  551. var sourceId = item.Id;
  552. if (item.ParentId != 0)
  553. sourceId = item.MiddleSonId;
  554. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  555. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  556. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  557. }
  558. result.Add(middleSearchFolderResult);
  559. }
  560. return result;
  561. }
  562. /// <summary>
  563. /// 查询文件夹包含父级
  564. /// </summary>
  565. /// <param name="request"></param>
  566. /// <returns></returns>
  567. public async Task<MiddleSearchFolderByParendIsResult> SearchFolderAndParentIdAsync(MiddleSearchFolderRequest request)
  568. {
  569. var query = Q.NewQuery();
  570. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
  571. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
  572. if (request.TypeId == -AllTypeConst.Note.GetHashCode())
  573. query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode());
  574. else
  575. query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
  576. query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  577. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  578. .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  579. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  580. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  581. if (request.TypeId == -AllTypeConst.Note.GetHashCode())
  582. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
  583. if (request.TypeId == AllTypeConst.Note.GetHashCode())
  584. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1 });
  585. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
  586. var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
  587. var result = new MiddleSearchFolderByParendIsResult() { Name = middle == null ? "" : middle.FolderName };
  588. if (middle != null && middle.ParentId > 0)
  589. {
  590. middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId));
  591. result.Id = middle == null ? 0 : middle.Id;
  592. result.Name = middle == null ? "" : middle.FolderName;
  593. }
  594. result.Item = new List<MiddleSearchFolderResult>();
  595. foreach (var item in middles)
  596. {
  597. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  598. {
  599. Name = item.FolderName,
  600. Id = item.Id,
  601. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  602. RoleId = item.RoleId,
  603. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  604. IsTop = item.IsTop
  605. };
  606. if (item.RoleId == 3)
  607. {
  608. var sourceId = item.Id;
  609. if (item.ParentId != 0)
  610. sourceId = item.MiddleSonId;
  611. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  612. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  613. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  614. }
  615. result.Item.Add(middleSearchFolderResult);
  616. }
  617. return result;
  618. }
  619. /// <summary>
  620. /// 修改权限
  621. /// </summary>
  622. /// <param name="request"></param>
  623. /// <returns></returns>
  624. public async Task<bool> UpdateFolderRoleAsync(MiddleUpdateFolderRoleRequest request)
  625. {
  626. var middle = await _middleRepository.GetMiddleAsync(request.MiddleId);
  627. if (middle == null)
  628. throw new BusinessException("文件夹不存在");
  629. var oldMiddle = await _middleRepository.GetMiddleAsync(Q
  630. .Where(nameof(Entity.Middle.Middle.UserId), middle.UserId)
  631. .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  632. .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
  633. .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
  634. .Where(nameof(Entity.Middle.Middle.ParentId), middle.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
  635. if (string.IsNullOrEmpty(request.FolderName))
  636. throw new BusinessException("文件名称未填写");
  637. if (oldMiddle != null && oldMiddle.Id != middle.Id)
  638. throw new BusinessException("文件夹名称重复,请重新命名!");
  639. if (middle.RoleId == 3)
  640. {
  641. if (request.RoleId != 3)
  642. {
  643. //删除共享用户
  644. await _folderUserRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.FolderUser.MiddleId), middle.Id));
  645. //删除
  646. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  647. }
  648. }
  649. if (request.RoleId == 3 && middle.ParentId == 0)
  650. {
  651. if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0)
  652. throw new BusinessException("共享成员必须");
  653. //添加人员
  654. await _folderUserRepository.DeleteAsync(Q.Where(nameof(FolderUser.MiddleId), request.MiddleId));
  655. var middleInsertRequest = new MiddleInsertRequest();
  656. middleInsertRequest.UserIds = request.UserIds;
  657. middleInsertRequest.UserMiddles = request.UserMiddles;
  658. middleInsertRequest.FolderType = middle.FolderType;
  659. if (request.UserMiddles.Count > 0)
  660. {
  661. //删除原有的数据
  662. await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
  663. //await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  664. //查询用户
  665. // var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middle.UserId);
  666. // middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  667. }
  668. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  669. }
  670. middle.RoleId = request.RoleId;
  671. middle.FolderName = request.FolderName;
  672. await _middleRepository.UpdateAsync(middle);
  673. //修改子集权限
  674. if (middle.ParentId == 0)
  675. await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), request.RoleId).Where(nameof(Entity.Middle.Middle.MiddleSonId), middle.Id));
  676. return true;
  677. }
  678. /// <summary>
  679. /// 修改共享文件夹下的所有文件夹属性
  680. /// </summary>
  681. /// <param name="request"></param>
  682. /// <returns></returns>
  683. public async Task<bool> UpdateChildrenMiddleFolder(MiddleUpdateFolderRoleRequest request)
  684. {
  685. //获取下级数据
  686. var middles = await _middleRepository.GetMiddleChildrenAsync(request.MiddleId);
  687. if (middles.Count() == 0)
  688. return true;
  689. foreach (var item in middles)
  690. {
  691. if (request.UserMiddles.Count > 0)
  692. {
  693. var middleInsertRequest = new MiddleInsertRequest();
  694. middleInsertRequest.UserIds = request.UserIds;
  695. middleInsertRequest.UserMiddles = request.UserMiddles;
  696. //删除原有的数据
  697. await _userMiddleService.DeleteAsync(item.Id, item.FolderType);
  698. await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
  699. //查询用户
  700. var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, item.UserId);
  701. middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
  702. //插入共享成员
  703. await InsertFolderUsers(middleInsertRequest, request.MiddleId);
  704. item.RoleId = request.RoleId;
  705. item.FolderName = request.FolderName;
  706. //修改
  707. await _middleRepository.UpdateAsync(item);
  708. request.MiddleId = item.Id;
  709. //修改下级权限和共享范围人
  710. await UpdateChildrenMiddleFolder(request);
  711. }
  712. }
  713. return true;
  714. }
  715. /// <summary>
  716. /// 根据文件夹ID获取文件夹信息
  717. /// </summary>
  718. /// <param name="middleId"></param>
  719. /// <returns></returns>
  720. public async Task<MiddleFolderInfoResult> GetFolderInfo(int middleId)
  721. {
  722. if (middleId == 0)
  723. throw new Common.Exceptions.BusinessException("文件夹不存在");
  724. var result = new MiddleFolderInfoResult();
  725. var middle = await _middleRepository.GetMiddleAsync(middleId);
  726. result.Id = middle.Id;
  727. result.FolderName = middle.FolderName;
  728. result.FolderType = middle.FolderType == 3 ? 12 : 13;
  729. result.UserId = middle.UserId;
  730. result.RoleId = middle.RoleId;
  731. result.FolderUsers = await _folderUserRepository.GetAllAsync(middleId);
  732. //获取共享人员
  733. if (result.RoleId == 3)
  734. {
  735. var sourceId = middle.Id;
  736. if (middle.ParentId > 0)
  737. sourceId = middle.MiddleSonId;
  738. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, sourceId);
  739. result.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  740. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middle.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  741. // result.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  742. }
  743. result.UserMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middleId);
  744. return result;
  745. }
  746. /// <summary>
  747. /// 获取默认的共享文件夹范围
  748. /// </summary>
  749. /// <param name="userId"></param>
  750. /// <returns></returns>
  751. public async Task<MiddleDefaultResult> GetEnjoyDefaultAsync(int userId, int folderType)
  752. {
  753. // 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));
  754. // var result = new MiddleDefaultResult();
  755. // result.IsDefault = middle != null;
  756. // result.FolderName = middle == null ? "" : middle.FolderName;
  757. // result.MiddleId = middle == null ? 0 : middle.Id;
  758. var recordFolder = await recordFolderRepository.GetAsync(folderType, userId);
  759. var result = new MiddleDefaultResult();
  760. //获取笔记默认
  761. if (recordFolder.Id == 0 && folderType == 21)
  762. {
  763. 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));
  764. result.IsDefault = middle != null ? middle.IsSystemDefault : false;
  765. result.FolderName = middle != null ? middle.FolderName : "";
  766. result.FolderType = middle != null ? middle.FolderType : 0;
  767. result.MiddleId = middle == null ? middle.Id : 0;
  768. return result;
  769. }
  770. result = new MiddleDefaultResult();
  771. result.IsDefault = recordFolder != null;
  772. result.FolderName = recordFolder == null ? "" : recordFolder.FolderName;
  773. result.FolderType = recordFolder == null ? 0 : recordFolder.FolderType;
  774. result.MiddleId = recordFolder == null ? 0 : recordFolder.FolderId;
  775. return result;
  776. }
  777. /// <summary>
  778. /// 获取话题 笔记共享范围文件夹
  779. /// </summary>
  780. /// <param name="request"></param>
  781. /// <returns></returns>
  782. public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
  783. {
  784. var query = Q.NewQuery();
  785. if (request.ParentId >= 0)
  786. query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId);
  787. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  788. .Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId)
  789. .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
  790. .Where(nameof(Entity.Middle.Middle.IsDelete), false);
  791. if (request.FolderIds.Count > 0)
  792. query.WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
  793. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  794. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  795. query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
  796. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsSystemDefault),
  797. nameof(Entity.Middle.Middle.AttributeValue), nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort),
  798. nameof(Entity.Middle.Middle.CreatedDate)));
  799. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  800. foreach (var item in middles)
  801. {
  802. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  803. {
  804. Name = item.FolderName,
  805. Id = item.Id,
  806. IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
  807. RoleId = item.RoleId,
  808. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  809. IsTop = item.IsTop,
  810. FileCount = await _middleRepository.CountAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id).Where(nameof(Entity.Middle.Middle.IsDelete), false)),
  811. IsSystemDefault = item.IsSystemDefault
  812. };
  813. if (item.RoleId == 3)
  814. {
  815. var sourceId = item.Id;
  816. if (item.ParentId != 0)
  817. sourceId = item.MiddleSonId;
  818. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  819. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  820. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  821. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  822. }
  823. result.Add(middleSearchFolderResult);
  824. }
  825. return result;
  826. }
  827. /// <summary>
  828. /// 获取话题最近使用的文件夹
  829. /// </summary>
  830. /// <param name="request"></param>
  831. /// <returns></returns>
  832. public async Task<List<MiddleSearchFolderResult>> GetLatelyNoteFolderAsync(int userId)
  833. {
  834. //获取用户最近发布的话题
  835. var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId);
  836. var query = Q.NewQuery();
  837. query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
  838. .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode())
  839. .Where(nameof(Entity.Middle.Middle.UserId), userId)
  840. .Where(nameof(Entity.Middle.Middle.IsDelete), false)
  841. .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
  842. query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
  843. var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsSystemDefault),
  844. nameof(Entity.Middle.Middle.AttributeValue), nameof(Entity.Middle.Middle.IsTop),
  845. nameof(Entity.Middle.Middle.CreatedDate)));
  846. List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
  847. foreach (var item in middles)
  848. {
  849. MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
  850. {
  851. Name = item.FolderName,
  852. Id = item.Id,
  853. IsDisable = false,
  854. RoleId = item.RoleId,
  855. IsFolder = await _middleRepository.IsFolderAsync(item.Id),
  856. IsTop = item.IsTop
  857. };
  858. if (item.RoleId == 3)
  859. {
  860. var sourceId = item.Id;
  861. if (item.ParentId != 0)
  862. sourceId = item.MiddleSonId;
  863. var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
  864. // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
  865. middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
  866. //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
  867. }
  868. result.Add(middleSearchFolderResult);
  869. }
  870. return result;
  871. }
  872. /// <summary> =-
  873. /// 删除全部
  874. /// </summary>
  875. /// <param name="folderType"></param>
  876. /// <param name="userId"></param>
  877. /// <returns></returns>
  878. public async Task<bool> DeleteAllAsync(int folderType, int userId)
  879. {
  880. 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));
  881. }
  882. }
  883. }