money-money/base.go

118 lines
2.5 KiB
Go
Raw Normal View History

2022-07-03 00:38:06 +00:00
package moneymoney
import (
"context"
"encoding/gob"
"log"
2022-07-03 00:38:06 +00:00
"os"
"time"
2022-07-03 00:38:06 +00:00
"github.com/474420502/random"
2022-07-03 00:38:06 +00:00
"github.com/474420502/structure/compare"
"github.com/474420502/structure/tree/treelist"
2022-07-04 17:47:54 +00:00
"github.com/klauspost/compress/zstd"
2022-07-03 00:38:06 +00:00
"go.mongodb.org/mongo-driver/bson"
)
const 亿 = 100000000
2022-07-03 00:38:06 +00:00
func GetDate(date string) time.Time {
ts, err := time.ParseInLocation("2006-01-02", date, time.Local)
2022-07-03 00:38:06 +00:00
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
2022-07-03 00:38:06 +00:00
f, err := os.Open("./stocks.gob")
if err == nil {
2022-07-04 17:47:54 +00:00
dec, err := zstd.NewReader(f)
2022-07-03 00:38:06 +00:00
if err != nil {
panic(err)
}
2022-07-04 17:47:54 +00:00
defer dec.Close()
// dec := f
err = gob.NewDecoder(dec).Decode(&stocks)
if err != nil {
panic(err)
}
defer f.Close()
2022-07-03 00:38:06 +00:00
} else {
cur, err := cstock.Find(cxt, bson.M{})
if err != nil {
log.Println(err)
}
2022-07-03 00:38:06 +00:00
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)
}
2022-07-04 17:47:54 +00:00
defer f.Close()
2022-07-03 00:38:06 +00:00
2022-07-04 17:47:54 +00:00
enc, err := zstd.NewWriter(f)
if err != nil {
panic(err)
}
defer enc.Close()
// enc := f
err = gob.NewEncoder(enc).Encode(&stocks)
2022-07-03 00:38:06 +00:00
if err != nil {
panic(err)
}
}
for _, s := range stocks {
var daymap *treelist.Tree[int64, *Stock]
2022-07-03 00:38:06 +00:00
idaymap, ok := result.Get(s.Date.Unix())
// daymap, ok := DateStocks[s.Date.Unix()]
if !ok {
idaymap = treelist.New[int64, *Stock](compare.Any[int64])
2022-07-03 00:38:06 +00:00
result.Put(s.Date.Unix(), idaymap)
// DateStocks[s.Date.Unix()] = daymap
}
daymap = idaymap
2022-07-03 00:38:06 +00:00
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
}
}