using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Entity;
using GxPress.Request.App.GroupUser;

namespace GxPress.Service.Implement.Group
{
    public partial class GroupService
    {
        /// <summary>
        /// 移除小组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(GroupUserDeRequest request)
        {
            var group = await _groupRepository.GetAsync(request.GroupId);
            // if (request.GroupUserIds.Contains(group.UserId))
            //     throw new BusinessException("创建人不能删除");
            await _groupUserRepository.DeleteAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId)
                .WhereIn(nameof(GroupUser.UserId), request.GroupUserIds));
            //删除小组会话界面
            return await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), 7)
                .Where(nameof(Entity.Middle.Middle.MiddleId), request.GroupId)
                .WhereIn(nameof(Entity.Middle.Middle.UserId), request.GroupUserIds));
        }

        /// <summary>
        /// 退出小组
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int userId, int groupId)
        {
            var group = await _groupRepository.GetAsync(groupId);
            if (group == null)
                throw new BusinessException("小组不存在！");
            // if (group.UserId == userId)
            //     throw new BusinessException("创建人不能退出");
            await _groupUserRepository.DeleteAsync(Q.Where(nameof(GroupUser.GroupId), groupId)
                .Where(nameof(GroupUser.UserId), userId));
            return await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), 7)
                .Where(nameof(Entity.Middle.Middle.MiddleId), groupId)
                .Where(nameof(Entity.Middle.Middle.UserId), userId));
        }
    }
}
