123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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<Attendance, AttendanceDto, AttendanceDto>, 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<PagingResultDto<AttendanceDto>> 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<IList<AttendanceDto>>(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<string, UserBasicInfoDto> employeeDictList = new Dictionary<string, UserBasicInfoDto>();
- foreach (var RequestingEmployeeId in employeesList)
- {
- if (RequestingEmployeeId != null)
- {
- var user = await _userService.GetUserWithAttachmentById(RequestingEmployeeId);
- if (user != null)
- {
- var entitiy = MapperObject.Mapper.Map<UserBasicInfoDto>(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<AttendanceDto>
- {
- Result = list ,
- //Result = ! string.IsNullOrEmpty( PagingInputDto.UserId ) ? list : dayGroupByUser,
- Total = total
- };
- return response;
- }
- public override async Task<AttendanceDto> 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<Attendance>(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<AttendanceDto>(newEntity);
- return response;
- }
- else
- {
- throw new AppException(ExceptionEnum.UserAlreadyCheckedIn);
- var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
- return response;
- }
- }
- public override async Task<AttendanceDto> 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<AttendanceDto>(entity);
- return response;
- }
- public async Task<AttendanceDto?> 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<AttendanceDto>(entity);
- return response;
- }
- }
- }
|