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;
}
///
/// 添加文件夹
///
///
///
public async Task InsertAsync(MiddleInsertRequest request)
{
var folderRoleId = new List { 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 { 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();
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;
}
///
/// 添加文件夹用户
///
///
///
///
public async Task 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();
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;
}
///
/// 修改文件夹名称
///
///
///
public async Task 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));
}
///
/// 添加数据
///
///
///
public async Task 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;
}
///
/// 添加数据
///
///
///
public async Task 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));
}
///
/// 排序
///
///
///
public async Task MiddleSortAsync(MiddleSortRequest request)
{
return await _middleRepository.SetSortAsync(request);
}
///
/// 设置置顶
///
///
///
public async Task SetTopAsync(int id)
{
return await _middleRepository.SetTopAsync(id);
}
///
/// 删除 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7 小组
///
///
///
public async Task 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 RecoverDeleteAsync(NoticeDeRequest request)
{
return await _middleRepository.RecoverDeleteAsync(request);
}
///
/// 移动到
///
///
///
public async Task 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)
{
///
/// 获取共享人员
///
///
var userMiddles = await _userMiddleService.FindUserMiddlesAsync(middle.FolderType, middle.MiddleSonId);
foreach (var id in request.Ids)
{
var userMiddleInRequest = new List();
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 { item }, new List { });
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;
}
///
/// 恢复全部
///
///
///
public async Task 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);
}
///
/// 修改文件夹名称
///
///
///
public async Task 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);
}
///
/// 查询文件夹
///
///
///
public async Task> 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 { 1, 3 });
if (request.TypeId == AllTypeConst.Note.GetHashCode())
query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List { 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 result = new List();
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;
}
///
/// 查询文件夹包含父级
///
///
///
public async Task 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 { 1, 3 });
if (request.TypeId == AllTypeConst.Note.GetHashCode())
query.WhereIn(nameof(Entity.Middle.Middle.RoleId), new List { 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();
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;
}
///
/// 修改权限
///
///
///
public async Task 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;
}
///
/// 修改共享文件夹下的所有文件夹属性
///
///
///
public async Task 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;
}
///
/// 根据文件夹ID获取文件夹信息
///
///
///
public async Task 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;
}
///
/// 获取默认的共享文件夹范围
///
///
///
public async Task 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;
}
///
/// 根据类型搜索文件夹
///
///
///
public async Task> 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);
}
///
/// 获取话题 笔记共享范围文件夹
///
///
///
public async Task> 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(sql);
List result = new List();
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;
}
///
/// 获取话题最近使用的文件夹
///
///
///
public async Task> 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() { 1, 3 });
var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
var middles = new List();
foreach (var folderId in folderIds.Distinct())
{
foreach (var item in models)
{
if (item.Id == folderId)
{
middles.Add(item);
break;
}
}
}
List result = new List();
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;
}
///
/// 获取笔记最近使用的文件夹
///
///
///
public async Task> 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() { 2 });
var models = await _middleRepository.FindAsync(query.OrderByDesc(nameof(Entity.Middle.Middle.CreatedDate)));
var middles = new List();
foreach (var folderId in folderIds.Distinct())
{
foreach (var item in models)
{
if (item.Id == folderId)
{
middles.Add(item);
break;
}
}
}
List result = new List();
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;
}
/// =-
/// 删除全部
///
///
///
///
public async Task 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);
}
}
}