money-money/base.go

118 lines
2.5 KiB
Go

package moneymoney
import (
"context"
"encoding/gob"
"log"
"os"
"time"
"github.com/474420502/random"
"github.com/474420502/structure/compare"
"github.com/474420502/structure/tree/treelist"
"github.com/klauspost/compress/zstd"
"go.mongodb.org/mongo-driver/bson"
)
const 亿 = 100000000
func GetDate(date string) time.Time {
ts, err := time.ParseInLocation("2006-01-02", date, time.Local)
if err != nil {
panic(err)
}
return ts
}
type StockTree *treelist.Tree[int64, *Stock]
func GetAll() (result *treelist.Tree[int64, *treelist.Tree[int64, *Stock]]) {
result = treelist.New[int64, *treelist.Tree[int64, *Stock]](compare.Any[int64])
var stocks []*Stock
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 {
cur, err := cstock.Find(cxt, bson.M{})
if err != nil {
log.Println(err)
}
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, *Stock]
idaymap, ok := result.Get(s.Date.Unix())
// daymap, ok := DateStocks[s.Date.Unix()]
if !ok {
idaymap = treelist.New[int64, *Stock](compare.Any[int64])
result.Put(s.Date.Unix(), idaymap)
// DateStocks[s.Date.Unix()] = daymap
}
daymap = idaymap
daymap.Put(s.Code, s)
}
return
}
func AggregateNewField(result []*Stock) {
}
func ShowSelectStocks(selectStocks []*Stock) {
for _, s := range selectStocks {
log.Printf("%s %s 当前价格:%f", s.Date.Local().Format("2006-01-02"), s.Name, s.ClosingPrice)
// log.Println(s.UpsDownsRatio, s)
}
}
func RandomRange[T float32 | float64 | int | int64 | uint32 | uint64](rd *random.Random, rv *RangeValue[T]) {
rv.Min = rv.MinLimt + T(rd.Int63n(int64((rv.MaxLimit-rv.MinLimt)/rv.Step)))*rv.Step
rv.Max = rv.MinLimt + T(rd.Int63n(int64((rv.MaxLimit-rv.MinLimt)/rv.Step)))*rv.Step
if rv.Min > rv.Max {
rv.Min, rv.Max = rv.Max, rv.Min
}
}