using _2021_backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using System; using System.IO.Compression; using System.Collections.Generic; using System.Linq; using _2021_backend.Utils; using System.Threading.Tasks; namespace _2021_backend.Pages.Students { public class IndexModel : PageModel { private readonly _2021_backend.Data.BackendContext Context; public static int PageSize { get; set; } public int PageCount { get; set; } public int PageId { get; set; } public string errmsg { get; set; } [BindProperty(SupportsGet = true)] public string SearchString { get; set; } [BindProperty(SupportsGet = true)] public grade Grade { get; set; } [BindProperty(SupportsGet = true)] public string SearchQQ { get; set; } [BindProperty(SupportsGet = true)] public string SearchTel { get; set; } [BindProperty(SupportsGet = true)] public status StatusSel { get; set; } = status.暂缺; public IndexModel(_2021_backend.Data.BackendContext context) { Context = context; } public List Student { get; set; } public IActionResult Construct(int pageId, string errInfo) { PageSize = 30; if (string.IsNullOrEmpty(errInfo)) { errmsg = ""; } else { errmsg = errInfo; } if (pageId == null) pageId = 0; IQueryable q; if (!string.IsNullOrEmpty(SearchString)) { q = Context.Students.Where(s => (s.Name.Contains(SearchString) || s.Stuid.Contains(SearchString))); } else { q = Context.Students; } if (Grade != grade.暂缺) q = q.Where(it => it.Grade == Grade); if (StatusSel != status.暂缺) q = q.Where(it => it.Status == StatusSel); if (!String.IsNullOrEmpty(SearchTel)) q = q.Where(it => it.Tel.Contains(SearchTel)); if (!String.IsNullOrEmpty(SearchQQ)) q = q.Where(it => it.Email.Contains(SearchQQ)); Student = q.ToList(); Student.Sort((Student a, Student b) => { return -a.RegisterTime.CompareTo(b.RegisterTime); }); int cnt = Student.Count; PageCount = (int)Math.Ceiling((double)cnt / (double)PageSize); if (pageId >= PageCount) pageId = PageCount - 1; if (pageId < 0) pageId = 0; PageId = (int)pageId; int count = (PageId + 1) * PageSize > cnt ? (cnt - PageId * PageSize) : PageSize; Student = Student.GetRange(PageId * PageSize, count); return Page(); } public async Task OnGetAsync(int? pageId, string errInfo) { if (pageId == null) pageId = 0; return Construct((int)pageId, errInfo); } public async Task OnPostAsync(int? pageId, string errInfo) { if (pageId == null) pageId = 0; return Construct((int)pageId, errInfo); } } }