2021-12-13 23:19:21 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"go.uber.org/multierr"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccountService struct {
|
|
|
|
DB *sqlx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccountService(db *sqlx.DB) *AccountService {
|
|
|
|
return &AccountService{DB: db}
|
|
|
|
}
|
|
|
|
|
2022-05-03 11:26:52 +00:00
|
|
|
func (s *AccountService) InsertAsset(time time.Time, session string, name types.ExchangeName, account string, assets types.AssetMap) error {
|
2021-12-13 23:19:21 +00:00
|
|
|
if s.DB == nil {
|
2022-05-03 09:51:42 +00:00
|
|
|
// skip db insert when no db connection setting.
|
2021-12-13 23:19:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, v := range assets {
|
|
|
|
_, _err := s.DB.Exec(`
|
2022-05-03 09:51:42 +00:00
|
|
|
INSERT INTO nav_history_details (
|
2022-05-03 11:26:52 +00:00
|
|
|
session,
|
2022-05-03 09:51:42 +00:00
|
|
|
exchange,
|
|
|
|
subaccount,
|
|
|
|
time,
|
|
|
|
currency,
|
|
|
|
balance_in_usd,
|
|
|
|
balance_in_btc,
|
|
|
|
balance,
|
|
|
|
available,
|
|
|
|
locked,
|
|
|
|
borrowed,
|
|
|
|
net_asset,
|
|
|
|
price_in_usd)
|
2022-05-03 15:36:44 +00:00
|
|
|
values (?,?,?,?,?,?,?,?,?,?,?,?,?);
|
2022-05-03 11:26:52 +00:00
|
|
|
`, session, name, account, time, v.Currency, v.InUSD, v.InBTC, v.Total, v.Available, v.Locked, v.Borrowed, v.NetAsset, v.PriceInUSD)
|
2021-12-13 23:19:21 +00:00
|
|
|
|
|
|
|
err = multierr.Append(err, _err) // successful request
|
|
|
|
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|