201 lines
9.7 KiB
C#
201 lines
9.7 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using HRM.BO;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace HRM.DA
|
|
{
|
|
/// <summary>
|
|
/// Server side of <app-virtual-grid>: turns any List<T> into one window of rows (search, sort,
|
|
/// skip/take, total count). A page endpoint calls it with its existing service method:
|
|
///
|
|
/// [HttpPost("getDiscontinueEmployeePage")]
|
|
/// public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request)
|
|
/// {
|
|
/// CurrentUser user = CurrentUser.GetCurrentUser(HttpContext.User);
|
|
/// DateTime fromDate = request.GetDate("fromDate", "From Date");
|
|
/// DateTime toDate = request.GetDate("toDate", "To Date");
|
|
/// return Ok(VirtualPaging.Page(request, user,
|
|
/// () => _employeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, user.PayrollTypeID.Value)));
|
|
/// }
|
|
///
|
|
/// The list is loaded on Preview (request.Refresh) and kept in memory for a few minutes, per user,
|
|
/// endpoint and filters, so scrolling, sorting, searching and Export to Excel do not call the
|
|
/// service again. See README.md next to this file.
|
|
/// </summary>
|
|
public static class VirtualPaging
|
|
{
|
|
private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); // max rows kept, all users together
|
|
private static readonly TimeSpan KeepFor = TimeSpan.FromMinutes(10); // since last use
|
|
private static readonly TimeSpan KeepAtMost = TimeSpan.FromMinutes(30); // since loaded
|
|
private const int MaxRowsPerPage = 1000;
|
|
|
|
public static VirtualPageResult Page<T>(VirtualPageRequest request, CurrentUser user, Func<List<T>> loadData,
|
|
[CallerFilePath] string callerFile = "", [CallerMemberName] string callerName = "") where T : class
|
|
{
|
|
if (request == null)
|
|
throw new ArgumentException("Invalid request.");
|
|
|
|
// 1. The list: from memory, or from the service on Preview / when it has expired.
|
|
string key = CacheKey(request, user, callerFile + "|" + callerName + "|" + typeof(T).FullName);
|
|
KeptList<T> kept;
|
|
if (request.Refresh || !Cache.TryGetValue(key, out kept))
|
|
{
|
|
kept = new KeptList<T>(loadData() ?? new List<T>());
|
|
Cache.Set(key, kept, new MemoryCacheEntryOptions
|
|
{
|
|
Size = Math.Max(1, kept.Items.Count),
|
|
SlidingExpiration = KeepFor,
|
|
AbsoluteExpirationRelativeToNow = KeepAtMost,
|
|
});
|
|
}
|
|
|
|
List<PropertyInfo> fields = GetFields<T>(request.Fields);
|
|
IEnumerable<int> rows = Enumerable.Range(0, kept.Items.Count);
|
|
|
|
// 2. Search: the text the grid shows, in any of the grid's columns.
|
|
string search = (request.Search ?? "").Trim();
|
|
if (search.Length > 0)
|
|
{
|
|
List<string> texts = kept.SearchTexts(fields);
|
|
rows = rows.Where(i => texts[i].IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0);
|
|
}
|
|
List<int> matched = rows.ToList();
|
|
|
|
// 3. Sort: only by one of the grid's columns.
|
|
PropertyInfo sortBy = fields.FirstOrDefault(p => string.Equals(p.Name, request.SortField, StringComparison.OrdinalIgnoreCase));
|
|
if (sortBy != null)
|
|
{
|
|
bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase);
|
|
matched = desc
|
|
? matched.OrderByDescending(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList()
|
|
: matched.OrderBy(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList();
|
|
}
|
|
|
|
// 4. The window: Take 0 = every row (Export to Excel).
|
|
int skip = Math.Max(0, request.Skip);
|
|
int take = request.Take <= 0 ? matched.Count : Math.Min(request.Take, MaxRowsPerPage);
|
|
|
|
var result = new VirtualPageResult { Total = matched.Count, TotalWithoutSearch = kept.Items.Count };
|
|
foreach (int i in matched.Skip(skip).Take(take))
|
|
{
|
|
var row = new Dictionary<string, object>(fields.Count);
|
|
foreach (PropertyInfo p in fields)
|
|
row[CamelCase(p.Name)] = p.GetValue(kept.Items[i]);
|
|
result.Data.Add(row);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#region Helpers
|
|
|
|
/// <summary>A loaded list, plus the search text of each row (worked out once, on the first search).</summary>
|
|
private sealed class KeptList<T>
|
|
{
|
|
private readonly ConcurrentDictionary<string, List<string>> _searchTexts = new ConcurrentDictionary<string, List<string>>();
|
|
|
|
public KeptList(List<T> items)
|
|
{
|
|
Items = items.Where(x => x != null).ToList();
|
|
}
|
|
|
|
public List<T> Items { get; }
|
|
|
|
public List<string> SearchTexts(List<PropertyInfo> fields)
|
|
{
|
|
string key = string.Join(",", fields.Select(p => p.Name));
|
|
return _searchTexts.GetOrAdd(key, _ => Items
|
|
.Select(item => string.Join("\u0001", fields.Select(p => DisplayText(p.GetValue(item)))))
|
|
.ToList());
|
|
}
|
|
}
|
|
|
|
/// <summary>The requested fields that are readable properties of T (unknown names are ignored). None requested = all.</summary>
|
|
private static List<PropertyInfo> GetFields<T>(List<string> names)
|
|
{
|
|
// When a class hides a base property with "new", keep the most derived one.
|
|
var properties = new Dictionary<string, PropertyInfo>(StringComparer.OrdinalIgnoreCase);
|
|
for (Type t = typeof(T); t != null && t != typeof(object); t = t.BaseType)
|
|
{
|
|
foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
|
|
{
|
|
if (p.CanRead && p.GetIndexParameters().Length == 0 && !properties.ContainsKey(p.Name))
|
|
properties[p.Name] = p;
|
|
}
|
|
}
|
|
|
|
if (names == null || names.Count == 0)
|
|
return properties.Values.ToList();
|
|
|
|
var result = new List<PropertyInfo>();
|
|
foreach (string name in names.Distinct(StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
PropertyInfo p;
|
|
if (name != null && properties.TryGetValue(name, out p))
|
|
result.Add(p);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>The text the grid shows for a value: dates "dd MMM yyyy", decimals "1,234.56".</summary>
|
|
private static string DisplayText(object value)
|
|
{
|
|
if (value == null)
|
|
return string.Empty;
|
|
if (value is DateTime d)
|
|
return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture);
|
|
if (value is decimal || value is double || value is float)
|
|
{
|
|
decimal number = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
|
|
return number.ToString("N2", CultureInfo.InvariantCulture) + "\u0001" + number.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
if (value is Enum)
|
|
return value + "\u0001" + Convert.ToInt32(value, CultureInfo.InvariantCulture);
|
|
return Convert.ToString(value, CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
private static string CacheKey(VirtualPageRequest request, CurrentUser user, string endpoint)
|
|
{
|
|
string filters = string.Join("&", (request.Filters ?? new Dictionary<string, string>())
|
|
.OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase)
|
|
.Select(f => f.Key.ToLowerInvariant() + "=" + f.Value));
|
|
return endpoint + "|" + user?.UserID + "|" + user?.PayrollTypeID + "|" + filters;
|
|
}
|
|
|
|
private static string CamelCase(string name) =>
|
|
string.IsNullOrEmpty(name) ? name : char.ToLowerInvariant(name[0]) + name.Substring(1);
|
|
|
|
/// <summary>Empty values first; numbers, dates and text compared as such (text ignoring case).</summary>
|
|
private sealed class ValueComparer : IComparer<object>
|
|
{
|
|
public static readonly ValueComparer Instance = new ValueComparer();
|
|
|
|
public int Compare(object x, object y)
|
|
{
|
|
bool xEmpty = x == null || (x is string xs && xs.Length == 0);
|
|
bool yEmpty = y == null || (y is string ys && ys.Length == 0);
|
|
if (xEmpty || yEmpty)
|
|
return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1);
|
|
if (x is string a && y is string b)
|
|
return StringComparer.InvariantCultureIgnoreCase.Compare(a, b);
|
|
if (IsNumber(x) && IsNumber(y))
|
|
return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture));
|
|
if (x.GetType() == y.GetType() && x is IComparable comparable)
|
|
return comparable.CompareTo(y);
|
|
return StringComparer.InvariantCultureIgnoreCase.Compare(
|
|
Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
private static bool IsNumber(object v) =>
|
|
v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|