1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using GxPress.Common.Exceptions;
- using GxPress.Repository.Interface;
- using GxPress.Request.App.Middle;
- using GxPress.Request.Notice;
- using GxPress.Service.Interface.Middle;
- using Datory;
- using GxPress.Entity.Middle;
- using GxPress.Result.App.Middle;
- using System.Transactions;
- using GxPress.Repository.Interface.Collection;
- using GxPress.Repository.Interface.Note;
- using GxPress.Repository.Interface.Missive;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.Analyze;
- using GxPress.Service.Interface.UserMiddle;
- using GxPress.Repository.Interface.RecordFolder;
- using GxPress.Service.Interface.Analyze;
- using GxPress.Request.App.Analyze;
- using GxPress.EnumConst;
- using System.Transactions;
- using GxPress.Request.UserMiddle;
- using Dapper;
- namespace GxPress.Service.Implement.Middle
- {
- public class MiddleService : IMiddleService
- {
- private readonly IMiddleRepository _middleRepository;
- private readonly IFolderUserRepository _folderUserRepository;
- private readonly IUserRepository _userRepository;
- private readonly ICollectionRepository _collectionRepository;
- private readonly INoteRepository _noteRepository;
- private readonly IMissiveRepository _missiveRepository;
- private readonly IGroupRepository _groupRepository;
- private readonly IAnalyzeRepository _analyzeRepository;
- private readonly INoticeRepository _noticeRepository;
- private readonly IUserMiddleService _userMiddleService;
- private readonly IAddresseeRepository _addresseeRepository;
- private readonly IRecordFolderRepository recordFolderRepository;
- private readonly IAnalyzeService analyzeService;
- public MiddleService(IMiddleRepository middleRepository, IFolderUserRepository folderUserRepository,
- IUserRepository userRepository, ICollectionRepository collectionRepository, INoteRepository noteRepository,
- IMissiveRepository missiveRepository, IGroupRepository groupRepository, INoticeRepository noticeRepository, IAnalyzeRepository analyzeRepository, IUserMiddleService userMiddleService, IAddresseeRepository addresseeRepository, IRecordFolderRepository recordFolderRepository, IAnalyzeService analyzeService)
- {
- _middleRepository = middleRepository;
- _folderUserRepository = folderUserRepository;
- _userRepository = userRepository;
- _collectionRepository = collectionRepository;
- _noteRepository = noteRepository;
- _missiveRepository = missiveRepository;
- _groupRepository = groupRepository;
- _noticeRepository = noticeRepository;
- _analyzeRepository = analyzeRepository;
- _userMiddleService = userMiddleService;
- _addresseeRepository = addresseeRepository;
- this.recordFolderRepository = recordFolderRepository;
- this.analyzeService = analyzeService;
- }
- /// <summary>
- /// 添加文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<MiddleInResult> InsertAsync(MiddleInsertRequest request)
- {
- var folderRoleId = new List<int> { 1, 2, 3 };
- if (!folderRoleId.Contains(request.RoleId) && (request.FolderType == 3 || request.FolderType == 4))
- throw new BusinessException("文件夹权限有误!");
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
- query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
- query.Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType);
- if (request.FolderType == AllTypeConst.Note.GetHashCode())
- {
- if (request.RoleId == 1 || request.RoleId == 3)
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
- else
- query.Where(nameof(Entity.Middle.Middle.RoleId), request.RoleId);
- }
- query.Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName);
- query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId);
- query.Where(nameof(Entity.Middle.Middle.IsDelete), false);
- var oldMiddle = await _middleRepository.GetMiddleAsync(query);
- if (string.IsNullOrEmpty(request.FolderName))
- throw new BusinessException("文件名称未填写!");
- if (oldMiddle != null)
- throw new BusinessException("文件夹名称重复,请重新命名!");
- var middle = new Entity.Middle.Middle
- {
- FolderName = request.FolderName,
- FolderType = request.FolderType,
- IsTop = false,
- AttributeValue = 2,
- ParentId = request.ParentId,
- RoleId = request.RoleId,
- UserId = request.UserId,
- Sort = 0
- };
- //存在父级
- var middleDto = new Entity.Middle.Middle();
- if (request.ParentId > 0)
- {
- //获取父级数据
- middleDto = await _middleRepository.GetMiddleAsync(request.ParentId);
- if (middleDto != null)
- {
- middle.RoleId = middleDto.RoleId;
- request.RoleId = middleDto.RoleId;
- if (middleDto.ParentId == 0)
- middle.MiddleSonId = middleDto.Id;
- else middle.MiddleSonId = middleDto.MiddleSonId;
- }
- }
- if (request.RoleId == 0)
- middle.RoleId = 1;
- if (request.RoleId == 3)
- {
- if (request.UserIds == null)
- request.UserIds = new List<int>();
- if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0 && request.ParentId == 0)
- throw new BusinessException("共享用户必须");
- }
- if (request.RoleId > 0)
- middle.RoleId = request.RoleId;
- var middleId = await _middleRepository.InsertAsync(middle);
- if (middle.ParentId == 0)
- {
- middle = await _middleRepository.GetMiddleAsync(middleId);
- if (middle != null)
- {
- middle.MiddleSonId = middleId;
- await _middleRepository.UpdateAsync(middle);
- }
- }
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
- .Where(nameof(Entity.Middle.Middle.Id), middleId));
- if (request.RoleId == 3 && request.ParentId == 0)
- //添加人员
- await InsertFolderUsers(request, middleId);
- var result = new MiddleInResult()
- {
- Id = middleId,
- Name = request.FolderName
- };
- return result;
- }
- /// <summary>
- /// 添加文件夹用户
- /// </summary>
- /// <param name="userIds"></param>
- /// <param name="middleId"></param>
- /// <returns></returns>
- public async Task<bool> InsertFolderUsers(MiddleInsertRequest request, int middleId)
- {
- //添加
- if (request.UserMiddles.Count > 0)
- {
- foreach (var item in request.UserMiddles)
- {
- item.DataSourceId = middleId;
- item.MiddleType = request.FolderType;
- }
- //添加中间表
- await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
- var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middleId);
- request.UserIds = users.Select(n => n.Id).ToList();
- }
- if (request.UserIds.Count > 0)
- {
- var users = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), request.UserIds));
- //插入用户
- var folderUsers = new List<FolderUser>();
- foreach (var user in users)
- {
- var folderUser = new FolderUser();
- folderUser.UserId = user.Id;
- folderUser.UserName = user.Name;
- folderUser.MiddleId = middleId;
- folderUsers.Add(folderUser);
- }
- return await _folderUserRepository.InsertsAsync(folderUsers.ToList());
- }
- return false;
- }
- /// <summary>
- /// 修改文件夹名称
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateNameAsync(MiddleUpdateNameRequest request)
- {
- var middle =
- await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.MiddleId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 1));
- if (middle == null)
- throw new BusinessException("文件不存在");
- return await _middleRepository.UpdateAsync(
- Q.Set(nameof(Entity.Middle.Middle.FolderName), request.FolderName).Where(
- nameof(Entity.Middle.Middle.Id),
- request.MiddleId));
- }
- /// <summary>
- /// 添加数据
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<int> InsertAsync(MiddleInsertTypeRequest request)
- {
- var middle = new Entity.Middle.Middle
- {
- MiddleId = request.MiddleId,
- FolderType = request.FolderType,
- IsTop = false,
- AttributeValue = request.AttributeValue,
- ParentId = request.FolderId,
- UserId = request.UserId,
- MiddleSonId = request.MiddleSonId,
- IsAdmin = request.IsAdmin,
- NoticeAddresseeType = request.NoticeAddresseeType,
- Title = request.Title,
- IsRead = false,
- IsUpload = false,
- TitleLong = request.TitleLong,
- Content = "",
- AddressUser = request.AddressUser,
- CcUser = request.CcUser,
- CcUserComplete = request.CcUserComplete,
- AddressUserComplete = request.AddressUserComplete,
- IsRecall = false
- };
- if (request.AttributeValue == 0)
- middle.AttributeValue = 1;
- var middleId = await _middleRepository.InsertAsync(middle);
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.Sort), middleId)
- .Where(nameof(Entity.Middle.Middle.Id), middleId));
- return middleId;
- }
- /// <summary>
- /// 添加数据
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddOrUpdateAsync(MiddleInsertTypeRequest request)
- {
- var middle = await _middleRepository.GetMiddleAsync(Q
- .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 3)
- .Where(nameof(Entity.Middle.Middle.FolderType), request.FolderType));
- if (middle == null)
- {
- middle = new Entity.Middle.Middle
- {
- MiddleId = request.MiddleId,
- FolderType = request.FolderType,
- IsTop = false,
- AttributeValue = request.AttributeValue,
- ParentId = request.FolderId,
- UserId = request.UserId,
- MiddleSonId = request.MiddleSonId
- };
- var middleId = await _middleRepository.InsertAsync(middle);
- //修改Middle sort
- await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middleId).Set(nameof(Entity.Middle.Middle.Sort), middleId));
- return true;
- }
- return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false)
- .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 3));
- }
- /// <summary>
- /// 排序
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> MiddleSortAsync(MiddleSortRequest request)
- {
- return await _middleRepository.SetSortAsync(request);
- }
- /// <summary>
- /// 设置置顶
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> SetTopAsync(int id)
- {
- return await _middleRepository.SetTopAsync(id);
- }
- /// <summary>
- /// 删除 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7 小组
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(NoticeDeRequest request)
- {
- try
- {
- var query = Q.NewQuery();
- using TransactionScope transactionScope = new TransactionScope();
- var middleIds = request.MiddleIds;
- //获取
- await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.MiddleIds, middleIds);
- request.MiddleIds = middleIds;
- //查询middle
- var middles = await _middleRepository.FindAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), request.MiddleIds));
- var ids = middles.Select(n => n.MiddleId).ToList();
- //收藏
- if (request.FolderType == AllTypeConst.Collect.GetHashCode())
- {
- //查询收藏
- var collections = await _collectionRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Collection.Id), ids));
- foreach (var item in collections)
- {
- var analyzeRequest = new AnalyzeRequest();
- analyzeRequest.AnalyzeType = 3;
- analyzeRequest.SourceId = item.CollectionDataId;
- analyzeRequest.UserId = item.UserId;
- //删除文章收藏
- if (item.CollectionType == AllTypeConst.Article.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- }
- //删除话题收藏
- if (item.CollectionType == AllTypeConst.Topic.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- }
- //删除笔记收藏
- if (item.CollectionType == AllTypeConst.Note.GetHashCode() || item.CollectionType == AllTypeConst.TopicNote.GetHashCode()
- || item.CollectionType == AllTypeConst.TopicNoteAll.GetHashCode() || item.CollectionType == AllTypeConst.TopicNoteFolder.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- }
- //删除通知收藏
- if (item.CollectionType == AllTypeConst.Notice.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- query = Q.NewQuery();
- query.Where(nameof(Entity.Addressee.UserId), request.UserId);
- query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
- query.Set(nameof(Entity.Addressee.IsDelete), true);
- await _addresseeRepository.UpdateAsync(query);
- }
- //删除站内信收藏
- if (item.CollectionType == AllTypeConst.Missive.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- query = Q.NewQuery();
- query.Where(nameof(Entity.Addressee.UserId), request.UserId);
- query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
- query.Set(nameof(Entity.Addressee.IsDelete), true);
- await _addresseeRepository.UpdateAsync(query);
- }
- //删除收藏会议
- if (item.CollectionType == AllTypeConst.Meeting.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- }
- //收藏笔记文件夹
- if (item.CollectionType == AllTypeConst.NoteFolder.GetHashCode() || item.CollectionType == AllTypeConst.CollectFolder.GetHashCode())
- {
- analyzeRequest.TypeValue = item.CollectionType;
- var analyze = await analyzeService.GetAnalyzeAsync(analyzeRequest);
- if (analyze != null)
- await _analyzeRepository.DeleteAsync(analyze.Id);
- }
- }
- //删除收藏
- request.IsDelete = true;
- //删除
- await _collectionRepository.RecursionDeleteAsync(request.MiddleIds);
- }
- //删除笔记
- if (request.FolderType == AllTypeConst.Note.GetHashCode())
- {
- await _noteRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Note.Note.Id), ids).Set(nameof(Entity.Note.Note.IsDelete), true));
- //删除默认共享文件夹
- await recordFolderRepository.DeleteAsync(request.FolderType, request.UserId, request.MiddleIds);
- }
- //小组
- if (request.FolderType == AllTypeConst.Group.GetHashCode())
- await _groupRepository.DeleteAsync(Q.WhereIn(nameof(Entity.Group.Id), ids));
- //删除收件箱
- if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
- {
- query = Q.NewQuery();
- query.Where(nameof(Entity.Addressee.UserId), request.UserId);
- query.WhereIn(nameof(Entity.Addressee.SourceId), ids);
- query.Set(nameof(Entity.Addressee.IsDelete), true);
- query.Set(nameof(Entity.Addressee.IsRead), true);
- await _addresseeRepository.UpdateAsync(query);
- //request.IsDelete = true;
- }
- if (request.FolderType == AllTypeConst.Inbox.GetHashCode())
- {
- foreach (var item in middles)
- {
- if (item.AttributeValue == 2)
- {
- //永久删除文件夹
- await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.Id));
- //修改文件夹下级数据
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), 1).Where(nameof(Entity.Middle.Middle.ParentId), item.Id));
- }
- }
- }
- //删除信源以及文件夹
- if (request.FolderType == AllTypeConst.Wechat.GetHashCode())
- {
- foreach (var middle in middles)
- {
- //判断是否文件夹
- if (middle.AttributeValue == 2)
- {
- query = Q.NewQuery();
- query.Set(nameof(Entity.Middle.Middle.ParentId), 0);
- query.Where(nameof(Entity.Middle.Middle.ParentId), middle.Id);
- query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Wechat.GetHashCode());
- await _middleRepository.UpdateAsync(query);
- }
- }
- }
- await _middleRepository.DeleteAsync(request);
- transactionScope.Complete();
- }
- catch (Exception ex)
- {
- throw new BusinessException(ex.Message);
- }
- return true;
- }
- public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
- {
- return await _middleRepository.RecoverDeleteAsync(request);
- }
- /// <summary>
- /// 移动到
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> SetMove(MiddleMoveRequest request)
- {
- if (request.ParentId == 0 && request.TypeId == -AllTypeConst.Note.GetHashCode())
- request.Ids = await _middleRepository.GetMiddleIsFolderAsync(request.Ids);
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
- if (request.ParentId > 0 && middle != null)
- {
- //获取父级子集集合
- var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(request.Ids.ToList(), request.Ids.ToList());
- //修改
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), middle.RoleId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
- 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));
- //修改共享
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), middle.MiddleSonId).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
- }
- if (request.ParentId == 0)
- {
- middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.Ids.First()));
- if (middle.RoleId == 3)
- {
- /// <summary>
- /// 获取共享人员
- /// </summary>
- /// <returns></returns>
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middle.MiddleSonId);
- foreach (var id in request.Ids)
- {
- var userMiddleInRequest = new List<UserMiddleInRequest>();
- foreach (var userMiddle in userMiddles)
- {
- userMiddleInRequest.Add(new UserMiddleInRequest
- {
- MiddleType = userMiddle.MiddleType,
- SourceType = userMiddle.SourceType,
- SourceId = userMiddle.SourceId,
- SourceName = userMiddle.SourceName,
- DataSourceId = id,
- SourceTypeValue = userMiddle.SourceTypeValue
- });
- }
- //添加共享人员
- await _userMiddleService.AddUserMiddleAsync(userMiddleInRequest);
- }
- }
- //修改
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.ParentId), request.ParentId).WhereIn(nameof(Entity.Middle.Middle.Id), request.Ids));
- foreach (var item in request.Ids)
- {
- var ids = await _middleRepository.GetAllMiddleChildrenByParentIdAsync(new List<int> { item }, new List<int> { });
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.MiddleSonId), item).WhereIn(nameof(Entity.Middle.Middle.Id), ids));
- }
- }
- transactionScope.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 恢复全部
- /// </summary>
- /// <param name="folderType"></param>
- /// <returns></returns>
- public async Task<bool> RecoverAllAsync(int folderType, int userId)
- {
- var query = Q.NewQuery();
- query.Set(nameof(Entity.Middle.Middle.IsDelete), false);
- query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
- query.Where(nameof(Entity.Middle.Middle.UserId), userId);
- return await _middleRepository.UpdateAsync(query);
- }
- /// <summary>
- /// 修改文件夹名称
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateFolderNameAsync(MiddleFolderNameRequest request)
- {
- var middle = await _middleRepository.GetMiddleAsync(request.Id);
- if (middle == null)
- throw new BusinessException("文件夹不存在");
- var oldMiddle = await _middleRepository.GetMiddleAsync(Q
- .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
- .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
- .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
- .Where(nameof(Entity.Middle.Middle.IsDelete), false));
- if (oldMiddle != null && oldMiddle.Id != middle.Id)
- throw new BusinessException("文件夹名称重复,请重新命名!");
- return await _middleRepository.UpdateFolderNameAsync(request);
- }
- /// <summary>
- /// 查询文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
- {
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
- query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
- query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.IsDelete), false);
- if (request.FolderIds != null && request.FolderIds.Count > 0)
- query.WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
- if (!string.IsNullOrWhiteSpace(request.KeyWord))
- query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
- if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
- if (request.TypeId == AllTypeConst.Note.GetHashCode())
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
- 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)));
- List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
- foreach (var item in middles)
- {
- MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
- {
- Name = item.FolderName,
- Id = item.Id,
- IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
- RoleId = item.RoleId,
- IsFolder = await _middleRepository.IsFolderAsync(item.Id),
- IsTop = item.IsTop,
- IsSystemDefault = item.IsSystemDefault,
- ParentId = item.ParentId
- };
- if (item.RoleId == 3)
- {
- var sourceId = item.Id;
- if (item.ParentId != 0)
- sourceId = item.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- }
- result.Add(middleSearchFolderResult);
- }
- return result;
- }
- /// <summary>
- /// 查询文件夹包含父级
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<MiddleSearchFolderByParendIsResult> SearchFolderAndParentIdAsync(MiddleSearchFolderRequest request)
- {
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.ParentId), request.ParentId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
- query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
- query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.UserId), request.UserId)
- .Where(nameof(Entity.Middle.Middle.IsDelete), false).WhereNotIn(nameof(Entity.Middle.Middle.Id), request.FolderIds);
- if (!string.IsNullOrWhiteSpace(request.KeyWord))
- query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%");
- if (request.TypeId == AllTypeConst.TopicNote.GetHashCode())
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 1, 3 });
- if (request.TypeId == AllTypeConst.Note.GetHashCode())
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int> { 2 });
- var middles = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.Sort), nameof(Entity.Middle.Middle.LastModifiedDate)));
- var middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), request.ParentId));
- var result = new MiddleSearchFolderByParendIsResult() { Name = middle == null ? "" : middle.FolderName };
- if (middle != null && middle.ParentId > 0)
- {
- middle = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId));
- result.Id = middle == null ? 0 : middle.Id;
- result.Name = middle == null ? "" : middle.FolderName;
- }
- result.Item = new List<MiddleSearchFolderResult>();
- foreach (var item in middles)
- {
- MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
- {
- Name = item.FolderName,
- Id = item.Id,
- IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
- RoleId = item.RoleId,
- IsFolder = await _middleRepository.IsFolderAsync(item.Id),
- IsTop = item.IsTop,
- ParentId = item.ParentId
- };
- if (item.RoleId == 3)
- {
- var sourceId = item.Id;
- if (item.ParentId != 0)
- sourceId = item.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- }
- result.Item.Add(middleSearchFolderResult);
- }
- return result;
- }
- /// <summary>
- /// 修改权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateFolderRoleAsync(MiddleUpdateFolderRoleRequest request)
- {
- var middle = await _middleRepository.GetMiddleAsync(request.MiddleId);
- if (middle == null)
- throw new BusinessException("文件夹不存在");
- var oldMiddle = await _middleRepository.GetMiddleAsync(Q
- .Where(nameof(Entity.Middle.Middle.UserId), middle.UserId)
- .Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
- .Where(nameof(Entity.Middle.Middle.FolderType), middle.FolderType)
- .Where(nameof(Entity.Middle.Middle.FolderName), request.FolderName)
- .Where(nameof(Entity.Middle.Middle.ParentId), middle.ParentId).Where(nameof(Entity.Middle.Middle.IsDelete), false));
- if (string.IsNullOrEmpty(request.FolderName))
- throw new BusinessException("文件名称未填写");
- if (oldMiddle != null && oldMiddle.Id != middle.Id)
- throw new BusinessException("文件夹名称重复,请重新命名!");
- if (middle.RoleId == 3)
- {
- if (request.RoleId != 3)
- {
- //删除共享用户
- await _folderUserRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.FolderUser.MiddleId), middle.Id));
- //删除
- await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
- }
- }
- if (request.RoleId == 3 && middle.ParentId == 0)
- {
- if (request.UserIds.Count == 0 && request.UserMiddles.Count == 0)
- throw new BusinessException("共享成员必须");
- //添加人员
- await _folderUserRepository.DeleteAsync(Q.Where(nameof(FolderUser.MiddleId), request.MiddleId));
- var middleInsertRequest = new MiddleInsertRequest();
- middleInsertRequest.UserIds = request.UserIds;
- middleInsertRequest.UserMiddles = request.UserMiddles;
- middleInsertRequest.FolderType = middle.FolderType;
- if (request.UserMiddles.Count > 0)
- {
- //删除原有的数据
- await _userMiddleService.DeleteAsync(middle.Id, middle.FolderType);
- //await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
- //查询用户
- // var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, middle.UserId);
- // middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
- }
- await InsertFolderUsers(middleInsertRequest, request.MiddleId);
- }
- middle.RoleId = request.RoleId;
- middle.FolderName = request.FolderName;
- await _middleRepository.UpdateAsync(middle);
- //修改子集权限
- if (middle.ParentId == 0)
- await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.RoleId), request.RoleId).Where(nameof(Entity.Middle.Middle.MiddleSonId), middle.Id));
- return true;
- }
- /// <summary>
- /// 修改共享文件夹下的所有文件夹属性
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateChildrenMiddleFolder(MiddleUpdateFolderRoleRequest request)
- {
- //获取下级数据
- var middles = await _middleRepository.GetMiddleChildrenAsync(request.MiddleId);
- if (middles.Count() == 0)
- return true;
- foreach (var item in middles)
- {
- if (request.UserMiddles.Count > 0)
- {
- var middleInsertRequest = new MiddleInsertRequest();
- middleInsertRequest.UserIds = request.UserIds;
- middleInsertRequest.UserMiddles = request.UserMiddles;
- //删除原有的数据
- await _userMiddleService.DeleteAsync(item.Id, item.FolderType);
- await _userMiddleService.AddUserMiddleAsync(request.UserMiddles);
- //查询用户
- var users = await _userMiddleService.FindUsersAsync(request.UserMiddles, item.UserId);
- middleInsertRequest.UserIds = users.Select(n => n.Id).ToList();
- //插入共享成员
- await InsertFolderUsers(middleInsertRequest, request.MiddleId);
- item.RoleId = request.RoleId;
- item.FolderName = request.FolderName;
- //修改
- await _middleRepository.UpdateAsync(item);
- request.MiddleId = item.Id;
- //修改下级权限和共享范围人
- await UpdateChildrenMiddleFolder(request);
- }
- }
- return true;
- }
- /// <summary>
- /// 根据文件夹ID获取文件夹信息
- /// </summary>
- /// <param name="middleId"></param>
- /// <returns></returns>
- public async Task<MiddleFolderInfoResult> GetFolderInfo(int middleId)
- {
- if (middleId == 0)
- throw new Common.Exceptions.BusinessException("文件夹不存在");
- var result = new MiddleFolderInfoResult();
- var middle = await _middleRepository.GetMiddleAsync(middleId);
- result.Id = middle.Id;
- result.FolderName = middle.FolderName;
- result.FolderType = middle.FolderType;
- result.UserId = middle.UserId;
- result.RoleId = middle.RoleId;
- result.FolderUsers = await _folderUserRepository.GetAllAsync(middleId);
- //获取共享人员
- if (result.RoleId == 3)
- {
- var sourceId = middle.Id;
- if (middle.ParentId > 0)
- sourceId = middle.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, sourceId);
- result.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), middle.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- // result.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
- }
- result.UserMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middleId);
- return result;
- }
- /// <summary>
- /// 获取默认的共享文件夹范围
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<MiddleDefaultResult> GetEnjoyDefaultAsync(int userId, int folderType)
- {
- var recordFolder = await recordFolderRepository.GetAsync(folderType, userId);
- var result = new MiddleDefaultResult();
- result.IsDefault = recordFolder != null;
- result.FolderName = recordFolder == null ? "" : recordFolder.FolderName;
- result.FolderType = recordFolder == null ? 0 : recordFolder.FolderType;
- result.MiddleId = recordFolder == null ? 0 : recordFolder.FolderId;
- return result;
- }
- /// <summary>
- /// 根据类型搜索文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<Entity.Middle.Middle>> GetSearchFolderResult(MiddleSearchFolderRequest request)
- {
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId);
- query.Where(query.WhereLike(nameof(Entity.Middle.Middle.FolderName), $"%{request.KeyWord}%"));
- query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
- query.OrderByDesc(nameof(Entity.Middle.Middle.IsTop), nameof(Entity.Middle.Middle.LastModifiedDate));
- return await _middleRepository.GetAllAsync(query);
- }
- /// <summary>
- /// 获取话题 笔记共享范围文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
- {
- string sqlStr = string.Empty;
- if (!string.IsNullOrWhiteSpace(request.KeyWord))
- sqlStr += $" and FolderName like '%{request.KeyWord}%'";
- string sql = $@"SELECT
- *
- FROM
- tede_middle
- WHERE
- FolderType ={request.TypeId} AND AttributeValue = 2 {sqlStr}
- AND UserId = {request.UserId}
- AND RoleId IN ({MiddleRoleConst.Publicity.GetHashCode()} , {MiddleRoleConst.Share.GetHashCode()})
- AND IsDelete = 0
- AND ParentId ={request.ParentId}
- ORDER BY IsSystemDefault DESC,AttributeValue desc , IsTop DESC , {nameof(Entity.Middle.Middle.LastModifiedDate)} DESC,Sort DESC";
- var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
- var connection = database.GetConnection();
- var middles = await connection.QueryAsync<Entity.Middle.Middle>(sql);
- List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
- if (request.TypeId == AllTypeConst.TopicNote.GetHashCode() && request.ParentId == 0 && string.IsNullOrWhiteSpace(request.KeyWord))
- {
- result.Add(new MiddleSearchFolderResult
- {
- Name = "草稿箱",
- Id = 0,
- IsDisable = false,
- RoleId = 1,
- IsFolder = false,
- IsTop = false,
- FileCount = await _noteRepository.GetDraftCountAsync(request.UserId, true),
- IsSystemDefault = true,
- IsDraft = true
- });
- }
- foreach (var item in middles)
- {
- MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
- {
- Name = item.FolderName,
- Id = item.Id,
- IsDisable = request.FolderIds.Any(n => n.Equals(item.Id)),
- RoleId = item.RoleId,
- IsFolder = await _middleRepository.IsFolderAsync(item.Id),
- IsTop = item.IsTop,
- FileCount = await _middleRepository.CountAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id).Where(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.FolderType), request.TypeId)),
- IsSystemDefault = item.IsSystemDefault
- };
- if (item.RoleId == MiddleRoleConst.Share.GetHashCode())
- {
- var sourceId = item.Id;
- if (item.ParentId != 0)
- sourceId = item.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
- }
- result.Add(middleSearchFolderResult);
- }
- return result;
- }
- /// <summary>
- /// 获取话题最近使用的文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<MiddleSearchFolderResult>> GetLatelyTopicFolderAsync(int userId)
- {
- //获取用户最近发布的话题
- var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, true);
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
- .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.TopicNote.GetHashCode())
- .Where(nameof(Entity.Middle.Middle.UserId), userId)
- .Where(nameof(Entity.Middle.Middle.IsDelete), false)
- .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 1, 3 });
- var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
- var middles = new List<Entity.Middle.Middle>();
- foreach (var folderId in folderIds.Distinct())
- {
- foreach (var item in models)
- {
- if (item.Id == folderId)
- {
- middles.Add(item);
- break;
- }
- }
- }
- List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
- foreach (var item in middles)
- {
- MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
- {
- Name = item.FolderName,
- Id = item.Id,
- IsDisable = false,
- RoleId = item.RoleId,
- IsFolder = await _middleRepository.IsFolderAsync(item.Id),
- IsTop = item.IsTop
- };
- if (item.RoleId == 3)
- {
- var sourceId = item.Id;
- if (item.ParentId != 0)
- sourceId = item.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
- }
- result.Add(middleSearchFolderResult);
- }
- return result;
- }
- /// <summary>
- /// 获取笔记最近使用的文件夹
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<MiddleSearchFolderResult>> GetLatelyNoteFolderAsync(int userId)
- {
- //获取用户最近发布的话题
- var folderIds = await _noteRepository.GetLatelyFolderIdAsync(userId, false);
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2)
- .Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Note.GetHashCode())
- .Where(nameof(Entity.Middle.Middle.UserId), userId)
- .Where(nameof(Entity.Middle.Middle.IsDelete), false)
- .WhereIn(nameof(Entity.Middle.Middle.Id), folderIds);
- query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List<int>() { 2 });
- var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
- var middles = new List<Entity.Middle.Middle>();
- foreach (var folderId in folderIds.Distinct())
- {
- foreach (var item in models)
- {
- if (item.Id == folderId)
- {
- middles.Add(item);
- break;
- }
- }
- }
- List<MiddleSearchFolderResult> result = new List<MiddleSearchFolderResult>();
- foreach (var item in middles)
- {
- MiddleSearchFolderResult middleSearchFolderResult = new MiddleSearchFolderResult
- {
- Name = item.FolderName,
- Id = item.Id,
- IsDisable = false,
- RoleId = item.RoleId,
- IsFolder = await _middleRepository.IsFolderAsync(item.Id),
- IsTop = item.IsTop
- };
- if (item.RoleId == 3)
- {
- var sourceId = item.Id;
- if (item.ParentId != 0)
- sourceId = item.MiddleSonId;
- var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
- // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
- middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
- //middleSearchFolderResult.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
- }
- result.Add(middleSearchFolderResult);
- }
- return result;
- }
- /// <summary> =-
- /// 删除全部
- /// </summary>
- /// <param name="folderType"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAllAsync(int folderType, int userId)
- {
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode());
- query.Where(nameof(Entity.Middle.Middle.UserId), userId);
- query.Where(nameof(Entity.Middle.Middle.IsDelete), true);
- return await _middleRepository.DeleteAsync(query);
- }
- }
- }
|