AttendanceService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 System.Linq.Dynamic.Core;
  16. using System.Linq;
  17. using Microsoft.AspNetCore.Http;
  18. using MTWorkHR.Core.Entities.Base;
  19. using System;
  20. namespace MTWorkHR.Application.Services
  21. {
  22. public class AttendanceService : BaseService<Attendance, AttendanceDto, AttendanceDto>, IAttendanceService
  23. {
  24. private readonly IUnitOfWork _unitOfWork;
  25. private readonly GlobalInfo _globalInfo;
  26. private readonly IUserService _userService;
  27. public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, IUserService userService) :base(unitOfWork)
  28. {
  29. _unitOfWork = unitOfWork;
  30. _globalInfo = globalInfo;
  31. _userService = userService;
  32. }
  33. public async Task<PagingResultDto<AttendanceDto>> GetAll(AttendancePagingInputDto pagingInputDto)
  34. {
  35. // Fetch all attendances with related data
  36. var attendanceResult = await _unitOfWork.Attendance.GetAllWithChildrenAsync();
  37. var query = attendanceResult.Item1.AsQueryable();
  38. // Apply filters based on user type and input
  39. if (_globalInfo.UserType != UserTypeEnum.Business)
  40. {
  41. query = query.Where(m => m.UserId == _globalInfo.UserId);
  42. }
  43. if (!string.IsNullOrEmpty(pagingInputDto.Filter))
  44. {
  45. query = query.Where(u => u.LeaveReason!.Contains(pagingInputDto.Filter) || u.UserName!.Contains(pagingInputDto.Filter));
  46. }
  47. if (pagingInputDto.SearchDate != null)
  48. {
  49. query = query.Where(u => u.AttendanceDate.Date == pagingInputDto.SearchDate.Value.Date);
  50. }
  51. if (!string.IsNullOrEmpty(pagingInputDto.UserId))
  52. {
  53. query = query.Where(u => u.UserId == pagingInputDto.UserId);
  54. }
  55. // Apply sorting
  56. var orderedQuery = query.OrderBy($"{pagingInputDto.OrderByField} {pagingInputDto.OrderType}");
  57. // Apply paging
  58. var pagedQuery = orderedQuery
  59. .Skip((pagingInputDto.PageNumber - 1) * pagingInputDto.PageSize)
  60. .Take(pagingInputDto.PageSize);
  61. // Fetch total count for pagination
  62. var totalCount = await query.CountAsync();
  63. // Map attendance data to DTOs
  64. var attendanceDtos = MapperObject.Mapper.Map<IList<AttendanceDto>>(await pagedQuery.ToListAsync());
  65. // Fetch all company employees
  66. var employeesList = await _userService.GetAllCompanyEmployees();
  67. // Filter employees by name if provided
  68. if (!string.IsNullOrEmpty(pagingInputDto.UserName))
  69. {
  70. var filter = pagingInputDto.UserName;
  71. var filters = filter.Split(" ");
  72. var firstNameFilter = filters[0];
  73. var lastNameFilter = filters.Length > 1 ? filters[1] : "";
  74. employeesList = employeesList.Where(u =>
  75. u.UserName.Contains(filter) || u.Email.Contains(filter) || u.FavoriteName.Contains(filter) ||
  76. u.FirstName.Contains(filter) || u.LastName.Contains(filter) ||
  77. (u.FirstName.Contains(firstNameFilter) && (string.IsNullOrEmpty(lastNameFilter) || u.LastName.Contains(lastNameFilter))) ||
  78. (u.LastName.Contains(lastNameFilter) && (string.IsNullOrEmpty(firstNameFilter) || u.FirstName.Contains(firstNameFilter)))
  79. ).ToList();
  80. }
  81. // Create a dictionary to store user data
  82. var userDataDictionary = new Dictionary<string, UserBasicInfoDto>();
  83. // Fetch user data for all employees
  84. foreach (var employee in employeesList)
  85. {
  86. var user = await _userService.GetUserWithAttachmentById(employee.Id);
  87. if (user != null)
  88. {
  89. var userDto = MapperObject.Mapper.Map<UserBasicInfoDto>(user);
  90. userDto.ProfileImage = await GetProfileImageAsync(user.UserAttachments);
  91. userDataDictionary[employee.Id] = userDto;
  92. }
  93. }
  94. // Combine attendance data with user data
  95. var result = new List<AttendanceDto>();
  96. if (string.IsNullOrEmpty(pagingInputDto.UserId))
  97. {
  98. foreach (var employee in employeesList)
  99. {
  100. var latestAttendance = attendanceDtos.LastOrDefault(a => a.UserId == employee.Id);
  101. if (latestAttendance != null)
  102. {
  103. latestAttendance.Employee = userDataDictionary[employee.Id];
  104. }
  105. else
  106. {
  107. latestAttendance = new AttendanceDto
  108. {
  109. UserId = employee.Id,
  110. UserName = employee.UserName,
  111. Employee = userDataDictionary[employee.Id]
  112. };
  113. }
  114. result.Add(latestAttendance);
  115. }
  116. }
  117. else
  118. {
  119. foreach (var attendance in attendanceDtos)
  120. {
  121. attendance.Employee = userDataDictionary[pagingInputDto.UserId];
  122. }
  123. result = attendanceDtos.ToList();
  124. }
  125. // Return the paged result
  126. return new PagingResultDto<AttendanceDto>
  127. {
  128. Result = result,
  129. Total = string.IsNullOrEmpty(pagingInputDto.UserId) ? result.Count : totalCount
  130. };
  131. }
  132. // Helper method to get profile image
  133. private async Task<FormFile> GetProfileImageAsync(ICollection<AttachmentDto> attachments)
  134. {
  135. var profileImage = attachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  136. if (profileImage == null) return null;
  137. using (var stream = new MemoryStream(profileImage.Content))
  138. {
  139. var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(profileImage.FileName), profileImage.FilePath)
  140. {
  141. Headers = new HeaderDictionary(),
  142. ContentType = profileImage.ContentType,
  143. ContentDisposition = new System.Net.Mime.ContentDisposition
  144. {
  145. FileName = profileImage.FileName
  146. }.ToString()
  147. };
  148. return file;
  149. }
  150. }
  151. public override async Task<AttendanceDto> Create(AttendanceDto input)
  152. {
  153. if (input.CheckInTime.HasValue)
  154. input.AttendanceDate = input.CheckInTime.Value.Date;
  155. var oldEntity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  156. if (oldEntity is null)
  157. {
  158. if (input.CheckInTime.HasValue)
  159. {
  160. if (input.CheckInTime.Value.Kind != DateTimeKind.Utc)
  161. {
  162. var timeZone = TimeZoneInfo.FindSystemTimeZoneById(GlobalInfo.timeZone);
  163. DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(input.CheckInTime.Value, timeZone);
  164. input.CheckInTime = utcDateTime;
  165. }
  166. }
  167. if (input.CheckOutTime.HasValue)
  168. {
  169. if (input.CheckOutTime.Value.Kind != DateTimeKind.Utc)
  170. {
  171. var timeZone = TimeZoneInfo.FindSystemTimeZoneById(GlobalInfo.timeZone);
  172. DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(input.CheckOutTime.Value, timeZone);
  173. input.CheckOutTime = utcDateTime;
  174. }
  175. }
  176. var entity = MapperObject.Mapper.Map<Attendance>(input);
  177. if (entity is null)
  178. throw new AppException(ExceptionEnum.MapperIssue);
  179. var newEntity = await _unitOfWork.Attendance.AddAsync(entity);
  180. var Success = await _unitOfWork.CompleteAsync();
  181. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(newEntity);
  182. return response;
  183. }
  184. else
  185. {
  186. throw new AppException(ExceptionEnum.UserAlreadyCheckedIn);
  187. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
  188. return response;
  189. }
  190. }
  191. public override async Task<AttendanceDto> Update(AttendanceDto input)
  192. {
  193. Attendance? entity = null;
  194. if (input.CheckInTime.HasValue)
  195. input.AttendanceDate = input.CheckInTime.Value.Date;
  196. else if (input.CheckOutTime.HasValue)
  197. input.AttendanceDate = input.CheckOutTime.Value.Date;
  198. if (input.Id > 0)
  199. entity = await _unitOfWork.Attendance.GetByIdAsync(input.Id);
  200. else
  201. entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  202. if (entity is null)
  203. throw new AppException(ExceptionEnum.RecordNotExist);
  204. if (input.CheckOutTime.HasValue)
  205. {
  206. if (input.CheckOutTime.Value.Kind != DateTimeKind.Utc)
  207. {
  208. var timeZone = TimeZoneInfo.FindSystemTimeZoneById(GlobalInfo.timeZone);
  209. DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(input.CheckOutTime.Value, timeZone);
  210. input.CheckOutTime = utcDateTime;
  211. }
  212. }
  213. entity.CheckOutTime = input.CheckOutTime;
  214. entity.LeaveType = input.LeaveType;
  215. entity.LeaveReason = input.LeaveReason;
  216. await _unitOfWork.CompleteAsync();
  217. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  218. return response;
  219. }
  220. public async Task<AttendanceDto?> GetAttendanceByUserId(string userId, DateTime attendanceDate)
  221. {
  222. var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(userId, attendanceDate);
  223. if (entity is null)
  224. return null;
  225. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  226. return response;
  227. }
  228. }
  229. }