mysql.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "go-common/app/service/main/usersuit/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _addInviteSQL = "INSERT INTO invite_code(mid,code,buy_ip,buy_ip_ng,expires,ctime) VALUES(?,?,?,?,?,?)"
  12. _updateInviteSQL = "UPDATE invite_code SET imid=?,used_at=? WHERE code=?"
  13. _getInviteSQL = "SELECT mid,imid,code,expires,used_at,ctime FROM invite_code WHERE code=?"
  14. _getInvitesSQL = "SELECT mid,imid,code,expires,used_at,ctime FROM invite_code WHERE mid=?"
  15. _getCurrentCountSQL = "SELECT count(1) FROM invite_code WHERE mid=? AND ctime>=? AND ctime<=?"
  16. )
  17. // Begin begin transaction
  18. func (d *Dao) Begin(c context.Context) (tx *xsql.Tx, err error) {
  19. return d.db.Begin(c)
  20. }
  21. // TxAddInvite transaction invite
  22. func (d *Dao) TxAddInvite(c context.Context, tx *xsql.Tx, inv *model.Invite) (affected int64, err error) {
  23. var res sql.Result
  24. if res, err = tx.Exec(_addInviteSQL, inv.Mid, inv.Code, inv.IP, inv.IPng, inv.Expires, inv.Ctime); err != nil {
  25. 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)
  26. return
  27. }
  28. return res.RowsAffected()
  29. }
  30. // UpdateInvite update invite
  31. func (d *Dao) UpdateInvite(c context.Context, imid int64, usedAt int64, code string) (affected int64, err error) {
  32. var res sql.Result
  33. if res, err = d.db.Exec(c, _updateInviteSQL, imid, usedAt, code); err != nil {
  34. log.Error("update invite, dao.db.Exec(%d, %d, %s) error(%v)", imid, usedAt, code, err)
  35. return
  36. }
  37. return res.RowsAffected()
  38. }
  39. // Invite invite info
  40. func (d *Dao) Invite(c context.Context, code string) (res *model.Invite, err error) {
  41. row := d.db.QueryRow(c, _getInviteSQL, code)
  42. res = new(model.Invite)
  43. if err = row.Scan(&res.Mid, &res.Imid, &res.Code, &res.Expires, &res.UsedAt, &res.Ctime); err != nil {
  44. if err == sql.ErrNoRows {
  45. res = nil
  46. err = nil
  47. } else {
  48. log.Error("get invite, row.Scan() error(%v)", err)
  49. }
  50. }
  51. return
  52. }
  53. // Invites invite list
  54. func (d *Dao) Invites(c context.Context, mid int64) (res []*model.Invite, err error) {
  55. var rows *xsql.Rows
  56. if rows, err = d.db.Query(c, _getInvitesSQL, mid); err != nil {
  57. log.Error("get invites, dao.db.Query(%d) error(%v)", mid, err)
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. inv := new(model.Invite)
  63. if err = rows.Scan(&inv.Mid, &inv.Imid, &inv.Code, &inv.Expires, &inv.UsedAt, &inv.Ctime); err != nil {
  64. log.Error("row.Scan() error(%v)", err)
  65. return
  66. }
  67. res = append(res, inv)
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. // CurrentCount current count
  73. func (d *Dao) CurrentCount(c context.Context, mid int64, start, end time.Time) (res int64, err error) {
  74. row := d.db.QueryRow(c, _getCurrentCountSQL, mid, start, end)
  75. if err = row.Scan(&res); err != nil {
  76. log.Error("get current count, mid: %d, start: %d, end: %d, row.Scan() error(%v)", mid, start, end, err)
  77. }
  78. return
  79. }