2019-09-29 04:56:39 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
|
|
|
|
2019-10-06 22:20:37 +00:00
|
|
|
var GroupPromotions GroupPromotionStore
|
2019-09-29 04:56:39 +00:00
|
|
|
|
|
|
|
type GroupPromotion struct {
|
|
|
|
ID int
|
|
|
|
From int
|
|
|
|
To int
|
|
|
|
TwoWay bool
|
|
|
|
|
|
|
|
Level int
|
2019-10-06 22:20:37 +00:00
|
|
|
Posts int
|
2019-09-29 04:56:39 +00:00
|
|
|
MinTime int
|
|
|
|
}
|
|
|
|
|
|
|
|
type GroupPromotionStore interface {
|
2019-10-06 22:20:37 +00:00
|
|
|
GetByGroup(gid int) (gps []*GroupPromotion, err error)
|
2019-09-29 04:56:39 +00:00
|
|
|
Get(id int) (*GroupPromotion, error)
|
2019-10-06 22:20:37 +00:00
|
|
|
PromoteIfEligible(u *User, level int, posts int) error
|
2019-09-29 04:56:39 +00:00
|
|
|
Delete(id int) error
|
2019-10-06 22:20:37 +00:00
|
|
|
Create(from int, to int, twoWay bool, level int, posts int) (int, error)
|
2019-09-29 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultGroupPromotionStore struct {
|
|
|
|
getByGroup *sql.Stmt
|
|
|
|
get *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
|
|
|
|
getByUser *sql.Stmt
|
|
|
|
updateUser *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultGroupPromotionStore(acc *qgen.Accumulator) (*DefaultGroupPromotionStore, error) {
|
|
|
|
ugp := "users_groups_promotions"
|
|
|
|
return &DefaultGroupPromotionStore{
|
2019-10-06 22:20:37 +00:00
|
|
|
getByGroup: acc.Select(ugp).Columns("pid, from_gid, to_gid, two_way, level, posts, minTime").Where("from_gid=? OR to_gid=?").Prepare(),
|
|
|
|
get: acc.Select(ugp).Columns("from_gid, to_gid, two_way, level, posts, minTime").Where("pid = ?").Prepare(),
|
2019-09-29 04:56:39 +00:00
|
|
|
delete: acc.Delete(ugp).Where("pid = ?").Prepare(),
|
2019-10-06 22:20:37 +00:00
|
|
|
create: acc.Insert(ugp).Columns("from_gid, to_gid, two_way, level, posts, minTime").Fields("?,?,?,?,?,?").Prepare(),
|
2019-09-29 04:56:39 +00:00
|
|
|
|
2019-10-06 22:20:37 +00:00
|
|
|
getByUser: acc.Select(ugp).Columns("pid, to_gid, two_way, level, posts, minTime").Where("from_gid=? AND level>=? AND posts>=?").Orderby("level DESC").Limit("1").Prepare(),
|
|
|
|
updateUser: acc.Update("users").Set("group = ?").Where("level >= ? AND posts >= ?").Prepare(),
|
2019-09-29 04:56:39 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultGroupPromotionStore) GetByGroup(gid int) (gps []*GroupPromotion, err error) {
|
|
|
|
rows, err := s.getByGroup.Query(gid, gid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
g := &GroupPromotion{}
|
2019-10-06 22:20:37 +00:00
|
|
|
err := rows.Scan(&g.ID, &g.From, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime)
|
2019-09-29 04:56:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gps = append(gps, g)
|
|
|
|
}
|
|
|
|
return gps, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Cache the group promotions to avoid hitting the database as much
|
|
|
|
func (s *DefaultGroupPromotionStore) Get(id int) (*GroupPromotion, error) {
|
|
|
|
/*g, err := s.cache.Get(id)
|
|
|
|
if err == nil {
|
|
|
|
return u, nil
|
|
|
|
}*/
|
|
|
|
|
|
|
|
g := &GroupPromotion{ID: id}
|
2019-10-06 22:20:37 +00:00
|
|
|
err := s.get.QueryRow(id).Scan(&g.From, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime)
|
2019-09-29 04:56:39 +00:00
|
|
|
if err == nil {
|
|
|
|
//s.cache.Set(u)
|
|
|
|
}
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
|
2019-10-06 22:20:37 +00:00
|
|
|
func (s *DefaultGroupPromotionStore) PromoteIfEligible(u *User, level int, posts int) error {
|
2019-09-29 04:56:39 +00:00
|
|
|
g := &GroupPromotion{From: u.Group}
|
2019-10-06 22:20:37 +00:00
|
|
|
err := s.getByUser.QueryRow(u.Group, level, posts).Scan(&g.ID, &g.To, &g.TwoWay, &g.Level, &g.Posts, &g.MinTime)
|
2019-09-29 04:56:39 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-06 22:20:37 +00:00
|
|
|
_, err = s.updateUser.Exec(g.To, g.Level, g.Posts)
|
2019-09-29 04:56:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultGroupPromotionStore) Delete(id int) error {
|
|
|
|
_, err := s.delete.Exec(id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-06 22:20:37 +00:00
|
|
|
func (s *DefaultGroupPromotionStore) Create(from int, to int, twoWay bool, level int, posts int) (int, error) {
|
|
|
|
res, err := s.create.Exec(from, to, twoWay, level, posts, 0)
|
2019-09-29 04:56:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
return int(lastID), err
|
|
|
|
}
|