using System.Threading.Tasks;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Entity;
using GxPress.Request.App.GroupUser;
using System.Transactions;
using GxPress.EnumConst;
namespace GxPress.Service.Implement.Group
{
public partial class GroupService
{
///
/// 移除小组成员
///
///
///
public async Task DeleteAsync(GroupUserDeRequest request)
{
var group = await _groupRepository.GetAsync(request.GroupId);
if (group == null)
throw new BusinessException("小组不存在!");
try
{
using (var transactionScope = new TransactionScope())
{
await _groupUserRepository.DeleteAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId)
.WhereIn(nameof(GroupUser.UserId), request.GroupUserIds));
//删除小组会话界面
await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Group.GetHashCode())
.Where(nameof(Entity.Middle.Middle.MiddleId), request.GroupId)
.WhereIn(nameof(Entity.Middle.Middle.UserId), request.GroupUserIds));
transactionScope.Complete();
}
}
catch
{
return false;
}
return true;
}
///
/// 小组创建者退出小组
///
///
///
///
public async Task IsDeleteGroupAsync(int userId, int groupId)
{
var query = Q.NewQuery();
query.Where(nameof(Entity.GroupUser.UserId), userId);
query.Where(nameof(Entity.GroupUser.GroupId), groupId);
var groupUser = await _groupUserRepository.GetAsync(query);
var groupUserCount = await _groupUserRepository.CountAsync(groupId);
if (groupUser == null)
throw new BusinessException("用户不存在");
//删除小组和小组用户
if (groupUserCount == 1)
{
await _groupRepository.DeleteAsync(groupId);
await _groupUserRepository.DeleteAsync(Q.Where(nameof(GroupUser.GroupId), groupId));
query = Q.NewQuery();
query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Group.GetHashCode());
query.Where(nameof(Entity.Middle.Middle.MiddleId), groupId);
await _middleRepository.DeleteAsync(query);
return true;
}
else
{
if (groupUser.GroupUserRoleId == GroupUserRoleTyeConst.SuperAdmin)
throw new BusinessException("你的身份是小组创建者,请先转让小组给他人,然后才能退出小组!");
query = Q.NewQuery();
query.Where(nameof(Entity.GroupUser.UserId), userId);
query.Where(nameof(Entity.GroupUser.GroupId), groupId);
await _groupUserRepository.DeleteAsync(query);
query = Q.NewQuery();
query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Group.GetHashCode());
query.Where(nameof(Entity.Middle.Middle.MiddleId), groupId);
query.Where(nameof(Entity.Middle.Middle.UserId), userId);
await _middleRepository.DeleteAsync(query);
return true;
}
// if (groupUser.GroupUserRoleId == GroupUserRoleTyeConst.SuperAdmin)
// throw new BusinessException("你的身份是小组创建者,请先转让小组给他人,然后才能退出小组!");
//return true;
}
///
/// 退出小组
///
///
///
///
public async Task DeleteAsync(int userId, int groupId)
{
var group = await _groupRepository.GetAsync(groupId);
if (group == null)
throw new BusinessException("小组不存在!");
return await IsDeleteGroupAsync(userId, groupId);
}
}
}