optz
This commit is contained in:
@@ -92,7 +92,7 @@ func runCollection(coll *Collector) {
|
|||||||
|
|
||||||
// 数据有变化,保存到数据库
|
// 数据有变化,保存到数据库
|
||||||
log.Println("数据已变化,开始存储到数据库...")
|
log.Println("数据已变化,开始存储到数据库...")
|
||||||
if err := SaveData(status); err != nil {
|
if err := SaveData(&status.Data); err != nil {
|
||||||
log.Printf("保存数据失败: %v", err)
|
log.Printf("保存数据失败: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// SaveStatus 保存完整状态数据(使用事务)
|
// SaveStatus 保存完整状态数据(使用事务)
|
||||||
func SaveData(status *types.StatusData) error {
|
func SaveData(data *types.Data) error {
|
||||||
// 验证必要字段 - AccountID是所有Upsert的共同条件
|
// 验证必要字段 - AccountID是所有Upsert的共同条件
|
||||||
if status.Data.Assets.AccountID == "" {
|
if data.Assets.AccountID == "" {
|
||||||
return fmt.Errorf("账户ID不能为空")
|
return fmt.Errorf("账户ID不能为空")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,16 +25,16 @@ func SaveData(status *types.StatusData) error {
|
|||||||
|
|
||||||
// 保存资产快照 (先查询后更新/插入)
|
// 保存资产快照 (先查询后更新/插入)
|
||||||
var existingAsset models.CollectorAssets
|
var existingAsset models.CollectorAssets
|
||||||
err := impl.DBService.Where("account_id = ? AND ymd = ?", status.Data.Assets.AccountID, ymd).First(&existingAsset).Error
|
err := impl.DBService.Where("account_id = ? AND ymd = ?", data.Assets.AccountID, ymd).First(&existingAsset).Error
|
||||||
|
|
||||||
asset := models.CollectorAssets{
|
asset := models.CollectorAssets{
|
||||||
AccountID: status.Data.Assets.AccountID,
|
AccountID: data.Assets.AccountID,
|
||||||
Ymd: ymd,
|
Ymd: ymd,
|
||||||
Cash: status.Data.Assets.Cash,
|
Cash: data.Assets.Cash,
|
||||||
FrozenCash: status.Data.Assets.FrozenCash,
|
FrozenCash: data.Assets.FrozenCash,
|
||||||
MarketValue: status.Data.Assets.MarketValue,
|
MarketValue: data.Assets.MarketValue,
|
||||||
Profit: status.Data.Assets.Profit,
|
Profit: data.Assets.Profit,
|
||||||
TotalAsset: status.Data.Assets.TotalAsset,
|
TotalAsset: data.Assets.TotalAsset,
|
||||||
CollectedAt: now,
|
CollectedAt: now,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +57,8 @@ func SaveData(status *types.StatusData) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 批量保存订单 (先查询后更新/插入)
|
// 批量保存订单 (先查询后更新/插入)
|
||||||
if len(status.Data.Orders) > 0 {
|
if len(data.Orders) > 0 {
|
||||||
for _, order := range status.Data.Orders {
|
for _, order := range data.Orders {
|
||||||
// 验证必要条件: OrderID和StockCode
|
// 验证必要条件: OrderID和StockCode
|
||||||
if order.OrderID == 0 {
|
if order.OrderID == 0 {
|
||||||
continue
|
continue
|
||||||
@@ -79,14 +79,14 @@ func SaveData(status *types.StatusData) error {
|
|||||||
|
|
||||||
// 查询是否存在
|
// 查询是否存在
|
||||||
var cnt int64
|
var cnt int64
|
||||||
impl.DBService.Model(&models.CollectorOrder{}).Where("account_id = ? AND order_id = ? AND ymd = ?", status.Data.Assets.AccountID, order.OrderID, ymd).Count(&cnt)
|
impl.DBService.Model(&models.CollectorOrder{}).Where("account_id = ? AND order_id = ? AND ymd = ?", data.Assets.AccountID, order.OrderID, ymd).Count(&cnt)
|
||||||
if cnt > 0 {
|
if cnt > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
orderRecord := models.CollectorOrder{
|
orderRecord := models.CollectorOrder{
|
||||||
OrderID: order.OrderID,
|
OrderID: order.OrderID,
|
||||||
AccountID: status.Data.Assets.AccountID,
|
AccountID: data.Assets.AccountID,
|
||||||
StockCode: order.StockCode,
|
StockCode: order.StockCode,
|
||||||
Ymd: ymd,
|
Ymd: ymd,
|
||||||
Price: order.Price,
|
Price: order.Price,
|
||||||
@@ -116,8 +116,8 @@ func SaveData(status *types.StatusData) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 批量保存持仓 (先查询后更新/插入)
|
// 批量保存持仓 (先查询后更新/插入)
|
||||||
if len(status.Data.Positions) > 0 {
|
if len(data.Positions) > 0 {
|
||||||
for _, pos := range status.Data.Positions {
|
for _, pos := range data.Positions {
|
||||||
// 验证必要条件: Code
|
// 验证必要条件: Code
|
||||||
if pos.Code == "" {
|
if pos.Code == "" {
|
||||||
continue
|
continue
|
||||||
@@ -126,10 +126,10 @@ func SaveData(status *types.StatusData) error {
|
|||||||
// 查询是否存在
|
// 查询是否存在
|
||||||
var existingPosition models.CollectorPosition
|
var existingPosition models.CollectorPosition
|
||||||
err := impl.DBService.Where("account_id = ? AND code = ? AND ymd = ?",
|
err := impl.DBService.Where("account_id = ? AND code = ? AND ymd = ?",
|
||||||
status.Data.Assets.AccountID, pos.Code, ymd).First(&existingPosition).Error
|
data.Assets.AccountID, pos.Code, ymd).First(&existingPosition).Error
|
||||||
|
|
||||||
positionRecord := models.CollectorPosition{
|
positionRecord := models.CollectorPosition{
|
||||||
AccountID: status.Data.Assets.AccountID,
|
AccountID: data.Assets.AccountID,
|
||||||
Code: pos.Code,
|
Code: pos.Code,
|
||||||
Ymd: ymd,
|
Ymd: ymd,
|
||||||
Volume: pos.Volume,
|
Volume: pos.Volume,
|
||||||
|
|||||||
Reference in New Issue
Block a user