asg_backend/asg_form/Controllers/InviteReferee.cs
2025-01-27 20:32:23 +08:00

308 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
[ApiController]
public class InviteReferee : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public InviteReferee(
RoleManager<Role> roleManager, UserManager<User> 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<ActionResult<object>> 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();
await runbot.runbotr.SendMessageAsync(MessageEnvironment.Channel, qqgroup, null, message, qqgroup);
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<ActionResult<object>> 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<InviteForUsers> invitationRecord = new List<InviteForUsers>();
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<ActionResult<object>> 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<BeInviteForUsers> invitationRecord = new List<BeInviteForUsers>();
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) return Ok(new { code = 200, message = "被邀请者不存在了" });
if (theGame == null) return Ok(new { code = 200, message = "没有比赛资料" });
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<ActionResult<object>> 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<ActionResult<object>> 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 });
}
}
}
}
}