invite.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "go-common/app/admin/main/usersuit/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _addIgnoreInviteSQL = "INSERT IGNORE INTO invite_code(mid,code,buy_ip,buy_ip_ng,expires,ctime) VALUES(?,?,?,?,?,?)"
  12. _getRangeInvitesSQL = "SELECT mid,imid,code,buy_ip,buy_ip_ng,expires,used_at,ctime FROM invite_code WHERE mid=? AND ctime>=? AND ctime<=?"
  13. )
  14. // AddIgnoreInvite add ignore invite code.
  15. func (d *Dao) AddIgnoreInvite(c context.Context, inv *model.Invite) (affected int64, err error) {
  16. var res sql.Result
  17. if res, err = d.db.Exec(c, _addIgnoreInviteSQL, inv.Mid, inv.Code, inv.IP, inv.IPng, inv.Expires, inv.Ctime); err != nil {
  18. log.Error("add invite, dao.db.Exec(%d, %s, %d, %v, %d, %v) error(%v)", inv.Mid, inv.Code, inv.IP, inv.IPng, inv.Expires, inv.Ctime, err)
  19. return
  20. }
  21. return res.RowsAffected()
  22. }
  23. // RangeInvites range invites.
  24. func (d *Dao) RangeInvites(c context.Context, mid int64, start, end time.Time) (res []*model.Invite, err error) {
  25. var rows *xsql.Rows
  26. if rows, err = d.db.Query(c, _getRangeInvitesSQL, mid, start, end); err != nil {
  27. log.Error("get range invites, dao.db.Query(%v, %v, %v) error(%v)", mid, start, end, err)
  28. return
  29. }
  30. defer rows.Close()
  31. for rows.Next() {
  32. inv := new(model.Invite)
  33. if err = rows.Scan(&inv.Mid, &inv.Imid, &inv.Code, &inv.IP, &inv.IPng, &inv.Expires, &inv.UsedAt, &inv.Ctime); err != nil {
  34. log.Error("row.Scan() error(%v)", err)
  35. return
  36. }
  37. res = append(res, inv)
  38. }
  39. err = rows.Err()
  40. return
  41. }