mysql.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/passport-game-cloud/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _memberShard = 30
  12. _addMemberInfoSQL = "INSERT INTO member_%02d (mid,face) VALUES(?,?) ON DUPLICATE KEY UPDATE face=?"
  13. _deleteMemberInfoSQL = "DELETE FROM member_%02d WHERE mid=?"
  14. _getMemberInfoSQL = "SELECT mid,face FROM member_%02d WHERE mid=?"
  15. _addTokenSQL = "INSERT INTO app_perm (mid,appid,app_subid,access_token,create_at,expires) VALUES (?,?,?,?,?,?)"
  16. _updateTokenSQL = "UPDATE app_perm SET expires=? WHERE access_token=? AND expires<?"
  17. _deleteTokenSQL = "DELETE FROM app_perm WHERE access_token=?"
  18. _getTokensSQL = "SELECT access_token FROM app_perm WHERE mid=?"
  19. _addAsoAccountSQL = "INSERT INTO aso_account (mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak) VALUES(?,?,?,?,?,?,?,?,?,?)"
  20. _updateAsoAccountSQL = "UPDATE aso_account SET userid=?,uname=?,pwd=?,salt=?,email=?,tel=?,country_id=?,mobile_verified=?,isleak=? WHERE mid=?"
  21. _deleteAsoAccountSQL = "DELETE FROM aso_account WHERE mid=?"
  22. )
  23. func hit(mid int64) int64 {
  24. return mid % _memberShard
  25. }
  26. // AddMemberInfo add member info.
  27. func (d *Dao) AddMemberInfo(c context.Context, info *model.Info) (affected int64, err error) {
  28. var res sql.Result
  29. if res, err = d.cloudDB.Exec(c, fmt.Sprintf(_addMemberInfoSQL, hit(info.Mid)), info.Mid, info.Face, info.Face); err != nil {
  30. log.Error("failed to add member info, dao.cloudDB.Exec(%d, %s, %s) error(%v)", info.Mid, info.Face, info.Face, err)
  31. return
  32. }
  33. return res.RowsAffected()
  34. }
  35. // DelMemberInfo delete member info.
  36. func (d *Dao) DelMemberInfo(c context.Context, mid int64) (affected int64, err error) {
  37. var res sql.Result
  38. if res, err = d.cloudDB.Exec(c, fmt.Sprintf(_deleteMemberInfoSQL, hit(mid)), mid); err != nil {
  39. log.Error("failed to delete member info, dao.cloudDB.Exec(%d) error(%v)", mid, err)
  40. return
  41. }
  42. return res.RowsAffected()
  43. }
  44. // AddToken add token.
  45. func (d *Dao) AddToken(c context.Context, t *model.Perm) (affected int64, err error) {
  46. var res sql.Result
  47. if res, err = d.cloudDB.Exec(c, _addTokenSQL, t.Mid, t.AppID, t.AppSubID, t.AccessToken, t.CreateAt, t.Expires); err != nil {
  48. log.Error("failed to 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)
  49. return
  50. }
  51. return res.RowsAffected()
  52. }
  53. // UpdateToken update token.
  54. func (d *Dao) UpdateToken(c context.Context, t *model.Perm) (affected int64, err error) {
  55. var res sql.Result
  56. if res, err = d.cloudDB.Exec(c, _updateTokenSQL, t.Expires, t.AccessToken, t.Expires); err != nil {
  57. log.Error("failed to update token, dao.cloudDB.Exec(%d, %s, %d) error(%v)", t.Expires, t.AccessToken, t.Expires, err)
  58. return
  59. }
  60. return res.RowsAffected()
  61. }
  62. // DelToken delete token.
  63. func (d *Dao) DelToken(c context.Context, accessToken string) (affected int64, err error) {
  64. var res sql.Result
  65. if res, err = d.cloudDB.Exec(c, _deleteTokenSQL, accessToken); err != nil {
  66. log.Error("failed to delete token, dao.cloudDB.Exec(%s) error(%v)", accessToken, err)
  67. return
  68. }
  69. return res.RowsAffected()
  70. }
  71. // Tokens get tokens by mid.
  72. func (d *Dao) Tokens(c context.Context, mid int64) (res []string, err error) {
  73. var rows *xsql.Rows
  74. if rows, err = d.cloudDB.Query(c, _getTokensSQL, mid); err != nil {
  75. log.Error("failed to get tokens, dao.cloudDB.Tokens(%s) error(%v)", _getTokensSQL, err)
  76. return
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. var at string
  81. if err = rows.Scan(&at); err != nil {
  82. log.Error("row.Scan() error(%v)", err)
  83. res = nil
  84. return
  85. }
  86. res = append(res, at)
  87. }
  88. return
  89. }
  90. // AddAsoAccount add aso account.
  91. func (d *Dao) AddAsoAccount(c context.Context, a *model.AsoAccount) (affected int64, err error) {
  92. var res sql.Result
  93. var telPtr, emailPtr *string
  94. if a.Tel != "" {
  95. telPtr = &a.Tel
  96. }
  97. if a.Email != "" {
  98. emailPtr = &a.Email
  99. }
  100. if res, err = d.cloudDB.Exec(c, _addAsoAccountSQL, a.Mid, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, telPtr, a.CountryID, a.MobileVerified, a.Isleak); err != nil {
  101. log.Error("failed to add aso account, dao.cloudDB.Exec() error(%v)", err)
  102. return
  103. }
  104. return res.RowsAffected()
  105. }
  106. // UpdateAsoAccount update aso account.
  107. func (d *Dao) UpdateAsoAccount(c context.Context, a *model.AsoAccount) (affected int64, err error) {
  108. var res sql.Result
  109. var telPtr, emailPtr *string
  110. if a.Tel != "" {
  111. telPtr = &a.Tel
  112. }
  113. if a.Email != "" {
  114. emailPtr = &a.Email
  115. }
  116. if res, err = d.cloudDB.Exec(c, _updateAsoAccountSQL, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, telPtr, a.CountryID, a.MobileVerified, a.Isleak, a.Mid); err != nil {
  117. log.Error("failed to add aso account, dao.cloudDB.Exec() error(%v)", err)
  118. return
  119. }
  120. return res.RowsAffected()
  121. }
  122. // DelAsoAccount delete aso account.
  123. func (d *Dao) DelAsoAccount(c context.Context, mid int64) (affected int64, err error) {
  124. var res sql.Result
  125. if res, err = d.cloudDB.Exec(c, _deleteAsoAccountSQL, mid); err != nil {
  126. log.Error("failed to delete aso account, dao.cloudDB.Exec(%s) error(%v)", mid, err)
  127. return
  128. }
  129. return res.RowsAffected()
  130. }