using System.Collections.Generic; using GxPress.Repository.Interface.UserMiddle; using GxPress.Request.UserMiddle; using GxPress.Service.Interface.UserMiddle; using GxPress.Common.Exceptions; using System.Threading.Tasks; using GxPress.Repository.Interface; using Datory; using System.Linq; namespace GxPress.Service.Implement.UserMiddle { public class UserMiddleService : IUserMiddleService { private readonly IUserMiddleRepository _userMiddleRepoitory; private readonly IUserRepository _userRepoitory; private readonly IAddressBookGroupUserRepository _addressBookGroupUserRepository; private readonly IAddressBookGroupRepository _addressBookGroupRepository; private readonly IDepartmentRepository _departmentRepository; private readonly IGroupChatUserRepository groupChatUserRepository; private readonly IGroupUserRepository groupUserRepository; public UserMiddleService(IUserMiddleRepository userMiddleRepository, IUserRepository userRepository, IAddressBookGroupUserRepository addressBookGroupUserRepository, IDepartmentRepository departmentRepository, IAddressBookGroupRepository addressBookGroupRepository, IGroupChatUserRepository groupChatUserRepository, IGroupUserRepository groupUserRepository) { _userMiddleRepoitory = userMiddleRepository; _userRepoitory = userRepository; _addressBookGroupUserRepository = addressBookGroupUserRepository; _departmentRepository = departmentRepository; _addressBookGroupRepository = addressBookGroupRepository; this.groupChatUserRepository = groupChatUserRepository; this.groupUserRepository = groupUserRepository; } /// /// 添加用户中间表 /// /// /// public async Task AddUserMiddleAsync(List request) { try { foreach (var userMiddle in request) { //添加用户中间件 var userMiddleDto = new Entity.UserMiddle.UserMiddle(); userMiddleDto.SourceId = userMiddle.SourceId; userMiddleDto.SourceType = userMiddle.SourceType; userMiddleDto.SourceTypeValue = userMiddle.SourceTypeValue; userMiddleDto.MiddleType = userMiddle.MiddleType; userMiddleDto.SourceName = userMiddle.SourceName; userMiddleDto.DataSourceId = userMiddle.DataSourceId; await _userMiddleRepoitory.InsertAsync(userMiddleDto); } } catch (System.Exception ex) { throw new BusinessException(ex.Message); } return true; } /// /// 删除 /// /// /// public async Task DeleteAsync(SqlKata.Query query) { return await _userMiddleRepoitory.DeleteAsync(query); } /// /// 删除收件人 /// /// /// 类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要 /// public async Task DeleteAsync(int dataSourceId, int middleType) { return await _userMiddleRepoitory.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId).Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType)); } /// /// 是否存在 /// /// /// public async Task ExistsAsync(SqlKata.Query query) { return await _userMiddleRepoitory.ExistsAsync(query); } /// /// 获取用户列表 /// /// 来源类型 1:通知收件人 2:通知抄送人 3:站内信收集人 4:站内信抄送人 5:话题 6:笔记共享文件夹 7:收藏共享文件夹 /// /// public async Task> FindUsersAsync(List userMiddles, int userId) { List users = new List(); var userIds = new List(); userIds = userMiddles.Where(n => n.SourceType == 0).Select(n => n.SourceId).ToList(); foreach (var userMiddle in userMiddles) { if (userMiddle.SourceType != 0) { if (userMiddle.SourceType == 1) { if (userMiddle.SourceId == 0) //获取所有用户 userIds.AddRange(await _userRepoitory.GetAllIntsAsync(Q.Select(nameof(Entity.User.Id)).Where(nameof(Entity.User.Id), ">", 0))); else { //递归获取部门 var departments = new List(); departments = await _departmentRepository.GetDepartmentById(userMiddle.SourceId, departments); userIds.AddRange(await _userRepoitory.GetAllIntsAsync(Q.WhereIn(nameof(Entity.User.DepartmentId), departments.Select(n => n.Id)))); } } if (userMiddle.SourceType == 2) { /// /// 我的小组 /// /// if (userMiddle.SourceTypeValue == 1) { if (userMiddle.SourceId == 0) { userIds.AddRange(await groupUserRepository.GetGroupUserByUserIdAsync(userId)); } else { var groupUsers = await groupUserRepository.GetAllAsync(Q.Where(nameof(Entity.GroupUser.GroupId), userMiddle.SourceId)); userIds.AddRange(groupUsers.Select(n => n.UserId)); } } /// /// 我的群聊 /// /// if (userMiddle.SourceTypeValue == 2) { if (userMiddle.SourceId == 0) { userIds.AddRange(await groupChatUserRepository.GetUserIdsAsync(userId)); } else { var groupChatUsers = await groupChatUserRepository.GetAllAsync(Q.Where(nameof(Entity.GroupChatUser.GroupChatId), userMiddle.SourceId)); userIds.AddRange(groupChatUsers.Select(n => n.UserId)); } } } if (userMiddle.SourceType == 3) { if (userMiddle.SourceId == 0) { var addressBookGroups = await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroup.UserId), userId)); var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), addressBookGroups.Select(n => n.Id))); userIds.AddRange(addressBookGroupUsers.Select(n => n.UserId).ToList()); } else { //获取自建分组 var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), userMiddle.SourceId)); userIds.AddRange(addressBookGroupUsers.Select(n => n.UserId).ToList()); } } } } if (userIds.Count() > 0) //获取用户 users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), userIds))); users = users.Distinct().ToList(); foreach (var item in users) { item.AvatarUrl = Common.Tools.StringUtils.AddDomainMin(item.AvatarUrl); } return users; } /// /// 查询列表 /// /// 类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要 /// /// public async Task> FindUserMiddlesAsync(int middleType, int dataSourceId) { var userMiddles = await _userMiddleRepoitory.GetAllAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType).Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId)); return userMiddles; } } }