fix: 添加 PortfolioNavHistory 到 CodeFirst 初始化,删除手动迁移脚本
- DatabaseService.InitializeDatabase() 添加 PortfolioNavHistory - 删除 migrations/ 目录,使用 CodeFirst 自动建表
This commit is contained in:
parent
849db7d2b2
commit
6a757f56da
@ -21,7 +21,8 @@ public class DatabaseService
|
|||||||
typeof(Transaction),
|
typeof(Transaction),
|
||||||
typeof(TiingoTicker),
|
typeof(TiingoTicker),
|
||||||
typeof(MarketPriceCache),
|
typeof(MarketPriceCache),
|
||||||
typeof(MarketKlineCache)
|
typeof(MarketKlineCache),
|
||||||
|
typeof(PortfolioNavHistory)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -53,7 +53,7 @@ public class Strategy
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 策略配置项(周期,阈值,资产配比)
|
/// 策略配置项(周期,阈值,资产配比)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarColumn(ColumnName = "config", IsJson = true)]
|
[SugarColumn(ColumnName = "config", ColumnDataType = "text")]
|
||||||
public string? Config { get; set; }
|
public string? Config { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
using AssetManager.Data;
|
using AssetManager.Data;
|
||||||
using AssetManager.Models.DTOs;
|
using AssetManager.Models.DTOs;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace AssetManager.Services;
|
namespace AssetManager.Services;
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ public class StrategyService : IStrategyService
|
|||||||
Description = request.description,
|
Description = request.description,
|
||||||
Tags = request.tags != null ? string.Join(",", request.tags) : null,
|
Tags = request.tags != null ? string.Join(",", request.tags) : null,
|
||||||
RiskLevel = request.riskLevel,
|
RiskLevel = request.riskLevel,
|
||||||
Config = request.parameters != null ? System.Text.Json.JsonSerializer.Serialize(request.parameters) : null,
|
Config = request.parameters != null ? JsonSerializer.Serialize(request.parameters) : null,
|
||||||
CreatedAt = DateTime.Now,
|
CreatedAt = DateTime.Now,
|
||||||
UpdatedAt = DateTime.Now
|
UpdatedAt = DateTime.Now
|
||||||
};
|
};
|
||||||
@ -63,7 +64,7 @@ public class StrategyService : IStrategyService
|
|||||||
strategy.Description = request.description;
|
strategy.Description = request.description;
|
||||||
strategy.Tags = request.tags != null ? string.Join(",", request.tags) : null;
|
strategy.Tags = request.tags != null ? string.Join(",", request.tags) : null;
|
||||||
strategy.RiskLevel = request.riskLevel;
|
strategy.RiskLevel = request.riskLevel;
|
||||||
strategy.Config = request.parameters != null ? System.Text.Json.JsonSerializer.Serialize(request.parameters) : null;
|
strategy.Config = request.parameters != null ? JsonSerializer.Serialize(request.parameters) : null;
|
||||||
strategy.UpdatedAt = DateTime.Now;
|
strategy.UpdatedAt = DateTime.Now;
|
||||||
|
|
||||||
_db.Updateable(strategy).ExecuteCommand();
|
_db.Updateable(strategy).ExecuteCommand();
|
||||||
|
|||||||
@ -1,34 +0,0 @@
|
|||||||
-- =============================================
|
|
||||||
-- 净值历史表迁移脚本
|
|
||||||
-- 创建时间: 2026-03-13
|
|
||||||
-- 说明: 用于记录组合每日净值和收益数据
|
|
||||||
-- =============================================
|
|
||||||
|
|
||||||
-- 创建净值历史表
|
|
||||||
CREATE TABLE IF NOT EXISTS portfolio_nav_history (
|
|
||||||
id VARCHAR(50) PRIMARY KEY COMMENT '主键ID',
|
|
||||||
portfolio_id VARCHAR(50) NOT NULL COMMENT '组合ID',
|
|
||||||
nav_date DATE NOT NULL COMMENT '净值日期',
|
|
||||||
total_value DECIMAL(18,4) DEFAULT 0 COMMENT '总资产价值(本位币)',
|
|
||||||
total_cost DECIMAL(18,4) DEFAULT 0 COMMENT '累计投入成本(本位币)',
|
|
||||||
nav DECIMAL(18,8) DEFAULT 1.0 COMMENT '单位净值',
|
|
||||||
daily_return DECIMAL(10,4) DEFAULT 0 COMMENT '日收益率(%)',
|
|
||||||
cumulative_return DECIMAL(10,4) DEFAULT 0 COMMENT '累计收益率(%)',
|
|
||||||
currency VARCHAR(10) DEFAULT 'CNY' COMMENT '本位币',
|
|
||||||
position_count INT DEFAULT 0 COMMENT '持仓数量',
|
|
||||||
source VARCHAR(20) DEFAULT 'calculated' COMMENT '数据来源',
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='组合净值历史表';
|
|
||||||
|
|
||||||
-- 创建组合日期联合索引
|
|
||||||
CREATE INDEX idx_portfolio_date ON portfolio_nav_history(portfolio_id, nav_date);
|
|
||||||
|
|
||||||
-- 创建唯一约束(同一组合同一天只能有一条记录)
|
|
||||||
ALTER TABLE portfolio_nav_history ADD CONSTRAINT uk_portfolio_date UNIQUE(portfolio_id, nav_date);
|
|
||||||
|
|
||||||
-- 为portfolio表的total_value字段添加注释
|
|
||||||
ALTER TABLE portfolios MODIFY COLUMN total_value DECIMAL(18,4) DEFAULT 0 COMMENT '当前总市值(冗余字段,实时计算)';
|
|
||||||
|
|
||||||
-- 为portfolio表添加净值相关字段
|
|
||||||
ALTER TABLE portfolios ADD COLUMN IF NOT EXISTS initial_cost DECIMAL(18,4) DEFAULT 0 COMMENT '初始投入成本';
|
|
||||||
ALTER TABLE portfolios ADD COLUMN IF NOT EXISTS last_nav_date DATE COMMENT '最后净值计算日期';
|
|
||||||
Loading…
Reference in New Issue
Block a user