AttendanceService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace MTWorkHR.Application.Services
  17. {
  18. public class AttendanceService : BaseService<Attendance, AttendanceDto, AttendanceDto>, IAttendanceService
  19. {
  20. private readonly IUnitOfWork _unitOfWork;
  21. private readonly GlobalInfo _globalInfo;
  22. public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo) :base(unitOfWork)
  23. {
  24. _unitOfWork = unitOfWork;
  25. _globalInfo = globalInfo;
  26. }
  27. public async Task<PagingResultDto<AttendanceDto>> GetAll(AttendancePagingInputDto PagingInputDto)
  28. {
  29. var res = await _unitOfWork.Attendance.GetAllWithChildrenAsync();
  30. var query = res.Item1;
  31. if (_globalInfo.UserType != UserTypeEnum.Business)
  32. {
  33. query = query.Where(m => m.UserId == _globalInfo.UserId);
  34. }
  35. if (PagingInputDto.Filter != null)
  36. {
  37. var filter = PagingInputDto.Filter;
  38. query = query.Where(u => u.LeaveReason!.Contains(filter) || u.UserName!.Contains(filter));
  39. }
  40. if(PagingInputDto.SearchDate != null)
  41. {
  42. query = query.Where(u => u.AttendanceDate.Date == PagingInputDto.SearchDate.Value.Date);
  43. }
  44. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  45. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  46. var total = await query.CountAsync();
  47. var list = MapperObject.Mapper
  48. .Map<IList<AttendanceDto>>(await page.ToListAsync());
  49. var response = new PagingResultDto<AttendanceDto>
  50. {
  51. Result = list,
  52. Total = total
  53. };
  54. return response;
  55. }
  56. public override async Task<AttendanceDto> Create(AttendanceDto input)
  57. {
  58. var entitiy = MapperObject.Mapper.Map<Attendance>(input);
  59. if (entitiy is null)
  60. throw new AppException(ExceptionEnum.MapperIssue);
  61. var newEntity = await _unitOfWork.Attendance.AddAsync(entitiy);
  62. var Success = await _unitOfWork.CompleteAsync();
  63. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(newEntity);
  64. return response;
  65. }
  66. public override async Task<AttendanceDto> Update(AttendanceDto input)
  67. {
  68. var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate);
  69. if (entity is null)
  70. throw new AppException(ExceptionEnum.RecordNotExist);
  71. entity.CheckOutTime = input.CheckOutTime;
  72. entity.LeaveType = input.LeaveType;
  73. entity.LeaveReason = input.LeaveReason;
  74. await _unitOfWork.CompleteAsync();
  75. var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(entity);
  76. return response;
  77. }
  78. }
  79. }