using System.Threading.Tasks;
using GxPress.Repository.Interface;
using GxPress.Request.AddressBookGroup;
using GxPress.Service.Interface.AddressBookGroup;
using System.Transactions;
using GxPress.Request.AddressBookGroupUser;
using Datory;
using System.Linq;
using GxPress.Repository.Interface.Friends;
namespace GxPress.Service.Implement.AddressBookGroup
{
public class AddressBookGroupService : IAddressBookGroupService
{
private readonly IAddressBookGroupUserRepository addressBookGroupUserRepository;
private readonly IAddressBookGroupRepository addressBookGroupRepository;
private readonly IFriendsRepository friendsRepository;
public AddressBookGroupService(IAddressBookGroupUserRepository addressBookGroupUserRepository, IAddressBookGroupRepository addressBookGroupRepository, IFriendsRepository friendsRepository)
{
this.addressBookGroupUserRepository = addressBookGroupUserRepository;
this.addressBookGroupRepository = addressBookGroupRepository;
this.friendsRepository = friendsRepository;
}
///
/// 添加
///
///
///
public async Task InsertAsync(AddressBookGroupInRequest request)
{
try
{
using (var transactionScope = new TransactionScope())
{
///
/// 添加自建
///
///
var addressBookGroup = await addressBookGroupRepository.InsertAsync(request);
if (request.UserIds != null)
{
//添加
await addressBookGroupUserRepository.InsertAsync(new AddressBookGroupUserInRequest
{
AddressBookGroupId = addressBookGroup.Id,
UserId = request.UserIds
});
}
transactionScope.Complete();
return addressBookGroup;
}
}
catch
{
return new Entity.AddressBookGroup();
}
}
///
/// 删除自建小组用户
///
///
///
public async Task DeleteAddressBookGroupUserAsync(AddressBookGroupUserDeleteRequest request)
{
try
{
var model = await addressBookGroupUserRepository.GetAllAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.Id), request.Ids));
if (model == null || model.Count() == 0)
return false;
using (var transactionScope = new TransactionScope())
{
await addressBookGroupUserRepository.DeletesAsync(request.Ids);
await friendsRepository.DeleteAsync(model.Select(n => n.UserId), request.UserId);
transactionScope.Complete();
}
}
catch
{
return false;
}
return true;
}
}
}