Files
2026-03-17 09:53:09 -05:00

131 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LCU.Library.Internal.IncentiveApiModels.Entities;
using Microsoft.EntityFrameworkCore;
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
using LCU.Library.Internal.IncentiveApiModels.Common;
using LCU.Library.Internal.IncentiveApiModels.Requests;
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
public class GoalRepository : BaseRepository, IGoalRepository
{
public GoalRepository(IApplicationDbContext context) : base(context)
{
}
public async Task<int> SaveGoalAsync(Goal goalChange)
{
await _context.Goals.AddAsync(goalChange);
return goalChange.Id;
}
public async Task<IReadOnlyCollection<Goal>> GetGoalsAsync(GoalSearchRequest goalSearchRequest)
{
var result = _context.Goals.AsQueryable();
if (goalSearchRequest != null)
{
if (goalSearchRequest.departmentId.HasValue)
{
result = result.Where(x => x.DepartmentId == goalSearchRequest.departmentId);
}
if (goalSearchRequest.DateRange != null && goalSearchRequest.DateRange.ValidFromRange)
{
var fromDate = goalSearchRequest.DateRange.StartTime;
result = result.Where(x => x.StartDate.Date >= fromDate.Date);
}
if (goalSearchRequest.DateRange != null && goalSearchRequest.DateRange.ValidToRange)
{
var toDate = goalSearchRequest.DateRange.EndTime;
result = result.Where(x => x.EndDate.Date <= toDate.Date);
}
if (goalSearchRequest.IncludePersonnel)
{
result = result.Where(x => x.EmployeeId != null);
}
else
{
result = result.Where(x => x.EmployeeId == null);
}
}
return await result.OrderBy(t => t.Id).ToListAsync();
}
public async Task<Goal> FindExistingGoal(GoalChange goalChange)
{
return await _context.Goals.FirstOrDefaultAsync(x => x.Product == goalChange.Product &&
x.StartDate.Date == goalChange.StartDate.Date &&
x.EndDate.Date == goalChange.EndDate.Date &&
x.DepartmentId == goalChange.DepartmentId &&
x.EmployeeId == goalChange.PersonId);
}
public async Task<ICollection<Goal>> FindDepartmentGoals(int departmentId, DateTime start, DateTime end)
{
return await _context.Goals.Where(x => x.StartDate.Date >= start.Date &&
x.EndDate.Date <= end.Date &&
x.EmployeeId == null &&
x.DepartmentId == departmentId).ToListAsync();
}
public async Task<ICollection<Goal>> FindAnnualDepartmentGoals(int departmentId, int year)
{
return await _context.Goals.Where(x => x.StartDate.Year == year &&
x.EndDate.Year == year &&
x.EmployeeId == null &&
x.DepartmentId == departmentId).ToListAsync();
}
public async Task<ICollection<Goal>> FindPersonnelGoals(int departmentId, List<AppUser> users, DateTime start,
DateTime end)
{
var employeeIds = users.Select(user => user.EmployeeId).ToList();
return await _context.Goals.Where(x => x.StartDate.Date == start.Date &&
x.EndDate.Date == end.Date &&
x.DepartmentId == departmentId &&
employeeIds.Contains(x.EmployeeId)).ToListAsync();
}
public async Task<ICollection<Goal>> FindMonthlyPersonnelGoals(int departmentId, List<AppUser> users, int year,
int month)
{
var employeeIds = users.Select(user => user.EmployeeId).ToList();
return await _context.Goals.Where(x => x.StartDate.Year == year && x.StartDate.Month == month &&
x.EndDate.Year == year && x.EndDate.Month == month &&
x.DepartmentId == departmentId &&
employeeIds.Contains(x.EmployeeId)).ToListAsync();
}
public async Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<AppUser> users, MonthRange monthRange)
{
var employeeIds = users.Select(user => user.EmployeeId).ToList();
return await _context.Goals.AsNoTracking().Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
x.EndDate.Date <= monthRange.EndTime.Date &&
x.DepartmentId == departmentId &&
employeeIds.Contains(x.EmployeeId)).ToListAsync();
}
public async Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<string> employeeIds, MonthRange monthRange)
{
return await _context.Goals.Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
x.EndDate.Date <= monthRange.EndTime.Date &&
x.DepartmentId == departmentId &&
employeeIds.Contains(x.EmployeeId)).ToListAsync();
}
public async Task<ICollection<Goal>> FindDepartmentGoalsByMonthRange(int departmentId, MonthRange monthRange)
{
return await _context.Goals.AsNoTracking().Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
x.EndDate.Date <= monthRange.EndTime.Date &&
x.EmployeeId == null &&
x.DepartmentId == departmentId).ToListAsync();
}
}