using Manganese.Text; using Masuit.Tools; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Buffers.Text; using System.Data; using System.Security.Claims; using static asg_form.blog; namespace asg_form.Controllers { public class user_form : ControllerBase { private readonly RoleManager roleManager; private readonly UserManager userManager; public user_form( RoleManager roleManager, UserManager userManager) { this.roleManager = roleManager; this.userManager = userManager; } /// /// 绑定表单 /// /// [Authorize] [Route("api/v1/user/uploadvideo")] [HttpPost] public async Task> Upvideo([FromForm]IFormFile file) { string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var ouser = userManager.Users.Include(a => a.haveform.role).Include(a => a.haveform.events).FirstOrDefault(a => a.Id == id.ToInt64()); if (ouser.haveform == null) { return BadRequest(new error_mb { code = 400, message = "你没有绑定表单" }); } if (file == null || file.Length == 0) return Content("file not selected"); var path = Path.Combine( Directory.GetCurrentDirectory(), file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } return RedirectToAction("Index"); } /// /// 绑定表单 /// /// 表单名称 /// 表单密码 /// [Authorize] [Route("api/v1/user/form")] [HttpPost] public async Task> Addform(string formname,string eventname, string formpassword) { string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var ouser = userManager.FindByIdAsync(id).Result; TestDbContext db = new TestDbContext(); form formok; try { formok = await db.Forms.Include(a=>a.events).FirstAsync(a => a.team_name == formname&&a.events.name==eventname); } catch { return NotFound(new error_mb { code = 404, message = "表单不存在" }); } if (ouser.haveform != null) { return BadRequest(new error_mb { code = 400, message = "无法多次绑定表单" }); } if(formok.team_password==formpassword) { ouser.haveform = await db.Forms.Include(a => a.events).FirstAsync(a => a.team_name == formname && a.events.name == eventname); await userManager.UpdateAsync(ouser); return "绑定成功"; } else { return BadRequest(new error_mb { code = 400, message = "表单密码错误" }); } } /// /// 获取我的表单 /// /// [Authorize] [Route("api/v1/user/form")] [HttpGet] public async Task> getmyform() { try { string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var ouser = userManager.Users.Include(a=>a.haveform.events).Include(a => a.haveform.role).FirstOrDefault(a => a.Id == id.ToInt64()); foreach (var role in ouser.haveform.role) { role.form = null; } ouser.haveform.events.forms=null; return Ok(ouser.haveform); } catch { return BadRequest(new error_mb { code = 400, message = "你没有绑定表单" }); } } /// /// 修改我的表单 /// /// [Authorize] [Route("api/v1/user/form")] [HttpPut] public async Task> putmyform([FromBody]form_get form) { string id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; var ouser = userManager.Users.Include(a => a.haveform.role).FirstOrDefault(a => a.Id == id.ToInt64()); if (ouser.haveform != null) { TestDbContext db = new TestDbContext(); var forme = ouser.haveform; if (forme == null) { return BadRequest(new error_mb { code = 400, message = "表单不存在" }); } else { //只能修改表单role ouser.haveform.role.Clear(); foreach (role_get a in form.role_get) { ouser.haveform.role.Add(new role { role_id = a.role_id, role_lin = a.role_lin, role_name = a.role_name,Common_Roles=a.Common_Roles,Game_Name=a.Game_Name,Id_Card=a.Id_Card,Id_Card_Name=a.Id_Card_Name,Phone_Number=a.Phone_Number,Historical_Ranks=a.Historical_Ranks }); } await userManager.UpdateAsync(ouser); return Ok("修改完成"); } } else { return BadRequest(new error_mb { code = 400, message = "你没有绑定表单" }); } } public class userRanking { public long userId { get; set; } public string username { get; set; } public string joinTime { get; set; } public string base64 { get; set; } public int count { get; set; } } /// /// 解说排行榜 /// /// [Route("api/v1/user/ranking")] [HttpGet] public async Task> rankingList(short page = 1, short limit = 10) { try { DateTime currentTime = DateTime.Now; var rankings = userManager.Users .Where(u => u.joinTime != null && u.officium == "Commentator") .AsEnumerable() .OrderByDescending(u => (currentTime - u.joinTime.ToDateTime()).TotalSeconds) .Skip((page - 1) * limit) .Take(limit) .Select(u => new userRanking { userId = u.Id, username = u.chinaname, joinTime = u.joinTime, base64 = u.UserBase64, //count = (int)((currentTime - u.joinTime.ToDateTime()).TotalSeconds/ 86400), }) .ToList(); return Ok(new { code = 200, message = "", rankings }); } catch (Exception ex) { return StatusCode(500, new { code = 500, message = "获取排行榜失败", error = ex.Message }); } } } }