using AutoMapper; using GroupService.Domain; using GroupService.Domain.IReposities; using GroupService.WebApi.Application.Dtos; using GroupService.WebApi.Application.IntegrationServices; using IM.Commons; namespace GroupService.WebApi.Application.GroupMember { public class GroupMemberService { private readonly IGroupMemberReposity reposity; private readonly IGroupReposity groupReposity; private readonly GroupMemberDomainService service; private readonly IIdentityIntegrationService idService; private IMapper mapper; private readonly IM.InitCommon.Management.RuntimePolicy runtime; public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime) { this.reposity = reposity; this.groupReposity = groupReposity; this.service = service; this.idService = idService; this.mapper = mapper; this.runtime = runtime; } public async Task>> GetByGroupIdAsync(Guid groupId, Guid userId) { var group = await groupReposity.FindByIdAsync(groupId); if (group is null) { return Result>.Fail(ResultCode.GROUP_NOT_FOUND); } if (!await reposity.CheckMemberExistAsync(groupId, userId)) { return Result>.Fail(ResultCode.PERMISSION_DENIED); } var members = await reposity.FindByGroupIdAsync(groupId); return Result>.Success(mapper.Map>(members.ToList())); } public async Task> CreateAsync(Guid groupId, Guid userId) { var group = await groupReposity.FindByIdAsync(groupId); if (group is null) { return Result.Fail(ResultCode.GROUP_NOT_FOUND); } if (group.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组已被封禁,不能加入"); var limit = runtime.Current.GroupMemberLimit; if (limit > 0 && (await reposity.FindByGroupIdAsync(groupId)).Count() >= limit) throw new IM.DomainCommons.DomainException("群成员数量已达到平台上限"); var userRes = await idService.FindUserByIdAsync(userId); if (!userRes.Succeeded) { return Result.Fail(userRes); } var memberRes = await service.CreateAsync(userId, groupId, userRes.Data.NickName, userRes.Data.Avatar); if (!memberRes.Succeeded) { return Result.Fail(userRes); } return Result.Success(mapper.Map(memberRes.Data)); } public async Task> CheckMemberAsync(Guid groupId, Guid userId) { var group = await groupReposity.FindByIdAsync(groupId); var exist = group?.Status == Domain.Enums.GroupState.Normal && await reposity.CheckMemberExistAsync(groupId, userId); return Result.Success(exist); } public async Task> DeleteAsync(Guid memberId, Guid operatorId) { var member = await reposity.FindByIdAsync(memberId); if (member is null) { return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT); } var operatorMember = await reposity.FindOneByGroupIdAndUserIdAsync(member.GroupId, operatorId); if (operatorMember is null || operatorMember.Id == member.Id || member.Role == Domain.Enums.GroupMemberRole.Master || operatorMember.Role <= member.Role) { return Result.Fail(ResultCode.PERMISSION_DENIED); } member.Leave(); return Result.Success(); } public async Task> LeaveAsync(Guid groupId, Guid userId) { var member = await reposity.FindOneByGroupIdAndUserIdAsync(groupId, userId); if (member is null) { return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT); } if (member.Role == Domain.Enums.GroupMemberRole.Master) { return Result.Fail(ResultCode.PERMISSION_DENIED, "群主不能直接退群,请使用解散群接口"); } member.Leave(); return Result.Success(); } } }