AttendanceService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. if (input.CheckInTime.HasValue)
  122. input.AttendanceDate = input.CheckInTime.Value.Date;
  123. var oldEntity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  124. if (oldEntity is null)
  125. {
  126. var entitiy = MapperObject.Mapper.Map<Attendance>(input);
  127. if (entitiy is null)
  128. throw new AppException(ExceptionEnum.MapperIssue);
  129. var newEntity = await _unitOfWork.Attendance.AddAsync(entitiy);
  130. var Success = await _unitOfWork.CompleteAsync();
  131. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(newEntity);
  132. return response;
  133. }
  134. else
  135. {
  136. throw new AppException(ExceptionEnum.UserAlreadyCheckedIn);
  137. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
  138. return response;
  139. }
  140. }
  141. public override async Task<AttendanceDto> Update(AttendanceDto input)
  142. {
  143. Attendance? entity = null;
  144. if (input.CheckInTime.HasValue)
  145. input.AttendanceDate = input.CheckInTime.Value.Date;
  146. else if (input.CheckOutTime.HasValue)
  147. input.AttendanceDate = input.CheckOutTime.Value.Date;
  148. if (input.Id > 0)
  149. entity = await _unitOfWork.Attendance.GetByIdAsync(input.Id);
  150. else
  151. entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  152. if (entity is null)
  153. throw new AppException(ExceptionEnum.RecordNotExist);
  154. entity.CheckOutTime = input.CheckOutTime;
  155. entity.LeaveType = input.LeaveType;
  156. entity.LeaveReason = input.LeaveReason;
  157. await _unitOfWork.CompleteAsync();
  158. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  159. return response;
  160. }
  161. public async Task<AttendanceDto?> GetAttendanceByUserId(string userId, DateTime attendanceDate)
  162. {
  163. var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(userId, attendanceDate);
  164. if (entity is null)
  165. return null;
  166. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  167. return response;
  168. }
  169. }
  170. }