mtgbot/models/card_price.go

69 lines
1.4 KiB
Go

package models
import (
"errors"
"time"
)
// CardPrice -
type CardPrice struct {
Card string `db:"card" cc:"card"`
PriceN float32 `db:"price_n" cc:"price_n"`
PriceF float32 `db:"price_f" cc:"price_f"`
Ctime time.Time `db:"ctime" cc:"ctime"`
}
// InsertCardPriceOpts -
type InsertCardPriceOpts struct {
Data []CardPrice
}
// InsertCardPrice -
func InsertCardPrice(arg InsertCardPriceOpts) (err error) {
if len(arg.Data) == 0 {
return errors.New("data list is empty")
}
tmp := make([]CardPrice, 0, len(arg.Data))
for _, v := range arg.Data {
if len(v.Card) > 0 {
tmp = append(tmp, v)
}
}
query := `insert into "public"."card_price" ("card", "price_n", "price_f", "ctime") values `
insStr := ""
vals := make([]interface{}, 0, len(tmp)*3)
for i := 0; i < len(tmp); i++ {
if len(insStr) > 0 {
insStr += ","
}
insStr += " (?, ?, ?, now()) "
vals = append(vals, tmp[i].Card, tmp[i].PriceN, tmp[i].PriceF)
}
query = x.Rebind(query + insStr)
_, err = x.Exec(query, vals...)
return
}
// GetLastPriceByCard -
func GetLastPriceByCard(id string) (price *CardPrice, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
price = &CardPrice{}
query := `select cp.card, cp.price_n, cp.price_f, cp.ctime from "public"."card_price" cp where "card" = $1`
err = x.Get(price, query, id)
if err != nil {
return nil, err
}
return
}