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 (
"errors"
"time"
"github.com/jmoiron/sqlx"
)
// CardPrice -
type CardPrice struct {
ID string `db:"id" cc:"id"`
Card string `db:"card" cc:"card"`
PriceN float32 `db:"price_n" cc:"price_n"`
PriceF float32 `db:"price_f" cc:"price_f"`
@ -51,18 +54,21 @@ func InsertCardPrice(arg InsertCardPriceOpts) (err error) {
return
}
// GetLastPriceByCard -
func GetLastPriceByCard(id string) (price *CardPrice, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
// GetCardsLastPrice -
func GetCardsLastPrice(ids []string) (price []*CardPrice, err error) {
if len(ids) == 0 {
return nil, errors.New("no card id")
}
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)
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, args, err := sqlx.In(query, ids)
if err != nil {
return nil, err
}
query = x.Rebind(query)
err = x.Select(&price, query, args...)
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": "20190106-1.sql", "version": 2},
{"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": []
}