JPHD-2021-backend/Pages/Students/Index.cshtml.cs

111 lines
3.7 KiB
C#

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> 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<Student> 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<IActionResult> OnGetAsync(int? pageId, string errInfo)
{
if (pageId == null) pageId = 0;
return Construct((int)pageId, errInfo);
}
public async Task<IActionResult> OnPostAsync(int? pageId, string errInfo)
{
if (pageId == null) pageId = 0;
return Construct((int)pageId, errInfo);
}
public async Task<IActionResult> OnPostFinalizeAsync(int? pageId, string errInfo)
{
if (pageId == null) pageId = 0;
foreach(var s in Context.Students)
{
if (s.RegisterTime.CompareTo(new DateTime(2021, 11, 11)) > 0) s.Status = status.;
else if (s.Status == status.) s.Status = status.;
}
Context.SaveChanges();
return Construct((int)pageId, errInfo);
}
}
}