mysql.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/job/main/passport-encrypt/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _getOriginAccountSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,mobile_verified,isleak,country_id,modify_time FROM aso_account where mid >= ? and mid < ?"
  11. _addAsoAccountSQL = "INSERT INTO aso_account (mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak) VALUES(?,?,?,?,?,?,?,?,?,?)"
  12. _updateAsoAccountSQL = "UPDATE aso_account SET userid=?,uname=?,pwd=?,salt=?,email=?,tel=?,country_id=?,mobile_verified=?,isleak=? WHERE mid=?"
  13. _deleteAsoAccountSQL = "DELETE FROM aso_account WHERE mid=?"
  14. )
  15. // AsoAccounts get tokens by mid.
  16. func (d *Dao) AsoAccounts(c context.Context, start, end int64) (res []*model.OriginAccount, err error) {
  17. var rows *xsql.Rows
  18. if rows, err = d.originDB.Query(c, _getOriginAccountSQL, start, end); err != nil {
  19. log.Error("failed to get AsoAccounts, dao.originDB.AsoAccounts(%s) error(%v)", _getOriginAccountSQL, err)
  20. return
  21. }
  22. defer rows.Close()
  23. for rows.Next() {
  24. var telPtr, emailPtr *string
  25. r := new(model.OriginAccount)
  26. 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 {
  27. log.Error("row.Scan() error(%v)", err)
  28. res = nil
  29. return
  30. }
  31. if telPtr != nil {
  32. r.Tel = *telPtr
  33. }
  34. if emailPtr != nil {
  35. r.Email = *emailPtr
  36. }
  37. res = append(res, r)
  38. }
  39. return
  40. }
  41. // AddAsoAccount add aso account.
  42. func (d *Dao) AddAsoAccount(c context.Context, a *model.EncryptAccount) (affected int64, err error) {
  43. var res sql.Result
  44. var emailPtr *string
  45. if a.Email != "" {
  46. emailPtr = &a.Email
  47. }
  48. if res, err = d.encryptDB.Exec(c, _addAsoAccountSQL, a.Mid, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, a.Tel, a.CountryID, a.MobileVerified, a.Isleak); err != nil {
  49. log.Error("failed to add aso account, dao.encryptDB.Exec() error(%v)", err)
  50. return
  51. }
  52. return res.RowsAffected()
  53. }
  54. // UpdateAsoAccount update aso account.
  55. func (d *Dao) UpdateAsoAccount(c context.Context, a *model.EncryptAccount) (affected int64, err error) {
  56. var res sql.Result
  57. var emailPtr *string
  58. if a.Email != "" {
  59. emailPtr = &a.Email
  60. }
  61. if res, err = d.encryptDB.Exec(c, _updateAsoAccountSQL, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, a.Tel, a.CountryID, a.MobileVerified, a.Isleak, a.Mid); err != nil {
  62. log.Error("failed to update aso account, dao.encryptDB.Exec() error(%v)", err)
  63. return
  64. }
  65. return res.RowsAffected()
  66. }
  67. // DelAsoAccount delete aso account.
  68. func (d *Dao) DelAsoAccount(c context.Context, mid int64) (affected int64, err error) {
  69. var res sql.Result
  70. if res, err = d.encryptDB.Exec(c, _deleteAsoAccountSQL, mid); err != nil {
  71. log.Error("failed to delete aso account, dao.encryptDB.Exec(%s) error(%v)", mid, err)
  72. return
  73. }
  74. return res.RowsAffected()
  75. }