AttendanceService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. foreach (var item in list)
  74. {
  75. if (item.UserId != null)
  76. {
  77. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  78. if (user != null)
  79. {
  80. var entitiy = MapperObject.Mapper.Map<UserBasicInfoDto>(user);
  81. item.Employee = entitiy;
  82. var attach = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
  83. if (attach != null)
  84. using (var stream = new MemoryStream(attach.Content))
  85. {
  86. var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(attach.FileName), attach.FileName)
  87. {
  88. Headers = new HeaderDictionary(),
  89. ContentType = attach.ContentType,
  90. };
  91. System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
  92. {
  93. FileName = file.FileName
  94. };
  95. file.ContentDisposition = cd.ToString();
  96. item.Employee.ProfileImage = file;
  97. }
  98. }
  99. }
  100. }
  101. var response = new PagingResultDto<AttendanceDto>
  102. {
  103. Result = list ,
  104. //Result = ! string.IsNullOrEmpty( PagingInputDto.UserId ) ? list : dayGroupByUser,
  105. Total = total
  106. };
  107. return response;
  108. }
  109. public override async Task<AttendanceDto> Create(AttendanceDto input)
  110. {
  111. var oldEntity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  112. if (oldEntity is null)
  113. {
  114. var entitiy = MapperObject.Mapper.Map<Attendance>(input);
  115. if (entitiy is null)
  116. throw new AppException(ExceptionEnum.MapperIssue);
  117. var newEntity = await _unitOfWork.Attendance.AddAsync(entitiy);
  118. var Success = await _unitOfWork.CompleteAsync();
  119. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(newEntity);
  120. return response;
  121. }
  122. else
  123. {
  124. throw new AppException(ExceptionEnum.UserAlreadyCheckedIn);
  125. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
  126. return response;
  127. }
  128. }
  129. public override async Task<AttendanceDto> Update(AttendanceDto input)
  130. {
  131. Attendance? entity = null;
  132. if (input.Id > 0)
  133. entity = await _unitOfWork.Attendance.GetByIdAsync(input.Id);
  134. else
  135. entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  136. if (entity is null)
  137. throw new AppException(ExceptionEnum.RecordNotExist);
  138. entity.CheckOutTime = input.CheckOutTime;
  139. entity.LeaveType = input.LeaveType;
  140. entity.LeaveReason = input.LeaveReason;
  141. await _unitOfWork.CompleteAsync();
  142. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  143. return response;
  144. }
  145. public async Task<AttendanceDto?> GetAttendanceByUserId(string userId, DateTime attendanceDate)
  146. {
  147. var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(userId, attendanceDate);
  148. if (entity is null)
  149. return null;
  150. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  151. return response;
  152. }
  153. }
  154. }