AddressBookGroupService.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Threading.Tasks;
  2. using GxPress.Repository.Interface;
  3. using GxPress.Request.AddressBookGroup;
  4. using GxPress.Service.Interface.AddressBookGroup;
  5. using System.Transactions;
  6. using GxPress.Request.AddressBookGroupUser;
  7. using Datory;
  8. using System.Linq;
  9. using GxPress.Repository.Interface.Friends;
  10. namespace GxPress.Service.Implement.AddressBookGroup
  11. {
  12. public class AddressBookGroupService : IAddressBookGroupService
  13. {
  14. private readonly IAddressBookGroupUserRepository addressBookGroupUserRepository;
  15. private readonly IAddressBookGroupRepository addressBookGroupRepository;
  16. private readonly IFriendsRepository friendsRepository;
  17. public AddressBookGroupService(IAddressBookGroupUserRepository addressBookGroupUserRepository, IAddressBookGroupRepository addressBookGroupRepository, IFriendsRepository friendsRepository)
  18. {
  19. this.addressBookGroupUserRepository = addressBookGroupUserRepository;
  20. this.addressBookGroupRepository = addressBookGroupRepository;
  21. this.friendsRepository = friendsRepository;
  22. }
  23. /// <summary>
  24. /// 添加
  25. /// </summary>
  26. /// <param name="request"></param>
  27. /// <returns></returns>
  28. public async Task<Entity.AddressBookGroup> InsertAsync(AddressBookGroupInRequest request)
  29. {
  30. try
  31. {
  32. using (var transactionScope = new TransactionScope())
  33. {
  34. /// <summary>
  35. /// 添加自建
  36. /// </summary>
  37. /// <returns></returns>
  38. var addressBookGroup = await addressBookGroupRepository.InsertAsync(request);
  39. if (request.UserIds != null)
  40. {
  41. //添加
  42. await addressBookGroupUserRepository.InsertAsync(new AddressBookGroupUserInRequest
  43. {
  44. AddressBookGroupId = addressBookGroup.Id,
  45. UserId = request.UserIds
  46. });
  47. }
  48. transactionScope.Complete();
  49. return addressBookGroup;
  50. }
  51. }
  52. catch
  53. {
  54. return new Entity.AddressBookGroup();
  55. }
  56. }
  57. /// <summary>
  58. /// 删除自建小组用户
  59. /// </summary>
  60. /// <param name="request"></param>
  61. /// <returns></returns>
  62. public async Task<bool> DeleteAddressBookGroupUserAsync(AddressBookGroupUserDeleteRequest request)
  63. {
  64. try
  65. {
  66. var model = await addressBookGroupUserRepository.GetAllAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.Id), request.Ids));
  67. if (model == null || model.Count() == 0)
  68. return false;
  69. using (var transactionScope = new TransactionScope())
  70. {
  71. await addressBookGroupUserRepository.DeletesAsync(request.Ids);
  72. await friendsRepository.DeleteAsync(model.Select(n => n.UserId), request.UserId);
  73. transactionScope.Complete();
  74. }
  75. }
  76. catch
  77. {
  78. return false;
  79. }
  80. return true;
  81. }
  82. }
  83. }