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; using System.Linq; using Microsoft.AspNetCore.Http; using MTWorkHR.Core.Entities.Base; namespace MTWorkHR.Application.Services { public class AttendanceService : BaseService, IAttendanceService { private readonly IUnitOfWork _unitOfWork; private readonly GlobalInfo _globalInfo; private readonly IUserService _userService; public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, IUserService userService) :base(unitOfWork) { _unitOfWork = unitOfWork; _globalInfo = globalInfo; _userService = userService; } 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); } if (PagingInputDto.UserId != null) { query = query.Where(u => u.UserId == PagingInputDto.UserId); } //var dayGroupByUser = query.GroupBy(m => new { m.UserId, m.UserName, AttendanceDate = m.AttendanceDate.Date }).Select // (g => new Attendance // { // UserId = g.Key.UserId, // UserName = g.Key.UserName, // AttendanceDate = g.Key.AttendanceDate, // TotalHours = g.Sum(s => (s.CheckInTime.HasValue && s.CheckOutTime.HasValue) ? (s.CheckOutTime.Value - s.CheckInTime.Value).TotalHours : 0) // }); 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 dayGroupByUser = list.GroupBy(m => new { m.UserId,m.WeekDay, m.UserName, AttendanceDate = m.AttendanceDate.Date }).Select // (g => new AttendanceDto { // UserId = g.Key.UserId, UserName=g.Key.UserName, // WeekDay= g.Key.WeekDay, // AttendanceDate = g.Key.AttendanceDate, // TotalHours = g.Sum(s=> s.TotalHours) // }).ToList(); var employeesList = list.Select(a => a.UserId).Distinct(); Dictionary employeeDictList = new Dictionary(); foreach (var RequestingEmployeeId in employeesList) { if (RequestingEmployeeId != null) { var user = await _userService.GetUserWithAttachmentById(RequestingEmployeeId); if (user != null) { var entitiy = MapperObject.Mapper.Map(user); //item.Employee = entitiy; var attach = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9); if (attach != null) using (var stream = new MemoryStream(attach.Content)) { var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(attach.FileName), attach.FileName) { Headers = new HeaderDictionary(), ContentType = attach.ContentType, }; System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition { FileName = file.FileName }; file.ContentDisposition = cd.ToString(); entitiy.ProfileImage = file; } employeeDictList.Add(RequestingEmployeeId, entitiy); } } } foreach (var item in list) { UserBasicInfoDto? employee; bool success = employeeDictList.TryGetValue(item.UserId, out employee); // var employee2 = employeeDictList[item.UserId]; if(success && employee != null) item.Employee = employee; } var response = new PagingResultDto { Result = list , //Result = ! string.IsNullOrEmpty( PagingInputDto.UserId ) ? list : dayGroupByUser, Total = total }; return response; } public override async Task Create(AttendanceDto input) { if (input.CheckInTime.HasValue) input.AttendanceDate = input.CheckInTime.Value.Date; var oldEntity = await _unitOfWork.Attendance.GetAttendanceByUserId(input.UserId, input.AttendanceDate); if (oldEntity is null) { 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; } else { throw new AppException(ExceptionEnum.UserAlreadyCheckedIn); var response = Mapper.MapperObject.Mapper.Map(oldEntity); return response; } } public override async Task Update(AttendanceDto input) { Attendance? entity = null; if (input.CheckInTime.HasValue) input.AttendanceDate = input.CheckInTime.Value.Date; else if (input.CheckOutTime.HasValue) input.AttendanceDate = input.CheckOutTime.Value.Date; if (input.Id > 0) entity = await _unitOfWork.Attendance.GetByIdAsync(input.Id); else 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; } public async Task GetAttendanceByUserId(string userId, DateTime attendanceDate) { var entity = await _unitOfWork.Attendance.GetAttendanceByUserId(userId, attendanceDate); if (entity is null) return null; var response = Mapper.MapperObject.Mapper.Map(entity); return response; } } }