money-money/base.go
2022-07-03 08:38:06 +08:00

72 lines
1.3 KiB
Go

package moneymoney
import (
"context"
"encoding/gob"
"os"
"github.com/474420502/structure/compare"
"github.com/474420502/structure/tree/treelist"
"go.mongodb.org/mongo-driver/bson"
)
func GetAll() (result *treelist.Tree[int64]) {
result = treelist.New[int64](compare.Any[int64])
var stocks []*Stock
cur, err := cstock.Find(context.TODO(), bson.M{})
if err != nil {
panic(err)
}
f, err := os.Open("./stocks.gob")
if err == nil {
err = gob.NewDecoder(f).Decode(&stocks)
if err != nil {
panic(err)
}
} else {
for cur.Next(context.TODO()) {
var s Stock
err := cur.Decode(&s)
if err != nil {
panic(err)
}
stocks = append(stocks, &s)
// result = append(result, &s)
}
f, err = os.OpenFile("./stocks.gob", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if err != nil {
panic(err)
}
err = gob.NewEncoder(f).Encode(stocks)
if err != nil {
panic(err)
}
}
for _, s := range stocks {
var daymap *treelist.Tree[int64]
idaymap, ok := result.Get(s.Date.Unix())
// daymap, ok := DateStocks[s.Date.Unix()]
if !ok {
idaymap = treelist.New(compare.Any[int64])
result.Put(s.Date.Unix(), idaymap)
// DateStocks[s.Date.Unix()] = daymap
}
daymap = idaymap.(*treelist.Tree[int64])
daymap.Put(s.Code, s)
}
return
}
func AggregateNewField(result []*Stock) {
}