mtfosbot/model/lottery.go

28 lines
621 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) {
p = &Lottery{}
2018-09-28 14:09:08 +00:00
offset := rand.Intn(10)
err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit 1`, t, offset)
2018-09-27 13:43:02 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
return
}