mysql.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/service/main/passport-game/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _memberShard = 30
  11. _getMemberInfoSQL = "SELECT mid,face FROM member_%02d WHERE mid=?"
  12. _getAppsSQL = "SELECT appid,appkey,app_secret FROM app"
  13. _addTokenSQL = "INSERT INTO app_perm (mid,appid,app_subid,access_token,create_at,expires) VALUES (?,?,?,?,?,?)"
  14. _updateTokenSQL = "UPDATE app_perm SET expires=? WHERE access_token=? AND expires<?"
  15. _getTokenSQL = "SELECT mid,appid,app_subid,access_token,create_at,expires FROM app_perm WHERE access_token=?"
  16. _getAsoAccountSQL = "SELECT mid,userid,uname,pwd,salt,isleak FROM aso_account WHERE userid=? OR email=? OR tel=?"
  17. _getAccountInfoSQL = "SELECT mid,userid,uname,email,tel FROM aso_account WHERE mid=?"
  18. )
  19. func hit(mid int64) int64 {
  20. return mid % _memberShard
  21. }
  22. // MemberInfo get member info.
  23. func (d *Dao) MemberInfo(c context.Context, mid int64) (res *model.Info, err error) {
  24. var row = d.getMemberStmt[hit(mid)].QueryRow(c, mid)
  25. res = new(model.Info)
  26. if err = row.Scan(&res.Mid, &res.Face); err != nil {
  27. if err == xsql.ErrNoRows {
  28. res = nil
  29. err = nil
  30. } else {
  31. log.Error("row.Scan() error(%v)", err)
  32. }
  33. }
  34. return
  35. }
  36. // Apps get all apps.
  37. func (d *Dao) Apps(c context.Context) (res []*model.App, err error) {
  38. var rows *xsql.Rows
  39. if rows, err = d.cloudDB.Query(c, _getAppsSQL); err != nil {
  40. log.Error("get apps, dao.cloudDB.Query(%s) error(%v)", _getAppsSQL, err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. app := new(model.App)
  46. if err = rows.Scan(&app.AppID, &app.AppKey, &app.AppSecret); err != nil {
  47. log.Error("row.Scan() error(%v)", err)
  48. res = nil
  49. return
  50. }
  51. res = append(res, app)
  52. }
  53. err = rows.Err()
  54. return
  55. }
  56. // AddToken add token.
  57. func (d *Dao) AddToken(c context.Context, t *model.Perm) (affected int64, err error) {
  58. var res sql.Result
  59. if res, err = d.cloudDB.Exec(c, _addTokenSQL, t.Mid, t.AppID, t.AppSubID, t.AccessToken, t.CreateAt, t.Expires); err != nil {
  60. log.Error("add token, dao.cloudDB.Exec(%d, %d, %d, %s, %d, %d) error(%v)", t.Mid, t.AppID, t.AppSubID, t.AccessToken, t.CreateAt, t.Expires, err)
  61. return
  62. }
  63. return res.RowsAffected()
  64. }
  65. // UpdateToken update token.
  66. func (d *Dao) UpdateToken(c context.Context, t *model.Perm) (affected int64, err error) {
  67. var res sql.Result
  68. if res, err = d.cloudDB.Exec(c, _updateTokenSQL, t.Expires, t.AccessToken, t.Expires); err != nil {
  69. log.Error("update token, dao.cloudDB.Exec(%d, %s, %d) error(%v)", t.Expires, t.AccessToken, t.Expires, err)
  70. return
  71. }
  72. return res.RowsAffected()
  73. }
  74. // Token get token.
  75. func (d *Dao) Token(c context.Context, accessToken string) (res *model.Perm, err error) {
  76. row := d.cloudDB.QueryRow(c, _getTokenSQL, accessToken)
  77. res = new(model.Perm)
  78. if err = row.Scan(&res.Mid, &res.AppID, &res.AppSubID, &res.AccessToken, &res.CreateAt, &res.Expires); err != nil {
  79. if err == xsql.ErrNoRows {
  80. res = nil
  81. err = nil
  82. } else {
  83. log.Error("row.Scan() error(%v)", err)
  84. }
  85. }
  86. return
  87. }
  88. // AsoAccount get aso account.
  89. func (d *Dao) AsoAccount(c context.Context, identify, identifyHash string) (res []*model.AsoAccount, err error) {
  90. var rows *xsql.Rows
  91. if rows, err = d.cloudDB.Query(c, _getAsoAccountSQL, identify, identifyHash, identifyHash); err != nil {
  92. log.Error("get apps, dao.cloudDB.Query(%s) error(%v)", _getAsoAccountSQL, err)
  93. return
  94. }
  95. for rows.Next() {
  96. aso := new(model.AsoAccount)
  97. if err = rows.Scan(&aso.Mid, &aso.UserID, &aso.Uname, &aso.Pwd, &aso.Salt, &aso.Isleak); err != nil {
  98. log.Error("row.Scan() error(%v)", err)
  99. res = nil
  100. return
  101. }
  102. res = append(res, aso)
  103. }
  104. err = rows.Err()
  105. return
  106. }
  107. // AccountInfo get account info.
  108. func (d *Dao) AccountInfo(c context.Context, mid int64) (res *model.AsoAccount, err error) {
  109. row := d.cloudDB.QueryRow(c, _getAccountInfoSQL, mid)
  110. res = new(model.AsoAccount)
  111. if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Email, &res.Tel); err != nil {
  112. if err == xsql.ErrNoRows {
  113. res = nil
  114. err = nil
  115. } else {
  116. log.Error("row.Scan() error(%v)", err)
  117. }
  118. }
  119. return
  120. }
  121. // TokenFromOtherRegion get token from otherRegion.
  122. func (d *Dao) TokenFromOtherRegion(c context.Context, accessToken string) (res *model.Perm, err error) {
  123. row := d.otherRegion.QueryRow(c, _getTokenSQL, accessToken)
  124. res = new(model.Perm)
  125. if err = row.Scan(&res.Mid, &res.AppID, &res.AppSubID, &res.AccessToken, &res.CreateAt, &res.Expires); err != nil {
  126. if err == xsql.ErrNoRows {
  127. res = nil
  128. err = nil
  129. return
  130. }
  131. log.Error("row.Scan() error(%v)", err)
  132. return
  133. }
  134. return
  135. }