mysql.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/job/main/passport-user/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _getOriginAccountSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,mobile_verified,isleak,country_id,modify_time FROM aso_account WHERE mid > ? limit ?"
  12. _getOriginAccountInfoSQL = "SELECT id,mid,spacesta,safe_question,safe_answer,join_time,join_ip,join_ip_v6,port,active_time,modify_time FROM aso_account_info%d WHERE id > ? limit ?"
  13. _getOriginAccountRegSQL = "SELECT id,mid,origintype,regtype,appid,ctime,mtime FROM aso_account_reg_origin_%d WHERE id > ? limit ?"
  14. _getOriginAccountSnsSQL = "SELECT mid,sina_uid,sina_access_token,sina_access_expires,qq_openid,qq_access_token,qq_access_expires FROM aso_account_sns WHERE mid > ? limit ?"
  15. _getOriginCountryCodeSQL = "SELECT id,code,cname,rank,type,ename FROM aso_country_code"
  16. _getOriginTelBindLogSQL = "SELECT timestamp FROM aso_telephone_bind_log WHERE mid = ? order by timestamp limit 1"
  17. _getOriginAccountByMidSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,mobile_verified,isleak,country_id,modify_time FROM aso_account WHERE mid = ?"
  18. _getOriginAccountSnsByMidSQL = "SELECT mid,sina_uid,sina_access_token,sina_access_expires,qq_openid,qq_access_token,qq_access_expires FROM aso_account_sns WHERE mid = ?"
  19. _getOriginAccountInfoByMidSQL = "SELECT id,mid,spacesta,safe_question,safe_answer,join_time,join_ip,join_ip_v6,port,active_time,modify_time FROM aso_account_info%d WHERE mid = ?"
  20. _getOriginAccountRegByMidSQL = "SELECT id,mid,origintype,regtype,appid,ctime,mtime FROM aso_account_reg_origin_%d WHERE mid = ?"
  21. )
  22. // AsoAccount get account by mid.
  23. func (d *Dao) AsoAccount(c context.Context, start, count int64) (res []*model.OriginAccount, err error) {
  24. var rows *xsql.Rows
  25. if rows, err = d.originDB.Query(c, _getOriginAccountSQL, start, count); err != nil {
  26. log.Error("fail to get AsoAccount, dao.originDB.Query(%s) error(%v)", _getOriginAccountSQL, err)
  27. return
  28. }
  29. defer rows.Close()
  30. for rows.Next() {
  31. var telPtr, emailPtr *string
  32. r := new(model.OriginAccount)
  33. if err = rows.Scan(&r.Mid, &r.UserID, &r.Uname, &r.Pwd, &r.Salt, &emailPtr, &telPtr, &r.MobileVerified, &r.Isleak, &r.CountryID, &r.MTime); err != nil {
  34. log.Error("row.Scan() error(%v)", err)
  35. res = nil
  36. return
  37. }
  38. if telPtr != nil {
  39. r.Tel = *telPtr
  40. }
  41. if emailPtr != nil {
  42. r.Email = strings.ToLower(*emailPtr)
  43. }
  44. res = append(res, r)
  45. }
  46. return
  47. }
  48. // AsoAccountInfo get account info by id.
  49. func (d *Dao) AsoAccountInfo(c context.Context, start, count, suffix int64) (res []*model.OriginAccountInfo, err error) {
  50. var rows *xsql.Rows
  51. if rows, err = d.originDB.Query(c, fmt.Sprintf(_getOriginAccountInfoSQL, suffix), start, count); err != nil {
  52. log.Error("fail to get AsoAccountInfo, dao.originDB.Query(%s) error(%v)", _getOriginAccountInfoSQL, err)
  53. return
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. r := new(model.OriginAccountInfo)
  58. if err = rows.Scan(&r.ID, &r.Mid, &r.Spacesta, &r.SafeQuestion, &r.SafeAnswer, &r.JoinTime, &r.JoinIP, &r.JoinIPV6, &r.Port, &r.ActiveTime, &r.MTime); err != nil {
  59. log.Error("row.Scan() error(%v)", err)
  60. res = nil
  61. return
  62. }
  63. res = append(res, r)
  64. }
  65. return
  66. }
  67. // AsoAccountReg get account reg by id.
  68. func (d *Dao) AsoAccountReg(c context.Context, start, count, suffix int64) (res []*model.OriginAccountReg, err error) {
  69. var rows *xsql.Rows
  70. if rows, err = d.originDB.Query(c, fmt.Sprintf(_getOriginAccountRegSQL, suffix), start, count); err != nil {
  71. log.Error("fail to get AsoAccountReg, dao.originDB.Query(%s) error(%v)", _getOriginAccountRegSQL, err)
  72. return
  73. }
  74. defer rows.Close()
  75. for rows.Next() {
  76. r := new(model.OriginAccountReg)
  77. if err = rows.Scan(&r.ID, &r.Mid, &r.OriginType, &r.RegType, &r.AppID, &r.CTime, &r.MTime); err != nil {
  78. log.Error("row.Scan() error(%v)", err)
  79. res = nil
  80. return
  81. }
  82. res = append(res, r)
  83. }
  84. return
  85. }
  86. // AsoAccountSns get account sns by id.
  87. func (d *Dao) AsoAccountSns(c context.Context, start, count int64) (res []*model.OriginAccountSns, err error) {
  88. var rows *xsql.Rows
  89. if rows, err = d.originDB.Query(c, _getOriginAccountSnsSQL, start, count); err != nil {
  90. log.Error("fail to get AsoAccountSns, dao.originDB.Query(%s) error(%v)", _getOriginAccountSnsSQL, err)
  91. return
  92. }
  93. defer rows.Close()
  94. for rows.Next() {
  95. r := new(model.OriginAccountSns)
  96. if err = rows.Scan(&r.Mid, &r.SinaUID, &r.SinaAccessToken, &r.SinaAccessExpires, &r.QQOpenid, &r.QQAccessToken, &r.QQAccessExpires); err != nil {
  97. log.Error("row.Scan() error(%v)", err)
  98. res = nil
  99. return
  100. }
  101. res = append(res, r)
  102. }
  103. return
  104. }
  105. // AsoCountryCode get aso country code.
  106. func (d *Dao) AsoCountryCode(c context.Context) (res []*model.CountryCode, err error) {
  107. var rows *xsql.Rows
  108. if rows, err = d.originDB.Query(c, _getOriginCountryCodeSQL); err != nil {
  109. log.Error("fail to get AsoCountryCode, dao.originDB.Query(%s) error(%v)", _getOriginCountryCodeSQL, err)
  110. return
  111. }
  112. defer rows.Close()
  113. for rows.Next() {
  114. r := new(model.CountryCode)
  115. if err = rows.Scan(&r.ID, &r.Code, &r.Cname, &r.Rank, &r.Type, &r.Ename); err != nil {
  116. log.Error("row.Scan() error(%v)", err)
  117. res = nil
  118. return
  119. }
  120. res = append(res, r)
  121. }
  122. return
  123. }
  124. // AsoTelBindLog get aso tel bind log.
  125. func (d *Dao) AsoTelBindLog(c context.Context, mid int64) (res int64, err error) {
  126. if err = d.originDB.QueryRow(c, _getOriginTelBindLogSQL, mid).Scan(&res); err != nil {
  127. if err == xsql.ErrNoRows {
  128. err = nil
  129. } else {
  130. log.Error("fail to get AsoTelBindLog, dao.originDB.QueryRow(%s) error(%v)", _getOriginTelBindLogSQL, err)
  131. }
  132. return
  133. }
  134. return
  135. }
  136. // GetAsoAccountByMid get aso account by mid.
  137. func (d *Dao) GetAsoAccountByMid(c context.Context, mid int64) (res *model.OriginAccount, err error) {
  138. row := d.originDB.QueryRow(c, _getOriginAccountByMidSQL, mid)
  139. var telPtr, emailPtr *string
  140. res = &model.OriginAccount{}
  141. if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
  142. log.Error("fail to get AsoAccount, dao.originDB.QueryRow(%s) error(%v)", _getOriginAccountByMidSQL, err)
  143. return
  144. }
  145. if telPtr != nil {
  146. res.Tel = *telPtr
  147. }
  148. if emailPtr != nil {
  149. res.Email = strings.ToLower(*emailPtr)
  150. }
  151. return
  152. }
  153. // GetAsoAccountSnsByMid get aso account sns by mid.
  154. func (d *Dao) GetAsoAccountSnsByMid(c context.Context, mid int64) (res *model.OriginAccountSns, err error) {
  155. row := d.originDB.QueryRow(c, _getOriginAccountSnsByMidSQL, mid)
  156. res = &model.OriginAccountSns{}
  157. if err = row.Scan(&res.Mid, &res.SinaUID, &res.SinaAccessToken, &res.SinaAccessExpires, &res.QQOpenid, &res.QQAccessToken, &res.QQAccessExpires); err != nil {
  158. log.Error("fail to get AsoAccountSns, dao.originDB.QueryRow(%s) error(%v)", _getOriginAccountSnsByMidSQL, err)
  159. return
  160. }
  161. return
  162. }
  163. // GetAsoAccountInfoByMid get aso account info by mid.
  164. func (d *Dao) GetAsoAccountInfoByMid(c context.Context, mid int64) (res *model.OriginAccountInfo, err error) {
  165. row := d.originDB.QueryRow(c, fmt.Sprintf(_getOriginAccountInfoByMidSQL, accountInfoIndex(mid)), mid)
  166. res = &model.OriginAccountInfo{}
  167. if err = row.Scan(&res.ID, &res.Mid, &res.Spacesta, &res.SafeQuestion, &res.SafeAnswer, &res.JoinTime, &res.JoinIP, &res.JoinIPV6, &res.Port, &res.ActiveTime, &res.MTime); err != nil {
  168. log.Error("fail to get AsoAccountInfo, dao.originDB.QueryRow(%s) error(%v)", _getOriginAccountInfoByMidSQL, err)
  169. return
  170. }
  171. return
  172. }
  173. // GetAsoAccountRegByMid get aso account reg by mid.
  174. func (d *Dao) GetAsoAccountRegByMid(c context.Context, mid int64) (res *model.OriginAccountReg, err error) {
  175. row := d.originDB.QueryRow(c, fmt.Sprintf(_getOriginAccountRegByMidSQL, accountRegIndex(mid)), mid)
  176. res = &model.OriginAccountReg{}
  177. if err = row.Scan(&res.ID, &res.Mid, &res.OriginType, &res.RegType, &res.AppID, &res.CTime, &res.MTime); err != nil {
  178. log.Error("fail to get AsoAccountReg, dao.originDB.QueryRow(%s) error(%v)", _getOriginAccountRegByMidSQL, err)
  179. return
  180. }
  181. return
  182. }
  183. func accountInfoIndex(mid int64) int64 {
  184. return mid % 30
  185. }
  186. func accountRegIndex(mid int64) int64 {
  187. return mid % 20
  188. }