HRM-10/HRM.BO/VirtualPaging/VirtualPaging.cs
2026-11-09 17:22:45 +06:00

125 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
namespace HRM.BO
{
/// <summary>
/// What &lt;app-virtual-grid&gt; sends to a page endpoint: the page's own filters plus the grid's columns,
/// scroll position, search text and sort. The same class is used by every page, so no page needs a
/// request class of its own. See HRM.DA/DA/VirtualPaging/README.md.
/// </summary>
public class VirtualPageRequest
{
/// <summary>The page's filters, e.g. { fromDate: "2025-01-01", toDate: "2025-12-31" }. Read them with GetDate / GetInt / GetString.</summary>
public Dictionary<string, string> Filters { get; set; } = new Dictionary<string, string>();
/// <summary>The grid's columns. Only these are selected, searched and sortable.</summary>
public List<VirtualPageColumn> Columns { get; set; } = new List<VirtualPageColumn>();
public int Skip { get; set; }
/// <summary>Rows wanted. 0 = all rows (used by Export to Excel).</summary>
public int Take { get; set; } = 100;
public string Search { get; set; }
public string SortField { get; set; }
/// <summary>"asc" or "desc".</summary>
public string SortDir { get; set; }
/// <summary>
/// True when the totals must be counted (Preview, new search). Scrolling and sorting do not change
/// the totals, so they send false and the COUNT is skipped.
/// </summary>
public bool CountTotal { get; set; }
/// <summary>
/// True for Export to Excel: every row matching the search, in the grid's sort, comes back as an .xlsx
/// file (VirtualPageResult.ExcelFile) instead of JSON.
/// </summary>
public bool ExportExcel { get; set; }
/// <summary>A filter as text, or null when it was not sent.</summary>
public string GetString(string name)
{
string value;
if (Filters != null && Filters.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value))
return value.Trim();
return null;
}
/// <summary>A "yyyy-MM-dd" filter. Throws a message fit for the user when it is missing or invalid.</summary>
public DateTime GetDate(string name, string label = null)
{
DateTime value;
if (!DateTime.TryParseExact(GetString(name), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
/// <summary>
/// A comma-separated list of whole numbers (e.g. selected employee IDs "12,15,40"), without duplicates.
/// Throws a message fit for the user when it is missing, empty or holds anything but numbers.
/// </summary>
public List<int> GetIntList(string name, string label = null)
{
var values = new List<int>();
var seen = new HashSet<int>();
foreach (string part in (GetString(name) ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
int value;
if (!int.TryParse(part.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
if (seen.Add(value))
values.Add(value);
}
if (values.Count == 0)
throw new ArgumentException("Please select " + (label ?? name) + ".");
return values;
}
/// <summary>A whole-number filter. Throws a message fit for the user when it is missing or invalid.</summary>
public int GetInt(string name, string label = null)
{
int value;
if (!int.TryParse(GetString(name), NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
}
/// <summary>One grid column: Field = a column alias of the page's query; Type = how it is shown.</summary>
public class VirtualPageColumn
{
public string Field { get; set; }
/// <summary>"text" (default), "number", "money", "date" or "datetime".</summary>
public string Type { get; set; }
/// <summary>Header text in the Excel file.</summary>
public string Title { get; set; }
/// <summary>Column width in the grid (px); the Excel column gets a matching width.</summary>
public int Width { get; set; }
}
/// <summary>One window of rows for the grid.</summary>
public class VirtualPageResult
{
/// <summary>Rows matching the search (all rows when there is no search). Null when not counted (CountTotal = false).</summary>
public int? Total { get; set; }
/// <summary>All rows, ignoring the search. Null when not counted.</summary>
public int? TotalWithoutSearch { get; set; }
/// <summary>The rows of this window, holding only the requested columns.</summary>
public List<Dictionary<string, object>> Data { get; set; } = new List<Dictionary<string, object>>();
/// <summary>The .xlsx file, set only for ExportExcel requests. The controller returns it as a file.</summary>
public byte[] ExcelFile { get; set; }
public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
}