feat: 完善用户界面,添加绑定与解除绑定的功能

This commit is contained in:
2025-07-12 17:46:23 +08:00
parent 0fb0c4e395
commit f253a33c83
11 changed files with 1654 additions and 185 deletions

View File

@@ -46,6 +46,12 @@ public class User
[Nullable]
public Guid BoardID { get; set; }
/// <summary>
/// 用户绑定板子的过期时间
/// </summary>
[Nullable]
public DateTime? BoardExpireTime { get; set; }
/// <summary>
/// 用户权限枚举
/// </summary>
@@ -98,6 +104,18 @@ public class Board
[NotNull]
public required BoardStatus Status { get; set; }
/// <summary>
/// 占用该板子的用户的唯一标识符
/// </summary>
[Nullable]
public Guid OccupiedUserID { get; set; }
/// <summary>
/// 占用该板子的用户的用户名
/// </summary>
[Nullable]
public string? OccupiedUserName { get; set; }
/// <summary>
/// FPGA 板子的固件版本号
/// </summary>
@@ -210,7 +228,7 @@ public class AppDataConnection : DataConnection
/// <returns>包含用户信息的结果,如果未找到或出错则返回相应状态</returns>
public Result<Optional<User>> GetUserByName(string name)
{
var user = this.User.Where((user) => user.Name == name).ToArray();
var user = this.UserTable.Where((user) => user.Name == name).ToArray();
if (user.Length > 1)
{
@@ -235,7 +253,7 @@ public class AppDataConnection : DataConnection
/// <returns>包含用户信息的结果,如果未找到或出错则返回相应状态</returns>
public Result<Optional<User>> GetUserByEMail(string email)
{
var user = this.User.Where((user) => user.EMail == email).ToArray();
var user = this.UserTable.Where((user) => user.EMail == email).ToArray();
if (user.Length > 1)
{
@@ -270,12 +288,12 @@ public class AppDataConnection : DataConnection
var user = ret.Value.Value;
if (user.Password == password)
if (user.Password == password)
{
logger.Info($"用户 {name} 密码验证成功");
return new(user);
}
else
else
{
logger.Warn($"用户 {name} 密码验证失败");
return new(Optional<User>.None);
@@ -287,15 +305,70 @@ public class AppDataConnection : DataConnection
/// </summary>
/// <param name="userId">用户的唯一标识符</param>
/// <param name="boardId">实验板的唯一标识符</param>
/// <param name="expireTime">绑定过期时间</param>
/// <returns>更新的记录数</returns>
public int BindUserToBoard(Guid userId, Guid boardId)
public int BindUserToBoard(Guid userId, Guid boardId, DateTime expireTime)
{
var result = this.User
// 获取用户信息
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
if (user == null)
{
logger.Error($"未找到用户: {userId}");
return 0;
}
// 更新用户的板子绑定信息
var userResult = this.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, boardId)
.Set(u => u.BoardExpireTime, expireTime)
.Update();
logger.Info($"用户 {userId} 已绑定到实验板 {boardId}");
return result;
// 更新板子的用户绑定信息
var boardResult = this.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.Status, Board.BoardStatus.Busy)
.Set(b => b.OccupiedUserID, userId)
.Set(b => b.OccupiedUserName, user.Name)
.Update();
logger.Info($"用户 {userId} ({user.Name}) 已绑定到实验板 {boardId},过期时间: {expireTime}");
return userResult + boardResult;
}
/// <summary>
/// 解除用户与实验板的绑定
/// </summary>
/// <param name="userId">用户的唯一标识符</param>
/// <returns>更新的记录数</returns>
public int UnbindUserFromBoard(Guid userId)
{
// 获取用户当前绑定的板子ID
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
Guid boardId = user?.BoardID ?? Guid.Empty;
// 清空用户的板子绑定信息
var userResult = this.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
// 如果用户原本绑定了板子,则清空板子的用户绑定信息
int boardResult = 0;
if (boardId != Guid.Empty)
{
boardResult = this.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.Status, Board.BoardStatus.Available)
.Set(b => b.OccupiedUserID, Guid.Empty)
.Set(b => b.OccupiedUserName, (string?)null)
.Update();
logger.Info($"实验板 {boardId} 状态已设置为空闲,用户绑定信息已清空");
}
logger.Info($"用户 {userId} 已解除实验板绑定");
return userResult + boardResult;
}
/// <summary>
@@ -326,7 +399,26 @@ public class AppDataConnection : DataConnection
/// <returns>删除的记录数</returns>
public int DeleteBoardByName(string name)
{
var result = this.Board.Where(board => board.BoardName == name).Delete();
// 先获取要删除的板子信息
var board = this.BoardTable.Where(b => b.BoardName == name).FirstOrDefault();
if (board == null)
{
logger.Warn($"未找到名称为 {name} 的实验板");
return 0;
}
// 如果板子被占用,先解除绑定
if (board.OccupiedUserID != Guid.Empty)
{
this.UserTable
.Where(u => u.ID == board.OccupiedUserID)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
logger.Info($"已解除用户 {board.OccupiedUserID} 与实验板 {name} 的绑定");
}
var result = this.BoardTable.Where(b => b.BoardName == name).Delete();
logger.Info($"实验板已删除: {name},删除记录数: {result}");
return result;
}
@@ -338,18 +430,62 @@ public class AppDataConnection : DataConnection
/// <returns>删除的记录数</returns>
public int DeleteBoardByID(Guid id)
{
var result = this.Board.Where(board => board.ID == id).Delete();
// 先获取要删除的板子信息
var board = this.BoardTable.Where(b => b.ID == id).FirstOrDefault();
if (board == null)
{
logger.Warn($"未找到ID为 {id} 的实验板");
return 0;
}
// 如果板子被占用,先解除绑定
if (board.OccupiedUserID != Guid.Empty)
{
this.UserTable
.Where(u => u.ID == board.OccupiedUserID)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
logger.Info($"已解除用户 {board.OccupiedUserID} 与实验板 {id} 的绑定");
}
var result = this.BoardTable.Where(b => b.ID == id).Delete();
logger.Info($"实验板已删除: {id},删除记录数: {result}");
return result;
}
/// <summary>
/// 根据实验板ID获取实验板信息
/// </summary>
/// <param name="id">实验板的唯一标识符</param>
/// <returns>包含实验板信息的结果,如果未找到则返回空</returns>
public Result<Optional<Board>> GetBoardByID(Guid id)
{
var boards = this.BoardTable.Where(board => board.ID == id).ToArray();
if (boards.Length > 1)
{
logger.Error($"数据库中存在多个相同ID的实验板: {id}");
return new(new Exception($"数据库中存在多个相同ID的实验板: {id}"));
}
if (boards.Length == 0)
{
logger.Info($"未找到ID对应的实验板: {id}");
return new(Optional<Board>.None);
}
logger.Debug($"成功获取实验板信息: {id}");
return new(boards[0]);
}
/// <summary>
/// 获取所有实验板信息
/// </summary>
/// <returns>所有实验板的数组</returns>
public Board[] GetAllBoard()
{
var boards = this.Board.ToArray();
var boards = this.BoardTable.ToArray();
logger.Debug($"获取所有实验板,共 {boards.Length} 块");
return boards;
}
@@ -357,10 +493,12 @@ public class AppDataConnection : DataConnection
/// <summary>
/// 获取一块可用的实验板并将其状态设置为繁忙
/// </summary>
/// <param name="userId">要分配板子的用户ID</param>
/// <param name="expireTime">绑定过期时间</param>
/// <returns>可用的实验板,如果没有可用的板子则返回空</returns>
public Optional<Board> GetAvailableBoard()
public Optional<Board> GetAvailableBoard(Guid userId, DateTime expireTime)
{
var boards = this.Board.Where(
var boards = this.BoardTable.Where(
(board) => board.Status == Database.Board.BoardStatus.Available
).ToArray();
@@ -372,12 +510,34 @@ public class AppDataConnection : DataConnection
else
{
var board = boards[0];
board.Status = Database.Board.BoardStatus.Busy;
this.Board
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
if (user == null)
{
logger.Error($"未找到用户: {userId}");
return new(null);
}
// 更新板子状态和用户绑定信息
this.BoardTable
.Where(target => target.ID == board.ID)
.Set(target => target.Status, board.Status)
.Set(target => target.Status, Board.BoardStatus.Busy)
.Set(target => target.OccupiedUserID, userId)
.Set(target => target.OccupiedUserName, user.Name)
.Update();
logger.Info($"实验板 {board.BoardName} ({board.ID}) 已分配,状态更新为繁忙");
// 更新用户的板子绑定信息
this.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, board.ID)
.Set(u => u.BoardExpireTime, expireTime)
.Update();
board.Status = Database.Board.BoardStatus.Busy;
board.OccupiedUserID = userId;
board.OccupiedUserName = user.Name;
logger.Info($"实验板 {board.BoardName} ({board.ID}) 已分配给用户 {user.Name} ({userId}),过期时间: {expireTime}");
return new(board);
}
}
@@ -385,10 +545,10 @@ public class AppDataConnection : DataConnection
/// <summary>
/// 用户表
/// </summary>
public ITable<User> User => this.GetTable<User>();
public ITable<User> UserTable => this.GetTable<User>();
/// <summary>
/// FPGA 板子表
/// </summary>
public ITable<Board> Board => this.GetTable<Board>();
public ITable<Board> BoardTable => this.GetTable<Board>();
}