TeamService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. private readonly GlobalInfo _globalInfo;
  25. public TeamService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo) : base(unitOfWork)
  26. {
  27. _unitOfWork = unitOfWork;
  28. _userService = userService;
  29. _globalInfo = globalInfo;
  30. }
  31. public override async Task<PagingResultDto<TeamAllDto>> GetAll(PagingInputDto PagingInputDto)
  32. {
  33. var res = await _unitOfWork.Team.GetAllWithChildrenAsync();
  34. var query = res.Item1;
  35. if (_globalInfo.UserType != UserTypeEnum.Business)
  36. {
  37. query = query.Where(m => m.TeamUsers != null && m.TeamUsers.Count > 0 && m.TeamUsers.Count(u => u.AssignedUserId == _globalInfo.UserId) > 0);
  38. }
  39. if (PagingInputDto.Filter != null)
  40. {
  41. var filter = PagingInputDto.Filter;
  42. query = query.Where(u => u.NameAr.Contains(filter) || u.NameEn.Contains(filter));
  43. }
  44. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  45. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  46. var total = await query.CountAsync();
  47. var list = MapperObject.Mapper
  48. .Map<IList<TeamAllDto>>(await page.ToListAsync());
  49. var response = new PagingResultDto<TeamAllDto>
  50. {
  51. Result = list,
  52. Total = total
  53. };
  54. return response;
  55. }
  56. public override async Task<TeamDto> GetById(long id)
  57. {
  58. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
  59. var response = MapperObject.Mapper.Map<TeamDto>(entity);
  60. if(response != null)
  61. response.TeamUserIds = response.TeamUsers != null ? response.TeamUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
  62. return response;
  63. }
  64. public override async Task<TeamDto> Create(TeamDto input)
  65. {
  66. var entity = MapperObject.Mapper.Map<Team>(input);
  67. if (entity is null)
  68. {
  69. throw new AppException(ExceptionEnum.MapperIssue);
  70. }
  71. entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s,AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
  72. var team = await _unitOfWork.Team.AddAsync(entity);
  73. await _unitOfWork.CompleteAsync();
  74. var response = MapperObject.Mapper.Map<TeamDto>(team);
  75. response.TeamUserIds = input.TeamUserIds;
  76. return response;
  77. }
  78. //private async Task<string> GetUserName(string userId)
  79. //{
  80. // var entity = await _userService.GetUserById(userId);
  81. // var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
  82. // return name;
  83. //}
  84. public override async Task<TeamDto> Update(TeamDto input)
  85. {
  86. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(input.Id);
  87. if (entity is null)
  88. throw new AppException(ExceptionEnum.RecordNotExist);
  89. entity.NameAr = input.NameAr;
  90. entity.NameEn = input.NameEn;
  91. var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
  92. var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
  93. var newTeamUsers = NewItems?.Select(s => new TeamUser {TeamId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
  94. var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
  95. _unitOfWork.BeginTran();
  96. foreach (var delUser in DeletedItems)
  97. {
  98. var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
  99. if(removeItem != null) await _unitOfWork.TeamUser.DeleteAsync(removeItem);
  100. }
  101. foreach (var newUsr in newTeamUsers)
  102. {
  103. await _unitOfWork.TeamUser.AddAsync(newUsr);
  104. }
  105. await _unitOfWork.CompleteAsync();
  106. _unitOfWork.CommitTran();
  107. var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
  108. return response;
  109. }
  110. public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
  111. {
  112. var result = false;
  113. var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
  114. if (team?.TeamUsers != null)
  115. {
  116. var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
  117. if (foundUser != null)
  118. {
  119. foundUser.IsAdmin = teamUser.IsAdmin;
  120. }
  121. else
  122. {
  123. team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
  124. }
  125. }
  126. else if (team != null)
  127. {
  128. team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
  129. }
  130. await _unitOfWork.CompleteAsync();
  131. return result;
  132. }
  133. public async Task<string> GetTeamName(long teamId)
  134. {
  135. var entity = await _unitOfWork.Team.GetByIdAsync(teamId);
  136. var name = entity == null ? "" : GlobalInfo.lang.ToLower() == "ar"? entity.NameAr: entity.NameEn;
  137. return name;
  138. }
  139. }
  140. }