mtfosbot/model/lottery.go

37 lines
909 B
Go
Raw Permalink Normal View History

2018-09-27 13:43:02 +00:00
package model
import (
"database/sql"
"time"
)
// Lottery -
type Lottery struct {
ID string `db:"id" cc:"id"`
Type string `db:"type" cc:"type"`
Message string `db:"message" cc:"message"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"mtime"`
}
// GetRandomLotteryByType -
func GetRandomLotteryByType(t string) (p *Lottery, err error) {
parr, err := GetRandomLotteryByTypeAndLimit(t, 1)
if err != nil {
return nil, err
}
if parr == nil {
return nil, nil
}
return parr[0], err
}
// GetRandomLotteryByTypeAndLimit -
func GetRandomLotteryByTypeAndLimit(t string, limit int) (p []*Lottery, err error) {
2018-12-18 08:50:38 +00:00
err = x.Select(&p, `select * from "public"."lottery" where "type" = $1 offset floor(random() * (select count(*) as c from "public"."lottery" where "type" = $2)) limit $3`, t, t, limit)
2018-09-27 13:43:02 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
return
}