UserTaskService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. await GetUserData(response);
  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. await GetUserData(response);
  40. return response;
  41. }
  42. public async Task<PagingResultDto<UserTaskAllDto>> GetAll(UserTaskPagingInputDto PagingInputDto)
  43. {
  44. var res = await _unitOfWork.UserTask.GetAllWithChildrenAsync();
  45. var query = res.Item1;
  46. if (_globalInfo.UserType != UserTypeEnum.Business)
  47. {
  48. query = query.Where(m => m.AssignedUserId == _globalInfo.UserId);
  49. }
  50. if (PagingInputDto.HiddenFilter != null)
  51. {
  52. query = query.Where(PagingInputDto.HiddenFilter);
  53. }
  54. if (PagingInputDto.Filter != null)
  55. {
  56. var filter = PagingInputDto.Filter;
  57. query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
  58. }
  59. if (PagingInputDto.ProjectId != null)
  60. {
  61. query = query.Where(u => u.ProjectId == PagingInputDto.ProjectId);
  62. }
  63. if (PagingInputDto.StatusId != null)
  64. {
  65. query = query.Where(u => u.StatusId == PagingInputDto.StatusId);
  66. }
  67. if (PagingInputDto.PriorityId != null)
  68. {
  69. query = query.Where(u => u.Priority == (PriorityEnum)PagingInputDto.PriorityId);
  70. }
  71. if (PagingInputDto.AssignedUserId != null)
  72. {
  73. query = query.Where(u => u.AssignedUserId == PagingInputDto.AssignedUserId);
  74. }
  75. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  76. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  77. var total = await query.CountAsync();
  78. var list = MapperObject.Mapper
  79. .Map<IList<UserTaskAllDto>>(await page.ToListAsync());
  80. foreach (var item in list)
  81. {
  82. if (item.AssignedUserId != null)
  83. {
  84. var user = await _userService.GetUserWithAttachmentById(item.AssignedUserId);
  85. if(user != null)
  86. {
  87. item.AssignedUserName = user.FirstName + " " + user.LastName;
  88. var image = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  89. item.ProfileImage = image != null ? image.FilePath:"";
  90. }
  91. }
  92. }
  93. var response = new PagingResultDto<UserTaskAllDto>
  94. {
  95. Result = list,
  96. Total = total
  97. };
  98. return response;
  99. }
  100. public override async Task<UserTaskDto> Create(UserTaskDto input)
  101. {
  102. if (input.TaskAttachments?.Count > 0)
  103. {
  104. var attachs = input.TaskAttachments.ToList();
  105. _fileService.CopyFileToCloud(ref attachs);
  106. }
  107. var entity = MapperObject.Mapper.Map<UserTask>(input);
  108. if (entity is null)
  109. {
  110. throw new AppException(ExceptionEnum.MapperIssue);
  111. }
  112. var task = await _unitOfWork.UserTask.AddAsync(entity);
  113. await _unitOfWork.CompleteAsync();
  114. var response = MapperObject.Mapper.Map<UserTaskDto>(task);
  115. await GetUserData(response);
  116. return response;
  117. }
  118. private async Task GetUserData(UserTaskDto response)
  119. {
  120. if (response.AssignedUserId != null)
  121. {
  122. var user = await _userService.GetUserWithAttachmentById(response.AssignedUserId);
  123. if (user != null)
  124. {
  125. response.AssignedUserName = user.FirstName + " " + user.LastName;
  126. var image = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  127. response.ProfileImage = image != null ? image.FilePath : "";
  128. }
  129. }
  130. }
  131. public override async Task<UserTaskDto> Update(UserTaskDto input)
  132. {
  133. var entity = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
  134. if (entity == null)
  135. throw new AppException(ExceptionEnum.RecordNotExist);
  136. MapperObject.Mapper.Map(input, entity, typeof(UserTaskDto), typeof(UserTask));
  137. await _unitOfWork.CompleteAsync();
  138. var response = Mapper.MapperObject.Mapper.Map<UserTaskDto>(entity);
  139. await GetUserData(response);
  140. return response;
  141. }
  142. public async Task ChangeStatus(long taskId, int statusId)
  143. {
  144. var entity = await _unitOfWork.UserTask.GetByIdAsync(taskId);
  145. if (entity == null)
  146. throw new AppException(ExceptionEnum.RecordNotExist);
  147. entity.StatusId = statusId;
  148. await _unitOfWork.CompleteAsync();
  149. }
  150. }
  151. }