123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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;
- namespace MTWorkHR.Application.Services
- {
- public class AttendanceService : BaseService<Attendance, AttendanceDto, AttendanceDto>, IAttendanceService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly GlobalInfo _globalInfo;
- public AttendanceService(IUnitOfWork unitOfWork, GlobalInfo globalInfo) :base(unitOfWork)
- {
- _unitOfWork = unitOfWork;
- _globalInfo = globalInfo;
- }
- 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 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)
- {
- 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
- {
- var response = Mapper.MapperObject.Mapper.Map<AttendanceDto>(oldEntity);
- return response;
- }
- }
- public override async Task<AttendanceDto> Update(AttendanceDto input)
- {
- Attendance? entity = null;
- 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;
- }
- }
- }
|