using System.ComponentModel.DataAnnotations; namespace KH.WMS.Core.Api.Responses; /// /// 分页请求参数 /// public class Pagination { private int _pageIndex = 1; private int _pageSize = 20; /// /// 页码(从1开始) /// [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] public int PageIndex { get => _pageIndex; set => _pageIndex = value < 1 ? 1 : value; } /// /// 每页数量 /// [Range(1, 100, ErrorMessage = "每页数量必须在1-100之间")] public int PageSize { get => _pageSize; set => _pageSize = value < 1 ? 20 : value > 100 ? 100 : value; } /// /// 排序字段 /// public string? SortField { get; set; } /// /// 排序方向(asc/desc) /// public string SortDirection { get; set; } = "asc"; /// /// 计算跳过数量 /// public int Skip => (PageIndex - 1) * PageSize; /// /// 获取数量 /// public int Take => PageSize; /// /// 创建分页参数 /// public static Pagination Create(int pageIndex = 1, int pageSize = 20, string? sortField = null, string sortDirection = "asc") { return new Pagination { PageIndex = pageIndex, PageSize = pageSize, SortField = sortField, SortDirection = sortDirection }; } } /// /// 分页结果 /// public class PagedResult { /// /// 数据列表 /// public List Items { get; set; } = new(); /// /// 总记录数 /// public int Total { get; set; } /// /// 当前页码 /// public int PageIndex { get; set; } /// /// 每页数量 /// public int PageSize { get; set; } /// /// 总页数 /// public int PageCount => PageSize > 0 ? (int)Math.Ceiling((double)Total / PageSize) : 0; /// /// 是否有上一页 /// public bool HasPrevious => PageIndex > 1; /// /// 是否有下一页 /// public bool HasNext => PageIndex < PageCount; /// /// 创建分页结果 /// public static PagedResult Create(List items, int total, int pageIndex, int pageSize) { return new PagedResult { Items = items, Total = total, PageIndex = pageIndex, PageSize = pageSize }; } /// /// 创建空分页结果 /// public static PagedResult Empty(int pageIndex = 1, int pageSize = 20) { return new PagedResult { Items = new List(), Total = 0, PageIndex = pageIndex, PageSize = pageSize }; } } /// /// 分页响应包装 /// public class PagedResponse : ApiResponse> { /// /// 创建成功分页响应 /// public static PagedResponse Ok(List items, int total, int pageIndex, int pageSize, string message = "查询成功") { return new PagedResponse { Code = "200", Message = message, Data = PagedResult.Create(items, total, pageIndex, pageSize) }; } /// /// 创建空分页响应 /// public static PagedResponse Empty(int pageIndex = 1, int pageSize = 20, string message = "暂无数据") { return new PagedResponse { Code = "200", Message = message, Data = PagedResult.Empty(pageIndex, pageSize) }; } }