diff --git a/AssetManager.API/Controllers/PortfolioController.cs b/AssetManager.API/Controllers/PortfolioController.cs index b6031ae..002f8d7 100644 --- a/AssetManager.API/Controllers/PortfolioController.cs +++ b/AssetManager.API/Controllers/PortfolioController.cs @@ -66,6 +66,51 @@ public class PortfolioController : ControllerBase } _logger.LogInformation("Request to create portfolio"); + + // 1. 获取用户默认币种 + var db = _databaseService.GetDb(); + var user = db.Queryable().Where(u => u.Id == userId).First(); + if (user == null) + { + return NotFound(new ApiResponse + { + code = AssetManager.Models.StatusCodes.NotFound, + data = null, + message = "用户不存在" + }); + } + + // 2. 校验组合币种,如果没传则用用户默认币种 + if (string.IsNullOrEmpty(request.currency)) + { + request.currency = user.DefaultCurrency; + } + if (!CurrencyHelper.IsSupported(request.currency)) + { + return BadRequest(new ApiResponse + { + code = StatusCodes.BadRequest, + data = null, + message = $"不支持的组合币种: {request.currency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}" + }); + } + + // 3. 校验持仓币种 + if (request.stocks != null) + { + foreach (var stock in request.stocks) + { + if (!string.IsNullOrEmpty(stock.currency) && !CurrencyHelper.IsSupported(stock.currency)) + { + return BadRequest(new ApiResponse + { + code = StatusCodes.BadRequest, + data = null, + message = $"不支持的持仓币种: {stock.currency}(标的: {stock.code}),支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}" + }); + } + } + } var response = _portfolioService.CreatePortfolio(request, userId); @@ -301,6 +346,32 @@ public class PortfolioController : ControllerBase } _logger.LogInformation("Request to create transaction"); + + // 校验交易币种 + if (!string.IsNullOrEmpty(request.currency) && !CurrencyHelper.IsSupported(request.currency)) + { + return BadRequest(new ApiResponse + { + code = StatusCodes.BadRequest, + data = null, + message = $"不支持的交易币种: {request.currency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}" + }); + } + + // 校验资产类型 + if (!string.IsNullOrEmpty(request.assetType)) + { + var validAssetTypes = new[] { "Stock", "Crypto" }; + if (!validAssetTypes.Contains(request.assetType, StringComparer.OrdinalIgnoreCase)) + { + return BadRequest(new ApiResponse + { + code = StatusCodes.BadRequest, + data = null, + message = $"不支持的资产类型: {request.assetType},支持的类型: {string.Join(", ", validAssetTypes)}" + }); + } + } var response = _portfolioService.CreateTransaction(request, userId); diff --git a/AssetManager.API/Controllers/UserController.cs b/AssetManager.API/Controllers/UserController.cs index 4c2f0f7..4937b40 100644 --- a/AssetManager.API/Controllers/UserController.cs +++ b/AssetManager.API/Controllers/UserController.cs @@ -68,7 +68,8 @@ public class UserController : ControllerBase MemberLevel = user.MemberLevel, RunningDays = user.RunningDays, Avatar = user.Avatar, - Email = user.Email + Email = user.Email, + DefaultCurrency = user.DefaultCurrency }; _logger.LogInformation("User info retrieved successfully"); @@ -206,8 +207,22 @@ public class UserController : ControllerBase }); } + // 校验币种 + if (!string.IsNullOrEmpty(request.DefaultCurrency) && !CurrencyHelper.IsSupported(request.DefaultCurrency)) + { + return BadRequest(new ApiResponse + { + code = StatusCodes.BadRequest, + data = null, + message = $"不支持的币种: {request.DefaultCurrency},支持的币种: {string.Join(", ", CurrencyHelper.GetSupportedCurrencies())}" + }); + } + // 更新用户信息 user.UserName = request.UserName; + if (!string.IsNullOrEmpty(request.Avatar)) user.Avatar = request.Avatar; + if (!string.IsNullOrEmpty(request.Email)) user.Email = request.Email; + if (!string.IsNullOrEmpty(request.DefaultCurrency)) user.DefaultCurrency = request.DefaultCurrency; user.UpdatedAt = DateTime.Now; db.Updateable(user).ExecuteCommand(); diff --git a/AssetManager.Data/User.cs b/AssetManager.Data/User.cs index 7a3498d..dae2e17 100644 --- a/AssetManager.Data/User.cs +++ b/AssetManager.Data/User.cs @@ -43,6 +43,12 @@ public class User /// [SugarColumn(ColumnName = "member_level", Length = 50)] public string MemberLevel { get; set; } + + /// + /// 用户默认本位币 (CNY/USD/HKD) + /// + [SugarColumn(ColumnName = "default_currency", Length = 10)] + public string DefaultCurrency { get; set; } = "CNY"; /// /// 连续运行天数 diff --git a/AssetManager.Models/Currency.cs b/AssetManager.Models/Currency.cs new file mode 100644 index 0000000..ffe88af --- /dev/null +++ b/AssetManager.Models/Currency.cs @@ -0,0 +1,49 @@ +namespace AssetManager.Models; + +/// +/// 支持的币种枚举 +/// +public enum Currency +{ + /// + /// 人民币 + /// + CNY = 0, + + /// + /// 美元 + /// + USD = 1, + + /// + /// 港币 + /// + HKD = 2 +} + +/// +/// 币种辅助类 +/// +public static class CurrencyHelper +{ + private static readonly HashSet _supportedCurrencies = new(StringComparer.OrdinalIgnoreCase) + { + "CNY", "USD", "HKD" + }; + + /// + /// 检查币种是否支持 + /// + public static bool IsSupported(string currency) + { + return !string.IsNullOrEmpty(currency) && _supportedCurrencies.Contains(currency); + } + + /// + /// 获取支持的币种列表 + /// + public static List GetSupportedCurrencies() + { + return new List { "CNY", "USD", "HKD" }; + } +} diff --git a/AssetManager.Models/DTOs/UserDTO.cs b/AssetManager.Models/DTOs/UserDTO.cs index 0571c7c..09d800a 100644 --- a/AssetManager.Models/DTOs/UserDTO.cs +++ b/AssetManager.Models/DTOs/UserDTO.cs @@ -7,6 +7,15 @@ public class UserInfoResponse public int RunningDays { get; set; } public string Avatar { get; set; } public string Email { get; set; } + public string DefaultCurrency { get; set; } +} + +public class UpdateUserRequest +{ + public string UserName { get; set; } + public string Avatar { get; set; } + public string Email { get; set; } + public string DefaultCurrency { get; set; } } public class UserStatsResponse