using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.WebUtilities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MTWorkHR.Application.Identity; using MTWorkHR.Application.Mapper; using MTWorkHR.Application.Models; using MTWorkHR.Core.Global; using MTWorkHR.Core.IRepositories; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Infrastructure.Entities; using MTWorkHR.Application.Services.Interfaces; using MTWorkHR.Core.Email; using MTWorkHR.Core.Entities; using System.Linq.Dynamic.Core; namespace MTWorkHR.Application.Services { public class AttendanceService : BaseService, IAttendanceService { private readonly IUnitOfWork _unitOfWork; private readonly GlobalInfo _globalInfo; public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo) :base(unitOfWork) { _unitOfWork = unitOfWork; _globalInfo = globalInfo; } public async Task> GetAll(AttendancePagingInputDto PagingInputDto) { var res = await _unitOfWork.Attendance.GetAllWithChildrenAsync(); var query = res.Item1; if (_globalInfo.UserType != UserTypeEnum.Business) { query = query.Where(m => m.UserId == _globalInfo.UserId); } if (PagingInputDto.Filter != null) { var filter = PagingInputDto.Filter; query = query.Where(u => u.LeaveReason!.Contains(filter) || u.UserName!.Contains(filter)); } if(PagingInputDto.SearchDate != null) { query = query.Where(u => u.AttendanceDate.Date == PagingInputDto.SearchDate.Value.Date); } var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType); var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize); var total = await query.CountAsync(); var list = MapperObject.Mapper .Map>(await page.ToListAsync()); var response = new PagingResultDto { Result = list, Total = total }; return response; } public override async Task Create(AttendanceDto input) { var entitiy = MapperObject.Mapper.Map(input); if (entitiy is null) throw new AppException(ExceptionEnum.MapperIssue); var newEntity = await _unitOfWork.Attendance.AddAsync(entitiy); var Success = await _unitOfWork.CompleteAsync(); var response = Mapper.MapperObject.Mapper.Map(newEntity); return response; } public override async Task Update(AttendanceDto input) { var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate); if (entity is null) throw new AppException(ExceptionEnum.RecordNotExist); entity.CheckOutTime = input.CheckOutTime; entity.LeaveType = input.LeaveType; entity.LeaveReason = input.LeaveReason; await _unitOfWork.CompleteAsync(); var response = Mapper.MapperObject.Mapper.Map(entity); return response; } } }