money-money/base.go
2022-07-05 01:47:54 +08:00

88 lines
1.6 KiB
Go

package moneymoney
import (
"context"
"encoding/gob"
"os"
"github.com/474420502/structure/compare"
"github.com/474420502/structure/tree/treelist"
"github.com/klauspost/compress/zstd"
"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 {
dec, err := zstd.NewReader(f)
if err != nil {
panic(err)
}
defer dec.Close()
// dec := f
err = gob.NewDecoder(dec).Decode(&stocks)
if err != nil {
panic(err)
}
defer f.Close()
} 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)
}
defer f.Close()
enc, err := zstd.NewWriter(f)
if err != nil {
panic(err)
}
defer enc.Close()
// enc := f
err = gob.NewEncoder(enc).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) {
}