mtfosbot/model/lottery.go

39 lines
890 B
Go
Raw Normal View History

2018-09-27 13:43:02 +00:00
package model
import (
"database/sql"
2018-09-28 14:09:08 +00:00
"math/rand"
2018-09-27 13:43:02 +00:00
"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-09-28 14:09:08 +00:00
offset := rand.Intn(10)
2018-10-09 15:57:41 +00:00
err = x.Select(&p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit $3`, t, offset, limit)
2018-09-27 13:43:02 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
return
}