AttendanceService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace MTWorkHR.Application.Services
  20. {
  21. public class AttendanceService : BaseService<Attendance, AttendanceDto, AttendanceDto>, IAttendanceService
  22. {
  23. private readonly IUnitOfWork _unitOfWork;
  24. private readonly GlobalInfo _globalInfo;
  25. private readonly IUserService _userService;
  26. public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, IUserService userService) :base(unitOfWork)
  27. {
  28. _unitOfWork = unitOfWork;
  29. _globalInfo = globalInfo;
  30. _userService = userService;
  31. }
  32. public async Task<PagingResultDto<AttendanceDto>> GetAll(AttendancePagingInputDto PagingInputDto)
  33. {
  34. var res = await _unitOfWork.Attendance.GetAllWithChildrenAsync();
  35. var query = res.Item1;
  36. if (_globalInfo.UserType != UserTypeEnum.Business)
  37. {
  38. query = query.Where(m => m.UserId == _globalInfo.UserId);
  39. }
  40. if (PagingInputDto.Filter != null)
  41. {
  42. var filter = PagingInputDto.Filter;
  43. query = query.Where(u => u.LeaveReason!.Contains(filter) || u.UserName!.Contains(filter));
  44. }
  45. if (PagingInputDto.SearchDate != null)
  46. {
  47. query = query.Where(u => u.AttendanceDate.Date == PagingInputDto.SearchDate.Value.Date);
  48. }
  49. if (PagingInputDto.UserId != null)
  50. {
  51. query = query.Where(u => u.UserId == PagingInputDto.UserId);
  52. }
  53. //var dayGroupByUser = query.GroupBy(m => new { m.UserId, m.UserName, AttendanceDate = m.AttendanceDate.Date }).Select
  54. // (g => new Attendance
  55. // {
  56. // UserId = g.Key.UserId,
  57. // UserName = g.Key.UserName,
  58. // AttendanceDate = g.Key.AttendanceDate,
  59. // TotalHours = g.Sum(s => (s.CheckInTime.HasValue && s.CheckOutTime.HasValue) ? (s.CheckOutTime.Value - s.CheckInTime.Value).TotalHours : 0)
  60. // });
  61. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  62. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  63. var total = await query.CountAsync();
  64. var list = MapperObject.Mapper
  65. .Map<IList<AttendanceDto>>(await page.ToListAsync());
  66. //var dayGroupByUser = list.GroupBy(m => new { m.UserId,m.WeekDay, m.UserName, AttendanceDate = m.AttendanceDate.Date }).Select
  67. // (g => new AttendanceDto {
  68. // UserId = g.Key.UserId, UserName=g.Key.UserName,
  69. // WeekDay= g.Key.WeekDay,
  70. // AttendanceDate = g.Key.AttendanceDate,
  71. // TotalHours = g.Sum(s=> s.TotalHours)
  72. // }).ToList();
  73. var employeesList = list.Select(a => a.UserId).Distinct();
  74. Dictionary<string, UserBasicInfoDto> employeeDictList = new Dictionary<string, UserBasicInfoDto>();
  75. foreach (var RequestingEmployeeId in employeesList)
  76. {
  77. if (RequestingEmployeeId != null)
  78. {
  79. var user = await _userService.GetUserWithAttachmentById(RequestingEmployeeId);
  80. if (user != null)
  81. {
  82. var entitiy = MapperObject.Mapper.Map<UserBasicInfoDto>(user);
  83. //item.Employee = entitiy;
  84. var attach = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  85. if (attach != null)
  86. using (var stream = new MemoryStream(attach.Content))
  87. {
  88. var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(attach.FileName), attach.FileName)
  89. {
  90. Headers = new HeaderDictionary(),
  91. ContentType = attach.ContentType,
  92. };
  93. System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
  94. {
  95. FileName = file.FileName
  96. };
  97. file.ContentDisposition = cd.ToString();
  98. entitiy.ProfileImage = file;
  99. }
  100. employeeDictList.Add(RequestingEmployeeId, entitiy);
  101. }
  102. }
  103. }
  104. foreach (var item in list)
  105. {
  106. UserBasicInfoDto? employee;
  107. bool success = employeeDictList.TryGetValue(item.UserId, out employee);
  108. // var employee2 = employeeDictList[item.UserId];
  109. if(success && employee != null) item.Employee = employee;
  110. }
  111. var response = new PagingResultDto<AttendanceDto>
  112. {
  113. Result = list ,
  114. //Result = ! string.IsNullOrEmpty( PagingInputDto.UserId ) ? list : dayGroupByUser,
  115. Total = total
  116. };
  117. return response;
  118. }
  119. public override async Task<AttendanceDto> Create(AttendanceDto input)
  120. {
  121. var oldEntity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  122. if (oldEntity is null)
  123. {
  124. var entitiy = MapperObject.Mapper.Map<Attendance>(input);
  125. if (entitiy is null)
  126. throw new AppException(ExceptionEnum.MapperIssue);
  127. var newEntity = await _unitOfWork.Attendance.AddAsync(entitiy);
  128. var Success = await _unitOfWork.CompleteAsync();
  129. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(newEntity);
  130. return response;
  131. }
  132. else
  133. {
  134. throw new AppException(ExceptionEnum.UserAlreadyCheckedIn);
  135. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
  136. return response;
  137. }
  138. }
  139. public override async Task<AttendanceDto> Update(AttendanceDto input)
  140. {
  141. Attendance? entity = null;
  142. if (input.Id > 0)
  143. entity = await _unitOfWork.Attendance.GetByIdAsync(input.Id);
  144. else
  145. entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  146. if (entity is null)
  147. throw new AppException(ExceptionEnum.RecordNotExist);
  148. entity.CheckOutTime = input.CheckOutTime;
  149. entity.LeaveType = input.LeaveType;
  150. entity.LeaveReason = input.LeaveReason;
  151. await _unitOfWork.CompleteAsync();
  152. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  153. return response;
  154. }
  155. public async Task<AttendanceDto?> GetAttendanceByUserId(string userId, DateTime attendanceDate)
  156. {
  157. var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(userId, attendanceDate);
  158. if (entity is null)
  159. return null;
  160. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  161. return response;
  162. }
  163. }
  164. }