TeamService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.WebUtilities;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using MTWorkHR.Application.Identity;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories;
  10. using MTWorkHR.Core.UnitOfWork;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using MTWorkHR.Application.Services.Interfaces;
  13. using MTWorkHR.Core.Email;
  14. using MTWorkHR.Core.Entities;
  15. using MTWorkHR.Infrastructure.UnitOfWorks;
  16. using System.Linq;
  17. using System.Linq.Dynamic.Core;
  18. namespace MTWorkHR.Application.Services
  19. {
  20. public class TeamService : BaseService<Team, TeamDto, TeamAllDto>, ITeamService
  21. {
  22. private readonly IUnitOfWork _unitOfWork;
  23. private readonly IUserService _userService;
  24. public TeamService(IUnitOfWork unitOfWork, IUserService userService) : base(unitOfWork)
  25. {
  26. _unitOfWork = unitOfWork;
  27. _userService = userService;
  28. }
  29. public override async Task<PagingResultDto<TeamAllDto>> GetAll(PagingInputDto PagingInputDto)
  30. {
  31. var res = await _unitOfWork.Team.GetAllWithChildrenAsync();
  32. var query = res.Item1;
  33. if (PagingInputDto.Filter != null)
  34. {
  35. var filter = PagingInputDto.Filter;
  36. query = query.Where(u => u.NameAr.Contains(filter) );
  37. }
  38. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  39. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  40. var total = await query.CountAsync();
  41. var list = MapperObject.Mapper
  42. .Map<IList<TeamAllDto>>(await page.ToListAsync());
  43. var response = new PagingResultDto<TeamAllDto>
  44. {
  45. Result = list,
  46. Total = total
  47. };
  48. return response;
  49. }
  50. public override async Task<TeamDto> GetById(long id)
  51. {
  52. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
  53. var response = MapperObject.Mapper.Map<TeamDto>(entity);
  54. if(response != null)
  55. response.TeamUserIds = response.TeamUsers != null ? response.TeamUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
  56. return response;
  57. }
  58. public override async Task<TeamDto> Create(TeamDto input)
  59. {
  60. var entity = MapperObject.Mapper.Map<Team>(input);
  61. if (entity is null)
  62. {
  63. throw new AppException(ExceptionEnum.MapperIssue);
  64. }
  65. entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s,AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
  66. var team = await _unitOfWork.Team.AddAsync(entity);
  67. await _unitOfWork.CompleteAsync();
  68. var response = MapperObject.Mapper.Map<TeamDto>(team);
  69. response.TeamUserIds = input.TeamUserIds;
  70. return response;
  71. }
  72. //private async Task<string> GetUserName(string userId)
  73. //{
  74. // var entity = await _userService.GetUserById(userId);
  75. // var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
  76. // return name;
  77. //}
  78. public override async Task<TeamDto> Update(TeamDto input)
  79. {
  80. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(input.Id);
  81. if (entity is null)
  82. throw new AppException(ExceptionEnum.RecordNotExist);
  83. entity.NameAr = input.NameAr;
  84. entity.NameEn = input.NameEn;
  85. var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
  86. var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
  87. var newTeamUsers = NewItems?.Select(s => new TeamUser {TeamId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
  88. var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
  89. _unitOfWork.BeginTran();
  90. foreach (var delUser in DeletedItems)
  91. {
  92. var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
  93. if(removeItem != null) await _unitOfWork.TeamUser.DeleteAsync(removeItem);
  94. }
  95. foreach (var newUsr in newTeamUsers)
  96. {
  97. await _unitOfWork.TeamUser.AddAsync(newUsr);
  98. }
  99. await _unitOfWork.CompleteAsync();
  100. _unitOfWork.CommitTran();
  101. var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
  102. return response;
  103. }
  104. public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
  105. {
  106. var result = false;
  107. var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
  108. if (team?.TeamUsers != null)
  109. {
  110. var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
  111. if (foundUser != null)
  112. {
  113. foundUser.IsAdmin = teamUser.IsAdmin;
  114. }
  115. else
  116. {
  117. team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
  118. }
  119. }
  120. else if (team != null)
  121. {
  122. team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
  123. }
  124. await _unitOfWork.CompleteAsync();
  125. return result;
  126. }
  127. }
  128. }