add background get price job

This commit is contained in:
Jay
2019-01-18 16:39:02 +08:00
parent efaf0c2fdd
commit ca453dd773
6 changed files with 58 additions and 12 deletions
+22 -2
View File
@@ -13,6 +13,7 @@ type CardPrice struct {
Ctime time.Time `db:"ctime" cc:"ctime"`
}
// InsertCardPriceOpts -
type InsertCardPriceOpts struct {
Data []CardPrice
}
@@ -34,15 +35,34 @@ func InsertCardPrice(arg InsertCardPriceOpts) (err error) {
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 += " (:card, :price_n, :price_f, now()) "
insStr += " (?, ?, ?, now()) "
vals = append(vals, tmp[i].Card, tmp[i].PriceN, tmp[i].PriceF)
}
_, err = x.NamedExec(query+insStr, tmp)
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
}
+6 -1
View File
@@ -155,7 +155,12 @@ func GetCardByTCGPlayerIDs(ids []int) (cards []*Cards, err error) {
query := `select c.id, c.name, c.cmc, c.mana_cost, c.text, c.layout, c.image_url, c.loyalty,
c.type, c.number, c.power, c.toughness, c.set, c.tcgplayerid from "public"."cards" c where c.tcgplayerid in (?)`
err = x.Select(&cards, query, ids)
query, args, err := sqlx.In(query, ids)
if err != nil {
return nil, err
}
query = x.Rebind(query)
err = x.Select(&cards, query, args...)
if err != nil {
return nil, err
}
+11 -7
View File
@@ -55,9 +55,11 @@ func CountSets(arg ListSetsOpts) (c int, err error) {
if err != nil {
return 0, err
}
query, args, err = sqlx.In(query, args)
if err != nil {
return 0, err
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args)
if err != nil {
return 0, err
}
}
query = x.Rebind(query)
@@ -73,7 +75,7 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if arg.Offset < 0 {
arg.Offset = 0
}
query := `select s.code, s.name, s.release_date from "public"."sets" s`
query := `select s.code, s.name, s.release_date, s.tcgplayerid from "public"."sets" s`
where := struct {
Code []string `db:"code"`
@@ -100,9 +102,11 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if err != nil {
return nil, err
}
query, args, err = sqlx.In(query, args)
if err != nil {
return nil, err
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args)
if err != nil {
return nil, err
}
}
query = x.Rebind(query)