From 564687bc1e7e56334dc2a2e5baa48cc817c2b499 Mon Sep 17 00:00:00 2001 From: niannian zheng Date: Mon, 2 Mar 2026 15:37:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=8A=95?= =?UTF-8?q?=E8=B5=84=E7=BB=84=E5=90=88=E8=AF=A6=E6=83=85=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=BA=A4=E6=98=93=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构投资组合详情响应,移除了不再需要的交易记录相关字段和逻辑 更新了README文档以反映API变更 --- AssetManager.Models/DTOs/PortfolioDTO.cs | 1 - AssetManager.Services/PortfolioService.cs | 23 +-- README.md | 230 +++++++++++++++++++--- 3 files changed, 210 insertions(+), 44 deletions(-) diff --git a/AssetManager.Models/DTOs/PortfolioDTO.cs b/AssetManager.Models/DTOs/PortfolioDTO.cs index 181ba44..0eac6b8 100644 --- a/AssetManager.Models/DTOs/PortfolioDTO.cs +++ b/AssetManager.Models/DTOs/PortfolioDTO.cs @@ -38,7 +38,6 @@ public class PortfolioDetailResponse public double todayProfit { get; set; } public string todayProfitCurrency { get; set; } public List positions { get; set; } - public List transactions { get; set; } } public class StrategyInfo diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index 3d59494..b4ecef7 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -137,12 +137,6 @@ public class PortfolioService : IPortfolioService .Where(pos => pos.PortfolioId == id) .ToList(); - var transactions = _db.Queryable() - .Where(t => t.PortfolioId == id) - .OrderByDescending(t => t.TransactionTime) - .Take(5) - .ToList(); - return new PortfolioDetailResponse { id = portfolio.Id, @@ -151,7 +145,7 @@ public class PortfolioService : IPortfolioService strategy = new StrategyInfo { id = portfolio.StrategyId, - name = "策略名称", // 实际应该从策略表获取 + name = "策略名称", description = "策略描述" }, portfolioValue = (double)portfolio.TotalValue, @@ -165,24 +159,11 @@ public class PortfolioService : IPortfolioService stockName = pos.StockName, amount = (int)pos.Shares, averagePrice = (double)pos.AvgPrice, - currentPrice = (double)pos.AvgPrice, // 实际应该从市场数据获取 + currentPrice = (double)pos.AvgPrice, totalValue = (double)(pos.Shares * pos.AvgPrice), profit = 0, profitRate = 0, currency = pos.Currency - }).ToList(), - transactions = transactions.Select(t => new TransactionItem - { - id = t.Id, - portfolioId = t.PortfolioId, - date = t.TransactionTime.ToString("yyyy-MM-dd"), - time = t.TransactionTime.ToString("HH:mm:ss"), - type = t.Type, - title = t.Title, - amount = (double)t.TotalAmount, - currency = t.Currency, - status = t.Status, - remark = t.Remark }).ToList() }; } diff --git a/README.md b/README.md index a629ad8..f0d9ee8 100644 --- a/README.md +++ b/README.md @@ -72,13 +72,110 @@ AssetManager ## 📡 API 示例 -### 创建策略 API +## 🎯 策略 API (Strategy API) -**请求 URL**: `POST /api/v1/strategies` +### 1. 获取策略列表 + +**请求 URL**: `GET /api/v1/strategy/strategies` + +**请求头**: +``` +Authorization: Bearer {token} +``` + +**响应示例**: + +```json +{ + "code": 200, + "data": [ + { + "Id": "12345678-1234-1234-1234-1234567890ab", + "Title": "双均线策略", + "Type": "ma_trend", + "Description": "经典趋势跟踪策略", + "RiskLevel": "medium", + "Tags": ["趋势", "均线"], + "Parameters": { + "maType": "SMA", + "shortPeriod": 20, + "longPeriod": 60 + }, + "UserId": "user-123", + "CreatedAt": "2024-01-01T10:00:00", + "UpdatedAt": "2024-01-01T10:00:00" + }, + { + "Id": "87654321-4321-4321-4321-210987654321", + "Title": "风险平价策略", + "Type": "risk_parity", + "Description": "资产配置策略", + "RiskLevel": "low", + "Tags": ["资产配置", "风险控制"], + "Parameters": { + "lookbackPeriod": 60, + "rebalanceThreshold": 0.05, + "assets": [ + { "symbol": "AAPL", "targetWeight": 0.6 }, + { "symbol": "BTC/USD", "targetWeight": 0.4 } + ] + }, + "UserId": "user-123", + "CreatedAt": "2024-01-02T10:00:00", + "UpdatedAt": "2024-01-02T10:00:00" + } + ], + "message": "Success" +} +``` + +### 2. 获取单个策略详情 + +**请求 URL**: `GET /api/v1/strategy/{id}` + +**请求头**: +``` +Authorization: Bearer {token} +``` + +**响应示例**: + +```json +{ + "code": 200, + "data": { + "Id": "12345678-1234-1234-1234-1234567890ab", + "Title": "双均线策略", + "Type": "ma_trend", + "Description": "经典趋势跟踪策略", + "RiskLevel": "medium", + "Tags": ["趋势", "均线"], + "Parameters": { + "maType": "SMA", + "shortPeriod": 20, + "longPeriod": 60 + }, + "UserId": "user-123", + "CreatedAt": "2024-01-01T10:00:00", + "UpdatedAt": "2024-01-01T10:00:00" + }, + "message": "Success" +} +``` + +### 3. 创建策略 + +**请求 URL**: `POST /api/v1/strategy` + +**请求头**: +``` +Authorization: Bearer {token} +Content-Type: application/json +``` **请求体示例**: -#### 1. 创建双均线策略 +#### 3.1 创建双均线策略 ```json { @@ -95,7 +192,7 @@ AssetManager } ``` -#### 2. 创建风险平价策略 +#### 3.2 创建风险平价策略 ```json { @@ -115,7 +212,7 @@ AssetManager } ``` -#### 3. 创建吊灯止损策略 +#### 3.3 创建吊灯止损策略 ```json { @@ -136,12 +233,115 @@ AssetManager ```json { - "Id": "12345678-1234-1234-1234-1234567890ab", - "Title": "双均线策略", - "Status": "created" + "code": 200, + "data": { + "Id": "12345678-1234-1234-1234-1234567890ab", + "Title": "双均线策略", + "Type": "ma_trend", + "Description": "经典趋势跟踪策略", + "RiskLevel": "medium", + "Tags": ["趋势", "均线"], + "Parameters": { + "maType": "SMA", + "shortPeriod": 20, + "longPeriod": 60 + }, + "UserId": "user-123", + "CreatedAt": "2024-01-01T10:00:00", + "UpdatedAt": "2024-01-01T10:00:00" + }, + "message": "Strategy created successfully" } ``` +### 4. 更新策略 + +**请求 URL**: `PUT /api/v1/strategy/{id}` + +**请求头**: +``` +Authorization: Bearer {token} +Content-Type: application/json +``` + +**请求体示例**: + +```json +{ + "name": "双均线策略(更新)", + "type": "ma_trend", + "parameters": { + "maType": "EMA", + "shortPeriod": 15, + "longPeriod": 50 + } +} +``` + +**响应示例**: + +```json +{ + "code": 200, + "data": { + "Id": "12345678-1234-1234-1234-1234567890ab", + "Title": "双均线策略(更新)", + "Type": "ma_trend", + "Description": "经典趋势跟踪策略", + "RiskLevel": "medium", + "Tags": ["趋势", "均线"], + "Parameters": { + "maType": "EMA", + "shortPeriod": 15, + "longPeriod": 50 + }, + "UserId": "user-123", + "CreatedAt": "2024-01-01T10:00:00", + "UpdatedAt": "2024-01-15T14:30:00" + }, + "message": "Strategy updated successfully" +} +``` + +### 5. 删除策略 + +**请求 URL**: `DELETE /api/v1/strategy/{id}` + +**请求头**: +``` +Authorization: Bearer {token} +``` + +**响应示例**: + +```json +{ + "code": 200, + "data": { + "id": "12345678-1234-1234-1234-1234567890ab", + "status": "deleted" + }, + "message": "Strategy deleted successfully" +} +``` + +### 错误响应格式 + +所有策略接口在发生错误时都会返回统一的错误格式: + +```json +{ + "code": 401, + "data": null, + "message": "用户未授权" +} +``` + +**常见错误码**: +- `401`: 用户未授权 +- `404`: 策略不存在 +- `500`: 服务器内部错误 + ## 💼 投资组合 API (Portfolio API) ### 1. 创建投资组合 @@ -325,20 +525,6 @@ Authorization: Bearer {token} "profitRate": -0.03, "currency": "USD" } - ], - "transactions": [ - { - "id": "trans-abc12345", - "portfolioId": "port-abc12345", - "date": "2024-01-01", - "time": "10:30:00", - "type": "buy", - "title": "初始建仓", - "amount": 100, - "currency": "USD", - "status": "completed", - "remark": "初始建仓" - } ] }, "message": "success"