123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface.DepartmentUser;
- using GxPress.Repository.Interface.Friends;
- using GxPress.Result.Department;
- 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;
- private readonly IDepartmentUserRepository departmentUserRepository;
- private readonly IFriendsRepository friendsRepository;
- public UserMiddleService(IUserMiddleRepository userMiddleRepository, IUserRepository userRepository, IAddressBookGroupUserRepository addressBookGroupUserRepository, IDepartmentRepository departmentRepository, IAddressBookGroupRepository addressBookGroupRepository, IGroupChatUserRepository groupChatUserRepository, IGroupUserRepository groupUserRepository, IDepartmentUserRepository departmentUserRepository, IFriendsRepository friendsRepository)
- {
- _userMiddleRepoitory = userMiddleRepository;
- _userRepoitory = userRepository;
- _addressBookGroupUserRepository = addressBookGroupUserRepository;
- _departmentRepository = departmentRepository;
- _addressBookGroupRepository = addressBookGroupRepository;
- this.groupChatUserRepository = groupChatUserRepository;
- this.groupUserRepository = groupUserRepository;
- this.departmentUserRepository = departmentUserRepository;
- this.friendsRepository = friendsRepository;
- }
- /// <summary>
- /// 添加用户中间表
- /// </summary>
- /// <param name="userMiddles"></param>
- /// <returns></returns>
- public async Task<bool> AddUserMiddleAsync(List<UserMiddleInRequest> 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;
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(SqlKata.Query query)
- {
- return await _userMiddleRepoitory.DeleteAsync(query);
- }
- /// <summary>
- /// 删除收件人
- /// </summary>
- /// <param name="dataSourceId"></param>
- /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要</param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(int dataSourceId, int middleType)
- {
- return await _userMiddleRepoitory.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId).Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType));
- }
- /// <summary>
- /// 是否存在
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public async Task<bool> ExistsAsync(SqlKata.Query query)
- {
- return await _userMiddleRepoitory.ExistsAsync(query);
- }
- /// <summary>
- /// 获取用户列表
- /// </summary>
- /// <param name="middleType">来源类型 1:通知收件人 2:通知抄送人 3:站内信收集人 4:站内信抄送人 5:话题 6:笔记共享文件夹 7:收藏共享文件夹 </param>
- /// <param name="sourceId"></param>
- /// <returns></returns>
- public async Task<List<Entity.User>> FindUsersAsync(List<Request.UserMiddle.UserMiddleInRequest> userMiddles, int userId)
- {
- List<Entity.User> users = new List<Entity.User>();
- var userIds = new List<int>();
- userIds = userMiddles.Where(n => n.SourceType == 0).Select(n => n.SourceId).ToList();
- foreach (var userMiddle in userMiddles)
- {
- if (userMiddle.SourceType != UserlinkConst.User.GetHashCode())
- {
- if (userMiddle.SourceType == UserlinkConst.Work.GetHashCode())
- {
- if (userMiddle.SourceId == 0)
- //获取所有用户
- userIds.AddRange(await departmentUserRepository.GetUserIdsByUserIdAsync(userId));
- else
- {
- //递归获取部门
- var departments = new List<DepartmentResult>();
- departments = await _departmentRepository.GetDepartmentById(userMiddle.SourceId, departments);
- //获取部门用户
- userIds.AddRange(await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id)));
- }
- }
- //系统
- if (userMiddle.SourceType == UserlinkConst.System.GetHashCode())
- {
- if (userMiddle.SourceTypeValue == 0 && userMiddle.SourceId == 0)
- {
- userIds.AddRange(await groupUserRepository.GetGroupUserByUserIdAsync(userId));
- }
- /// <summary>
- /// 我的小组
- /// </summary>
- /// <value></value>
- else if (userMiddle.SourceTypeValue == UserlinkConst.MyGroup.GetHashCode())
- {
- if (userMiddle.SourceId == 0)
- {
- userIds.AddRange(await groupUserRepository.GetGroupUserByUserIdAsync(userId));
- userIds.AddRange(await groupChatUserRepository.GetUserIdsAsync(userId));
- }
- else
- {
- var groupUsers = await groupUserRepository.GetAllAsync(Q.Where(nameof(Entity.GroupUser.GroupId), userMiddle.SourceId));
- userIds.AddRange(groupUsers.Select(n => n.UserId));
- }
- }
- /// <summary>
- /// 我的群聊
- /// </summary>
- /// <value></value>
- else if (userMiddle.SourceTypeValue == UserlinkConst.MyGroupChat.GetHashCode())
- {
- 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 == UserlinkConst.My.GetHashCode())
- {
- 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 (userMiddle.SourceType == UserlinkConst.Other.GetHashCode())
- {
- if (userMiddle.SourceId == 0)
- userIds.AddRange(await friendsRepository.GetFriendsIdsAsync(userId, 0));
- }
- }
- }
- 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;
- }
- /// <summary>
- /// 查询列表
- /// </summary>
- /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要 </param>
- /// <param name="sourceId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<Entity.UserMiddle.UserMiddle>> 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;
- }
- }
- }
|