add get cards price model

This commit is contained in:
Jay 2019-01-22 17:42:18 +08:00
parent 605f20faf1
commit 9f17333c38
3 changed files with 23 additions and 8 deletions

View File

@ -3,10 +3,13 @@ package models
import ( import (
"errors" "errors"
"time" "time"
"github.com/jmoiron/sqlx"
) )
// CardPrice - // CardPrice -
type CardPrice struct { type CardPrice struct {
ID string `db:"id" cc:"id"`
Card string `db:"card" cc:"card"` Card string `db:"card" cc:"card"`
PriceN float32 `db:"price_n" cc:"price_n"` PriceN float32 `db:"price_n" cc:"price_n"`
PriceF float32 `db:"price_f" cc:"price_f"` PriceF float32 `db:"price_f" cc:"price_f"`
@ -51,18 +54,21 @@ func InsertCardPrice(arg InsertCardPriceOpts) (err error) {
return return
} }
// GetLastPriceByCard - // GetCardsLastPrice -
func GetLastPriceByCard(id string) (price *CardPrice, err error) { func GetCardsLastPrice(ids []string) (price []*CardPrice, err error) {
if len(id) == 0 { if len(ids) == 0 {
return nil, errors.New("id is empty") return nil, errors.New("no card id")
} }
price = &CardPrice{} query := `select distinct on (cp.id) cp.id, cp.card, cp.price_n, cp.price_f, cp.ctime from card_price cp where cp.card in (?) order by cp.id, cp.ctime desc`
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) query, args, err := sqlx.In(query, ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }
query = x.Rebind(query)
err = x.Select(&price, query, args...)
return return
} }

8
schema/20190122-1.sql Normal file
View File

@ -0,0 +1,8 @@
alter table card_price
add id uuid default uuid_generate_v4() not null;
alter table card_price
add constraint card_price_pk
primary key (id);

View File

@ -3,7 +3,8 @@
{"file": "main.sql", "version": 1}, {"file": "main.sql", "version": 1},
{"file": "20190106-1.sql", "version": 2}, {"file": "20190106-1.sql", "version": 2},
{"file": "20190115-1.sql", "version": 3}, {"file": "20190115-1.sql", "version": 3},
{"file": "20190116-1.sql", "version": 4} {"file": "20190116-1.sql", "version": 4},
{"file": "20190122-1.sql", "version": 5}
], ],
"test": [] "test": []
} }