ProjectService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Dynamic.Core;
  17. namespace MTWorkHR.Application.Services
  18. {
  19. public class ProjectService : BaseService<Project, ProjectDto, ProjectDto>, IProjectService
  20. {
  21. private readonly IUnitOfWork _unitOfWork;
  22. //private readonly AppSettingsConfiguration _configuration;
  23. //private readonly GlobalInfo _globalInfo;
  24. private readonly ITeamService _teamService;
  25. public ProjectService(IUnitOfWork unitOfWork, ITeamService teamService) : base(unitOfWork)
  26. {
  27. _unitOfWork = unitOfWork;
  28. _teamService = teamService;
  29. }
  30. public override async Task<ProjectDto> GetById(long id)
  31. {
  32. var entity = await _unitOfWork.Project.GetByIdWithAllChildren(id);
  33. var response = MapperObject.Mapper.Map<ProjectDto>(entity);
  34. // if (response != null)
  35. // response.ProjectTeamIds = response.ProjectTeams != null ? response.ProjectTeams?.Select(u => u.TeamId).ToList() : new List<long>();
  36. return response;
  37. }
  38. public override async Task<ProjectDto> Create(ProjectDto input)
  39. {
  40. var entity = MapperObject.Mapper.Map<Project>(input);
  41. if (entity is null)
  42. {
  43. throw new AppException(ExceptionEnum.MapperIssue);
  44. }
  45. entity.ProjectTeams = input.ProjectTeamIds?.Select(s => new ProjectTeam { TeamId = s, TeamName = _teamService.GetTeamName(s).Result }).ToList();
  46. var Project = await _unitOfWork.Project.AddAsync(entity);
  47. await _unitOfWork.CompleteAsync();
  48. var response = MapperObject.Mapper.Map<ProjectDto>(Project);
  49. response.ProjectTeamIds = input.ProjectTeamIds;
  50. return response;
  51. }
  52. public override async Task<ProjectDto> Update(ProjectDto input)
  53. {
  54. var entity = await _unitOfWork.Project.GetByIdWithAllChildren(input.Id);
  55. if (entity is null)
  56. throw new AppException(ExceptionEnum.RecordNotExist);
  57. entity.NameAr = input.NameAr;
  58. entity.NameEn = input.NameEn;
  59. var oldUsers = entity.ProjectTeams?.Select(s => s.TeamId);
  60. var NewItems = input.ProjectTeamIds?.Where(u => !oldUsers.Contains(u));
  61. var newProjectTeams = NewItems?.Select(s => new ProjectTeam { ProjectId = input.Id, TeamId = s, TeamName = _teamService.GetTeamName(s).Result}).ToList();
  62. var DeletedItems = oldUsers?.Where(u => !input.ProjectTeamIds.Contains(u));
  63. _unitOfWork.BeginTran();
  64. foreach (var delUser in DeletedItems)
  65. {
  66. var removeItem = entity.ProjectTeams?.FirstOrDefault(u => u.TeamId == delUser);
  67. if (removeItem != null) await _unitOfWork.ProjectTeam.DeleteAsync(removeItem);
  68. }
  69. foreach (var newteam in newProjectTeams)
  70. {
  71. await _unitOfWork.ProjectTeam.AddAsync(newteam);
  72. }
  73. await _unitOfWork.CompleteAsync();
  74. _unitOfWork.CommitTran();
  75. var response = Mapper.MapperObject.Mapper.Map<ProjectDto>(entity);
  76. return response;
  77. }
  78. public override async Task<PagingResultDto<ProjectDto>> GetAll(PagingInputDto PagingInputDto)
  79. {
  80. var res = await _unitOfWork.Project.GetAllWithChildrenAsync();
  81. var query = res.Item1;
  82. if (PagingInputDto.HiddenFilter != null)
  83. {
  84. query = query.Where(PagingInputDto.HiddenFilter);
  85. }
  86. if (PagingInputDto.Filter != null)
  87. {
  88. var filter = PagingInputDto.Filter;
  89. query = query.Where(u => u.NameAr.Contains(filter));
  90. }
  91. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  92. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  93. var total = await query.CountAsync();
  94. var list = MapperObject.Mapper
  95. .Map<IList<ProjectDto>>(await page.ToListAsync());
  96. var response = new PagingResultDto<ProjectDto>
  97. {
  98. Result = list,
  99. Total = total
  100. };
  101. return response;
  102. }
  103. }
  104. }