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 SaveGoalAsync(Goal goalChange) { await _context.Goals.AddAsync(goalChange); return goalChange.Id; } public async Task> 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 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> 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> 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> FindPersonnelGoals(int departmentId, List 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> FindMonthlyPersonnelGoals(int departmentId, List 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> FindPersonnelGoalsByMonthRange(int departmentId, List 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> FindPersonnelGoalsByMonthRange(int departmentId, List 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> 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(); } }