|
@@ -1,11 +1,10 @@
|
|
|
-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;
|
|
|
+using System.Transactions;
|
|
|
+using GxPress.EnumConst;
|
|
|
|
|
|
namespace GxPress.Service.Implement.Group
|
|
|
{
|
|
@@ -19,17 +18,56 @@ namespace GxPress.Service.Implement.Group
|
|
|
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));
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 小组创建者退出小组
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userId"></param>
|
|
|
+ /// <param name="groupId"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<bool> 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);
|
|
|
+ }
|
|
|
+ if (groupUser.GroupUserRoleId == GroupUserRoleTyeConst.SuperAdmin)
|
|
|
+ throw new BusinessException("你的身份是小组创建者,请先转让小组给他人,然后才能退出小组!");
|
|
|
+ return true;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
|
|
|
/// <summary>
|
|
|
/// 退出小组
|
|
@@ -42,13 +80,7 @@ namespace GxPress.Service.Implement.Group
|
|
|
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));
|
|
|
+ return await IsDeleteGroupAsync(userId, groupId);
|
|
|
}
|
|
|
}
|
|
|
}
|