using _2021_backend.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace _2021_backend.Pages.Message { public class IndexModel : PageModel { public readonly _2021_backend.Data.BackendContext Context; [BindProperty] public string Errmsg { get; set; } [BindProperty] public string SearchString { get; set; } [BindProperty] public bool showSend { get; set; } [BindProperty] public bool showReceive { get; set; } [BindProperty] public int pageid { get; set; } public static int pageSize = 30; public List Messages { get; set; } public int PageCount { get; set; } public IndexModel(_2021_backend.Data.BackendContext context) { Context = context; } public IActionResult Construct(int PageId) { List stus; IQueryable q; if (!String.IsNullOrEmpty(SearchString)) q = from e in Context.Students where e.Name.Contains(SearchString) select e.Guid; else q = from e in Context.Students select e.Guid; stus = q.ToList(); var q2 = from e in Context.SMS where stus.Contains(e.Host) select e; if (showReceive && !showSend) q2 = from e in q2 where e.Type == SMSType.Reply select e; else if (!showReceive && showSend) q2 = from e in q2 where e.Type != SMSType.Reply select e; Messages = q2.ToList(); Messages.Sort((SMS a, SMS b) => b.SendTime.CompareTo(a.SendTime)); int cnt = Messages.Count; PageCount = (int)Math.Ceiling((double)cnt / (double)pageSize); if (PageId >= PageCount) PageId = PageCount - 1; if (PageId < 0) PageId = 0; pageid = PageId; int count = (PageId + 1) * pageSize > cnt ? (cnt - PageId * pageSize) : pageSize; Messages = Messages.GetRange(PageId * pageSize, count); return Page(); } public async Task OnGetAsync(int PageId) { return Construct(PageId); } public async Task OnPostAsync(int PageId) { return Construct(PageId); } public async void OnPostPullAsync(int PageId) { var stu = Context.Students.ToList(); foreach (var e in stu) { await Utils.TencentSMS.Pull(Context, e, true); } Construct(PageId); } public async Task OnPostSignAsync(int PageId) { var stu = Context.Students.ToList(); foreach (var e in stu) { if (e.Status == status.刚报名) await Utils.TencentSMS.Send(Context, SMSType.Signed, e, Context.Users.Find(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value)).Name); } return Construct(PageId); } public async Task OnPostResultRejectAsync(int PageId) { var stu = Context.Students.ToList(); foreach (var e in stu) { if (e.Status == status.不通过) { if (!Context.SMS.Any(it => it.Host == e.Guid && it.Type == SMSType.Reject)) await Utils.TencentSMS.Send(Context, SMSType.Reject, e, Context.Users.Find(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value)).Name); } } return Construct(PageId); } public async Task OnPostResultAccpetAsync(int PageId) { var stu = Context.Students.ToList(); foreach (var e in stu) { if (e.Status == status.通过) { if (!Context.SMS.Any(it => it.Host == e.Guid && it.Type == SMSType.Accept)) await Utils.TencentSMS.Send(Context, SMSType.Accept, e, Context.Users.Find(Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value)).Name); } } return Construct(PageId); } } }