143 lines
5.1 KiB
C#
143 lines
5.1 KiB
C#
using LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
using LCU.Library.Internal.IncentiveApiModels.Common;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
|
|
|
public class DeadActivityRepository : BaseRepository, IDeadActivityRepository
|
|
{
|
|
public DeadActivityRepository(IApplicationDbContext context) : base(context)
|
|
{
|
|
}
|
|
|
|
public async Task<DeadActivity> SaveDeadActivity(DeadActivity newDeadActivity)
|
|
{
|
|
await _context.DeadActivities.AddAsync(newDeadActivity);
|
|
return newDeadActivity;
|
|
}
|
|
|
|
public async Task<List<DeadActivity>> SaveDeadActivityRange(List<DeadActivity> newActivities)
|
|
{
|
|
await _context.DeadActivities.AddRangeAsync(newActivities);
|
|
return newActivities;
|
|
}
|
|
|
|
public async Task<List<DeadActivity>> GetDeadActivitiesBySourceIds(List<string> sourceIds)
|
|
{
|
|
return await _context.DeadActivities.Include(x => x.DeadDetail).Where(x => sourceIds.Contains(x.SourceId)).ToListAsync();
|
|
}
|
|
|
|
public async Task<List<DeadActivity>> GetAllDeadActivities()
|
|
{
|
|
return await _context.DeadActivities.Include(x => x.DeadDetail).ToListAsync();
|
|
}
|
|
|
|
public async Task<DeadActivity> GetDeadActivityById(int activityId)
|
|
{
|
|
return await _context.DeadActivities
|
|
.Include(x => x.DeadDetail)
|
|
.FirstOrDefaultAsync(x => x.Id == activityId);
|
|
}
|
|
|
|
public async Task<PagedResult<DeadActivity>> GetPagedDeadActivities(int page, int limit)
|
|
{
|
|
var results = new PagedResult<DeadActivity>();
|
|
var skip = (page - 1) * limit;
|
|
var deadActivities = _context.DeadActivities
|
|
.Include(activity => activity.DeadDetail)
|
|
.OrderByDescending(t => t.Created);
|
|
|
|
results.Total = deadActivities.Count();
|
|
results.Items = await deadActivities.Skip(skip).Take(limit).ToListAsync();
|
|
return results;
|
|
}
|
|
|
|
public async Task<PagedResult<DeadActivity>> GetFilteredDeadActivities(DeadActivityFilter filter)
|
|
{
|
|
var results = new PagedResult<DeadActivity>();
|
|
var skip = (filter.Page - 1) * filter.Limit;
|
|
var activities = _context
|
|
.DeadActivities.Include(activity => activity.DeadDetail)
|
|
.Where(activity => activity.LivingActivityId == null);
|
|
IQueryable<DeadActivity> querySet = activities;
|
|
|
|
if (filter.Product != null && filter.Product.Any())
|
|
{
|
|
var products = filter.Product;
|
|
querySet = querySet.Where(x => products.Contains((Product)x.Product));
|
|
}
|
|
|
|
if (filter.Type != null && filter.Type.Any())
|
|
{
|
|
var types = filter.Type;
|
|
querySet = querySet.Where(x => types.Contains((ActivityType)x.Type));
|
|
}
|
|
|
|
if (filter.State != null && filter.State.Any())
|
|
{
|
|
var states = filter.State;
|
|
querySet = querySet.Where(x => states.Contains((ActivityState)x.State));
|
|
}
|
|
|
|
if (filter.DateLow != null)
|
|
{
|
|
querySet = querySet.Where(x => x.Accepted >= filter.DateLow);
|
|
}
|
|
|
|
if (filter.DateHigh != null)
|
|
{
|
|
querySet = querySet.Where(x => x.Accepted <= filter.DateHigh);
|
|
}
|
|
|
|
if (filter.DepartmentId != 0)
|
|
{
|
|
querySet = querySet.Where(x => x.DepartmentId == filter.DepartmentId);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(filter.EmployeeId))
|
|
{
|
|
querySet = querySet.Where(x => x.EmployeeId == filter.EmployeeId);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(filter.Query))
|
|
{
|
|
var query = filter.Query.ToLower().Trim();
|
|
querySet = querySet.Where(x =>
|
|
(x.DeadDetail.MemberName ?? "").ToLower().Contains(query)
|
|
|| (x.DeadDetail.AccountNumber ?? "").ToLower().Contains(query)
|
|
|| (x.DeadDetail.SubAccountNumber ?? "").ToLower().Contains(query)
|
|
|| (x.DeadDetail.SubjectPersonNbr ?? "").ToLower().Contains(query)
|
|
|| (x.DeadDetail.SubjectOrgNbr ?? "").ToLower().Contains(query)
|
|
|| (x.SourceId ?? "").ToLower() == query
|
|
|| query == x.Id.ToString());
|
|
}
|
|
|
|
querySet = SortQuerySet(querySet, filter);
|
|
results.Total = querySet.Count();
|
|
results.Items = await querySet.Skip(skip).Take(filter.Limit).ToListAsync();
|
|
return results;
|
|
}
|
|
|
|
private IQueryable<DeadActivity> SortQuerySet(IQueryable<DeadActivity> querySet, ActivityFilter filter)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(filter.SortBy))
|
|
{
|
|
return querySet
|
|
.OrderByDescending(t => t.Accepted);
|
|
}
|
|
|
|
if (filter.SortDescending.HasValue && filter.SortDescending.Value)
|
|
{
|
|
return (IQueryable<DeadActivity>)querySet.OrderByDescending<DeadActivity>(filter.SortBy);
|
|
}
|
|
|
|
return (IQueryable<DeadActivity>)querySet.OrderBy<DeadActivity>(filter.SortBy);
|
|
}
|
|
}
|