using AngleSharp.Text; using Manganese.Array; using Masuit.Tools; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.EntityFrameworkCore; using System; using System.Security.Claims; using System.Text.Json; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; namespace asg_form.Controllers { public class InviteReferee : ControllerBase { private readonly RoleManager roleManager; private readonly UserManager userManager; public InviteReferee( RoleManager roleManager, UserManager userManager) { this.roleManager = roleManager; this.userManager = userManager; } public class idRequest { public int invited_id { get; set; } public int match_id { get; set; } } public class InviteBg { public int id { get; set; } public int user_id { get; set; } public int invited_id { get; set; } public int match_id { get; set; } public int status { get; set; } public string? create_time { get; set; } } public class InviteForUsers { public string invitedChinaname { get; set; } public schedule.team_game match { get; set; } public string? create_time { get; set; } } public class inv { public int invitedId { get; set; } public int matchId { get; set; } } [Route("api/v1/Invite")] [HttpPost] [Authorize] public async Task> Toinvite([FromBody] inv msg) { string Invitorid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Invitorid); long userId = user.Id; using (TestDbContext sb = new TestDbContext()) { try { var invited = await userManager.Users.FirstOrDefaultAsync(u => u.Id == msg.invitedId); if (invited != null) return Ok(new { code = 404, message = "用户未找到" }); var isInvited = await sb.T_Invitation.AnyAsync(i => i.user_id == userId && i.invited_id==msg.invitedId && i.match_id==msg.matchId); if (!isInvited) { string createTime = DateTime.Now.ToString(); var invitation = new InviteBg { user_id = (int)userId, invited_id = msg.invitedId, match_id = msg.matchId, status = 0, create_time = createTime, }; sb.Add(invitation); sb.SaveChanges(); var match = sb.team_Games.FirstOrDefault(n => n.id == msg.matchId); var data = new { userName = user.chinaname, invitedName = invited.chinaname, match }; return Ok(new { code = 200, message = "成功发送邀请" ,data}); } return Ok(new { code = 200, message = "已经在本场次邀请过这个人" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } [Route("api/v1/myInvitation")] [HttpGet] [Authorize] public async Task> GetMyInvitor([FromQuery]short page = 1, short limit = 6) { string Forid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Forid); long userId = user.Id; int wrongPart = -1; using (TestDbContext sub = new TestDbContext()) { try { var query = sub.T_Invitation .AsQueryable(); var TotalRecords = await query.CountAsync(); var invitationRecordId = query .Where(x => x.user_id == userId) .OrderByDescending(t => t.status) .ThenBy(t => t.create_time) .Skip((page - 1) * limit) .Take(limit) .ToList(); List invitationRecord = new List(); foreach (var record in invitationRecordId) { var invited = await userManager.Users.FirstOrDefaultAsync(u => u.Id == record.invited_id); var addData = new InviteForUsers { invitedChinaname = invited.chinaname, match = sub.team_Games.FirstOrDefault(i => i.id == record.match_id), create_time = record.create_time, }; } var data = new { rows = invitationRecord, total = TotalRecords, }; return Ok(new { code = 200, message = "邀请记录如下", data }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } public class BeInviteForUsers { public string invitorChinaname { get; set; } public schedule.team_game match { get; set; } public string? create_time { get; set; } } [Route("api/v1/toInvite")] [HttpGet] [Authorize] public async Task> ToInvite([FromQuery]short page = 1, short limit = 6) { string Myid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Myid); long userId = user.Id; int wrongPart = -1; using (TestDbContext sub = new TestDbContext()) { try { var query = sub.T_Invitation .AsQueryable(); var TotalRecords = await query.CountAsync(); var invitationRecordId = query .Where(x => x.invited_id == userId && x.status==0) .OrderBy(t => t.create_time) .Skip((page - 1) * limit) .Take(limit) .ToList(); List invitationRecord = new List(); foreach (var record in invitationRecordId) { var invitor = await userManager.Users.FirstOrDefaultAsync(u => u.Id == record.user_id); var addData = new BeInviteForUsers { invitorChinaname = invitor.chinaname, match = sub.team_Games.FirstOrDefault(i => i.id == record.match_id), create_time = record.create_time, }; } var data = new { rows = invitationRecord, total = TotalRecords, }; return Ok(new { code = 200, message = "收到的未处理的邀请如下", data }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } public class msg2 { public int id { get; set; } public int status { get; set; } } [Route("api/v1/recordAgreeOrDisagree")] [HttpPost] [Authorize] public async Task> PassAssign([FromBody] msg2 askMsg) { using (TestDbContext sub = new TestDbContext()) { try { var query = sub.T_Invitation.FirstOrDefault(n => n.id == askMsg.id); if (query != null) { string message = "无效操作"; query.status = askMsg.status; if(askMsg.status == 1) { message = "你同意了邀请,记得及时到位哦~"; }else if (askMsg.status == 2) { message = "你拒绝了邀请"; } sub.SaveChanges(); return Ok(new { code = 200, message}); } return Ok(new { code = 404, message = "没有对应记录" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } } }