UserTaskService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 
  2. using MTWorkHR.Application.Models;
  3. using MTWorkHR.Core.UnitOfWork;
  4. using MTWorkHR.Application.Services.Interfaces;
  5. using MTWorkHR.Core.Entities;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Core.Global;
  8. using Microsoft.EntityFrameworkCore;
  9. using System.Linq.Dynamic.Core;
  10. using MTWorkHR.Core.IDto;
  11. using MTWorkHR.Application.Identity;
  12. namespace MTWorkHR.Application.Services
  13. {
  14. public class UserTaskService : BaseService<UserTask, UserTaskDto, UserTaskDto>, IUserTaskService
  15. {
  16. private readonly IUnitOfWork _unitOfWork;
  17. //private readonly AppSettingsConfiguration _configuration;
  18. //private readonly GlobalInfo _globalInfo;
  19. private readonly IFileService _fileService;
  20. private readonly GlobalInfo _globalInfo;
  21. private readonly IUserService _userService;
  22. public UserTaskService(IUnitOfWork unitOfWork, IFileService fileService, GlobalInfo globalInfo, IUserService userService) : base(unitOfWork)
  23. {
  24. _unitOfWork = unitOfWork;
  25. _fileService = fileService;
  26. _globalInfo = globalInfo;
  27. _userService = userService;
  28. }
  29. public override async Task<UserTaskDto> GetById(long id)
  30. {
  31. var entity = await _unitOfWork.UserTask.GetByIdWithAllChildren(id);
  32. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  33. return response;
  34. }
  35. public async Task<UserTaskDto> GetByUserId(string userId)
  36. {
  37. var entity = await _unitOfWork.UserTask.GetByUserIdWithAllChildren(userId);
  38. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  39. return response;
  40. }
  41. public async Task<PagingResultDto<UserTaskAllDto>> GetAll(UserTaskPagingInputDto PagingInputDto)
  42. {
  43. var res = await _unitOfWork.UserTask.GetAllWithChildrenAsync();
  44. var query = res.Item1;
  45. if (_globalInfo.UserType != UserTypeEnum.Business)
  46. {
  47. query = query.Where(m => m.AssignedUserId == _globalInfo.UserId);
  48. }
  49. if (PagingInputDto.HiddenFilter != null)
  50. {
  51. query = query.Where(PagingInputDto.HiddenFilter);
  52. }
  53. if (PagingInputDto.Filter != null)
  54. {
  55. var filter = PagingInputDto.Filter;
  56. query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
  57. }
  58. if (PagingInputDto.ProjectId != null)
  59. {
  60. query = query.Where(u => u.ProjectId == PagingInputDto.ProjectId);
  61. }
  62. if (PagingInputDto.StatusId != null)
  63. {
  64. query = query.Where(u => u.StatusId == PagingInputDto.StatusId);
  65. }
  66. if (PagingInputDto.PriorityId != null)
  67. {
  68. query = query.Where(u => u.Priority == (PriorityEnum)PagingInputDto.PriorityId);
  69. }
  70. if (PagingInputDto.AssignedUserId != null)
  71. {
  72. query = query.Where(u => u.AssignedUserId == PagingInputDto.AssignedUserId);
  73. }
  74. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  75. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  76. var total = await query.CountAsync();
  77. var list = MapperObject.Mapper
  78. .Map<IList<UserTaskAllDto>>(await page.ToListAsync());
  79. foreach (var item in list)
  80. {
  81. if (item.AssignedUserId != null)
  82. {
  83. var user = await _userService.GetUserWithAttachmentById(item.AssignedUserId);
  84. if(user != null)
  85. {
  86. item.AssignedUserName = user.FirstName + " " + user.LastName;
  87. var image = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  88. item.ProfileImage = image != null ? image.FilePath:"";
  89. }
  90. }
  91. }
  92. var response = new PagingResultDto<UserTaskAllDto>
  93. {
  94. Result = list,
  95. Total = total
  96. };
  97. return response;
  98. }
  99. public override async Task<UserTaskDto> Create(UserTaskDto input)
  100. {
  101. if(input.TaskAttachments?.Count > 0)
  102. if ( !await _fileService.CopyFileToActualFolder(input.TaskAttachments.ToList()))
  103. throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  104. var entity = MapperObject.Mapper.Map<UserTask>(input);
  105. if (entity is null)
  106. {
  107. throw new AppException(ExceptionEnum.MapperIssue);
  108. }
  109. var task = await _unitOfWork.UserTask.AddAsync(entity);
  110. await _unitOfWork.CompleteAsync();
  111. var response = MapperObject.Mapper.Map<UserTaskDto>(task);
  112. return response;
  113. }
  114. public override async Task<UserTaskDto> Update(UserTaskDto input)
  115. {
  116. var entitiy = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
  117. if (entitiy == null)
  118. throw new AppException(ExceptionEnum.RecordNotExist);
  119. MapperObject.Mapper.Map(input, entitiy, typeof(UserTaskDto), typeof(UserTask));
  120. await _unitOfWork.CompleteAsync();
  121. return input;
  122. }
  123. }
  124. }