123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<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);
- }
- 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 response = new PagingResultDto<AttendanceDto>
- {
- Result = list,
- Total = total
- };
- return response;
- }
- public override async Task<AttendanceDto> Create(AttendanceDto input)
- {
- 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;
- }
- public override async Task<AttendanceDto> 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<AttendanceDto>(entity);
- return response;
- }
- }
- }
|