using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using GxPress.Common.Exceptions;

namespace GxPress.Service.Implement.GroupChatUser
{
   public partial class GroupChatUserService
    {
        /// <summary>
        /// 移除用户成员
        /// </summary>
        /// <param name="groupChatUserIds"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(List<int> groupChatUserIds)
        {
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    foreach (var item in groupChatUserIds)
                    {
                        //获取成员
                        var groupChartUser = await _groupChatUserRepository.FindGroupChatUserByIdAsync(item);
                        //获取群
                        var groupChart = await _groupChatRepository.FindGroupChatByIdAsync(groupChartUser.GroupChatId);
                        //获取聊天界面
                        var oftenContact =
                            await _oftenContactRepository.FindOftenContactAsync(groupChartUser.UserId,
                                groupChart.GroupChatImId);
                        //删除聊天界面
                        await _oftenContactRepository.DeleteAsync(oftenContact.Id);
                        //删除群成员
                        await _groupChatUserRepository.DeleteAsync(groupChartUser.Id);
                    }

                    transactionScope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new BusinessException(e.Message);
            }

            return true;
        }
    }
}
