|
@@ -17,6 +17,7 @@ using System.Linq.Dynamic.Core;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using MTWorkHR.Core.Entities.User;
|
|
|
using MTWorkHR.Core.Entities.Base;
|
|
|
+using System.Linq;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
|
{
|
|
@@ -121,7 +122,8 @@ namespace MTWorkHR.Application.Services
|
|
|
}
|
|
|
if (PagingInputDto.SearchDate != null)
|
|
|
{
|
|
|
- query = query.Where(u => u.StartDate.Date <= PagingInputDto.SearchDate.Value.Date && u.EndDate!.Value.Date >= PagingInputDto.SearchDate.Value.Date) ;
|
|
|
+ // query = query.Where(u => u.StartDate.Date <= PagingInputDto.SearchDate.Value.Date && u.EndDate!.Value.Date >= PagingInputDto.SearchDate.Value.Date) ;
|
|
|
+ query = query.Where(u => u.CreateDate.Date == PagingInputDto.SearchDate.Value.Date) ;
|
|
|
}
|
|
|
if (PagingInputDto.OrderTypeId != null)
|
|
|
{
|
|
@@ -135,42 +137,33 @@ namespace MTWorkHR.Application.Services
|
|
|
{
|
|
|
query = query.Where(u => (long?)u.OrderStatus == PagingInputDto.StatusId);
|
|
|
}
|
|
|
- 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<OrderRequestDto>>(await page.ToListAsync());
|
|
|
- foreach (var item in list)
|
|
|
+ // UserName filter (join with users table)
|
|
|
+ if (!string.IsNullOrEmpty(PagingInputDto.UserName))
|
|
|
{
|
|
|
- if (item.RequestingEmployeeId != null)
|
|
|
- {
|
|
|
- var user = await _userService.GetUserWithAttachmentById(item.RequestingEmployeeId);
|
|
|
- if (user != null)
|
|
|
- {
|
|
|
- item.Employee = new EmployeeDto { Id = item.RequestingEmployeeId, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Position = !string.IsNullOrEmpty( user.Position )? user.Position : user.JobTitleName };
|
|
|
- var attach = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9);
|
|
|
- item.Employee.ProfileImagePath = attach?.FilePath;
|
|
|
- // 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();
|
|
|
- //}
|
|
|
- }
|
|
|
- }
|
|
|
+ var filter = PagingInputDto.UserName.ToLower();
|
|
|
+ query = query.Join(
|
|
|
+ _userManager.Users,
|
|
|
+ order => order.RequestingEmployeeId,
|
|
|
+ user => user.Id,
|
|
|
+ (order, user) => new { Order = order, User = user }
|
|
|
+ )
|
|
|
+ .Where(joined =>
|
|
|
+ (joined.User.FirstName + " " + joined.User.LastName).ToLower().Contains(filter)
|
|
|
+ // ||joined.User.Email.ToLower().Contains(filter)
|
|
|
+ )
|
|
|
+ .Select(joined => joined.Order);
|
|
|
}
|
|
|
+ var total = await query.CountAsync();
|
|
|
+ var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
|
|
|
+
|
|
|
+ var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
|
|
|
+
|
|
|
+ var list = await MapToOrderRequestDtos(page);
|
|
|
+
|
|
|
var response = new PagingResultDto<OrderRequestDto>
|
|
|
{
|
|
|
Result = list,
|
|
@@ -179,6 +172,46 @@ namespace MTWorkHR.Application.Services
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private async Task<List<OrderRequestDto>> MapToOrderRequestDtos(IQueryable<OrderRequest> page)
|
|
|
+ {
|
|
|
+ var list = MapperObject.Mapper.Map<List<OrderRequestDto>>(await page.ToListAsync());
|
|
|
+
|
|
|
+ // Batch load user data to improve performance
|
|
|
+ var employeeIds = list.Where(x => x.RequestingEmployeeId != null)
|
|
|
+ .Select(x => x.RequestingEmployeeId)
|
|
|
+ .Distinct()
|
|
|
+ .ToList();
|
|
|
+
|
|
|
+ if (employeeIds.Any())
|
|
|
+ {
|
|
|
+ var users = await _userService.GetUsersWithAttachmentsByIds(employeeIds);
|
|
|
+ if (users != null)
|
|
|
+ {
|
|
|
+ var userLookup = users.ToDictionary(u => u.Id, u => u);
|
|
|
+
|
|
|
+ foreach (var item in list)
|
|
|
+ {
|
|
|
+ if (item.RequestingEmployeeId != null && userLookup.TryGetValue(item.RequestingEmployeeId, out var user))
|
|
|
+ {
|
|
|
+ item.Employee = new EmployeeDto
|
|
|
+ {
|
|
|
+ Id = user.Id,
|
|
|
+ FirstName = user.FirstName,
|
|
|
+ LastName = user.LastName,
|
|
|
+ Email = user.Email,
|
|
|
+ Position = user.Position ?? user.JobTitleName,
|
|
|
+ EmployeeName = $"{user.FirstName} {user.LastName}",
|
|
|
+ ProfileImagePath = user.UserAttachments?.FirstOrDefault(a => a.AttachmentTypeId == 9)?.FilePath
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
public override async Task<OrderRequestDto> Create(OrderRequestDto input)
|
|
|
{
|
|
|
var period = DateTime.Now.Year;
|
|
@@ -215,7 +248,8 @@ namespace MTWorkHR.Application.Services
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- int daysRequested = (int)(input.EndDate - input.StartDate).TotalDays;
|
|
|
+ int sub = (int)(input.EndDate.Date - input.StartDate.Date).TotalDays;
|
|
|
+ int daysRequested = input.TotalDays > 0 ? (int)input.TotalDays : sub;
|
|
|
if (daysRequested > allocation.NumberOfDays)
|
|
|
{
|
|
|
throw new AppException(ExceptionEnum.NoVacationBalance, "You do not have enough days for this request");
|