using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Request.App.IM;

namespace GxPress.Service.Implement.GroupChatUser
{
    public partial class GroupChatUserService
    {
        /// <summary>
        /// 添加群用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(InsertGroupChatUsersRequest request)
        {
            return await AddUsers(request);
        }

        /// <summary>
        /// 添加群用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> AddUsers(InsertGroupChatUsersRequest request)
        {
            var groupChat = await _groupChatRepository.GetGroupChatByImIdAsync(request.GroupChatImId);
            var users = await _groupChatUserRepository.FindGroupChatUserByImId(groupChat.GroupChatImId);
            var userInts = new List<int>();
            if (request.UserIds.Count == 0)
                return true;
            foreach (var item in request.UserIds.Distinct())
            {
                if (!users.Any(n => n.UserId == item))
                {
                    userInts.Add(item);
                }
            }
            request.UserIds = userInts;
            return await _imService.InsertGroupChatUsersAsync(request);
        }
    }
}
