using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using AutoMapper;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.AdminVerify;
using GxPress.Request.App.AdminVerify;
using GxPress.Request.App.Group;
using GxPress.Request.App.GroupUser;
using GxPress.Request.App.Middle;
using GxPress.Result.App.Group;
using GxPress.Result.App.GroupUser;
using GxPress.Service.Interface.AdminVerify;
using GxPress.Service.Interface.Group;
using GxPress.Service.Interface.Middle;
using GxPress.Repository.Interface.Group;
using GxPress.Result;
namespace GxPress.Service.Implement.Group
{
public partial class GroupService : IGroupService
{
private readonly IGroupRepository _groupRepository;
private readonly IGroupUserRepository _groupUserRepository;
private readonly ITopicRepository _topicRepository;
private readonly IMiddleService _middleService;
private readonly IAdminVerifyInsertService _adminVerifyService;
private readonly IMiddleRepository _middleRepository;
private readonly IMapper _mapper;
private readonly IUserRepository _userRepository;
private readonly IAdminVerifyRepository adminVerifyRepository;
private readonly IGroupCategoryRepository groupCategoryRepository;
public GroupService(IGroupRepository groupRepository, IGroupUserRepository groupUserRepository,
ITopicRepository topicRepository, IMiddleService middleService,
IAdminVerifyInsertService adminVerifyService, IMiddleRepository middleRepository, IMapper mapper,
IUserRepository userRepository, IAdminVerifyRepository adminVerifyRepository, IGroupCategoryRepository groupCategoryRepository)
{
_groupRepository = groupRepository;
_groupUserRepository = groupUserRepository;
_topicRepository = topicRepository;
_middleService = middleService;
_adminVerifyService = adminVerifyService;
_middleRepository = middleRepository;
_mapper = mapper;
_userRepository = userRepository;
this.adminVerifyRepository = adminVerifyRepository;
this.groupCategoryRepository = groupCategoryRepository;
}
///
/// 添加小组
///
///
///
public async Task InsertGroup(GroupInRequest request)
{
if (string.IsNullOrEmpty(request.Name))
throw new BusinessException("小组名称不能为空");
// if (string.IsNullOrEmpty(request.Introduce))
// throw new BusinessException("小组简介不能为空");
if (string.IsNullOrEmpty(request.AvatarUrl))
throw new BusinessException("小组头像不能为空");
try
{
using (TransactionScope transactionScope = new TransactionScope())
{
var groupId = await _groupRepository.InsertAsync(request);
var groupUser = new Entity.GroupUser()
{
UserId = request.UserId,
GroupId = groupId,
GroupUserRoleId = GroupUserRoleTyeConst.SuperAdmin,
IsContacts = request.IsContacts
};
await _groupUserRepository.InsertAsync(groupUser);
//插入用户
//await AddUsersAsync(new GroupUserInRequest { GroupId = groupId, UserIds = request.UserIds });
//新建中间件
var result = new MiddleInsertTypeRequest
{
MiddleSonId = 0,
FolderId = request.GroupFolderId,
FolderType = AllTypeConst.Group.GetHashCode(),
MiddleId = groupId,
UserId = request.UserId
};
await _middleService.InsertAsync(result);
var group = await _groupRepository.GetAsync(groupId);
var groupDetail = _mapper.Map(group);
var user = await _userRepository.GetAsync(group.UserId);
groupDetail.UserName = user.Name;
groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl);
groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
transactionScope.Complete();
return groupDetail;
}
}
catch (Exception e)
{
throw new BusinessException(e.Message);
}
}
///
/// 根据用户ID文件夹ID查询小组
///
///
///
public async Task> FindGroupByGroupFolderIdAsync(GroupSearchRequest request)
{
var result = await _groupRepository.FindGroupByGroupFolderId(request);
foreach (var item in result.Items)
{
item.TypeId = UserlinkConst.System.GetHashCode();
if (item.AttributeValue == 2)
item.TypeValue = UserlinkConst.MyGroupFolder.GetHashCode();
else
item.TypeValue = UserlinkConst.MyGroup.GetHashCode();
// //共享数量
// item.TopicSharingCount = await _groupRepository.SharingCountAsync(item.GroupId);
// //总话题数量
// item.TopicAllCount = await _topicRepository.GetGroupTopicCountAsync(item.GroupId);
//未读话题数量
//item.UReadTopicCount = await _groupRepository.GetUReadCountAsync(request.UserId, item.GroupId);
//获取目录名称
// var middle =
// await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.ParentId));
if (string.IsNullOrWhiteSpace(item.CategoryName))
{
item.CategoryName = "根目录";
}
}
return result;
}
///
/// 添加成员
///
///
///
public async Task> InsertsAsync(GroupUserInRequest request)
{
var result = new AppResult