UserTaskService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. using MTWorkHR.Core.Entities.Base;
  13. namespace MTWorkHR.Application.Services
  14. {
  15. public class UserTaskService : BaseService<UserTask, UserTaskDto, UserTaskDto>, IUserTaskService
  16. {
  17. private readonly IUnitOfWork _unitOfWork;
  18. private readonly IFileService _fileService;
  19. private readonly GlobalInfo _globalInfo;
  20. private readonly IUserService _userService;
  21. public UserTaskService(IUnitOfWork unitOfWork, IFileService fileService, GlobalInfo globalInfo, IUserService userService) : base(unitOfWork)
  22. {
  23. _unitOfWork = unitOfWork;
  24. _fileService = fileService;
  25. _globalInfo = globalInfo;
  26. _userService = userService;
  27. }
  28. public override async Task<UserTaskDto> GetById(long id)
  29. {
  30. var entity = await _unitOfWork.UserTask.GetByIdWithAllChildren(id);
  31. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  32. foreach (var attach in response.TaskAttachments)
  33. {
  34. var user = await _userService.GetUserNameEmail(attach.CreateUser);
  35. attach.ProfileImage = user.ProfileImage;
  36. attach.CreatedUserName = user.FullName;
  37. }
  38. await GetUserData(response);
  39. return response;
  40. }
  41. public async Task<UserTaskDto> GetByUserId(string userId)
  42. {
  43. var entity = await _unitOfWork.UserTask.GetByUserIdWithAllChildren(userId);
  44. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  45. await GetUserData(response);
  46. return response;
  47. }
  48. public async Task<PagingResultDto<UserTaskAllDto>> GetAll(UserTaskPagingInputDto PagingInputDto)
  49. {
  50. var res = await _unitOfWork.UserTask.GetAllWithChildrenAsync();
  51. var query = res.Item1;
  52. if (_globalInfo.UserType != UserTypeEnum.Business)
  53. {
  54. query = query.Where(m => m.AssignedUserId == _globalInfo.UserId);
  55. }
  56. if (PagingInputDto.HiddenFilter != null)
  57. {
  58. query = query.Where(PagingInputDto.HiddenFilter);
  59. }
  60. if (PagingInputDto.Filter != null)
  61. {
  62. var filter = PagingInputDto.Filter;
  63. query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
  64. }
  65. if (PagingInputDto.ProjectId != null)
  66. {
  67. query = query.Where(u => u.ProjectId == PagingInputDto.ProjectId);
  68. }
  69. if (PagingInputDto.StatusId != null)
  70. {
  71. query = query.Where(u => u.StatusId == PagingInputDto.StatusId);
  72. }
  73. if (PagingInputDto.PriorityId != null)
  74. {
  75. query = query.Where(u => u.Priority == (PriorityEnum)PagingInputDto.PriorityId);
  76. }
  77. if (PagingInputDto.AssignedUserId != null)
  78. {
  79. query = query.Where(u => u.AssignedUserId == PagingInputDto.AssignedUserId);
  80. }
  81. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  82. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  83. var total = await query.CountAsync();
  84. var list = MapperObject.Mapper
  85. .Map<IList<UserTaskAllDto>>(await page.ToListAsync());
  86. foreach (var item in list)
  87. {
  88. if (item.AssignedUserId != null)
  89. {
  90. var user = await _userService.GetUserWithAttachmentById(item.AssignedUserId);
  91. if(user != null)
  92. {
  93. item.AssignedUserName = user.FirstName + " " + user.LastName;
  94. var image = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  95. item.ProfileImage = image != null ? image.FilePath:"";
  96. }
  97. }
  98. }
  99. var response = new PagingResultDto<UserTaskAllDto>
  100. {
  101. Result = list,
  102. Total = total
  103. };
  104. return response;
  105. }
  106. public override async Task<UserTaskDto> Create(UserTaskDto input)
  107. {
  108. if (input.TaskAttachments?.Count > 0)
  109. {
  110. var attachs = input.TaskAttachments.ToList();
  111. _fileService.CopyFileToCloud(ref attachs);
  112. }
  113. var entity = MapperObject.Mapper.Map<UserTask>(input);
  114. if (entity is null)
  115. {
  116. throw new AppException(ExceptionEnum.MapperIssue);
  117. }
  118. var task = await _unitOfWork.UserTask.AddAsync(entity);
  119. await _unitOfWork.CompleteAsync();
  120. var response = MapperObject.Mapper.Map<UserTaskDto>(task);
  121. await GetUserData(response);
  122. return response;
  123. }
  124. private async Task GetUserData(UserTaskDto response)
  125. {
  126. if (response.AssignedUserId != null)
  127. {
  128. var user = await _userService.GetUserWithAttachmentById(response.AssignedUserId);
  129. if (user != null)
  130. {
  131. response.AssignedUserName = user.FirstName + " " + user.LastName;
  132. var image = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  133. response.ProfileImage = image != null ? image.FilePath : "";
  134. }
  135. }
  136. }
  137. public override async Task<UserTaskDto> Update(UserTaskDto input)
  138. {
  139. var entity = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
  140. if (entity == null)
  141. throw new AppException(ExceptionEnum.RecordNotExist);
  142. MapperObject.Mapper.Map(input, entity, typeof(UserTaskDto), typeof(UserTask));
  143. await _unitOfWork.CompleteAsync();
  144. var response = Mapper.MapperObject.Mapper.Map<UserTaskDto>(entity);
  145. await GetUserData(response);
  146. return response;
  147. }
  148. public async Task ChangeStatus(long taskId, int statusId)
  149. {
  150. var entity = await _unitOfWork.UserTask.GetByIdAsync(taskId);
  151. if (entity == null)
  152. throw new AppException(ExceptionEnum.RecordNotExist);
  153. entity.StatusId = statusId;
  154. await _unitOfWork.CompleteAsync();
  155. }
  156. }
  157. }