using AngleSharp.Text; using Flandre.Core.Common; using Flandre.Core.Messaging.Segments; using Flandre.Core.Messaging; 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 int id { get; set; } public string invitedChinaname { get; set; } public schedule.team_game match { get; set; } public int status { 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; if (user.officium != "Commentator" ) return Ok(new error_mb { code = 401, message = "您不是解说无法完成邀请" }); 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); if(match == null) return Ok(new { code = 200, message = "未找到比赛" }); var data = new { userName = user.chinaname, invitedName = invited.chinaname, match }; string mesg = $"[ASG管理系统]解说{invited.chinaname}同学,您收到一条来自解说{user.chinaname}同学的解说邀约,时间为{match.opentime},请您根据自身时间安排,前往解说端确认是否接受邀请。"; string qqgroup = "925510646"; var atuserqq = invited.qqnumber; if (atuserqq == null) return Ok(new { code = 500, message = "服务器错误" }); var message = new MessageBuilder().Add(new AtSegment(atuserqq)).Text(mesg).Build(); try { await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, qqgroup, null, message, qqgroup); return Ok(new { code = 200, message = "成功发送邀请", data }); } catch { return Ok(new error_mb { code = 500, message = "机器人错误" }); } } return Ok(new { code = 500, 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 = 0; 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 theGame = sub.team_Games.FirstOrDefault(i => i.id == record.match_id); if (theGame == null) continue; var addData = new InviteForUsers { id = record.id, invitedChinaname = invited.chinaname, match = theGame, create_time = record.create_time, status = record.status, }; invitationRecord.Add(addData); TotalRecords++; } 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 int id { get; set; } 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 = 0; 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 theGame = sub.team_Games.FirstOrDefault(i => i.id == record.match_id); if (invitor == null) continue; if (theGame == null || theGame.belong == null) continue; var addData = new BeInviteForUsers { id = record.id, invitorChinaname = invitor.chinaname, match = theGame, create_time = record.create_time, }; invitationRecord.Add(addData); TotalRecords++; } 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 }); } } } [Route("api/v1/delRecord")] [HttpDelete] [Authorize] public async Task> DelAssign([FromQuery] int id) { using (TestDbContext sub = new TestDbContext()) { try { string Myid = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var user = await userManager.FindByIdAsync(Myid); long userId = user.Id; var query = sub.T_Invitation.Where(n => n.id == id).FirstOrDefault(); if(query.user_id != (int)userId) return Ok(new error_mb { code = 200, message = "非法操作!" }); sub.T_Invitation.Remove(query); await sub.SaveChangesAsync(); return Ok(new error_mb { code = 200, message = "成功删除" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", details = ex }); } } } } }